Fix bug from last commit, don't apply lights at all if none are enabled

This commit is contained in:
Luke Benstead 2020-05-22 07:20:08 +01:00
parent 0a92240c64
commit 9bfb0c6eea
2 changed files with 42 additions and 26 deletions

View File

@ -27,10 +27,22 @@ static GLenum COLOR_MATERIAL_MODE = GL_AMBIENT_AND_DIFFUSE;
static GLenum COLOR_MATERIAL_MASK = AMBIENT_MASK | DIFFUSE_MASK; static GLenum COLOR_MATERIAL_MASK = AMBIENT_MASK | DIFFUSE_MASK;
static LightSource LIGHTS[MAX_LIGHTS]; static LightSource LIGHTS[MAX_LIGHTS];
static GLuint ENABLED_LIGHT_COUNT = 0;
static Material MATERIAL; static Material MATERIAL;
GL_FORCE_INLINE void _glPrecalcLightingValues(GLuint mask); GL_FORCE_INLINE void _glPrecalcLightingValues(GLuint mask);
static void recalcEnabledLights() {
GLubyte i;
ENABLED_LIGHT_COUNT = 0;
for(i = 0; i < MAX_LIGHTS; ++i) {
if(LIGHTS[i].isEnabled) {
ENABLED_LIGHT_COUNT++;
}
}
}
void _glInitLights() { void _glInitLights() {
static GLfloat ONE [] = {1.0f, 1.0f, 1.0f, 1.0f}; static GLfloat ONE [] = {1.0f, 1.0f, 1.0f, 1.0f};
static GLfloat ZERO [] = {0.0f, 0.0f, 0.0f, 1.0f}; static GLfloat ZERO [] = {0.0f, 0.0f, 0.0f, 1.0f};
@ -71,10 +83,12 @@ void _glInitLights() {
} }
_glPrecalcLightingValues(~0); _glPrecalcLightingValues(~0);
recalcEnabledLights();
} }
void _glEnableLight(GLubyte light, GLboolean value) { void _glEnableLight(GLubyte light, GLboolean value) {
LIGHTS[light].isEnabled = value; LIGHTS[light].isEnabled = value;
recalcEnabledLights();
} }
GL_FORCE_INLINE void _glPrecalcLightingValues(GLuint mask) { GL_FORCE_INLINE void _glPrecalcLightingValues(GLuint mask) {
@ -441,15 +455,12 @@ GL_FORCE_INLINE void _glLightVertexPoint(
#undef _PROCESS_COMPONENT #undef _PROCESS_COMPONENT
} }
void _glPerformLighting(Vertex* vertices, const EyeSpaceData* es, const int32_t count) { void _glPerformLighting(Vertex* vertices, EyeSpaceData* es, const int32_t count) {
int16_t i; GLubyte i;
int32_t j; GLuint j;
Vertex* vertex = vertices; Vertex* vertex = vertices;
const EyeSpaceData* data = es; EyeSpaceData* data = es;
/* Final colour of lighting output (will be clamped to argb) */
float final[4];
/* Calculate the colour material function once */ /* Calculate the colour material function once */
void (*updateColourMaterial)(const GLubyte*) = NULL; void (*updateColourMaterial)(const GLubyte*) = NULL;
@ -472,16 +483,21 @@ void _glPerformLighting(Vertex* vertices, const EyeSpaceData* es, const int32_t
} }
/* Calculate the ambient lighting and set up colour material */ /* Calculate the ambient lighting and set up colour material */
for(j = 0; j < count; ++j, ++vertex) { for(j = 0; j < count; ++j, ++vertex, ++data) {
if(updateColourMaterial) { if(updateColourMaterial) {
updateColourMaterial(vertex->bgra); updateColourMaterial(vertex->bgra);
} }
/* Copy the base colour across */ /* Copy the base colour across */
vec4cpy(final, MATERIAL.baseColour); vec4cpy(data->finalColour, MATERIAL.baseColour);
}
if(!ENABLED_LIGHT_COUNT) {
return;
} }
vertex = vertices; vertex = vertices;
data = es;
for(j = 0; j < count; ++j, ++vertex, ++data) { for(j = 0; j < count; ++j, ++vertex, ++data) {
/* Direction to vertex in eye space */ /* Direction to vertex in eye space */
float Vx = -data->xyz[0]; float Vx = -data->xyz[0];
@ -494,13 +510,15 @@ void _glPerformLighting(Vertex* vertices, const EyeSpaceData* es, const int32_t
const float Nz = data->n[2]; const float Nz = data->n[2];
for(i = 0; i < MAX_LIGHTS; ++i) { for(i = 0; i < MAX_LIGHTS; ++i) {
if(!LIGHTS[i].isEnabled) continue; if(!LIGHTS[i].isEnabled) {
continue;
}
if(LIGHTS[i].isDirectional) {
float Lx = LIGHTS[i].position[0] - data->xyz[0]; float Lx = LIGHTS[i].position[0] - data->xyz[0];
float Ly = LIGHTS[i].position[1] - data->xyz[1]; float Ly = LIGHTS[i].position[1] - data->xyz[1];
float Lz = LIGHTS[i].position[2] - data->xyz[2]; float Lz = LIGHTS[i].position[2] - data->xyz[2];
if(LIGHTS[i].isDirectional) {
float Hx = (Lx + 0); float Hx = (Lx + 0);
float Hy = (Ly + 0); float Hy = (Ly + 0);
float Hz = (Lz + 1); float Hz = (Lz + 1);
@ -521,13 +539,10 @@ void _glPerformLighting(Vertex* vertices, const EyeSpaceData* es, const int32_t
if(NdotH < 0.0f) NdotH = 0.0f; if(NdotH < 0.0f) NdotH = 0.0f;
_glLightVertexDirectional( _glLightVertexDirectional(
final, data->finalColour,
i, LdotN, NdotH i, LdotN, NdotH
); );
} else { } else {
float Lx = LIGHTS[i].position[0] - data->xyz[0];
float Ly = LIGHTS[i].position[1] - data->xyz[1];
float Lz = LIGHTS[i].position[2] - data->xyz[2];
float D; float D;
vec3f_length(Lx, Ly, Lz, D); vec3f_length(Lx, Ly, Lz, D);
@ -561,17 +576,17 @@ void _glPerformLighting(Vertex* vertices, const EyeSpaceData* es, const int32_t
if(NdotH < 0.0f) NdotH = 0.0f; if(NdotH < 0.0f) NdotH = 0.0f;
_glLightVertexPoint( _glLightVertexPoint(
final, data->finalColour,
i, LdotN, NdotH, att i, LdotN, NdotH, att
); );
} }
} }
} }
vertex->bgra[R8IDX] = clamp(final[0] * 255.0f, 0, 255); vertex->bgra[R8IDX] = clamp(data->finalColour[0] * 255.0f, 0, 255);
vertex->bgra[G8IDX] = clamp(final[1] * 255.0f, 0, 255); vertex->bgra[G8IDX] = clamp(data->finalColour[1] * 255.0f, 0, 255);
vertex->bgra[B8IDX] = clamp(final[2] * 255.0f, 0, 255); vertex->bgra[B8IDX] = clamp(data->finalColour[2] * 255.0f, 0, 255);
vertex->bgra[A8IDX] = clamp(final[3] * 255.0f, 0, 255); vertex->bgra[A8IDX] = clamp(data->finalColour[3] * 255.0f, 0, 255);
} }
} }

View File

@ -360,11 +360,12 @@ GLboolean _glIsNormalizeEnabled();
GLboolean _glRecalcFastPath(); GLboolean _glRecalcFastPath();
typedef struct { typedef struct {
float xyz[3]; float xyz[3]; // 12 bytes
float n[3]; float n[3]; // 12 bytes
float finalColour[4]; //16 bytes (to 40)
} EyeSpaceData; } EyeSpaceData;
extern void _glPerformLighting(Vertex* vertices, const EyeSpaceData* es, const int32_t count); extern void _glPerformLighting(Vertex* vertices, EyeSpaceData *es, const int32_t count);
unsigned char _glIsClippingEnabled(); unsigned char _glIsClippingEnabled();
void _glEnableClipping(unsigned char v); void _glEnableClipping(unsigned char v);