Skip to content

Commit ca5bdde

Browse files
committed
Restore point light functionnality, in addition to tube light.
1 parent f7c29c8 commit ca5bdde

File tree

8 files changed

+58
-59
lines changed

8 files changed

+58
-59
lines changed

src/apps/ch10_area_lights/ch10_area_lights/Scene.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ void Scene::render(math::Size<2, int> aRenderResolution)
225225
entity.mLocalToWorld =
226226
math::trans3d::scaleUniform(pointLight.mRadius.mMin)
227227
* math::trans3d::translate(pointLight.mPosition.as<math::Vec>());
228-
entity.mColorFactor = pointLight.mColors.mDiffuseColor;
228+
entity.mColorFactor = pointLight.mColors.mSpecularColor;
229229

230230
// Populate the line segments representing the tube lights
231231
if (pointLightIdx % 2 == 1)
@@ -234,6 +234,7 @@ void Scene::render(math::Size<2, int> aRenderResolution)
234234
renderer::LineSegment_glsl{
235235
.mPointA = mLights.mPointLights[pointLightIdx - 1].mPosition,
236236
.mPointB = mLights.mPointLights[pointLightIdx].mPosition,
237+
.mColor = mLights.mPointLights[pointLightIdx - 1].mColors.mSpecularColor,
237238
.mWidth = 2 * mLights.mPointLights[pointLightIdx - 1].mRadius.mMin,
238239
});
239240
}
@@ -285,6 +286,7 @@ void Scene::render(math::Size<2, int> aRenderResolution)
285286
// TODO: should be done only once for each pair of VAO-program
286287
validateVertexAttributes(mSurfaceProgram);
287288
glUseProgram(mSurfaceProgram);
289+
graphics::setUniform(mSurfaceProgram, "u_TubeCount", mFrameControl.mTubeCount);
288290

289291
glDrawElementsInstancedBaseInstance(
290292
GL_PATCHES,
@@ -320,7 +322,7 @@ void Scene::render(math::Size<2, int> aRenderResolution)
320322
}
321323
glBindVertexArray(mLineDrawer.mVao);
322324
glUseProgram(mLineDrawer.mProgram);
323-
glDrawArrays(GL_TRIANGLES, 0, mLineDrawer.mVerticesCount);
325+
glDrawArraysInstanced(GL_TRIANGLES, 0, mLineDrawer.mVerticesCount, mFrameControl.mTubeCount);
324326
glEnable(GL_CULL_FACE);
325327
}
326328
}
@@ -351,6 +353,10 @@ void Scene::presentUi(bool * aOpen)
351353

352354
ImGui::Checkbox("Show Punctual Lights", &mFrameControl.mShowPunctualLights);
353355

356+
ImGui::InputScalar("Tube count", ImGuiDataType_U32, &mFrameControl.mTubeCount);
357+
mFrameControl.mTubeCount = std::min(mFrameControl.mTubeCount, mLights.mPointCount / 2);
358+
359+
354360
DearImguiWitness witness;
355361

356362
ImGui::Spacing();

src/apps/ch10_area_lights/ch10_area_lights/Scene.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ struct Scene
101101

102102
decltype(gPolygonModes)::const_iterator mPolygonMode = gPolygonModes.begin() + 2;
103103
bool mShowPunctualLights = true;
104+
unsigned int mTubeCount = 1;
104105
};
105106

106107
Scene(graphics::AppInterface & aAppInterface, const imguiui::ImguiUi & aImgui);

src/apps/ch10_area_lights/ch10_area_lights/resources/shaders/ch10_area_lights_Pbr.frag

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ in vec3 ex_Position_view;
1414

1515
out vec4 out_Color;
1616

17+
uniform uint u_TubeCount;
18+
1719

1820
LightContributions applyLight_pbr(vec3 aView, vec3 aDiffuseLightDir, vec3 aSpecularLightDir, vec3 aShadingNormal,
1921
PbrParameters aParams, LightColors aColors)
@@ -149,35 +151,38 @@ void main(void)
149151
}
150152

151153

