gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Lighting problems

#112329 - pettersson - Fri Dec 15, 2006 8:07 am

hi guys,

working on a small 3d test application, i've encountered a problem. all my objects do not get lit properly. all i can see is ambient color, and i just can't find the error in my code. I've broken down the code to a single lightsource and a single triangle with one normal for every vertex. Here is the triangle code
Code:

u32 triangle[] =
{
   12,
   FIFO_COMMAND_PACK(FIFO_BEGIN, FIFO_NORMAL, FIFO_VERTEX16, FIFO_NORMAL),
   GL_TRIANGLE,
   NORMAL_PACK( 0, 0, floattov10(1.0) ),
   VERTEX_PACK(inttov16(-1),inttov16(-1)), VERTEX_PACK(0,0),
   NORMAL_PACK( 0, 0, floattov10(1.0) ),
   FIFO_COMMAND_PACK(FIFO_VERTEX16, FIFO_NORMAL, FIFO_VERTEX16, FIFO_END),
   VERTEX_PACK(inttov16(1),inttov16(-1)), VERTEX_PACK(0,0),
   NORMAL_PACK( 0, 0, floattov10(1.0) ),
   VERTEX_PACK(inttov16(0),inttov16(1)), VERTEX_PACK(0,0),
};

And here goes the rendering code
Code:

glReset();
gluPerspective(35, 256.0 / 192.0, 0.1, 40);
   
glLight( 0, RGB15(31,31,31) , 0, 0, floattov10(-1.f) );
         
gluLookAt( 0.0, 0.0, 3.0,      //camera position
           0.0, 0.0, 0.0,      //look at
           0.0, 1.0, 0.0 );   //up
   
            
glMaterialf(GL_AMBIENT, RGB15(8,8,8));
glMaterialf(GL_DIFFUSE, BIT(15)|RGB15(31,0,0) );
glMaterialf(GL_SPECULAR, BIT(15) | RGB15(31,31,31));
glMaterialf(GL_EMISSION, RGB15(0,0,0));
glMaterialShinyness();
   
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
  glPolyFmt( POLY_ALPHA(31) | POLY_CULL_BACK | POLY_FORMAT_LIGHT0 );
  glCallList( triangle );
glPopMatrix( 0 );
glFlush();

and all i get is just a triangle showing its ambient color. So obviously i must be doing something wrong, i just can't see what.
Maybe one of you guys can help me

thanks in advance,
pettersson

#112408 - memoni - Sat Dec 16, 2006 12:29 am

If I dont remember horribly wrong, the DS supports only directional lights, so the z component of the normal dot light direction is:

1 * -1 = -1, which clamps to 0 in light calculations.

Try to make the light direction (0,0,1)

#112425 - pettersson - Sat Dec 16, 2006 7:19 am

Yes, the DS only supports parallel light sources. But 1*-1 = -1 is not the way you calculate lighting. (0,0,-1) is the light's directional vector. So it should be ok this way. (it doesn't help to change to 0,0,1). Still only ambient color.
Heck, what am i doing wrong here.

.pettersson

#112427 - DiscoStew - Sat Dec 16, 2006 8:01 am

I'm not sure, but doesn't the DS have problems when using the float value of 1 (or -1) with normals? Try 0.9999 (or -0.9999).
_________________
DS - It's all about DiscoStew

#112430 - pettersson - Sat Dec 16, 2006 8:59 am

Ooouu.. Thx a lot. That's it. Changing values from 1.0 to 0.999 fixes my problem :)

Danke
.pettersson

#112454 - silent_code - Sat Dec 16, 2006 4:19 pm

sorry for this afterthrow. you can simply subtract 1 from the 10 bit fixed point number and it works (like "floattov10(1.0) - 1")... i remember there was some note that was supposed to remind people they can't use 1.0 for the number part, as the tenth bit is the sign... ;)

#112471 - ikaris - Sat Dec 16, 2006 5:56 pm

pettersson wrote:
Ooouu.. Thx a lot. That's it. Changing values from 1.0 to 0.999 fixes my problem :)


I had the same problem with my Maya exporter, and now I detect if the normal is 1 and swap it for 0.99999 instead...

fixes the problem.