Home > Net >  Why does my second 3D object not have four faces in Open GL
Why does my second 3D object not have four faces in Open GL

Time:08-15

As the title says I'm tyring to model a simple giraffe out of arraycubes in open GL wiht C , now I got the concepts done, but ran into an issue, when I start on the neck for some reaosn I lose 5 out of the 6 faces of my cube, the example I'm following doesn't result in this. I linked a small video below to show the visual result and I'm wondering what might be causing this. If there's an easier way to go about this as well please do let me know.

Visual Result

Code Sample

#include <glut.h>
    
    float angle[4];
    GLfloat corners[8][3] = { {-0.5,0.5,-0.5},{0.5,0.5,-0.5},
                            {0.5,-0.5,-0.5},{-0.5,-0.5,-0.5},
                            {-0.5,0.5,0.5},{0.5,0.5,0.5},
                            {0.5,-0.5,0.5},{-0.5,-0.5,0.5} };
    
    void drawFace(int a, int b, int c, int d) {
        glBegin(GL_POLYGON);
        glVertex3fv(corners[a]);
        glVertex3fv(corners[b]);
        glVertex3fv(corners[c]);
        glVertex3fv(corners[d]);
        glEnd();
    }
    
    void ArrayCube() {
    
        glColor3f(1.0, 1.0, 1.0);
        drawFace(0, 3, 2, 1);
        glColor3f(1.0, 1.0, 1.0);
        drawFace(3, 0, 4, 7);
        glColor3f(1.0, 1.0, 1.0);
        drawFace(2, 3, 7, 6);
        glColor3f(1.0, 1.0, 1.0);
        drawFace(1, 2, 6, 5);
        glColor3f(1.0, 1.0, 1.0);
        drawFace(4, 5, 6, 7);
        glColor3f(1.0, 1.0, 1.0);
        drawFace(5, 4, 0, 1);
    }
    
    void LowerNeck()
    {
        glPushMatrix();
        glTranslatef(0.5, 0.25, -0.125);
        glScalef(0.0, 0.5, 0.25);
        ArrayCube();
        glPopMatrix();
    }
    
    void MainBody()
    {
        glPushMatrix();
        glScalef(1.25, 0.25, 0.5);
        ArrayCube();
        glPopMatrix();
    }
    
    void DrawGiraffe()
    {
        glRotatef(angle[0], 0.0, 1.0, 0.0);
        MainBody();
        LowerNeck();
    
    }
    
    void rotate() {
        angle[0]  = 1.0;
        if (angle[0] > 360) angle[0] -= 360;
        glutPostRedisplay();
    }
    
    void display() {
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(0.6, 0.6, 0.6, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
        DrawGiraffe();
        glutSwapBuffers();
    }
    
    void init() {
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glColor3f(1.0, 1.0, 1.0);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 2.5);
    }
    
    int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
        glutInitWindowSize(500, 500);
        glutInitWindowPosition(0, 0);
        glutCreateWindow("Basic 3D");
        glutDisplayFunc(display);
        init();
        glutIdleFunc(rotate);
        glutMainLoop();
    }

CodePudding user response:

For the second object (the neck) you apply a scale transformation on x that scales the x component of all the following drawn vertices to 0.0: glScalef(0.0, 0.5, 0.25);

That 0.0 should've probably been a 1.0.

That's the reason you only see one quad in the render video: That's the quad/face (actually two faces) which still have a dimension in Y and Z. The faces that have a dimension on x are squished to degenerate quads and not displayed at all.

  • Related