Why don't I see the cude???

Hi all,
I’m using display lists to view a cube, with lighting and materials turned on. However, I am unable to see it. What am I doing wrong?

Thanks,
Jim the confused

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <unistd.h>

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

GLuint theCube;

GLfloat s1[] = {0.2, 0.0, 0.5, 1.0};
GLfloat s2[] = {0.4, 0.1, 0.1, 1.0};
GLfloat s3[] = {0.1, 0.5, 0.2, 1.0};
GLfloat s4[] = {0.9, 0.1, 0.1, 1.0};
GLfloat s5[] = {0.1, 0.3, 0.0, 1.0};
GLfloat s6[] = {0.1, 1.0, 1.0, 1.0};
GLfloat d1[] = {1.0, 0.3, 0.6, 1.0};
GLfloat d2[] = {0.3, 0.0, 0.0, 1.0};
GLfloat d3[] = {0.6, 0.4, 1.0, 1.0};
GLfloat d4[] = {0.3, 0.7, 0.2, 1.0};
GLfloat d5[] = {0.6, 1.0, 0.3, 1.0};
GLfloat d6[] = {0.1, 0.1, 0.3, 1.0};

GLfloat light_diffuse[]={0.85f, 0.1f, 0.1f, 1.0f};
GLfloat light_ambient[]={0.75f, 0.1f, 0.1f, 1.0f};
GLfloat light_position[] = {3.0, 3.0, 3.0, 0.0};

GLfloat v1[] = {-1.0f, -1.0f, 1.0f};
GLfloat v2[] = {1.0f, -1.0f, 1.0f};
GLfloat v3[] = {1.0f, 1.0f, 1.0f};
GLfloat v4[] = {-1.0f, 1.0f, 1.0f};
GLfloat v5[] = {1.0f, -1.0f, -1.0f};
GLfloat v6[] = {-1.0f, -1.0f, -1.0f};
GLfloat v7[] = {-1.0f, 1.0f, -1.0f};
GLfloat v8[] = {1.0f, 1.0f, -1.0f};
GLfloat n1[] = {0.0f, 0.0f, 1.0f};
GLfloat n2[] = {1.0f, 0.0f, 0.0f};
GLfloat n3[] = {0.0f, 0.0f, -1.0f};
GLfloat n4[] = {-1.0f, 0.0f, 0.0f};
GLfloat n5[] = {0.0f, -1.0f, 0.0f};
GLfloat n6[] = {0.0f, 1.0f, 0.0f};

static void Cube()
{
glTranslatef(0.0f, 0.0f, 20.0f);
glBegin(GL_QUADS);
// Front face
glMaterialfv(GL_FRONT, GL_SPECULAR, s1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, d1);
glNormal3fv(n1); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3);
glVertex3fv(v4);

  // Right face
  glMaterialfv(GL_FRONT, GL_SPECULAR, s2);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, d2);
  glNormal3fv(n2); glVertex3fv(v2); glVertex3fv(v5); glVertex3fv(v8);
  glVertex3fv(v3);

  // Back face
  glMaterialfv(GL_FRONT, GL_SPECULAR, s3);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, d3);
  glNormal3fv(n3); glVertex3fv(v5); glVertex3fv(v6); glVertex3fv(v7);
  glVertex3fv(v8);

  // Left face
  glMaterialfv(GL_FRONT, GL_SPECULAR, s4);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, d4);
  glNormal3fv(n4); glVertex3fv(v1); glVertex3fv(v4); glVertex3fv(v7);
  glVertex3fv(v6);

  // Bottom face 
  glMaterialfv(GL_FRONT, GL_SPECULAR, s5);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, d5);
  glNormal3fv(n5); glVertex3fv(v1); glVertex3fv(v6); glVertex3fv(v5);
  glVertex3fv(v2);

  // Top face 
  glMaterialfv(GL_FRONT, GL_SPECULAR, s6);
  glMaterialfv(GL_FRONT, GL_DIFFUSE, d6);
  glNormal3fv(n6); glVertex3fv(v3); glVertex3fv(v8); glVertex3fv(v7);
  glVertex3fv(v4);

glEnd();
}

void init(int width, int height)
{
theCube = glGenLists(1);
glNewList(theCube, GL_COMPILE);
Cube();
glEndList();

glClearColor (0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glDepthFunc(GL_LESS);

glShadeModel (GL_SMOOTH);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.0f,100.0f);
glMatrixMode(GL_MODELVIEW);

}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//gluLookAt(4.0f, -4.0f, 4.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
glLoadIdentity();

glCallList(theCube);
glutSwapBuffers();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.0f,100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init (480, 480);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

One definite problem is that you call glutInit(…) with the GLUT_SINGLE option indicating that you want single buffering. Then you call glutSwapBuffers() after each rendering.

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.0f,
100.0f);

  • i think thats your prob: near clip needs to be more than 0.0 i usually use 0.5 or higher:

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.5f
,100.0f);

Great. Thanks folks. I fixed the above two problems but I still get a black window. I removed the material stuff before I define each face of the cube and put instead glColor3f and still wont see a thing. Could it be the DL?

Thanks

I believe you have moved the cube outside your view volume. Since the don’t move the viewpoint, you have the viewpoint located at the origin when you draw your scene. The translate inside the display list will move the cube 20 units along the positive Z-axis, which is pointing out from the screen, not into it. So you have the cube behind the viewpoint.

Are you sure you fixed the parameters of the right gluPerspective call? You have two of them; one in the init function, and one in the reshape function. Ditch the call in the init function, and keep the one in the reshape function.

Thanks. That did it.