# 3D pyramid appears scattered

• 11-17-2012, 05:44 PM
ramy1989
3D pyramid appears scattered
I have a struct that I called pyramid:

Code :

```typedef struct { GLfloat xUp; GLfloat yUp; GLfloat zUp; GLfloat base; GLfloat height; }pyramid;```

There are the coordinated of the uppest point:xUp, yUp, zUp.
So when I need to draw the pyramid I just calculate the 4 points that are at the base of the pyramid, then draw the triangles with all the vertices in counter clockwise order:

Code :

```void drawPyramid(pyramid pyr) { GLfloat p1[]= {pyr.xUp+pyr.base/2.0, pyr.yUp-pyr.height, pyr.zUp-pyr.base/2.0}; GLfloat p2[]= {pyr.xUp+pyr.base/2.0, pyr.yUp-pyr.height, pyr.zUp+pyr.base/2.0}; GLfloat p3[]= {pyr.xUp-pyr.base/2.0, pyr.yUp-pyr.height, pyr.zUp+pyr.base/2.0}; GLfloat p4[]= {pyr.xUp-pyr.base/2.0, pyr.yUp-pyr.height, pyr.zUp-pyr.base/2.0}; GLfloat up[]= {pyr.xUp, pyr.yUp, pyr.zUp};   glBegin(GL_TRIANGLES);   glColor4f(1.0, 0.0, 0.0, 0.0); glVertex3fv(up); glVertex3fv(p1); glVertex3fv(p2);   glColor4f(0.0, 1.0, 0.0, 0.0); glVertex3fv(up); glVertex3fv(p2); glVertex3fv(p3);   glColor4f(0.0, 0.0, 1.0, 0.0); glVertex3fv(up); glVertex3fv(p3); glVertex3fv(p4);   glColor4f(1.0, 1.0, 0.0, 0.0); glVertex3fv(up); glVertex3fv(p4); glVertex3fv(p1); glEnd();       glColor4f(0.0, 1.0, 1.0, 0.0); glBegin(GL_QUADS); glVertex3fv(p1); glVertex3fv(p2); glVertex3fv(p3); glVertex3fv(p4); glEnd();   }```

I give them different color just to make them more visible.
I am using a perspective projection, and the problem is that the sides of the pyramid appears scattered: it doesn't even seem a pyramid:

http://www.opengl.org/discussion_boa...1&d=1353199239

Lastly I would show how initialize everything:

Code :

```void init() { glEnable(GL_DEPTH); glViewport(-1.0, 1.0, -1.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, 1.0, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, -25.0, 0.0, 0.0, 0.0, 50.0, 0.0, 1.0, 0.0); }```

And this is the function where I render:

Code :

```void display() { glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glTranslatef(0.0, -25.0, 50.0); glRotatef(-angle, 0.0, 1.0, 0.0); glTranslatef(0.0, 25.0, -50.0);   pyramid pyr; pyr.xUp=0.0; pyr.yUp=10.0; pyr.zUp=50.0; pyr.base=10.0; pyr.height=18.0; glColor4f(1.0, 0.0, 0.0, 0.0); drawPyramid(pyr);   glutSwapBuffers(); }```

angle is just a float that I use to rotate the pyramid, to see it better.
I would know why sometimes I see three sides (3 different colors) of the pyramix mixed up, like if there aren't precise sides but there's jast a triangle embedded in another triangle, and it doesn't look like a pyramid.
• 11-18-2012, 12:32 PM
Carmine
I'm guessing there's something wrong with your definitions of p1, p2, p3, ....
Try drawing one or two faces at time in wireframe mode.
To render the faces in wireframe add one line of code:

glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);

Good luck.