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 > Help, glLight.

#158015 - AntonioND - Tue Jun 03, 2008 3:25 pm

Hello everybody!

I'm coding a 3D game, but I just can't make the lights work in real DS (in iDeaS they seem to work...). I have read all 3D examples of libnds where light is used, and changed my code a lots of times, but it's the same, I always see my 3D scene in it's normal colors...

Thanks for your help.


Note: I haven't tried no$gba because my game uses FAT, I don't know how to use fscr dldi and I don't want to learn only for this, i usually check on ds using wifiloader.

Note 2: I'm spanish, if you don't understand something, just tell me and I'll try to explain myself better.

---------------------
Here is my code:
---------------------

Before this, I init PAlib.

I use this to init 3D mode:

Code:

int texture[128];

void My_Init_3D()
{
lcdMainOnTop();
videoSetMode(MODE_0_3D);
vramSetBankA(VRAM_A_TEXTURE);
vramSetBankB(VRAM_B_TEXTURE);

glInit();
glEnable(GL_TEXTURE_2D | GL_ANTIALIAS | GL_BLEND);
glClearColor(0,0,0,31);
glClearPolyID(63);
glClearDepth(GL_MAX_DEPTH);
glViewport(0,0,255,191);

glGenTextures(128, &texture[0]);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, 256.0 / 192.0, 0.1, 40);

glMaterialf(GL_AMBIENT, RGB15(16,16,16));
glMaterialf(GL_DIFFUSE, RGB15(20,20,20));
glMaterialf(GL_SPECULAR, BIT(15) | RGB15(8,8,8));
glMaterialf(GL_EMISSION, RGB15(5,5,5));
glMaterialShinyness();
}



I call this function in the main loop once a frame:

Code:
void 3D()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(  -12.0, 4.0, 0.0,   
         8.0, 0.0, 0.0,   
         0.0, 1.0, 0.0);   

//This is only for testing...
if(Pad.Held.X) glLight(0, RGB15(31, 0,0) , 0, floattov10(-1.0), 0);
else glLight(0, RGB15(1, 0,0) , 0, floattov10(-1.0), 0);
if(Pad.Held.B) glLight(1, RGB15(0, 31, 0) , 0, floattov10(-1.0), 0);
else glLight(1, RGB15(0, 1, 0) , 0, floattov10(-1.0), 0);
if(Pad.Held.Y) glLight(2, RGB15(0, 0, 31) , 0, floattov10(-1.0), 0);
else glLight(2, RGB15(0, 0, 1) , 0, floattov10(-1.0), 0);

glPolyFmt(POLY_ID(1) | POLY_ALPHA(31) | POLY_CULL_BACK | POLY_FORMAT_LIGHT0| POLY_FORMAT_LIGHT1| POLY_FORMAT_LIGHT2);


Draw_Objects();
/*For example:
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glColor3f(1,0,0);
glVertex3f(-8,0,-8);
glColor3f(0,1,0);
glVertex3f(-8,0,8);
glColor3f(0,0,1);
glVertex3f(8,0,8);
glColor3f(1,1,0);
glVertex3f(8,0,-8);
glEnd();
*/

glFlush(0);
}

#158020 - elhobbs - Tue Jun 03, 2008 5:38 pm

I think this is your problem
Code:
floattov10(-1.0)

the values for the light vectors are 10 bit - 1 bit for the sign and 9 bits for the fractional value. In other words there is no whole number piece. so you need to use a value slightly smaller (in magnitude) than -1.0 like -0.99. the floattov10 macro handles values > 0.998 correctly but it does not handle values <= -0.998 correctly.

here is the macro for reference:
Code:

#define floattov10(n)        ((n>.998) ? 0x1FF : ((v10)((n)*(1<<9)))) /*!< \brief convert float to v10 */

#158021 - DiscoStew - Tue Jun 03, 2008 5:44 pm

Code:
glNormal3f(0,1,0);
glColor3f(1,0,0);


One thing to take note with the NDS is that glNormal and glColor cannot be used together, because they share the same spot when entering vertex data. While glColor write the color directly, glNormal calculates the color using the active lights and it's own vector inputted. You've called glNormal once, but right after, you called glColor, which overwrites any color calculation made by glNormal. If you know how normals are calculated on the NDS, you "could" essentially mix the two via software.
_________________
DS - It's all about DiscoStew

#158022 - AntonioND - Tue Jun 03, 2008 6:02 pm

elhobbs wrote:
[...]

I didn't know that, but even changing the values it doesn't work... It's another thing:

DiscoStew wrote:
[...]

Thanks for your comment... I thought that if I used lights and colours in the same polygon I wouldn's see the colors and see the lights.

If I take out glColor3f(1,0,0); in that quad, I can see the light. This made me think... The problem with the other objects (displayed using glCallList) is that the callList modify their colour and I can't apply light in them... I'm converting them with this:
http://forum.gbadev.org/viewtopic.php?t=14947
I'll made an app that removes the things I don't want. I think I can do it. :)