152-
//// Point lights
153-
//for(uint pointIdx = 0; pointIdx != ub_PointCount.x; ++pointIdx)
154-
//{
155-
// PointLight point = ub_PointLights[pointIdx];
154+
// The u_TubeCount first pairs of point lights are representing tube lights
155+
uint firstPoint = min(2 * u_TubeCount, ub_PointCount);
156+
157+
// Point lights
158+
for(uint pointIdx = firstPoint; pointIdx != ub_PointCount; ++pointIdx)
159+
{
160+
PointLight point = ub_PointLights[pointIdx];
156161

157-
// // see rtr 4th p110 (5.10)
158-
// vec3 lightRay_view = point.position.xyz - ex_Position_view;
159-
// float radius = length(lightRay_view);
160-
// vec3 lightDir_view = lightRay_view / radius;
162+
// see rtr 4th p110 (5.10)
163+
vec3 lightRay_view = point.position.xyz - ex_Position_view;
164+
float radius = length(lightRay_view);
165+
vec3 lightDir_view = lightRay_view / radius;
161166

162-
// vec3 specularLightDir_view = normalize(
163-
// representativePoint_sphere(ex_Position_view,
164-
// point.position.xyz,
165-
// reflect(-viewDir_view, shadingNormal_view),
166-
// point.radius.x));
167+
vec3 specularLightDir_view = normalize(
168+
representativePoint_sphere(ex_Position_view,
169+
point.position.xyz,
170+
reflect(-viewDir_view, shadingNormal_view),
171+
point.radius.x));
167172

168-
// LightContributions lighting =
169-
// applyLight_pbr(
170-
// viewDir_view, lightDir_view, specularLightDir_view, shadingNormal_view,
171-
// pbrParameters, point.colors);
173+
LightContributions lighting =
174+
applyLight_pbr(
175+
viewDir_view, lightDir_view, specularLightDir_view, shadingNormal_view,
176+
pbrParameters, point.colors);
172177

173-
// float falloff = attenuatePoint(point, radius);
174-
// diffuseAccum += lighting.diffuse * falloff;
175-
// specularAccum += lighting.specular * falloff;
176-
//}
178+
float falloff = attenuatePoint(point, radius);
179+
diffuseAccum += lighting.diffuse * falloff;
180+
specularAccum += lighting.specular * falloff;
181+
}
177182

178183

179184
// Tube lights
180-
for(uint pointIdx = 0; pointIdx != ub_PointCount; pointIdx += 2)
185+
for(uint pointIdx = 0; (pointIdx + 1) < firstPoint; pointIdx += 2)
181186
{
182187
PointLight p0 = ub_PointLights[pointIdx];
183188
PointLight p1 = ub_PointLights[pointIdx+1];

src/libs/engine/engine/Lights.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ 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.f;
75-
GLfloat mMax = 0.f;
74+
GLfloat mMin = 0.25f;
75+
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
7878
// us

src/libs/engine/engine/Lines.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33

4+
#include <math/Color.h>
45
#include <math/Vector.h>
56

67
#include <renderer/GL_Loader.h>
@@ -15,6 +16,7 @@ struct LineSegment_glsl
1516
{
1617
alignas(4 * sizeof(GLfloat)) math::Position<3, GLfloat> mPointA;
1718
alignas(4 * sizeof(GLfloat)) math::Position<3, GLfloat> mPointB;
19+
alignas(4 * sizeof(GLfloat)) math::hdr::Rgba_f mColor = math::hdr::gWhite<GLfloat>;
1820
alignas(4 * sizeof(GLfloat)) GLfloat mWidth;
1921
};
2022

src/libs/engine/engine/resources/shaders/Line.vert

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#version 460
22

33

4+
#include "LineBlock.glsl"
45
#include "ViewProjectionBlock.glsl"
56

67

@@ -9,21 +10,6 @@ out vec4 ex_Color;
910

1011
uniform ivec2 u_FramebufferSize;
1112

12-
struct LineSegment
13-
{
14-
// We never use vec3 in buffer-backed interface blocks due to alignment complications
15-
// We could use the last float for a per-point width
16-
vec4 pointA;
17-
vec4 pointB;
18-
float width;
19-
};
20-
21-
22-
layout(std140, binding = 8) readonly buffer LinesSsbo
23-
{
24-
LineSegment ub_Segments[];
25-
};
26-
2713
const vec2 pos_data[4] = vec2[] (
2814
vec2( 1.0, -0.5),
2915
vec2( 1.0, 0.5),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct LineSegment
2+
{
3+
// We never use vec3 in buffer-backed interface blocks due to alignment complications
4+
// We could use the last float for a per-point width
5+
vec4 pointA;
6+
vec4 pointB;
7+
vec4 color;
8+
float width;
9+
};
10+
11+
12+
layout(std140, binding = 8) readonly buffer LinesSsbo
13+
{
14+
LineSegment ub_Segments[];
15+
};

src/libs/engine/engine/resources/shaders/LineRound.vert

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
#include "Constants.glsl"
5+
#include "LineBlock.glsl"
56
#include "ViewProjectionBlock.glsl"
67

78

@@ -11,29 +12,12 @@ out vec4 ex_Color;
1112

1213
uniform ivec2 u_FramebufferSize;
1314

14-
struct LineSegment
15-
{
16-
// We never use vec3 in buffer-backed interface blocks due to alignment complications
17-
// We could use the last float for a per-point width
18-
vec4 pointA;
19-
vec4 pointB;
20-
float width;
21-
};
22-
23-
24-
layout(std140, binding = 8) readonly buffer LinesSsbo
25-
{
26-
LineSegment ub_Segments[];
27-
};
28-
29-
3015
// Note: the idea to use a special pattern of positions to draw the line and the round endpoints
3116
// was inspired by: https://wwwtyro.net/2019/11/18/instanced-lines.html
3217
void main()
3318
{
34-
ex_Color = vec4(1);
35-
3619
LineSegment segment = ub_Segments[gl_InstanceID];
20+
ex_Color = segment.color;
3721
float width = segment.width;
3822
vec3 point = v_Position;
3923

0 commit comments

Comments
 (0)