PDA

View Full Version : Having a problem with depth testing.



seckley
08-04-2007, 04:33 PM
HI;
i have written a program for the graphics class i'm in. it is a sun with planets rotating around it and moons rotating around the planet. my problem is this... when the planets are go behind the sun from the camera perspective they are larger and appear to be closer than when they pass in front of the sun. any idea what the problem might be? the camera class i'm using is very similar to the one on my text book's website. here is the link to the book's source.
thttp://www.fshilljr.com/ch7source.zip

ZbuffeR
08-04-2007, 05:01 PM
Make sure you request and really get a framebuffer with depth buffer.
Enable depth testing.

Voilą.

EDIT: ie. with glut, init your window like this :
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

seckley
08-04-2007, 06:02 PM
actually i am using that call. here is the code in Main()

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA |GLUT_DEPTH);
glutInitWindowSize(ScreenWidth, ScreenHeight);
glutInitWindowPosition(110, 0);
glutCreateWindow("SolSys Test");
glutKeyboardFunc(myKeyboard);
glutDisplayFunc(RenderScene);
glClearColor(0.0f, 0.0f, 0.0f, 01.0f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glColor3f(0,0,0);
glEnable(GL_DEPTH_TEST);
glViewport(0,0,ScreenWidth, ScreenHeight);
glutIdleFunc(RenderScene);
l1.CreateLight();
l2.CreateLight();
l3.CreateLight();
cam.Set(0,50,25,0,0,0,0,1,1);
cam.SetShape(30.0f,1, -10,10);
glutMainLoop();

I have classes that handle the camera, light, planets and moons. the program is also setup to toggle the lights on and off, move the camera, change the speed application and toggle between alpha blending and depth testing. here is my RenderScene method.

void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//

//draw the sun
glPushMatrix();
{
glRotated(-SolarDay, 0,0,1);
glTranslated(0,0,0);
glutSolidSphere(2,20,20);
}
glPopMatrix();

//apply the time multiplier to the planets and moons
p1.SetTimeMulti(nSpeedMultiplier);
p2.SetTimeMulti(nSpeedMultiplier);
p3.SetTimeMulti(nSpeedMultiplier);
p4.SetTimeMulti(nSpeedMultiplier);
m1.SetTimeMulti(nSpeedMultiplier);
m2.SetTimeMulti(nSpeedMultiplier);
m3.SetTimeMulti(nSpeedMultiplier);
m4.SetTimeMulti(nSpeedMultiplier);
m5.SetTimeMulti(nSpeedMultiplier);

//draw the planets and moons++
m1.Draw();
m2.Draw();
m3.Draw();
m4.Draw();
m5.Draw();
p1.Draw();
p2.Draw();
p3.Draw();
p4.Draw();

SolarDay += .004 * nSpeedMultiplier; //25 earth day = 1 solar day
glFlush();
glutSwapBuffers();
}

Komat
08-05-2007, 01:40 AM
cam.SetShape(30.0f,1, -10,10);Based on your camera code this call will send negative near plane distance to gluPerspective which expects the value to always be positive.

seckley
08-05-2007, 04:48 AM
Thanks That did the trick.