This should work but doesn't ! HEEELPP !

Hi,

This is my first OpenGL program and it doesn’t work, all I get is black. I can’t figure out why. If anyone out there knows (I’m sure it’s a stupid mistake) piece reply ! Thanks

// Tony’s First Test GL program

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

#ifdef GL_VERSION_1_1

void initPointers(void){
static GLint vertices[] = {20,20,20,
20,20,40,
40,20,40,
40,20,20,
20,40,20,
20,40,40,
40,40,40,
40,40,20};

static GLfloat colors[] = {1.0,0.2,0.2,
	                       0.2,0.2,1.0,
						   0.8,1.0,0.2,
						   0.75,0.75,0.75,
						   0.35,0.35,0.35,
						   0.5,0.5,0.5};

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3,GL_INT,0,vertices);
glColorPointer(3,GL_FLOAT,0,colors);

}

void init(void){
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_SMOOTH);
initPointers();
}

void display(void){
glClear(GL_COLOR_BUFFER_BIT);
static GLint allIndices[] = {0,3,7,6,3,2,6,7,
1,0,4,5,2,1,5,6,
4,7,6,5,3,0,1,2};

glDrawElements(GL_QUADS,24,GL_INT,allIndices);
glFlush();

}

void reshape (int w,int h){
glViewport(0, 0,(GLsizei) w,(GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,(GLdouble)(w/h),1.0,150.0);
}

void keyboard(unsigned char key,int x,int y){
switch (key){
case ‘q’:
exit(0);
break;
}
}

int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
#else
int main(int argc, char** argv)
{
printf("This program demonstrates a feature which is not in OpenGL Version 1.0.
");
printf("If your implementation of OpenGL Version 1.0 has the right extensions,
");
printf("you may be able to modify this program to make it run.
");
return 0;
}
#endif

I’m not sure, but I think I haven’t see you using the modelview matrix, i miss something like:

glMatrixMode(GL_MODELVIEW); glLoadIdentity();

i.e. at the end of reshape().

Hope this helps,

Daniel Palomo van Es.

Ps. I think that if this is your first app. written in OpenGL, maybe you should try to draw a single primitive like a trig with glVertex Calls instead of using vertex-lists.

I think your problem might be because you are defining your vertices locally to the initPointers function. You realize that even if your initPointer function is global that doesn’t mean its locally defined data types are global as well. I would move those vertices outside of the initPointers function.

As well I believe that glVertexPointer needs to be called every time you want to draw, but I’m not sure. Other calls to glVertexPointer will override previous ones. Oh, and move:

static GLint allIndices[] = 		            {0,3,7,6,3,2,6,7,
         1,0,4,5,2,1,5,6,
         4,7,6,5,3,0,1,2};

outside of the display function if at all possible. The creation of this datatype every time a display is called is expensive. Declare that as global maybe.

Hope I was helpful.

Random Task