Displaying object troubles

Hey, I am having problems with displaying a 3D Sierpinski Gasket within my program and I’m not overly sure why it isn’t displaying. I think it has something to do with my pre-compiled objects in my program that are getting displayed before I call onto the method that draws the Gasket. Or it may be a buffer issue, I’m quite confused.

Sorry, my crystal ball is not working :slight_smile:
Try to do a very small example that works, to isolate the problem(s).
Put printf traces to verify execution order of your code.
Use a debugger, verify runtime values of your ‘buffer’ (which buffer by the way ?)

I believe it may be my depth buffer, though I could be completely wrong. The code itself compiles and runs fine, no errors. I have a menu button of where a user picks an object to render to the screen, it starts off with a pre-compiled Torus and for the rest of the objects it calls from a display list. Since the Gasket is not a prebuilt function I had to make my own method and when the method is called upon the torus it still being display, it doesn’t render the Gasket. Even when I call upon the Gasket first it doesn’t render. Would it be an animation issue since my objects rotate through the idle function?

Its really hard for us 2 see unless u show us how u r doing it now.

Its quite a bit of lengthy code



#include <stdlib.h>
#include <GL/glut.h>

//Windows Definitions and Variables
int winIdMain;                                       /* Main Window handle         */
int menIdMain;                                       /* Main menu handle           */
int menIdSub;                                        /* Sub-menu handle            */
int menIdSubMain;
int value;
int n=3;

//Animation and scene definitions and variables
#define SMALL_ANGLE  1.0                            /* Small rotation angle        */
static double rotation_angle = 0.0;                 /* Current angle increment     */
static double spin = 0.0;                           /* Current Spin Angle          */

#define GL_TORUS 1                              /* Call List Objects           */
#define SOLID_CUBE 2
#define TEA_POT 3
#define GL_SPHERE 4
#define GL_GASKET 5
static int object = GL_TORUS;                   /* Object to use in scene      */
GLfloat light_diffuse[]={1.0,1.0,1.0,1.0};
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat v[4][3]={{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333},
     {-0.816497, -0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}};

void triangle(GLfloat *va, GLfloat *vb, GLfloat *vc){
      glVertex3fv(va);
      glVertex3fv(vb);
      glVertex3fv(vc);
}

void tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d){
   
    triangle(a, b, c);
    triangle(a, c, d);
    triangle(a, d, b);
    triangle(b, d, c);
}

void divide_tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d, int m){
    GLfloat mid[6][3];
    int j;
    if(m>0){

       /* compute six midpoints */
       for(j=0; j<3; j++) mid[0][j]=(a[j]+b[j])/2;
       for(j=0; j<3; j++) mid[1][j]=(a[j]+c[j])/2;
       for(j=0; j<3; j++) mid[2][j]=(a[j]+d[j])/2;
       for(j=0; j<3; j++) mid[3][j]=(b[j]+c[j])/2;
       for(j=0; j<3; j++) mid[4][j]=(c[j]+d[j])/2;
       for(j=0; j<3; j++) mid[5][j]=(b[j]+d[j])/2;

       /* create 4 tetrahedrons by subdivision */
       divide_tetra(a, mid[0], mid[1], mid[2], m-1);
       divide_tetra(mid[0], b, mid[3], mid[5], m-1);
       divide_tetra(mid[1], mid[3], c, mid[4], m-1);
       divide_tetra(mid[2], mid[4], d, mid[5], m-1);
    }

    else(tetra(a,b,c,d)); /* draw tetrahedron at end of recursion */
}

//Commands to listen for the up and down arrow keys
void SpecialKeys(int key, int x, int y) {
   
   switch (key){
       case GLUT_KEY_UP:{
           n++;
       break;
   }
   case GLUT_KEY_DOWN:{
       n--;
   break;
   case GLUT_KEY_RIGHT:{
       //
   break;
   }
   case GLUT_KEY_LEFT:{
       //
   break;
   }
   }

}
}


