Skip to content

Commit

Permalink
Fix support for directional lights
Browse files Browse the repository at this point in the history
The default light is a directional one, so we must initialize its
position (which, according to the standard, is (0, 0, 1)) following the
same algorithm we apply when glLightfv() is called for directional
lights: we multiply its position by 100000. The reason why we do this is
that GX does not support ideal directional lights, so we simulate them
by placing the light very far away and disabling the distance
attenuation.

Also, the light direction parameter was not set on the GX light, so we
fix that too.
  • Loading branch information
mardy committed Apr 29, 2024
1 parent 6dcb126 commit 1abf505
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/gc_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,11 @@ void ogx_initialize()
glparamstate.lighting.lights[i].atten[1] = 0;
glparamstate.lighting.lights[i].atten[2] = 0;

/* The default value for light position is (0, 0, 1), but since it's a
* directional light we need to transorm it to 100000. */
glparamstate.lighting.lights[i].position[0] = 0;
glparamstate.lighting.lights[i].position[1] = 0;
glparamstate.lighting.lights[i].position[2] = 1;
glparamstate.lighting.lights[i].position[2] = 100000;
glparamstate.lighting.lights[i].position[3] = 0;

glparamstate.lighting.lights[i].direction[0] = 0;
Expand Down Expand Up @@ -1726,9 +1728,12 @@ int __prepare_lighting()

// FIXME: Need to consider spotlights
if (glparamstate.lighting.lights[i].position[3] == 0) {
// Directional light, it's a point light very far without atenuation
// Directional light, it's a point light very far without attenuation
GX_InitLightAttn(&glparamstate.lighting.lightobj[i], 1, 0, 0, 1, 0, 0);
GX_InitLightAttn(&glparamstate.lighting.lightobj[i + 4], 1, 0, 0, 1, 0, 0);
float *dir = glparamstate.lighting.lights[i].direction;
GX_InitLightDir(&glparamstate.lighting.lightobj[i], dir[0], dir[1], dir[2]);
GX_InitLightDir(&glparamstate.lighting.lightobj[i + 4], dir[0], dir[1], dir[2]);
} else {
// Point light
GX_InitLightAttn(&glparamstate.lighting.lightobj[i], 1, 0, 0,
Expand Down

0 comments on commit 1abf505

Please sign in to comment.