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 > 3D Problem

#174878 - m2 - Wed Jul 28, 2010 12:55 pm

Hello, i have been playing around with 3D a bit, trying to get alpha blending to work, but there seems to be no way i can get rid of the flickering that seem to occur when i rotate my alpha blended cube.. I assume this might be some type of sorting problem, but i don't know how to solve it. Hope someone can help me out a bit in case there is something i have missed.

Here is the code:
Code:

#include <nds.h>

int DrawGLScene();

float rot;
int main() {   
   
   // Setup the Main screen for 3D
   videoSetMode(MODE_0_3D);
   
   // initialize the geometry engine
   glInit();
   

   glEnable(GL_BLEND);
   
   // setup the rear plane
   glClearColor(0,0,0,31); // BG must be opaque for AA to work
   glClearPolyID(63); // BG must have a unique polygon ID for AA to work
   glClearDepth(0x7FFF);
   
   // Set our viewport to be the same size as the screen
   glViewport(0,0,255,191);
   
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluPerspective(70, 256.0 / 192.0, 0.1, 100);

   
   while (1)
   {
      // 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(0);
      
      // wait for the screen to refresh
      swiWaitForVBlank();
   
   }
   
   return 0;
}

int DrawGLScene()                                 // Here's Where We Do All The Drawing
{
   glLoadIdentity();                           // Reset The Current Modelview Matrix
   glTranslatef(0.0f,0.0f,-3.0f);                  // Move Left 1.5 Units And Into The Screen 6.0
   glRotatef(rot,1.0f,1.0f,1.0f);                  // Rotate The Triangle On The Y axis ( NEW )
glPolyFmt(POLY_ID(0)|POLY_ALPHA(15) | POLY_CULL_NONE );
glBegin(GL_QUADS);
      glColor3f(0.0f,1.0f,0.0f);         // Set The Color To Green
      glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Top)
      glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Top)
      glVertex3f(-1.0f, 1.0f, 1.0f);         // Bottom Left Of The Quad (Top)
      glVertex3f( 1.0f, 1.0f, 1.0f);         // Bottom Right Of The Quad (Top)

glEnd();
glPolyFmt(POLY_ID(1)|POLY_ALPHA(15) | POLY_CULL_NONE );
glBegin(GL_QUADS);
      glColor3f(1.0f,0.5f,0.0f);         // Set The Color To Orange
      glVertex3f( 1.0f,-1.0f, 1.0f);         // Top Right Of The Quad (Bottom)
      glVertex3f(-1.0f,-1.0f, 1.0f);         // Top Left Of The Quad (Bottom)
      glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Bottom)
      glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Bottom)

glEnd();
glPolyFmt(POLY_ID(2)|POLY_ALPHA(15) | POLY_CULL_NONE );
glBegin(GL_QUADS);
      glColor3f(1.0f,0.0f,0.0f);         // Set The Color To Red
      glVertex3f( 1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Front)
      glVertex3f(-1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Front)
      glVertex3f(-1.0f,-1.0f, 1.0f);         // Bottom Left Of The Quad (Front)
      glVertex3f( 1.0f,-1.0f, 1.0f);         // Bottom Right Of The Quad (Front)

glEnd();
glPolyFmt(POLY_ID(3)|POLY_ALPHA(15) | POLY_CULL_NONE );
glBegin(GL_QUADS);
      glColor3f(1.0f,1.0f,0.0f);         // Set The Color To Yellow
      glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Back)
      glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Back)
      glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Back)
      glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Back)

glEnd();
glPolyFmt(POLY_ID(4)|POLY_ALPHA(15) | POLY_CULL_NONE );
glBegin(GL_QUADS);
      glColor3f(0.0f,0.0f,1.0f);         // Set The Color To Blue
      glVertex3f(-1.0f, 1.0f, 1.0f);         // Top Right Of The Quad (Left)
      glVertex3f(-1.0f, 1.0f,-1.0f);         // Top Left Of The Quad (Left)
      glVertex3f(-1.0f,-1.0f,-1.0f);         // Bottom Left Of The Quad (Left)
      glVertex3f(-1.0f,-1.0f, 1.0f);         // Bottom Right Of The Quad (Left)

glEnd();
glPolyFmt(POLY_ID(5)|POLY_ALPHA(15) | POLY_CULL_NONE );
glBegin(GL_QUADS);
      glColor3f(1.0f,0.0f,1.0f);         // Set The Color To Violet
      glVertex3f( 1.0f, 1.0f,-1.0f);         // Top Right Of The Quad (Right)
      glVertex3f( 1.0f, 1.0f, 1.0f);         // Top Left Of The Quad (Right)
      glVertex3f( 1.0f,-1.0f, 1.0f);         // Bottom Left Of The Quad (Right)
      glVertex3f( 1.0f,-1.0f,-1.0f);         // Bottom Right Of The Quad (Right)

glEnd();
   rot+=0.2f;                                 // Increase The Rotation Variable For The Triangle ( NEW )
   return TRUE;                              // Keep Going
}

#174879 - elhobbs - Wed Jul 28, 2010 2:07 pm

I think you want to take a look at the parameters to glFlush which is a wrapper for SWAP_BUFFERS. I am thinking you are going to want to manually sort the transparent polys - so glFlush(1) may give better results.

#174881 - m2 - Wed Jul 28, 2010 3:33 pm

Ok thank you, i will try that. Guess i will have to write some code to sort by Z order for every frame after new rotation?