#158023 - DiscoStew - Tue Jun 03, 2008 6:18 pm

In a way, glNormal is an extention to glColor (though not directly), in which the end result is a color value that is written into the buffer to be executed by the hardware. glColor will overwrite glNormal, and vice versa. That's why they can't be mixed together normally.
_________________
DS - It's all about DiscoStew

#158030 - silent_code - Tue Jun 03, 2008 7:40 pm

but you can use the color you would like to set with glColor() and make it the material's diffuse color and still have lighting work correctly. ;^)

gbatek wrote:
40004C0h - Cmd 30h - DIF_AMB - MaterialColor0 - Diffuse/Ambient Reflect. (W)

0-4 Diffuse Reflection Red ;\light(s) that directly hits the polygon,
5-9 Diffuse Reflection Green ; ie. max when NormalVector has opposite
10-14 Diffuse Reflection Blue ;/direction of LightVector
15 Set Vertex Color (0=No, 1=Set Diffuse Reflection Color as Vertex Color)
16-20 Ambient Reflection Red ;\light(s) that indirectly hits the polygon,
21-25 Ambient Reflection Green ; ie. assuming that light is reflected by
26-30 Ambient Reflection Blue ;/walls/floor, regardless of LightVector
31 Not used

With Bit15 set, the lower 15bits are applied as VertexColor (exactly as when when executing the Color command), the purpose is to use it as default color (eg. when outcommenting the Normal command), normally, when using lighting, the color setting gets overwritten (as soon as executing the Normal command).

_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158035 - AntonioND - Tue Jun 03, 2008 8:05 pm

I don't know how to do that XD (i'm noob at this).
Instead I'll code my own app to delete FIFO_COLOR and its arguments from a callList. I've been looking at libnds and I think I understand how it works.
Isn't it ironic? :P

#158036 - silent_code - Tue Jun 03, 2008 9:03 pm

there's a demo on my site that uses this "technique". :^)
you are free to check it out. it's really easy to do.

Code:
   glMaterialf(GL_AMBIENT, RGB15(0, 0, 0));
   glMaterialf(GL_DIFFUSE, RGB15(31, 31, 31));
   glMaterialf(GL_SPECULAR, RGB15(0, 0, 0) | BIT(15)); // notice the additional BIT(15)!
   glMaterialf(GL_EMISSION, RGB15(0, 0, 0));

_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158083 - AntonioND - Wed Jun 04, 2008 2:59 pm

silent_code wrote:
there's a demo on my site that uses this "technique". :^)
you are free to check it out. it's really easy to do.

Code:
   glMaterialf(GL_AMBIENT, RGB15(0, 0, 0));
   glMaterialf(GL_DIFFUSE, RGB15(31, 31, 31));
   glMaterialf(GL_SPECULAR, RGB15(0, 0, 0) | BIT(15)); // notice the additional BIT(15)!
   glMaterialf(GL_EMISSION, RGB15(0, 0, 0));


It doesn't work, I don't know why. But...

Yesterday I investigated a bit, and now I understand perfectly the way callLists work. I managed to fix one simple model, and this afternoon I'll make an app to do the same to all my models. ^^

EDIT: Half-done :P.

#158100 - silent_code - Wed Jun 04, 2008 10:11 pm

what doesn't work? the material or the demo?

if it's the demo, run it in no$gba. you won't be able to see all features, as some of them aren't emulated, yet, but you'll be able to see that you can change the material color and still get perfectly valid lighting. :^)

if it's the material command, there are some restrictions in some cases and you need to set the normal it again, after issuing certain commands:

gbatek wrote:
When using lighting, the Normal command must be re-executed after switching Lighting on/off, or after changing light/material parameters.


NOTE: (in case it wasn't clear before) don't use the glColor() command! you have to set the color with the material!
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.


Last edited by silent_code on Wed Jun 04, 2008 10:15 pm; edited 1 time in total

#158101 - AntonioND - Wed Jun 04, 2008 10:15 pm

Even if I put your code, my models from callLists are shown as before.

In addition, I prefer to fix my callLists and set the material propierties I want.
I have almost finished my program to do this, so don't worry.

EDIT: It's finished. It calculates bad the final size, but it works!! As soon as I fix it and I fix some models to test it i'll upload it.

#158103 - silent_code - Wed Jun 04, 2008 10:53 pm

yes, you have to set the material command in a dl, that's right. :^)
i was ignoring the fact that you use dl, my bad. ;^)

happy coding! :^)
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#158138 - AntonioND - Thu Jun 05, 2008 2:43 pm

http://forum.gbadev.org/viewtopic.php?p=158137#158137

I have uploaded my program. If someone wants to modify it feel free to do it. ^^