/*  function that processes keyboard events */
void keyboard (unsigned char key, int x, int y){
static int is_rotation = 0;
switch (key){

    case 'q':
    case 'Q':
   case 27 :
    exit (0);
    break;

   
case 32 :

     if(is_rotation) {
    rotation_angle = 0.0;
    is_rotation = 0;
  
    }
    else {
    rotation_angle = SMALL_ANGLE;
    is_rotation = 1;
    };
    break;
    };
};

//Event Handling
void mainMenu(int value){        

switch(value){

   case 1:{
       object = GL_TORUS;
    break;
      }
   case 2:{
       object = SOLID_CUBE;
    break;
      }
   case 3:{
       object = TEA_POT;
   break;
      }
   case 4:{
       object =GL_SPHERE;
   break;
      }
   case 5:{
      divide_tetra(v[0], v[1], v[2], v[3], n);
	
   break;
      }
   case 6:{
       exit(0);
    break;
      }
};
};

//List of cases from the submenu 
void subMenu(int value){

switch(value){

   case 1:{
       // Change the colour of the object to Red
        light_diffuse[0]=1.0;
        light_diffuse[1]=0.0;
        light_diffuse[2]=0.0;
    break;
          }
   case 2:{
               // Change the colour of the object to Green
        light_diffuse[0]=0.0;
        light_diffuse[1]=1.0;
        light_diffuse[2]=0.0;
    break;
   }
   case 3:{
               // Change the colour of the object to Blue
       light_diffuse[0]=0.0;
        light_diffuse[1]=0.0;
        light_diffuse[2]=1.0;
    break;
          }
   case 4:{
               // Change the colour of the object to Yellow
       light_diffuse[0]=1.0;
        light_diffuse[1]=1.0;
        light_diffuse[2]=0.0;    
    break;
          }
   case 5:{
               //Change the colour of the object to Purple
    light_diffuse[0]=1.0;
        light_diffuse[1]=0.0;
        light_diffuse[2]=1.0;
   break;
          }
   case 6:{
               //Change the colour of the object to Cyan
        light_diffuse[0]=0.0;
        light_diffuse[1]=1.0;
        light_diffuse[2]=1.0;
   break;
          }
};
};

/* There can be only one idle() callback function. In an
animation, this idle() function must update not only the
main window but also all derived subwindows */
void idle (void){
spin += rotation_angle;
/* Update main */
glutSetWindow (winIdMain);
glutPostRedisplay();
};

void reshape(int w, int h){
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION); /* switch matrix mode */
    glLoadIdentity();
    if (w <= h)
    gluOrtho2D(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
        2.0 * (GLfloat) h / (GLfloat) w);
    else  gluOrtho2D(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 *
        (GLfloat) w / (GLfloat) h, -2.0, 2.0);
    glMatrixMode(GL_MODELVIEW); /* return to modelview mode */
}

void init(){
   glEnable(GL_LIGHTING);
   
   glEnable(GL_DEPTH_TEST);
   
   /* set clear color to black */
   glShadeModel (GL_SMOOTH);
   glClearColor (0.0, 0.0, 0.0, 0.0);
   /* set fill  color to white */
    glColor3f(1.0, 1.0, 1.0);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   glOrtho(-2.0, 2.0, -2.0, 2.0, 1.0, 50.0);  
}

//Windows updating callback functions
void mainDisplay (void){

/* Clean drawing board */
glutSetWindow (winIdMain);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
   glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE ) ;
   glEnable(GL_COLOR_MATERIAL);
   glEnable(GL_LIGHT0);

/* Draw rotating Objects*/
glPushMatrix ();
glRotatef (spin, 0.0, 0.0, 1.0);
glCallList(object);
glPopMatrix ();

glutSwapBuffers ();
};

