From e70922ebb7dbdd193a4cc5ccb3d638e9410de5c8 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Wed, 6 Jun 2018 21:23:17 +0100 Subject: [PATCH] Fix various problems with the lighting calculations --- GL/lighting.c | 12 ++++++------ samples/lerabot01/main.c | 4 +++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/GL/lighting.c b/GL/lighting.c index e40f105..53cd500 100644 --- a/GL/lighting.c +++ b/GL/lighting.c @@ -250,7 +250,7 @@ void calculateLightingContribution(const GLint light, const GLfloat* pos, const }; GLfloat d; - vec3f_length(V.x, V.y, V.z, d); + vec3f_length(L.x, L.y, L.z, d); vec3f_normalize(L.x, L.y, L.z); vec3f_normalize(V.x, V.y, V.z); @@ -266,13 +266,13 @@ void calculateLightingContribution(const GLint light, const GLfloat* pos, const GLfloat VdotR = VdotN - NdotL; GLfloat specularPower = FPOW(VdotR > 0 ? VdotR : 0, MATERIAL.exponent); - GLfloat att = (l->position[3] == 0) ? 1.0f : ( - l->constant_attenuation / (1.0f + l->linear_attenuation * d) * (1.0f + l->quadratic_attenuation * d * d) + GLfloat att = (l->position[3] == 0.0f) ? 1.0f : ( + 1.0f / (l->constant_attenuation + (l->linear_attenuation * d) + (l->quadratic_attenuation * d * d)) ); - colour[0] = att * l->ambient[0] * MATERIAL.ambient[0] + f * (l->diffuse[0] * MATERIAL.diffuse[0] * NdotL + l->specular[0] * MATERIAL.specular[0] * specularPower); - colour[1] = att * l->ambient[1] * MATERIAL.ambient[1] + f * (l->diffuse[1] * MATERIAL.diffuse[1] * NdotL + l->specular[1] * MATERIAL.specular[1] * specularPower); - colour[2] = att * l->ambient[2] * MATERIAL.ambient[2] + f * (l->diffuse[2] * MATERIAL.diffuse[2] * NdotL + l->specular[2] * MATERIAL.specular[2] * specularPower); + colour[0] = att * (l->ambient[0] * MATERIAL.ambient[0] + f * (l->diffuse[0] * MATERIAL.diffuse[0] * NdotL + l->specular[0] * MATERIAL.specular[0] * specularPower)); + colour[1] = att * (l->ambient[1] * MATERIAL.ambient[1] + f * (l->diffuse[1] * MATERIAL.diffuse[1] * NdotL + l->specular[1] * MATERIAL.specular[1] * specularPower)); + colour[2] = att * (l->ambient[2] * MATERIAL.ambient[2] + f * (l->diffuse[2] * MATERIAL.diffuse[2] * NdotL + l->specular[2] * MATERIAL.specular[2] * specularPower)); colour[3] = MATERIAL.diffuse[3]; if(colour[0] > 1.0f) colour[0] = 1.0f; diff --git a/samples/lerabot01/main.c b/samples/lerabot01/main.c index d002df9..773b937 100644 --- a/samples/lerabot01/main.c +++ b/samples/lerabot01/main.c @@ -151,7 +151,6 @@ void InitGL(int Width, int Height) // We call this right after our OpenG glEnable(GL_LIGHTING); glDisable(GL_LIGHT0); glEnable(GL_LIGHT1); - glEnable(GL_COLOR_MATERIAL); GLfloat l1_pos[] = {5.0, 0.0, 1.0, 1.0}; GLfloat l1_diff[] = {1.0, 0.0, 0.0, 1.0}; @@ -160,6 +159,9 @@ void InitGL(int Width, int Height) // We call this right after our OpenG //glLightfv(GL_LIGHT1, GL_AMBIENT, l1_amb); glLightfv(GL_LIGHT1, GL_DIFFUSE, l1_diff); glLightfv(GL_LIGHT1, GL_POSITION, l1_pos); + glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.0f); + glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.0001); + glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.00001); } /* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */