Need help to check this simple program

I am a newbie to OpenGL.

I made a simple program with glut.
The program was compiled successfully under
my debian OS with gcc.
While, when I ran it, I got nothing but a blank window.
What’s wrong with my code?

Any advise?
thx in advance.

here is my code:

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

static GLint vertices[] = {
		25,25,
		100,325,
		175,25,
		175,325,
		250,25,
		325,325};

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};

void init(void)
{
	glClearColor(0.0,0.0,0.0,0.0);
	glShadeModel(GL_FLAT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0,400.0,0.0,400.0,-1.0,1.0);
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_POLYGON);
	//	glEnableClientState(GL_COLOR_ARRAY);
		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_COLOR_ARRAY);

	//	glColorPointer(3,GL_FLOAT,0,colors);
		glVertexPointer(2,GL_INT,0,vertices);
		glColorPointer(3,GL_FLOAT,0,colors);
	glEnd();
	//glColor3f(1.0,1.0,1.0);
	//glBegin(GL_POLYGON);
	//	glVertex3f(0.25,0.25,0.0);
	//	glVertex3f(0.75,0.25,0.0);
	//	glVertex3f(0.75,0.75,0.0);
	//	glVertex3f(0.25,0.75,0.0);
	//glEnd();
	
	glFlush();
}

void reshape(int w , int h)
{
	glViewport(0,0,(GLsizei)w,(GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-500,500,-500,500,-1,1);
}

int main(int argc, char** argv)
{
	glutInit(&argc,argv);
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutInitWindowSize(500,500);
	glutInitWindowPosition(100,100);
	glutCreateWindow("Example");
	init();
	glutDisplayFunc(display);
	//glutReshapeFunc(reshape);
	glutMainLoop();
	return 0;
}

 

I have just briefly read it so maybe there are some more problems but for example you never set glViewport so openGl does not know where to draw ( you actualy set it in reshape function but you have commented out it from your code :slight_smile: )

the next thing is that you should always set your matrix mode to GL_MODELVIEW matrix before drawing anything ( you always set you matrix mode to GL_PROJECTION but you never change it back to GL_MODELVIEW )

Thx so much.
Can u give me some detail advise on modifying the program.
I have no idea about how to make the program correctly run.

Originally posted by Trahern:
[b]I have just briefly read it so maybe there are some more problems but for example you never set glViewport so openGl does not know where to draw ( you actualy set it in reshape function but you have commented out it from your code :slight_smile: )

the next thing is that you should always set your matrix mode to GL_MODELVIEW matrix before drawing anything ( you always set you matrix mode to GL_PROJECTION but you never change it back to GL_MODELVIEW )[/b]

well the best advise is probably to find some basic tutorial using GLUT on the web. Im sure there is plenty of tutorials out there :slight_smile:

I remember reading one of the OpelGL books that are freely available in the documentation section. There was a sample in there very similar to the one you posted and it gave instructions on how to modify it and I was able to create a 3D looking cube using shading, covered in the book, and changing some vertecies, also covered. I’m not sure if it was The Red Book' orThe Blue Book.’ It wasn’t the reference book.

Checking the site it’s called `The OpenGl Programming Guide: The Red Book.’ It was within the first few chapter. It’s also tells you waht opn GL can and can’t do. I’m not done reading it yet but it seems like a great starting point.

It also uses GLUT.

Thx so much.
I am just reading the book you mentioned.
in fact, the above code was made according to
examples of redbook.

Originally posted by Cheese:
[b]I remember reading one of the OpelGL books that are freely available in the documentation section. There was a sample in there very similar to the one you posted and it gave instructions on how to modify it and I was able to create a 3D looking cube using shading, covered in the book, and changing some vertecies, also covered. I’m not sure if it was The Red Book' or The Blue Book.’ It wasn’t the reference book.

Checking the site it’s called `The OpenGl Programming Guide: The Red Book.’ It was within the first few chapter. It’s also tells you waht opn GL can and can’t do. I’m not done reading it yet but it seems like a great starting point.

It also uses GLUT.[/b]

if you use vertex/color arrays, you do not need glBegin/glEnd. instead you use glDrawArrays:

glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_INT, 0, vertices);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, colors);
glDrawArrays(GL_POLYGON, 0, 6);
glFlush();

note that you pass a primitive type (here: GL_POLYGON) to glDrawArrays. if you want to
draw multiple types of primitives (quads, trias) you have to setup vertex arrays for each type.

Thx SO MUCH!
And I am also sorry for cross-post.

Originally posted by RigidBody:
[b]if you use vertex/color arrays, you do not need glBegin/glEnd. instead you use glDrawArrays:

glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_INT, 0, vertices);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, colors);
glDrawArrays(GL_POLYGON, 0, 6);
glFlush();

note that you pass a primitive type (here: GL_POLYGON) to glDrawArrays. if you want to
draw multiple types of primitives (quads, trias) you have to setup vertex arrays for each type.[/b]