// Initializes GLUT machine, opens frames, windows and menus and runs GLUT's  main loop                         
int main (int argc, char *argv[]){

/* Glut initializations */
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition (5, 5);
glutInitWindowSize (600, 600);

/* Main window creation and setup */
winIdMain = glutCreateWindow ("Test");
glutDisplayFunc (mainDisplay);
glutReshapeFunc(reshape);
glutKeyboardFunc (keyboard);
glutSpecialFunc(SpecialKeys);
glutIdleFunc (idle);
init();

/* Create submenu */
menIdSub = glutCreateMenu(subMenu);
glutAddMenuEntry("Red", 1);
glutAddMenuEntry("Green",2);
glutAddMenuEntry("Blue", 3);
glutAddMenuEntry("Yellow", 4);
glutAddMenuEntry("Purple", 5);
glutAddMenuEntry("Cyan",6);

/* Create main menu */
menIdMain = glutCreateMenu(mainMenu);
glutAddMenuEntry("Torus", 1);
glutAddMenuEntry("Cube",2);
glutAddMenuEntry("Teapot",3);
glutAddMenuEntry("Sphere",4);
glutAddMenuEntry("3D Gasket",5);
glutAddSubMenu("Colours", menIdSub);
glutAddMenuEntry("Quit", 6);
glutAttachMenu(GLUT_LEFT_BUTTON);

/* Create call list elements  */
glNewList(GL_TORUS, GL_COMPILE);
glColor3f(1.0,1.0,1.0);
glPushMatrix();
glRotatef(45,1,1,0);
glutSolidTorus(0.25,0.5,25,25);
glPopMatrix();
glEndList();

glNewList(SOLID_CUBE, GL_COMPILE);
glPushMatrix();
glRotatef(25,1,1,0);
glutSolidCube(1.0);
glPopMatrix();
glEndList();

glNewList(TEA_POT, GL_COMPILE);
glPushMatrix();
glRotatef(45,1,1,0);
glutSolidTeapot(0.5);
glPopMatrix();
glEndList();

glNewList(GL_SPHERE, GL_COMPILE);
glPushMatrix();
glutSolidSphere(0.5,25,25);
glPopMatrix();
glEndList();

glNewList(GL_GASKET, GL_COMPILE);
glPushMatrix();
divide_tetra(v[0], v[1], v[2], v[3], n);
glPopMatrix();
glEndList();

glutMainLoop ();
return 0;
};

