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 > libnds glOrtho depth issue

#65237 - FireSlash - Sun Jan 01, 2006 5:43 am

Has anyone else had trouble with depth in glOrtho?

It should ignore the Z values for perspective, but its also ignoring them for depth in a weird way... The higher (Y axis) of two polygons gets drawn on top, instead of the proper Z axis.

Am I just doing something wrong, or is this a known issue? Maybe theres a fix? :)
_________________
FireSlash.net

#65330 - crossraleigh - Mon Jan 02, 2006 2:20 am

Yes, I had the same problem, but both neimod and Extreme Coder confidently assured me that depth with glOrtho worked fine for them. I never figured out what I was doing wrong.

Code:
Depth would start working if I enabled perspective by using

[2/w 0   0  -1]
[0   2/h 0  -1]
[0   0  -1   0]
[0   0  -1   1]

instead of

[2/w 0   0  -1]
[0   2/h 0  -1]
[0   0  -1   0]
[0   0   0   1].

_________________
My world is black and white, but if I blink fast enough, I see it in grayscale.

#65339 - FireSlash - Mon Jan 02, 2006 4:41 am

Well then, perhaps its time for some code. I'd like to sort out why glOrtho isn't working, instead of just patch it by using a modified perspective matrix :)

Excuse the mess, I'm still implementing a lot of code, so there are a lot of bits commented out and thrown in at weird spots. This code is based off the Ortho libnds example, and some code from ecurtz.

Code:
int main()
{   
   touchPosition touch;
   // Turn on everything
   powerON(POWER_ALL);
   
   // Setup the Main screen for 3D
   videoSetMode(MODE_0_3D);               

   videoSetModeSub(MODE_5_2D | DISPLAY_BG2_ACTIVE | DISPLAY_SPR_ACTIVE | DISPLAY_SPR_2D_BMP_256);
   SUB_BG2_CR = BG_BMP16_256x256;
   SUB_BG2_XDX = 256;
   SUB_BG2_XDY = 0;
   SUB_BG2_YDX = 0;
   SUB_BG2_YDY = 256;
   SUB_BG2_CY = 0;
   SUB_BG2_CX = 0;
      
   vramSetMainBanks(VRAM_A_TEXTURE, VRAM_B_LCD, VRAM_C_SUB_BG, VRAM_D_SUB_SPRITE);

   int y = 0;
   int x = 0;
   for (y = 0; y < SCREEN_HEIGHT; y++)
   for (x = 0; x < SCREEN_WIDTH; x++)
   {
      BG_GFX_SUB[y * SCREEN_WIDTH + x]  = RGB15(31, x % 32, y % 32) | BIT(15);
   }

   // IRQ basic setup
   irqInit();
   irqSet(IRQ_VBLANK, 0);

   glViewPort(0,0,255,191);
   
   // Specify the Clear Color and Depth
   glClearColor(0,0,0);
   glClearDepth(0x7FFF);
   
   InitSprites();
   
   int i = 0;
   for (y = 0; y < 3; y++)
   for (x = 0; x < 4; x++)
   {
      sprites[i].attribute[0] = ATTR0_BMP | ATTR0_SQUARE | (64 * y);
      sprites[i].attribute[1] = ATTR1_SIZE_64 | (64 * x);
      sprites[i].attribute[2] = ATTR2_ALPHA(1) | (8 * 32 * y) | (8 * x);
      i++;
   }
   
   uint32 frameCount = 0;
   dsScene * scene = new dsIntro;
   scene->load(frameCount);
   while (1)
   {
      // Check if our scene needs changed
      if(scene->getState() == state_UNLOADED)
      {
         free(scene);
         scene = new dsMenu;
      }   
      // Reset the screen and setup the view
      glReset();
      scanKeys();
      
      touch=touchReadXY();
       
      scene->touch(touch);

      glOrtho(0,1,0,1,-6,6);

      glColor3f(1,1,1);
      
      glLight(0, RGB15(31,31,31) , 0,    floattov10(-1.0), 0);
      glLight(1, RGB15(31,31,31) , 0,     0,   floattov10(-1.0));
      glLight(2, RGB15(31,31,31) , 0,     0,   floattov10(1.0));

      glPushMatrix();
      
      glMatrixMode(GL_TEXTURE);
      glIdentity();
      
      glMatrixMode(GL_MODELVIEW);

      //need to set up some material properties since DS does not have them set by default
      glMaterialf(GL_AMBIENT, RGB15(16,16,16));
      glMaterialf(GL_DIFFUSE, RGB15(16,16,16));
      glMaterialf(GL_SPECULAR, BIT(15) | RGB15(8,8,8));
      glMaterialf(GL_EMISSION, RGB15(16,16,16));

      //ds uses a table for shinyness..this generates a half-ass one
      glMaterialShinyness();
      
      
      if(!(keysHeld() & KEY_L))
         glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE  | POLY_FORMAT_LIGHT0| POLY_FORMAT_LIGHT1| POLY_FORMAT_LIGHT2);
      else
         glPolyFmt(POLY_ALPHA(0)  | POLY_CULL_NONE  | POLY_FORMAT_LIGHT0| POLY_FORMAT_LIGHT1| POLY_FORMAT_LIGHT2);
      
      // Set the current matrix to be the model matrix
      glMatrixMode(GL_MODELVIEW);
      
      //Push our original Matrix onto the stack (save state)
      glPushMatrix();   


      // TODO: Remove reliance on two VRAM banks. It can be done with one, I think.
      if (frameCount & 0x1)
      {
         vramSetBankC(VRAM_C_SUB_BG);
         vramSetBankD(VRAM_D_LCD);
         SetRegCapture(true, 0, 15, 3, 0, 3, 0, 0);
         scene->draw_top(frameCount);
      } else {
         vramSetBankC(VRAM_C_LCD);
         vramSetBankD(VRAM_D_SUB_SPRITE);
         SetRegCapture(true, 0, 15, 2, 0, 3, 0, 0);
         scene->draw_bottom(frameCount);
      }
      // Pop our Matrix from the stack (restore state)
      glPopMatrix(1);
      frameCount++;
      // flush to screen   
      glFlush();
      lcdSwap();
      UpdateOAM();
   }
   
   return 0;
}

_________________
FireSlash.net

#75626 - MrK - Tue Mar 14, 2006 8:05 pm

I don't know if this is a known response but changing videoGL.inl

Code:

GL_STATIC_INL void glFlush(void) { GFX_FLUSH = 2; }

to

GL_STATIC_INL void glFlush(void) { GFX_FLUSH = 0; }


makes the glOrtho depth sorting work (at least for me). if somebody could confirm this it'd be a nice step into fixing the issue ;)

#75714 - Extreme Coder - Wed Mar 15, 2006 1:20 pm

Did you try this? I sure will:D
*Prays it should work*

EDIT: YES!!!!!!!!!! IT worked! Finally. Thanks a lot:D
dovoto/wintermute/whoever in charge of libnds UPDATE THIS;D

#75802 - dovoto - Thu Mar 16, 2006 3:28 am

hmm...
_________________
www.drunkencoders.com