Newbie question on OpenGL

Here is the code that I have written based on the NeHe tutorials :

 
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

#define kWindowWidth	400
#define kWindowHeight	300

GLfloat rtri;
GLfloat rquad;

GLvoid InitGL(GLvoid);
GLvoid DrawGLScene(GLvoid);
GLvoid ReSizeGLScene(int width, int height);

int main (int argc, char** argv)
{
	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(kWindowWidth, kWindowHeight);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("OpenGL");

	InitGL();

	glutDisplayFunc(DrawGLScene);
	glutReshapeFunc(ReSizeGLScene);

	glutMainLoop();

	return 0;
}

GLvoid ReSizeGLScene(int width, int height)
{
	glViewport(0, 0, (GLsizei) width, (GLsizei) height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

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

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

GLvoid InitGL(GLvoid)
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_DEPTH_TEST);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_COLOR_MATERIAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

GLvoid DrawGLScene(GLvoid)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glTranslatef(-1.5f, 0.0f, -6.0f);

	glRotatef(rtri, 0.0f, 1.0f, 0.0f);

	glBegin(GL_TRIANGLES);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(0.0f, 1.0f, 0.0f);
		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(-1.0f, -1.0f, 0.0f);
		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(1.0f, -1.0f, 0.0f);
	glEnd();

	glLoadIdentity();
	glTranslatef(1.5f, 0.0f, -6.0f);
	glRotatef(rquad, 1.0f, 0.0f, 0.0f);

	glColor3f(0.5f, 0.5f, 1.0f);
	glBegin(GL_QUADS);
		glVertex3f(-1.0f, 1.0f, 0.0f);
		glVertex3f(1.0f, 1.0f, 0.0f);
		glVertex3f(1.0f, -1.0f, 0.0f);
		glVertex3f(-1.0f, -1.0f, 0.0f);
	glEnd();
	
	glFlush();

	rtri = rtri + 0.2f;
	rquad = rquad - 0.15f;
}
 

The code is supposed to make the triangle and the quad spin, but whenever I try they remain static. Does anybody have an idea ?

Thanks in advance

You’ve asked for a double-buffered context (GLUT_DOUBLE), as you should, but this means you need to use glutSwapBuffers() instead of glFlush().

Rule of thumb – if you call glFlush() or glFinish(), you’re doing something wrong :wink:

Well thanks to have given an answer so quickly. I just did what you said, I deleted the

glFlush();

line and replaced it by

glutSwapBuffers();

. However the elements still refuse to move :confused:
Did I forget an important library or something like that ? I am compiling the program with

g++ -c main.c

and

g++ main.o -o -framework OpenGL -framework GLUT -framework Foundation

maybe the error comes from here.

Thanks for the help

You haven’t asked GLUT to animate anything. It’s working as you’ve specified-- init, and then draw the scene once.

Try adding glutPostRedisplay(); at the end of your displayFunc, so that GLUT will know it has to display again and again.

Put glIdleFunc(DrawGLScene); before glutMainLoop(); call.

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