Please excuse any “newb” errors you see =(

Hi do two things

  1. Wrao glBegin(GL_TRIANGLES); and glEnd() around your divide tetra function when u create the display list in main func. as follows,

glNewList(GL_GASKET, GL_COMPILE);
glPushMatrix();
glBegin(GL_TRIANGLES);
divide_tetra(v[0], v[1], v[2], v[3], n);
glEnd();
glPopMatrix();
glEndList();

And assign object=GL_GASKET in the switch case


 case 5:{
    object=GL_GASKET;
    divide_tetra(v[0], v[1], v[2], v[3], n);	
   break;

That should do it. Here is the complete code


#include <stdlib.h>
#include <GL/glut.h>

//Windows Definitions and Variables
int winIdMain;                                       /* Main Window handle         */
int menIdMain;                                       /* Main menu handle           */
int menIdSub;                                        /* Sub-menu handle            */
int menIdSubMain;
int value;
int n=3;

//Animation and scene definitions and variables
#define SMALL_ANGLE  1.0                            /* Small rotation angle        */
static double rotation_angle = 0.0;                 /* Current angle increment     */
static double spin = 0.0;                           /* Current Spin Angle          */

#define GL_TORUS 1                              /* Call List Objects           */
#define SOLID_CUBE 2
#define TEA_POT 3
#define GL_SPHERE 4
#define GL_GASKET 5
static int object = GL_TORUS;                   /* Object to use in scene      */
GLfloat light_diffuse[]={1.0,1.0,1.0,1.0};
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat v[4][3]={{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333},
     {-0.816497, -0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}};

void triangle(GLfloat *va, GLfloat *vb, GLfloat *vc){
      glVertex3fv(va);
      glVertex3fv(vb);
      glVertex3fv(vc);
}

void tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d){
   
    triangle(a, b, c);
    triangle(a, c, d);
    triangle(a, d, b);
    triangle(b, d, c);
}

void divide_tetra(GLfloat *a, GLfloat *b, GLfloat *c, GLfloat *d, int m){
    GLfloat mid[6][3];
    int j;
    if(m>0){

       /* compute six midpoints */
       for(j=0; j<3; j++) mid[0][j]=(a[j]+b[j])/2;
       for(j=0; j<3; j++) mid[1][j]=(a[j]+c[j])/2;
       for(j=0; j<3; j++) mid[2][j]=(a[j]+d[j])/2;
       for(j=0; j<3; j++) mid[3][j]=(b[j]+c[j])/2;
       for(j=0; j<3; j++) mid[4][j]=(c[j]+d[j])/2;
       for(j=0; j<3; j++) mid[5][j]=(b[j]+d[j])/2;

       /* create 4 tetrahedrons by subdivision */
       divide_tetra(a, mid[0], mid[1], mid[2], m-1);
       divide_tetra(mid[0], b, mid[3], mid[5], m-1);
       divide_tetra(mid[1], mid[3], c, mid[4], m-1);
       divide_tetra(mid[2], mid[4], d, mid[5], m-1);
    }

    else(tetra(a,b,c,d)); /* draw tetrahedron at end of recursion */
}

//Commands to listen for the up and down arrow keys
void SpecialKeys(int key, int x, int y) {
   
   switch (key){
       case GLUT_KEY_UP:{
           n++;
       break;
   }
   case GLUT_KEY_DOWN:{
       n--;
   break;
   case GLUT_KEY_RIGHT:{
       //
   break;
   }
   case GLUT_KEY_LEFT:{
       //
   break;
   }
   }

}
}


/*  function that processes keyboard events */
void keyboard (unsigned char key, int x, int y){
static int is_rotation = 0;
switch (key){

    case 'q':
    case 'Q':
   case 27 :
    exit (0);
    break;

   
case 32 :

     if(is_rotation) {
    rotation_angle = 0.0;
    is_rotation = 0;
  
    }
    else {
    rotation_angle = SMALL_ANGLE;
    is_rotation = 1;
    };
    break;
    };
};

//Event Handling
void mainMenu(int value){        

switch(value){

   case 1:{
       object = GL_TORUS;
    break;
      }
   case 2:{
       object = SOLID_CUBE;
    break;
      }
   case 3:{
       object = TEA_POT;
   break;
      }
   case 4:{
       object =GL_SPHERE;
   break;
      }
   case 5:{
	   object=GL_GASKET;
      divide_tetra(v[0], v[1], v[2], v[3], n);
	
   break;
      }
   case 6:{
       exit(0);
    break;
      }
};
};

//List of cases from the submenu 
void subMenu(int value){

switch(value){

   case 1:{
       // Change the colour of the object to Red
        light_diffuse[0]=1.0;
        light_diffuse[1]=0.0;
        light_diffuse[2]=0.0;
    break;
          }
   case 2:{
               // Change the colour of the object to Green
        light_diffuse[0]=0.0;
        light_diffuse[1]=1.0;
        light_diffuse[2]=0.0;
    break;
   }
   case 3:{
               // Change the colour of the object to Blue
       light_diffuse[0]=0.0;
        light_diffuse[1]=0.0;
        light_diffuse[2]=1.0;
    break;
          }
   case 4:{
               // Change the colour of the object to Yellow
       light_diffuse[0]=1.0;
        light_diffuse[1]=1.0;
        light_diffuse[2]=0.0;    
    break;
          }
   case 5:{
               //Change the colour of the object to Purple
    light_diffuse[0]=1.0;
        light_diffuse[1]=0.0;
        light_diffuse[2]=1.0;
   break;
          }
   case 6:{
               //Change the colour of the object to Cyan
        light_diffuse[0]=0.0;
        light_diffuse[1]=1.0;
        light_diffuse[2]=1.0;
   break;
          }
};
};

/* There can be only one idle() callback function. In an
animation, this idle() function must update not only the
main window but also all derived subwindows */
void idle (void){
spin += rotation_angle;
/* Update main */
//glutSetWindow (winIdMain);
glutPostRedisplay();
};

void reshape(int w, int h){
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION); /* switch matrix mode */
    glLoadIdentity();
    if (w <= h)
    gluOrtho2D(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
        2.0 * (GLfloat) h / (GLfloat) w);
    else  gluOrtho2D(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 *
        (GLfloat) w / (GLfloat) h, -2.0, 2.0);
    glMatrixMode(GL_MODELVIEW); /* return to modelview mode */
}

