Another problem

My os is debian3.1.
The following code is made from Redbook example:
I have no idea what is wrong with the code, it just
showed a black window in my computer.
Where is the problem?

thx in advance.

 
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

static GLubyte frontIndices[] = {4,5,6,7};
static GLubyte rightIndices[] = {1,2,6,5};
static GLubyte bottomIndices[] = {0,1,5,4};
static GLubyte backIndices[] = {0,3,2,1};
static GLubyte leftIndices[] = {0,4,7,3};
static GLubyte topIndices[] = {2,3,7,6};
		
void Init()
{
	glClearColor(0.0,0.0,0.0,0.0);
	glShadeModel(GL_FLAT);
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0,1.0,1.0);
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(4,GL_INT,0,frontIndices);
	glVertexPointer(4,GL_INT,0,rightIndices);
	glVertexPointer(4,GL_INT,0,bottomIndices);
	glVertexPointer(4,GL_INT,0,backIndices);
	glVertexPointer(4,GL_INT,0,leftIndices);
	glVertexPointer(4,GL_INT,0,topIndices);
	
	glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,frontIndices);
	glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,rightIndices);
	glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,bottomIndices);
	glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,backIndices);
	glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,leftIndices);
	glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,topIndices);
	
	
	glutSwapBuffers();
}

void reshape(int w, int h)
{
	glViewport(0,0,(GLsizei)w,(GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0,5.0,0.0,5.0,0.0,5.0);
}

int main(int argc,char** argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutInitWindowSize(400,400);
	glutInitWindowPosition(100,100);
	glutCreateWindow("Example2-11");
	Init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMainLoop();
	return 0;
}

 

you need 2 things:

  1. an array of vertices, by which the geometry is defined.

  2. an array of indices, which defines the order in which the vertices are drawn.

in the following example, vertices[] defines the corner nodes of a cube.

the faces of the cube are defined by the index arrays.

be sure to enable GL_DEPTH_TEST, otherwise the result might look strange.

  GLfloat vertices[] = {
                -1., -1., -1., // NODE 0
                 1., -1., -1., // NODE 1
                 1.,  1., -1., // NODE 2
                -1.,  1., -1., // NODE 3
                -1., -1.,  1., // NODE 4
                 1., -1.,  1., // NODE 5
                 1.,  1.,  1., // NODE 6
                -1.,  1.,  1. }; // NODE 7

 GLubyte frontIndices[]  = { 4,5,6,7 }; // FACE 1 : NODES 4, 5, 6, 7
 GLubyte rightIndices[]  = { 1,2,6,5 }; // FACE 2
 GLubyte bottomIndices[] = { 0,1,5,4 }; // FACE 3
 GLubyte backIndices[]   = { 0,3,2,1 }; // FACE 4
 GLubyte leftIndices[]   = { 0,4,7,3 }; // FACE 5
 GLubyte topIndices[]    = { 2,3,7,6 }; // FACE 6

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glOrtho(-2., 2., -2., 2., 1., 100.);

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 gluLookAt(15., 10., 10., 0., 0., 0., 0., 0., 1.);

 glEnable(GL_DEPTH_TEST);

 glEnableClientState(GL_VERTEX_ARRAY);
 glVertexPointer(3, GL_FLOAT,  0, vertices);

 glColor3f(1.0, 0.0, 0.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, frontIndices);
 glColor3f(1.0, 1.0, 0.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, backIndices);
 glColor3f(0.0, 1.0, 0.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, leftIndices);
 glColor3f(0.0, 1.0, 1.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, rightIndices);
 glColor3f(1.0, 0.0, 1.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, topIndices);
 glColor3f(1.0, 1.0, 1.0); glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, bottomIndices);
 

Thx so much!
I got the program work according to your advise.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.