Skip to content

Commit 47aced4

Browse files
committed
Fix camera calculations
1 parent 5265880 commit 47aced4

File tree

6 files changed

+18
-25
lines changed

6 files changed

+18
-25
lines changed

src/Cluster.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,15 @@ void Cluster::initialize(int _argc, char* _argv[])
114114

115115
// Sponza
116116
// debug camera + lights
117-
118-
//scene->camera.move({ -7.0f, 2.0f, 0.0f });
119-
//scene->camera.rotate({ -45.0f, -90.0f });
120-
121-
//scene->camera.lookAt({ -7.0f, 2.0f, 0.0f }, scene->center, glm::vec3(0.0f, 1.0f, 0.0f));
122-
117+
scene->camera.lookAt({ -7.0f, 2.0f, 0.0f }, scene->center, glm::vec3(0.0f, 1.0f, 0.0f));
123118
scene->pointLights.lights = {
124119
// pos, power
125120
{ { -5.0f, 0.3f, 0.0f }, { 100.0f, 100.0f, 100.0f } },
126121
{ { 0.0f, 0.3f, 0.0f }, { 100.0f, 100.0f, 100.0f } },
127122
{ { 5.0f, 0.3f, 0.0f }, { 100.0f, 100.0f, 100.0f } }
128123
};
129124
scene->pointLights.update();
130-
config->lights = 3;
131-
132-
//generateLights(config->lights);
125+
config->lights = scene->pointLights.lights.size();
133126

134127
frameNumber = 0;
135128
}
@@ -413,7 +406,8 @@ void Cluster::generateLights(unsigned int count)
413406
for(size_t i = keep; i < count; i++)
414407
{
415408
glm::vec3 position = glm::vec3(dist(mt), dist(mt), dist(mt)) * scale - (scale * 0.5f);
416-
position.y = glm::abs(position.y);
409+
//position += scene->center; // not Sponza
410+
position.y = glm::abs(position.y); // Sponza
417411
glm::vec3 color = glm::vec3(dist(mt), dist(mt), dist(mt));
418412
glm::vec3 power = color * (dist(mt) * (POWER_MAX - POWER_MIN) + POWER_MIN);
419413
lights[i] = {

src/Renderer/Shaders/fs_deferred_pointlight.sc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
SAMPLER2D(s_texDiffuseA, SAMPLER_DEFERRED_DIFFUSE_A);
99
SAMPLER2D(s_texNormal, SAMPLER_DEFERRED_NORMAL);
1010
SAMPLER2D(s_texF0Metallic, SAMPLER_DEFERRED_F0_METALLIC);
11-
SAMPLER2D(s_texEmissiveOcclusion, SAMPLER_DEFERRED_EMISSIVE_OCCLUSION);
1211
SAMPLER2D(s_texDepth, SAMPLER_DEFERRED_DEPTH);
1312

1413
uniform vec4 u_lightIndexVec;
@@ -35,8 +34,6 @@ void main()
3534
screen.z = texture2D(s_texDepth, texcoord).x;
3635
vec3 fragPos = screen2Eye(screen).xyz;
3736

38-
vec3 V = normalize(-fragPos);
39-
4037
// lighting
4138

4239
PointLight light = getPointLight(u_lightIndex);
@@ -47,6 +44,7 @@ void main()
4744
float attenuation = smoothAttenuation(dist, light.radius);
4845
if(attenuation > 0.0)
4946
{
47+
vec3 V = normalize(-fragPos);
5048
vec3 L = normalize(light.position - fragPos);
5149
vec3 radianceIn = light.intensity * attenuation;
5250
float NoL = saturate(dot(N, L));

src/Renderer/Shaders/pbr.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ vec3 pbrNormal(vec2 texcoord)
8181
{
8282
if(u_hasNormalTexture)
8383
{
84-
return normalize((texture2D(s_texNormal, texcoord).rgb * 2.0) - 1.0) * u_normalScale;
84+
// the normal scale can cause problems and serves no real purpose
85+
// normal compression and BRDF calculations assume unit length
86+
return normalize((texture2D(s_texNormal, texcoord).rgb * 2.0) - 1.0); // * u_normalScale;
8587
}
8688
else
8789
{

src/Scene/Camera.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void Camera::rotate(glm::vec2 delta)
1717
delta = glm::radians(delta);
1818

1919
// limit pitch
20-
float dot = glm::dot(orthUp, forward());
20+
float dot = glm::dot(upAxis, forward());
2121
if((dot < -0.99f && delta.x < 0.0f) || // angle nearing 180 degrees
2222
(dot > 0.99f && delta.x > 0.0f)) // angle nearing 0 degrees
2323
delta.x = 0.0f;
@@ -40,21 +40,19 @@ void Camera::zoom(float offset)
4040
void Camera::lookAt(const glm::vec3& position, const glm::vec3& target, const glm::vec3& up)
4141
{
4242
pos = position;
43+
upAxis = up;
4344

44-
rotation = glm::quatLookAtLH(target - position, up);
45-
46-
/*
4745
// model rotation
4846
// maps vectors to camera space (x, y, z)
4947
glm::vec3 forward = glm::normalize(target - position);
5048
rotation = glm::rotation(forward, Z);
5149

5250
// correct the up vector
53-
glm::vec3 right = glm::cross(glm::normalize(up), forward); // left-handed coordinate system
54-
orthUp = -glm::cross(right, forward);
51+
// the cross product of non-orthogonal vectors is not normalized
52+
glm::vec3 right = glm::normalize(glm::cross(glm::normalize(up), forward)); // left-handed coordinate system
53+
glm::vec3 orthUp = glm::cross(forward, right);
5554
glm::quat upRotation = glm::rotation(rotation * orthUp, Y);
5655
rotation = upRotation * rotation;
57-
*/
5856

5957
// inverse of the model rotation
6058
// maps camera space vectors to model vectors

src/Scene/Camera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct Camera
4040
static constexpr float MIN_FOV = 10.0f;
4141
static constexpr float MAX_FOV = 90.0f;
4242

43-
glm::vec3 orthUp = Y;
43+
glm::vec3 upAxis = Y;
4444

4545
glm::vec3 pos = { 0.0f, 0.0f, 0.0f };
4646
glm::quat rotation = glm::identity<glm::quat>();

src/Scene/Scene.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ bool Scene::load(const char* file)
150150
}
151151
else
152152
{
153-
Log->info("No camera, using default");
154-
155-
camera.lookAt(center - glm::vec3(0.0f, 0.0f, diagonal / 2.0f), center, glm::vec3(0.0f, 1.0f, 0.0f));
153+
Log->info("No camera");
154+
camera.lookAt(center - glm::vec3(0.0f, 0.0f, diagonal / 2.0f),
155+
center,
156+
glm::vec3(0.0f, 1.0f, 0.0f));
156157
camera.zFar = diagonal;
157158
camera.zNear = camera.zFar / 50.0f;
158159
}

0 commit comments

Comments
 (0)