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 > Gl problem with ortho/frustum

#54593 - Extreme Coder - Tue Sep 20, 2005 6:40 pm

Hi there.
I needed a glortho function, but since there isnt one, i took the one Dovoto at irc gave me:
Code:

//---------------------------------------------------------------------------------
void glOrtho3f32(f32 left, f32 right, f32 bottom, f32 top, f32 near, f32 far) {
//---------------------------------------------------------------------------------
   glMatrixMode(GL_PROJECTION);
 
   MATRIX_LOAD4x4 = divf32(intof32(2), right - left);     
   MATRIX_LOAD4x4 = 0; 
   MATRIX_LOAD4x4 = 0;     
   MATRIX_LOAD4x4 = divf32(right + left, right - left);
 
   MATRIX_LOAD4x4 = 0; 
   MATRIX_LOAD4x4 = divf32(intof32(2), top - bottom);     
   MATRIX_LOAD4x4 = 0;   
   MATRIX_LOAD4x4 = divf32(top + bottom, top - bottom);
 
   MATRIX_LOAD4x4 = 0; 
   MATRIX_LOAD4x4 = 0; 
   MATRIX_LOAD4x4 = -divf32(intof32(-2), far - near);     
   MATRIX_LOAD4x4 = divf32(far + near, far - near);
 
   MATRIX_LOAD4x4 = 0; 
   MATRIX_LOAD4x4 = 0; 
   MATRIX_LOAD4x4 = 0; 
   MATRIX_LOAD4x4 = floatof32(-1.0F);
 
   glStoreMatrix(0);
}


And I use it in my program like this:
Code:

/****************************************
 *       NDS NeHe Lesson 06             *
 *       Author: Dovoto               *
 ****************************************/

// include your ndslib
#include <nds.h>
#include <malloc.h>
//needed to load pcx files
#include <nds/arm9/image.h>
#include "drunkenlogo_pcx.h"

//Glortho3f32 function in the same post above
//---------------------------------------------------------------------------------
void glOrtho(float left, float right, float bottom, float top, float near, float far) {
//---------------------------------------------------------------------------------
glOrtho3f32(floatof32(left), floatof32(right), floatof32(bottom), floatof32(top),floatof32(near), floatof32(far));
}

int DrawGLScene();

int   texture[1];         // Storage For One Texture ( NEW )

void image8to16(sImage* img)
 {
    int i;

    u16* temp = (u16*)malloc(img->height*img->width*2);
   
    for(i = 0; i < img->height * img->width; i++)
       temp[i] = img->palette[img->data8[i]] | (1<<15);

    img->data16 = temp;
 }

int LoadGLTextures()                           // Load PCX files And Convert To Textures
{
   sImage pcx;                //////////////(NEW) and different from nehe.

   //load our texture
   loadPCX((u8*)drunkenlogo_pcx, &pcx);
   
   image8to16(&pcx);

   glGenTextures(1, &texture[0]);
   glBindTexture(0, texture[0]);
   glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, pcx.data8);

   return TRUE;
}

int main()
{   
   // Turn on everything
   powerON(POWER_ALL);
   
   irqInit();
   irqEnable(IRQ_VBLANK);
   
   // Setup the Main screen for 3D
   videoSetMode(MODE_0_3D);
   vramSetBankA(VRAM_A_TEXTURE);                        //NEW  must set up some memory for textures

   // Set our viewport to be the same size as the screen
   glViewPort(0,0,255,191);
   
   // Specify the Clear Color and Depth
   glClearColor(0,0,0);
   glClearDepth(0x7FFF);
   
   LoadGLTextures();
   while (1)
   {
      scanKeys();
      // Reset the screen and setup the view
      glReset();
      //gluPerspective(i, 256.0/192.0,0.1, 100);
      glOrtho(0,256,192,0,0,1);
      glColor3f(1,1,1);
      
      glPushMatrix();
      
      glMatrixMode(GL_TEXTURE);
      glIdentity();

      
      glMatrixMode(GL_MODELVIEW);
      
      //ds specific, several attributes can be set here   
      glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);
      
      // Set the current matrix to be the model matrix
      glMatrixMode(GL_MODELVIEW);
      
      //Push our original Matrix onto the stack (save state)
      glPushMatrix();   

      DrawGLScene();
      
      // Pop our Matrix from the stack (restore state)
      glPopMatrix(1);

      // flush to screen   
      glFlush();
   
   }
   
   return 0;
}

int DrawGLScene()                                 // Here's Where We Do All The Drawing
{
   glLoadIdentity();                           // Reset The View
   glTranslatef(0,0,0);

   glBindTexture(GL_TEXTURE_2D, texture[0]);

   glBegin(GL_QUADS);
      // Front Face
      glTexCoord2f(0, 0); glVertex3f( 0,  0, 0.0);
      glTexCoord2f(1, 0); glVertex3f(10,  0, 0.0);
      glTexCoord2f(1, 1); glVertex3f(10, 10, 0.0);
      glTexCoord2f(0, 1); glVertex3f( 0, 10, 0.0);
   glEnd();
   return TRUE;         
}


But i get black screens. I tried changing glOrtho(0,256,192,0,0,1); to glOrtho(0,256,192,0,-1,1);, yet same results. And i changed glVertex's z coordinate aswell, no luck.
Does anyone know whats wrong with this code?
Or if you think the problem is with the function, then whats wrong when i use glfrustum with the same arguments and get the same results?
Oh, and please, add a working Glortho function in the next libnds:D


Thanks so much in advance.


Last edited by Extreme Coder on Wed Sep 21, 2005 1:42 pm; edited 1 time in total

#54658 - duencil - Wed Sep 21, 2005 10:04 am

Did you try setting near to a small positive value instead of 0?

#54668 - Extreme Coder - Wed Sep 21, 2005 1:39 pm

well, something like 0.1? I tried that, and I supposed I must change the vertex coordinates to 0.1. Doesnt work.
If someone thinks the mistake is with the function, then can someone tell me how is glFrustum supposed to work? i tried the same arguments, same results.

#54673 - wintermute - Wed Sep 21, 2005 3:10 pm

Extreme Coder wrote:
Hi there.

Oh, and please, add a working Glortho function in the next libnds:D

Thanks so much in advance.


done :)

http://www.devkitpro.org/

#54678 - duencil - Wed Sep 21, 2005 4:23 pm

The function in libnds is correct after all.

Last edited by duencil on Tue Jan 17, 2006 8:15 pm; edited 1 time in total

#54693 - Extreme Coder - Wed Sep 21, 2005 6:06 pm

Thanks wintermute and duencil, I'm gonna test now that stuff, and will edit or reply with the results:D.


Thanks so much,
Extreme Coder

#54983 - LunarCrisis - Sat Sep 24, 2005 4:44 am

What I always do for ortho projections is a call to glLoadIdentity, which gives you an ortho with the range -1, 1 mapped to the screen in both the X and Y direction, then scale it from there.
_________________
If a tree falls in the forest and no one is there to hear it, why the heck do you care?

#73817 - acox - Tue Feb 28, 2006 6:09 am

Extreme Coder wrote:
Thanks wintermute and duencil, I'm gonna test now that stuff, and will edit or reply with the results:D.


Thanks so much,
Extreme Coder


So was there any resolution to this issue?

What is the status of devkitPro on this?

Does the ortho frustum func work and are there any gotchas?
_________________
3D on GBA

#73853 - Extreme Coder - Tue Feb 28, 2006 1:00 pm

Yes, glortho works well on devkitpro now, but depth sorting doesnt work at all:( hope this is soon fixed