Gluperspective causes blackout

Simply put the gluPerspective call creates a black screen. If I comment out gluPerspective, the white triangle persists as predicted.
I’ve researched this problem, however it seems when people have solved it they don’t stick around and document the answer. I’ve checked it against github however I am not using visual studio ide.


#define GLUT_DISABLE_ATEXIT_HACK
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

void changeSize(int w, int h)
{

	if(h == 0)
			h = 1;
		float ratio = 1.0* w / h;
		//use projection mode
		glMatrixMode(GL_PROJECTION);
		// reset matrix

		glLoadIdentity();
		// Set the viewport to be the entire window
		glViewport(0,0,w,h);
		// Set the correct perspective.
		//gluPerspective(45,ratio,1,100);

		gluPerspective(45.0f,ratio,0.1f,100.0f);
		//glOrtho(-40/3.0f,40/3.0f,-10,10,-10,10);
		// Get Back to the Modelview
		glMatrixMode(GL_MODELVIEW);

}

void renderScene(void) {

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBegin(GL_TRIANGLES);
		glVertex3f(-0.5,-0.5,0.0);
		glVertex3f(0.5,0.0,0.0);
		glVertex3f(0.0,0.5,0.0);
	glEnd();

        glutSwapBuffers();
}

int main(int argc, char **argv) {

	// init GLUT and create Window
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("Lighthouse3D - GLUT Tutorial - reshape");

	// register callbacks
	glutDisplayFunc(renderScene);
	glutReshapeFunc(changeSize);
	// enter GLUT event processing cycle
	glutMainLoop();

	return 1;
}

Found an easter egg of a 15 year old workaround here:

http://comp.graphics.api.opengl.narkive.com/qe5ahQH7/gluperspective-not-working-try-this

The author states:

If you’re pulling your hair out over gluPerspective not working, and
you’re tired of being told to RTFM whenever you ask, try this:

  1. You need to call gluLookAt as well, and
  2. You need to call it AFTER gluPerspective.

If you call these functions in the wrong order, you’ll see nothing.

so I cp’d an example gluLookAt example. and did just that. Viola! Triangle shows up first time and every time with resize. And I really don’t know why.
post the code:


#define GLUT_DISABLE_ATEXIT_HACK
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

void changeSize(int w, int h)
{

	if(h == 0)
			h = 1;
		float ratio = 1.0 * w / h;
		//use projection mode
		glMatrixMode(GL_PROJECTION);

		// reset matrix
		glLoadIdentity();

		glViewport(0,0,w,h);
		// Set the correct perspective.

		gluPerspective(45.0f,ratio,0.1f,45.0f);
		//glOrtho(-40/3.0f,40/3.0f,-10,10,-10,10);

		gluLookAt(0.0, 0.0, 5.0,
		             0.0, 0.0, 0.0,
		             0.0, 1.0, 0.0); //say what!!!!!!!!!!!
				//http://comp.graphics.api.opengl.narkive.com/qe5ahQH7/gluperspective-not-working-try-this

						// Set the viewport to be the entire window
		// Get Back to the Modelview
		glMatrixMode(GL_MODELVIEW);
}

void drawScene(void) {

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBegin(GL_TRIANGLES);
		glVertex3f(-0.5,-0.5,0.0);
		glVertex3f(0.5,0.0,0.0);
		glVertex3f(0.0,0.5,0.0);
	glEnd();

        glutSwapBuffers();
}

int main(int argc, char **argv) {

	// init GLUT and create Window
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("Lighthouse3D - GLUT Tutorial - reshape");

	// register callbacks
	glutDisplayFunc(drawScene);
	glutReshapeFunc(changeSize);

	// enter GLUT event processing cycle
	glutMainLoop();

	return 1;
}


In your original code, your gluPerspective call positions your near clipping plane at 0.1 and your far clipping plane at 100.

You draw your triangle with z = 0. Look at the third parameter of your glVertex calls.

So your triangle is being discarded by your near clipping plane and OpenGL is just doing what you told it to do.

This isn’t a problem, error or bug. This is OpenGL working the way it should and it would be a problem if it didn’t.

Calling gluLookat isn’t some kind of secret sauce or magic trick. All that does is position a viewpoint, but you’re calling it on your projection matrix which is a little weird. This kind of thing is only going to lead you down the route of picking up bad habits without understanding what you’re doing or why you’re doing it. Better to get rid of that gluLookat nonsense and go back and understand what caused your original problem: You drew your triangle with Z= 0 and it was discarded by your near clipping plane. The fix isn’t to parachute in another bunch of code that you don’t understand. The fix is to draw it with the correct Z values. Try drawing it with Z = 5 and Z = -5. What happens? Then try it with 10 and -10. Now what happens? Does that help you understand how the Z values in your glVertex calls work?

Moreover, this is going to cause you problems in the future that will show you why you should be putting the viewing (e.g. gluLookAt) transform on the MODELVIEW stack rather than the PROJECTION stack. Read this:

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