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 > [SOLVED]Help with glLight() ,please.

#123281 - adzz182 - Mon Mar 26, 2007 11:15 pm

Im new to openGL programming, im kinda making a 3D terrain navigating engine to practice programming. I think i have got the navigation bit sorted, but i am having trouble with toon shading and lighting. Could you please take a quick look over my code and discover why the lighting doesnt seem to work. I know its abit long, but i included the whole code so anyone that would like to compile it themselves can.
Code:

#include <nds.h>
#include <stdio.h>
#define ROTSPEED 6
#define MOVESPEED 15
#define piOver180 0.01745329252

int DrawGLScene();

int32 xpos;
int32 zpos;
int heading,heading90;
int map_size=8;
bool done = FALSE;

int main()
{
   powerON(POWER_ALL);

   videoSetMode(MODE_0_3D);
   videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE);
   vramSetBankC(VRAM_C_SUB_BG);
   BG_PALETTE_SUB[255] = RGB15(31,31,0);
   consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(31), (u16*)CHAR_BASE_BLOCK_SUB(0), 16);

   irqInit();
   irqEnable(IRQ_VBLANK);

   glViewPort(0,0,255,191);


   glClearColor(8,8,31);
   glClearDepth(0x7FFF);

   glSetToonTableRange( 0, 0, RGB15(0,0,0));
   glSetToonTableRange( 1, 12, RGB15(10,10,10) );
   glSetToonTableRange( 13, 31, RGB15(25,25,25) );

   while(1)
   {
      glReset();

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

      glLight(0, RGB15(31,31,31),0,floattov10(-1.0),0);

      glMatrixMode(GL_MODELVIEW);

      glMaterialf(GL_AMBIENT, RGB15(8,8,8));
      glMaterialf(GL_DIFFUSE, RGB15(24,24,24));
      glMaterialf(GL_SPECULAR, RGB15(0,0,0));
      glMaterialf(GL_EMISSION, RGB15(0,0,0));

      glPolyFmt(POLY_ALPHA(31) | POLY_CULL_BACK | POLY_FORMAT_LIGHT0 | POLY_FORMAT_LIGHT1 | POLY_TOON_SHADING);


scanKeys();
      heading90  = ((heading+128)&511);
      if (keysHeld() & KEY_LEFT)
      {
         xpos -= SIN[heading90] >> 4;
         zpos += COS[heading90] >> 4;
      }
      if (keysHeld() & KEY_RIGHT)
      {
         xpos += SIN[heading90] >> 4;
         zpos -= COS[heading90] >> 4;
      }

      if (keysHeld() & KEY_DOWN)
      {

        xpos -= (SIN[heading]>>4);
        zpos += (COS[heading]>>4);
      }
      if (keysHeld() & KEY_UP)
      {
        xpos += (SIN[heading]>>4);
        zpos -= (COS[heading]>>4);
      }

      if (keysHeld() & KEY_R)
      {
        heading=(heading+ROTSPEED)&511;
      }
      if(keysHeld() & KEY_L)
      {
        heading=(heading-ROTSPEED)&511;
      }


            glPushMatrix();   

      DrawGLScene();
      
      glPopMatrix(1);
      swiWaitForVBlank();
      glFlush();
   }

   return 0;
}

int DrawGLScene()
{
   glRotatef32i(-25,(1<<12),0,0);
   glRotatef32i((512-heading),0,(1<<12),0);
   glTranslate3f32(-xpos+(SIN[heading]<<1),0.0f,-zpos-(COS[heading]<<1));   

   glBegin(GL_QUADS);
     int x,z;
     glVertex3f(-1.0f,-1.0f,1.0f);
     glVertex3f(1.0f,-1.0f,1.0f);
     glVertex3f(1.0f,1.0f,1.0f);
     glVertex3f(-1.0f,1.0f,1.0f);

     glVertex3f(1.0f,-1.0f,1.0f);
     glVertex3f(1.0f,-1.0f,-1.0f);
     glVertex3f(1.0f,1.0f,-1.0f);
     glVertex3f(1.0f,1.0f,1.0f);

     glVertex3f(1.0f,-1.0f,-1.0f);
     glVertex3f(-1.0f,-1.0f,-1.0f);
     glVertex3f(-1.0f,1.0f,-1.0f);
     glVertex3f(1.0f,1.0f,-1.0f);

     glVertex3f(-1.0f,-1.0f,-1.0f);
     glVertex3f(-1.0f,-1.0f,1.0f);
     glVertex3f(-1.0f,1.0f,1.0f);
     glVertex3f(-1.0f,1.0f,-1.0f);

     glVertex3f(-1.0f,1.0f,1.0f);
     glVertex3f(1.0f,1.0f,1.0f);
     glVertex3f(1.0f,1.0f,-1.0f);
     glVertex3f(-1.0f,1.0f,-1.0f);

     glVertex3f(-1.0f,-1.0f,1.0f);
     glVertex3f(1.0f,-1.0f,1.0f);
     glVertex3f(1.0f,-1.0f,-1.0f);
     glVertex3f(-1.0f,-1.0f,-1.0f);


   for(x=-map_size;x<map_size;x+=1.0f)
   {
     for(z=-map_size;z<map_size;z+=1.0f)
     {
       glVertex3f(x     ,-1.0f,z     );
       glVertex3f(x     ,-1.0f,z-1.0f);

       glVertex3f(x-1.0f,-1.0f,z-1.0f);
       glVertex3f(x-1.0f,-1.0f,z     );
     }
     }
   glEnd();
   SUB_BG0_CR = BG_MAP_BASE(31);
   consoleClear();
   iprintf("X-Position : %d\n",xpos);
   iprintf("Z-Position : %d\n",zpos);
   iprintf("SIN[hding] : %d\n",SIN[heading]>>4);
   iprintf("COS[hding] : %d\n",COS[heading]>>4);
   iprintf("Heading    : %d\n",heading);
   iprintf("512-Rot    : %d\n",(512-heading));
   iprintf("Map Size   : %d\n",map_size);
   return TRUE;
}



Thanks alot,
adam


Last edited by adzz182 on Tue Mar 27, 2007 9:41 pm; edited 1 time in total

#123305 - ikaris - Tue Mar 27, 2007 5:00 am

Your geometry doesn't have any normals...

You can't have lighting without normals.

Look into using
Code:
glNormal3f

#123331 - adzz182 - Tue Mar 27, 2007 9:31 am

thanks alot, ill look into it

#123362 - ikaris - Tue Mar 27, 2007 5:50 pm

adzz182 wrote:
thanks alot, ill look into it


Also, here's the source for a simple viewer I wrote that has a glLight working on a torus.

Take a look at it, and it might help you out.

http://www.vurtx.com/ds/DSViewerLights.zip

#123375 - adzz182 - Tue Mar 27, 2007 7:28 pm

ah thanks alot, its ok i have it working now.