Search code examples
openglglutvisual-c++-2010

Why is this quad not rendering? Or if it is why can't I see it?


I am using Visual C++ and GLUT. Am I missing something such a normals? this is the exact code I am using. I tried to insert glNormal3f(0, 0, 1); before the vertex statements but this did not change anything. The glutSolidSphere renders just as I expect it to but not the quad.

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


float spin = 0.0f;
float zDir = 0;
GLfloat diffuseIntensity[] = {.75, .75, .75, 1};
GLfloat specularHue[] = {0, 0, .5f, 1};
GLfloat shininess[] = {5};


void moveCamera();
void checkKeys(int, int, int);
void setUpLighting();
void changeSize(int w, int h) {


    if (h == 0)
        h = 1;

    float ratio =  w * 1.0 / h;


    glMatrixMode(GL_PROJECTION);    
    glLoadIdentity();   
    glViewport(0, 0, w, h); 
    gluPerspective(45,ratio,1,1000);    
    glMatrixMode(GL_MODELVIEW);
}

void renderScene(void) {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    moveCamera();
    glTranslatef(0, 0, zDir-5);
    glEnable(GL_COLOR_MATERIAL);
    glShadeModel(GL_FLAT);
    glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
    glMaterialfv(GL_FRONT, GL_SPECULAR, specularHue);
    glPushMatrix();
    glRotatef(90, 1, 0, 0);
    glRotatef(spin, 0, 0, 1);
    glutSolidSphere(.5f, 24, 24);
    glPopMatrix();
    spin += .01f;
    if(spin > 360) spin -= 360;

    glBegin(GL_QUADS);
    glVertex3f(-10, 0, -10);
    glVertex3f(-10, 0, 10);
    glVertex3f(10, 0, 10);
    glVertex3f(10, 0, -10);
    glEnd();
    glutSwapBuffers();
    glutPostRedisplay();
}

int main(int argc, char **argv) {


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(800,600);
    glutCreateWindow("Lighthouse3D - GLUT Tutorial");


    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutSpecialFunc(checkKeys);
    glEnable(GL_DEPTH_TEST);
    setUpLighting();

    glutMainLoop();

    return 1;
}
void moveCamera(){


    glTranslatef(0, 0, zDir);

}

void checkKeys(int key, int x, int y){
    switch(key){

    case GLUT_KEY_UP:
        zDir += .1f;
        glutPostRedisplay();
        break;

    case GLUT_KEY_DOWN:
        zDir += -.1f;
        glutPostRedisplay();
        break;

    case GLUT_KEY_END:
        exit(0);
    }
}

void setUpLighting(){
    GLfloat position0[] = {3, 1, 0, 1};
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, position0);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseIntensity);
}

Solution

  • Give this a shot:

    #include <GL/glut.h>
    
    float zDir = 12;
    void checkKeys(int key, int x, int y)
    {
        switch(key)
        {
        case GLUT_KEY_UP:
            zDir += -.5f;
            glutPostRedisplay();
            break;
    
        case GLUT_KEY_DOWN:
            zDir += .5f;
            glutPostRedisplay();
            break;
    
        case GLUT_KEY_END:
            exit(0);
        }
    }
    
    float spin = 0;
    void renderScene(void)
    {
        glEnable(GL_DEPTH_TEST);
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        double w = glutGet( GLUT_WINDOW_WIDTH );
        double h = glutGet( GLUT_WINDOW_HEIGHT );
        gluPerspective(45,w/h,0.1,100);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        glTranslatef(0, 0, -zDir);
    
        // set up light
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        GLfloat diffuseIntensity[] = {.75, .75, .75, 1};
        glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseIntensity);
    
        // draw sphere at light position
        glDisable( GL_LIGHTING );
        glPushMatrix();
        // spin light position around the y axis
        glRotatef( -spin, 0, 1, 0 );
        GLfloat position0[] = {3,3,3, 1};
        glLightfv(GL_LIGHT0, GL_POSITION, position0);
        glTranslatef( position0[0], position0[1], position0[2] );
        glColor3ub(255,255,255);
        glutSolidSphere(0.1,8,8);
        glPopMatrix();
        glEnable( GL_LIGHTING );
    
        // draw sphere
        glEnable(GL_COLOR_MATERIAL);
        glShadeModel(GL_FLAT);
        GLfloat specularHue[] = {0, 0, .5f, 1};
        GLfloat shininess[] = {5};
        glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
        glMaterialfv(GL_FRONT, GL_SPECULAR, specularHue);
    
        spin += .01f;
        if(spin > 360) spin -= 360;
    
        glPushMatrix();
        glRotatef(spin, 0, 1, 0);
        glutSolidSphere(2, 24, 24);
        glPopMatrix();
    
        // draw quad
        glColor3ub(255,0,0);
        glPushMatrix();
        glScalef( 3, 3, 3 );
        glBegin(GL_QUADS);
        glNormal3f( 0, 0, 1 );
        glVertex2f( -1, -1 );
        glVertex2f(  1, -1 );
        glVertex2f(  1,  1 );
        glVertex2f( -1,  1 );
        glEnd();
        glPopMatrix();
    
        glutSwapBuffers();
    
        glutPostRedisplay();
    }
    
    int main(int argc, char **argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
        glutInitWindowPosition(100,100);
        glutInitWindowSize(800,600);
        glutCreateWindow("Lighthouse3D - GLUT Tutorial");
    
        glutDisplayFunc(renderScene);
        glutSpecialFunc(checkKeys);
        glutMainLoop();
        return 1;
    }