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 > Problems with glOrtho()

#165954 - Kath - Wed Jan 14, 2009 9:19 am

Hello, I'm trying to use glOrtho() to be able to draw to the screen using pixel coordinates in 3d, so setting it to 256x192. I did this before in an older project but copying this code to the new one results in nothing displaying, I've since tried using the Ortho example code which works but when I change the values I get nothing again.

For example this code displays as expected, a quad taking up the top left 1/4 of the screen:

Code:
glOrtho(0, 6, 6, 0, 1, 100);

glBegin(GL_QUADS);

glVertex3v16( inttov16(0), inttov16(0), inttov16(-1) );
glVertex3v16( inttov16(0), inttov16(3), inttov16(-1) );
glVertex3v16( inttov16(3), inttov16(3), inttov16(-1) );
glVertex3v16( inttov16(3), inttov16(0), inttov16(-1) );

glEnd();


However this code results in nothing being drawn:

Code:
glOrtho(0, 256, 192, 0, 1, 100);

glBegin(GL_QUADS);

glVertex3v16( inttov16(100), inttov16(100), inttov16(-1) );
glVertex3v16( inttov16(100), inttov16(150), inttov16(-1) );
glVertex3v16( inttov16(200), inttov16(150), inttov16(-1) );
glVertex3v16( inttov16(200), inttov16(100), inttov16(-1) );

glEnd();


I'm wondering if I'm doing something extremely wrong here. My 3d setup is below:

Code:
void Setup3d()
{
   glInit();
 
   // Enable AA, textures and blending.
   glEnable( GL_ANTIALIAS );
   glEnable( GL_TEXTURE_2D );
   glEnable( GL_BLEND );

   // Setup the background.
   glClearColor( 16, 16, 16, 31 );
   glClearPolyID( 63 );
   glClearDepth( 0x7FFF );

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   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));

   glPolyFmt( POLY_ID(0) | POLY_ALPHA(31)  | POLY_CULL_NONE );

   // Set our viewport to be the same size as the screen
   glViewport(0,0,255,191);
   
   // Set ortho mode.
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0, 6, 6, 0, 1, 100);
}


Thanks in advance.

#165963 - AxemRed - Wed Jan 14, 2009 3:31 pm

inttov16(200) overflows, 200<<12 doesn't fit inside a 16 bit number.

#165973 - Kaiser - Wed Jan 14, 2009 9:06 pm

You can use my ortho setup for reference if you want:

Code:

void GL_Enable2D(int znear)
{
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glOrtho(0, 256>>5, 192>>5, 0, znear, 4);
   glScalef(128.0f, 128.0f, 128.0f);
   glPushMatrix();
}

void GL_Disable2D(void)
{
   glPopMatrix(1);
}



Just add GL_Enable2D(znear value) before drawing stuff and add GL_Disable2D when finished.

I added glScalef(128.0f, 128.0f, 128.0f) so you don't have to convert the x, y values into fixed numbers, so basically it adding a value of 0 - 255 for the draw x and 0 - 191 for the draw y will work instead of having to always shift by 7.