Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: 3D pyramid appears scattered

Threaded View

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2012
    Location
    Italy
    Posts
    12

    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.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	pyramid.jpg 
Views:	153 
Size:	4.4 KB 
ID:	906  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •