erkisnotgod's picture

glEnable(GL_DEPTH_TEST) issues

I tried to refrain from posting this here, but the opengl forum site refuses to let me post eventhough I'm a registered user.
Hopefully, someone can help answere this.

I am following some OpenGl tutorials, and have stumbled across an issue where glEnable(GL_DEPTH_TEST) causes the black screen of death.
The problem existy ONLY on Windows 7 (64bit installed). I can take the exact same code, run it on xp and everything is perfect.
Removing the glEnable(GL_DEPTH_TEST) will allow the objects to draw.
I have also tried tweaking the gluLookAt, gluPerspective and glViewport paremeters and always end up with a black window until glEnable(GL_DEPTH_TEST) is removed.

I have created many other projects and have not had an issue running under either OS.

Any suggestions on what the actual issue may be?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Make sure your Win7 installation is using a proper OpenGL ICD rather than Microsoft's OpenGL emulation (check glGetString(GL_VENDOR)).

You might be hitting some form of undefined behavior (e.g. caused by forgetting to initialize something) that happens to work on some hardware/OS but fail on another. Without seeing some code (setup and rendering) it's impossible to guess the cause.

erkisnotgod's picture

I will check the calls when I return to the computer with the issue, but this is the code from the tutorial which isn't anything really fancy.

#include "stdafx.h"
#include <GL/gl.h>
#include <GL/glut.h>
 
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
 
 
// The one and only application object
 
CWinApp theApp;
 
using namespace std;
 
void setMaterial(float ambientR, float ambientG, float ambientB, float diffuseR, 
float diffuseG, float diffuseB, float specularR, float specularG, float specularB, float shininess)
{
	float ambient[] = {ambientR, ambientG, ambientB};
	float diffuse[] = {diffuseR, diffuseG, diffuseB};
	float specular[] = {specularR, specularG, specularB};
 
	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
}
 
void display()
{
	//Clear the window
	glClear(GL_COLOR_BUFFER_BIT);
	//Future matrix manipulations should affect the matrix model view
	glMatrixMode(GL_MODELVIEW);
	//Draw scene
	glPushMatrix();
 
	//house
	glPushMatrix();
	setMaterial(0.0, 0.5, 1.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0, 1);
	glutSolidCube(2);//building
	glTranslatef(0,1,0);
 
	glPushMatrix();//roof
	glRotatef(-90,1,0,0);
	setMaterial(0.0, 0.5, 1.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0, 50);
	glutSolidCone(1.5,1,16,8);
	glPopMatrix();
 
	glTranslatef(.75, .5, -.75);//chimney
	glPushMatrix();
	glScalef(1,3,1);
	setMaterial(1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1);
	glutSolidCube(.25);
	glPopMatrix();
	glPopMatrix();
 
	glTranslatef(0, -.65,2);
 
	//car
	setMaterial(1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1);
	glPushMatrix();
	glPushMatrix();
	glScalef(2,.5,1); //body
	glutSolidCube(.5);
	glPopMatrix();
	glTranslatef(0,0,.25);
	glPushMatrix();
	glTranslatef(-.4,-.2,0);
	glutSolidTorus(.05, .1,8,8); //left rear wheel
	glTranslatef(.8,0,0);
	glutSolidTorus(.05, .1,8,8); //left front wheel
	glPopMatrix();
	glTranslatef(0,0,-.5);
	glPushMatrix();
	glTranslatef(-.4, -.2,0); 
	glutSolidTorus(.05, .1,8,8);//right rear wheel
	glTranslatef(.8,0,0);
	glutSolidTorus(.05,.1,8,8);	 //right front wheel
	glPopMatrix();
	glPopMatrix();
	glPopMatrix();
	glFlush();
}
 
 void reshape(int w, int h)
 {
	 //define viewport transformation
	 glViewport(0,0,w,h);
 }
 
int _tmain(int argc, char* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
 
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{
		//init glut w/commanf line params
		glutInit(&argc, argv);
		//setup the size, position and display mode for new windows
		glutInitWindowSize(500,500);
		glutInitWindowPosition(0,0);
		glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH);
 
		//create and setup the window
		glutCreateWindow("hello, Weird Thingy 3!");
		glutDisplayFunc(display);
		glutReshapeFunc(reshape);
 
		//Enable depth buffering
		glEnable(GL_DEPTH_TEST);
 
		//setup lighting
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		float lightpos[] = {0.0, 15.0, 15.0};
		float lightcolor[] = {.5, .5, .5};
		float ambcolor[] = {0.2, 0.2, 0.0};
		glEnable(GL_LIGHTING);
		glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambcolor);
		glEnable(GL_LIGHT0);
		glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
		glLightfv(GL_LIGHT0, GL_AMBIENT, lightcolor);
		glLightfv(GL_LIGHT0, GL_DIFFUSE, lightcolor);
		glLightfv(GL_LIGHT0, GL_SPECULAR, lightcolor);
 
		//define the projection transformation
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(40, 1, 4, 20);
 
		//define the viewing transformation
		glMatrixMode(GL_MODELVIEW);	  
		glLoadIdentity();
		gluLookAt(5.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
 
		//tell glut to wait for events
		glutMainLoop();
	}	  
	return nRetCode;
}
erkisnotgod's picture

Ok, so I ran the GL_VERSION and GL_VENDOR
XP = V2.1. Nvidia
Win7 = V3.0, Nvidia

SO, I'm kind of leaning towards a driver problem, is this a valid assumption?

zahirtezcan's picture

just to mention:
glClear(GL_COLOR_BUFFER_BIT); // you need to add GL_DEPTH_BUFFER_BIT flag here

erkisnotgod's picture

Yup, that was it.
Still doesn't explain why that project ran on WinXP, but not Win7.
Thx for finding my oversight!

c2woody's picture

Most likely the "default" values found in the depth buffer are different (there could be anything so the nondeterministic behaviour is correct).