From db0d39d487428df505386dee55e26cba0997e876 Mon Sep 17 00:00:00 2001 From: Luke Benstead Date: Sat, 4 Aug 2018 19:34:07 +0100 Subject: [PATCH] Cleanup lighting code a bit --- GL/lighting.c | 36 ++++++++++++++++++++++++------------ GL/private.h | 1 + 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/GL/lighting.c b/GL/lighting.c index 3a9aa9a..75509b8 100644 --- a/GL/lighting.c +++ b/GL/lighting.c @@ -47,6 +47,8 @@ void initLights() { LIGHTS[i].constant_attenuation = 1.0f; LIGHTS[i].linear_attenuation = 0.0f; LIGHTS[i].quadratic_attenuation = 0.0f; + + LIGHTS[i].is_directional = GL_FALSE; } } @@ -109,6 +111,8 @@ void APIENTRY glLightfv(GLenum light, GLenum pname, const GLfloat *params) { _matrixLoadModelView(); memcpy(LIGHTS[idx].position, params, sizeof(GLfloat) * 4); + LIGHTS[idx].is_directional = (params[3] == 0.0f) ? GL_TRUE : GL_FALSE; + mat_trans_single4( LIGHTS[idx].position[0], LIGHTS[idx].position[1], @@ -269,26 +273,34 @@ void calculateLightingContribution(const GLint light, const GLfloat* pos, const vec3f_normalize(V.x, V.y, V.z); - GLfloat NdotL; + GLfloat NdotL, VdotN; vec3f_dot(N.x, N.y, N.z, L.x, L.y, L.z, NdotL); - - GLfloat f = (NdotL < 0) ? 0 : 1; - - GLfloat VdotN; vec3f_dot(V.x, V.y, V.z, N.x, N.y, N.z, VdotN); GLfloat VdotR = VdotN - NdotL; GLfloat specularPower = FPOW(VdotR > 0 ? VdotR : 0, MATERIAL.exponent); - 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] = l->ambient[0] * MATERIAL.ambient[0]; + colour[1] = l->ambient[1] * MATERIAL.ambient[1]; + colour[2] = l->ambient[2] * MATERIAL.ambient[2]; colour[3] = MATERIAL.diffuse[3]; + if(NdotL >= 0) { + colour[0] += (l->diffuse[0] * MATERIAL.diffuse[0] * NdotL + l->specular[0] * MATERIAL.specular[0] * specularPower); + colour[1] += (l->diffuse[1] * MATERIAL.diffuse[1] * NdotL + l->specular[1] * MATERIAL.specular[1] * specularPower); + colour[2] += (l->diffuse[2] * MATERIAL.diffuse[2] * NdotL + l->specular[2] * MATERIAL.specular[2] * specularPower); + } + + if(!l->is_directional) { + GLfloat att = ( + 1.0f / (l->constant_attenuation + (l->linear_attenuation * d) + (l->quadratic_attenuation * d * d)) + ); + + colour[0] *= att; + colour[1] *= att; + colour[2] *= att; + } + if(colour[0] > 1.0f) colour[0] = 1.0f; if(colour[1] > 1.0f) colour[1] = 1.0f; if(colour[2] > 1.0f) colour[2] = 1.0f; diff --git a/GL/private.h b/GL/private.h index 120460d..6ef39b1 100644 --- a/GL/private.h +++ b/GL/private.h @@ -59,6 +59,7 @@ typedef struct { GLfloat diffuse[4]; GLfloat specular[4]; GLfloat ambient[4]; + GLboolean is_directional; } LightSource;