Skip to content

Commit c2abd14

Browse files
committed
Configure several coloredl light for ch10_area_lights, add to README.
1 parent ca5bdde commit c2abd14

File tree

5 files changed

+102
-32
lines changed

5 files changed

+102
-32
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
# Real-Time Rendering Prototypes
22

3-
Prototypes developed while reading RTR 4th edition
3+
Prototypes developed while reading RTR 4th edition.
4+
5+
## Content
6+
7+
The repository contains several standalone applications demonstrating real-time rendering techniques.
8+
9+
### ch10_area_lights
10+
11+
Implement sphere lights and tube lights with fast approximations, such as most-representative point.
12+
13+
[![area-lights roughness scale](https://adnn.github.io/assets/shared/area_lights-roughness_variations-864_646.jpg)](https://adnn.github.io/assets/shared/area_lights-roughness_variations.jpg)

src/apps/ch10_area_lights/ch10_area_lights/Scene.cpp

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ Scene::Scene(graphics::AppInterface & aAppInterface, const imguiui::ImguiUi & aI
132132
mLightProgram{mEngine.loadProgram(renderer::ReferencePath{gLightProgramPath})},
133133
mLineDrawer{mEngine.loadProgram(renderer::ReferencePath{gLineProgramPath})}
134134
{
135+
setupLights();
136+
135137
graphics::attachIndexBuffer(mIndexBuffer, mVertexSpecification.mVertexArray);
136138

137139
graphics::appendToVertexSpecification(
@@ -168,6 +170,68 @@ Scene::Scene(graphics::AppInterface & aAppInterface, const imguiui::ImguiUi & aI
168170
}
169171

170172

173+
void Scene::setupLights()
174+
{
175+
constexpr unsigned int tubeCount = 3;
176+
constexpr unsigned int bulbCount = 1;
177+
std::array<math::hdr::Rgb<GLfloat>, tubeCount + bulbCount> colors{{
178+
{0.55f, 0.05f, 0.05f},
179+
{0.13f, 0.64f, 0.62f},
180+
{1.f, 0.73f, 0.29f},
181+
{0.44f, 0.f, 0.37f},
182+
}};
183+
float height = 1.5f;
184+
float radius = 3.f;
185+
186+
const math::Radian<GLfloat> pi{ math::pi<GLfloat> };
187+
const math::Radian<GLfloat> reserve{pi / 8};
188+
math::Radian<GLfloat> step{ 2 * pi / tubeCount };
189+
190+
auto makePosition = [&](unsigned int i, math::Radian<GLfloat> aReserve)
191+
{
192+
return math::Position<3, GLfloat>{
193+
radius * cos(i * step + aReserve),
194+
height,
195+
radius * sin(i * step + aReserve)};
196+
};
197+
198+
for (unsigned int i = 0; i != tubeCount; ++i)
199+
{
200+
unsigned int pointIdx = 2 * i;
201+
mLights.mPointLights[pointIdx] =
202+
renderer::PointLight_glsl{
203+
.mPosition{makePosition(pointIdx, reserve)},
204+
.mColors{
205+
.mDiffuseColor = colors[i] * 30,
206+
.mSpecularColor = colors[i] * 30,
207+
},
208+
};
209+
mLights.mPointLights[pointIdx + 1] =
210+
renderer::PointLight_glsl{
211+
.mPosition{makePosition(pointIdx + 1, -reserve)},
212+
.mColors{
213+
.mDiffuseColor = colors[i] * 30,
214+
.mSpecularColor = colors[i] * 30,
215+
},
216+
};
217+
}
218+
mLights.mPointCount = 2 * tubeCount;
219+
mFrameControl.mTubeCount = tubeCount;
220+
221+
mLights.mPointLights[mLights.mPointCount++] =
222+
renderer::PointLight_glsl{
223+
.mPosition{2.f, -3.f, 0.f},
224+
.mRadius{
225+
.mMin = 1.f,
226+
.mMax = 5.f,
227+
},
228+
.mColors{
229+
.mDiffuseColor = colors[tubeCount] * 30,
230+
.mSpecularColor = colors[tubeCount] * 30,
231+
},
232+
};
233+
}
234+
171235
void Scene::loadPrograms()
172236
{
173237
mSurfaceProgram =
@@ -208,6 +272,19 @@ renderer::LightsDataCommon transformLightsData(
208272
}
209273

210274

275+
math::hdr::Rgb<float> capColor(math::hdr::Rgb<float> aColor)
276+
{
277+
float maxElement = *aColor.getMaxMagnitudeElement();
278+
if (maxElement > 1)
279+
{
280+
return aColor / maxElement;
281+
}
282+
else
283+
{
284+
return aColor;
285+
}
286+
}
287+
211288
void Scene::render(math::Size<2, int> aRenderResolution)
212289
{
213290
//
@@ -225,16 +302,19 @@ void Scene::render(math::Size<2, int> aRenderResolution)
225302
entity.mLocalToWorld =
226303
math::trans3d::scaleUniform(pointLight.mRadius.mMin)
227304
* math::trans3d::translate(pointLight.mPosition.as<math::Vec>());
228-
entity.mColorFactor = pointLight.mColors.mSpecularColor;
305+
entity.mColorFactor = capColor(pointLight.mColors.mSpecularColor);
229306

230307
// Populate the line segments representing the tube lights
308+
// Note: whereas for point lights we populate the entities UBO to render the spheres
309+
// with classical matrix tansforms,
310+
// for tube lights we populate a line segment UBO, directly with world positions
231311
if (pointLightIdx % 2 == 1)
232312
{
233313
mLines.mSegments.push_back(
234314
renderer::LineSegment_glsl{
235315
.mPointA = mLights.mPointLights[pointLightIdx - 1].mPosition,
236316
.mPointB = mLights.mPointLights[pointLightIdx].mPosition,
237-
.mColor = mLights.mPointLights[pointLightIdx - 1].mColors.mSpecularColor,
317+
.mColor = capColor(mLights.mPointLights[pointLightIdx - 1].mColors.mSpecularColor),
238318
.mWidth = 2 * mLights.mPointLights[pointLightIdx - 1].mRadius.mMin,
239319
});
240320
}

src/apps/ch10_area_lights/ch10_area_lights/Scene.h

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ struct Scene
116116

117117
void presentUi(bool * aOpen = nullptr);
118118

119+
void setupLights();
120+
119121
Engine mEngine;
120122

121123
scenic::geodesic::Sphere mSphere{ 4 };
@@ -147,38 +149,12 @@ struct Scene
147149
},
148150
},
149151
};
152+
// Note: see setupLights() for point and tube lights definitions
150153
renderer::LightsDataCommon mLights{
151154
.mDirectionalCount = 0,
152-
.mPointCount = 2,
153155
// We decode a sRGB 10% white (which is also perceptually ~10%)
154156
// to linear space for computation.
155157
.mAmbientColor = math::decode_sRGB(math::hdr::gWhite<float> * 0.1f),
156-
.mDirectionalLights = {
157-
renderer::DirectionalLight_glsl{
158-
.mDirection = math::UnitVec<3, float>{ {0.5f, 0.f, -0.5f} },
159-
// TODO: decode the srgb value to have it show correctly in Imgui
160-
// (and have it perceptually proportional to the factor)
161-
.mColors = renderer::LightColors_glsl{} * 0.2,
162-
},
163-
},
164-
.mPointLights = {
165-
renderer::PointLight_glsl{
166-
.mPosition = {-2.f, 3.f, 0.f},
167-
.mRadius{
168-
.mMin = 0.2f,
169-
.mMax = 5.f,
170-
},
171-
.mColors = renderer::LightColors_glsl{} * 30.f,
172-
},
173-
renderer::PointLight_glsl{
174-
.mPosition = {+2.f, 3.f, 0.f},
175-
.mRadius{
176-
.mMin = 0.2f,
177-
.mMax = 5.f,
178-
},
179-
.mColors = renderer::LightColors_glsl{} * 30.f,
180-
},
181-
},
182158
};
183159
renderer::LinesSsbo_glsl mLines;
184160
OrbitalCamera mOrbitalCamera;

src/apps/ch10_area_lights/ch10_area_lights/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ int main(int argc, const char * argv[])
2727
{
2828
spdlog::set_level(spdlog::level::debug);
2929

30-
ad::graphics::ApplicationGlfw application("ch10_area_lights", 1080, 600);
30+
ad::graphics::ApplicationGlfw application{
31+
"ch10_area_lights", 1080, 600,
32+
ad::graphics::ApplicationFlag::None,
33+
4, 6,
34+
{ {GLFW_SAMPLES, 8} }};
3135

3236
// Ensures the messages are sent synchronously with the event triggering them
3337
// This makes debug stepping much more feasible.

src/libs/engine/engine/Lights.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct Radius
7171
// * r_0 (the distance at which the intensities are given)
7272
// * r_min (the distance below which light will not gain intensities anymore)
7373
// max is used as r_max, i.e. where both light intensity & derivative should reach zero.
74-
GLfloat mMin = 0.25f;
74+
GLfloat mMin = 0.2f;
7575
GLfloat mMax = 10.f;
7676
// Until the maybe_uninitialized warning works better we need
7777
// mMin and mMax to have default values or gcc will shout on

0 commit comments

Comments
 (0)