void init(){
   glEnable(GL_LIGHTING);
   
   glEnable(GL_DEPTH_TEST);
   
   /* set clear color to black */
   glShadeModel (GL_SMOOTH);
   glClearColor (0.0, 0.0, 0.0, 0.0);
   /* set fill  color to white */
    glColor3f(1.0, 1.0, 1.0);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
   glOrtho(-2.0, 2.0, -2.0, 2.0, 1.0, 50.0);  
}

//Windows updating callback functions
void mainDisplay (void){

/* Clean drawing board */
glutSetWindow (winIdMain);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity ();

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
   glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE ) ;
   glEnable(GL_COLOR_MATERIAL);
   glEnable(GL_LIGHT0);

/* Draw rotating Objects*/
glPushMatrix ();
glRotatef (spin, 0.0, 0.0, 1.0);
glCallList(object);
glPopMatrix ();

glutSwapBuffers ();
};

// Initializes GLUT machine, opens frames, windows and menus and runs GLUT's  main loop                         
int main (int argc, char *argv[]){

/* Glut initializations */
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition (5, 5);
glutInitWindowSize (600, 600);

/* Main window creation and setup */
winIdMain = glutCreateWindow ("Test");
glutDisplayFunc (mainDisplay);
glutReshapeFunc(reshape);
glutKeyboardFunc (keyboard);
glutSpecialFunc(SpecialKeys);
glutIdleFunc (idle);
init();

/* Create submenu */
menIdSub = glutCreateMenu(subMenu);
glutAddMenuEntry("Red", 1);
glutAddMenuEntry("Green",2);
glutAddMenuEntry("Blue", 3);
glutAddMenuEntry("Yellow", 4);
glutAddMenuEntry("Purple", 5);
glutAddMenuEntry("Cyan",6);

/* Create main menu */
menIdMain = glutCreateMenu(mainMenu);
glutAddMenuEntry("Torus", 1);
glutAddMenuEntry("Cube",2);
glutAddMenuEntry("Teapot",3);
glutAddMenuEntry("Sphere",4);
glutAddMenuEntry("3D Gasket",5);
glutAddSubMenu("Colours", menIdSub);
glutAddMenuEntry("Quit", 6);
glutAttachMenu(GLUT_LEFT_BUTTON);

/* Create call list elements  */
glNewList(GL_TORUS, GL_COMPILE);
glColor3f(1.0,1.0,1.0);
glPushMatrix();
glRotatef(45,1,1,0);
glutSolidTorus(0.25,0.5,25,25);
glPopMatrix();
glEndList();

glNewList(SOLID_CUBE, GL_COMPILE);
glPushMatrix();
glRotatef(25,1,1,0);
glutSolidCube(1.0);
glPopMatrix();
glEndList();

glNewList(TEA_POT, GL_COMPILE);
glPushMatrix();
glRotatef(45,1,1,0);
glutSolidTeapot(0.5);
glPopMatrix();
glEndList();

glNewList(GL_SPHERE, GL_COMPILE);
glPushMatrix();
glutSolidSphere(0.5,25,25);
glPopMatrix();
glEndList();

glNewList(GL_GASKET, GL_COMPILE);
glPushMatrix();
glBegin(GL_TRIANGLES);
divide_tetra(v[0], v[1], v[2], v[3], n);
glEnd();
glPopMatrix();
glEndList();

glutMainLoop ();
return 0;
};

See if this helps,
Mobeen

Thanks it worked, I figured it be something minor!