@@ -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+
171235void 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+
211288void 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 }
0 commit comments