PDA

View Full Version : Depth problem



vivek vyas
05-25-2009, 01:08 AM
< I A M A B E G I N N E R >

I have problem Regarding to GL_DEPTH_TEST
In following code when i remove glEnable(GL_DEPTH_TEST) and glDisable(GL_DEPTH_TEST)
every thing ok but not appear real
but after applying these enable it shows unwanted result: a red cone with green base sphere is line
main.cpp

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

int WinWidth = 640,WinHeight = 480;
void Cylinder(GLdouble base, GLdouble top, GLdouble height);
void Disk(GLdouble inner, GLdouble outer);
void pyram(GLfloat rad,GLfloat Height)
{// Pyramidical
glColor3f(1.0f,0.0f,0.0f);
Cylinder(0.0f, rad, Height);
glColor3f(0.0f,1.0f,0.0f);
glTranslatef(0.0f,0.0f,Height);
Disk(0.0f,rad);
}
GLfloat zr = 0.5f;
void RScene()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glEnable(GL_CW);
glEnable(GL_DEPTH_TEST);
glRotatef(zr,1.0f,0.0f,0.0f);
glPushMatrix();
pyram(0.5,0.5);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glDisable(GL_CW);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


zr+= 0.5f;
glutSwapBuffers();
glutPostRedisplay();
}

void WinSize(GLsizei w,GLsizei h)
{
GLfloat Ratio = (GLdouble)w/(GLdouble)h;
if(h==0)h=1;



glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,Ratio,-1.0f,10.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,w,h);

}
void Idle(){glutPostRedisplay();}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(WinWidth,WinHeight);
glutCreateWindow("Camera in a room");
glutDisplayFunc(RScene);
glutReshapeFunc(WinSize);

glutMainLoop();


}

void Cylinder(GLdouble base, GLdouble top, GLdouble height)
{GLUquadric *Cy;
Cy = gluNewQuadric ();
gluQuadricDrawStyle ( Cy, GLU_FILL);
gluQuadricNormals ( Cy, GLU_SMOOTH);
gluQuadricOrientation ( Cy, GLU_INSIDE);
gluCylinder (Cy, base, top, height, 32,24);
}
void Disk(GLdouble inner, GLdouble outer)
{GLUquadric *Dsk;
Dsk = gluNewQuadric ();
gluQuadricDrawStyle ( Dsk, GLU_FILL);
gluQuadricNormals ( Dsk, GLU_SMOOTH);
gluQuadricOrientation ( Dsk, GLU_INSIDE);
gluDisk ( Dsk, inner, outer,32,24);
}
makefile:
LIB = -I/usr/lib
INC = -I/usr/include
SRC = main.cpp
COMP = gcc
LINK = -lglut -lGL
1st:
$(COMP) -o cam1 $(SRC) $(LIB) $(INC) $(LINK)

Q.1 is glTranslatef problem give problem?

Q2. is there any way that fun Cylinder and Disk combine and make
one object CyPyramid which really act as one object

< I A M A B E G I N N E R >

scratt
05-25-2009, 01:59 AM
GL_DEPTH_TEST, once disabled means that anything drawn to the frame buffer will overwrite the content that is already there.. So drawing is order based, rather than depth based.

You might want to look into glDepthMask(GL_TRUE / GL_FALSE);
With this depth testing is still performed (if enabled), but anything that is drawn when the depth mask is false will not write to the depth buffer, so has no effect on subsequent draw calls, as it has no effect on the depth value for pixels it overwrites.

There are various primitives that you can get from various libraries for OpenGL.
Someone else may be able to help there more.

What you would be best doing however is learning how the common shapes like spheres and so forth are made by those libraries. Because the same systems are available to you. If you construct your vertices for the objects you want to draw procedurally you won't have the kind of problems you are facing here with overlapping geometry and z-fighting, which is what I suspect you are seeing.

Another thing you might try is to play with the resolution and range of the Depth Buffer. If you know your objects fall within a certain range in Z, then altering your znear and zfar values for the viewing frustum so that they clamp your objects more closely will give you better resolution when geometry intersects, and may in the short term resolve some of your issues. ALso Depth Buffers can have varying bit depths. The higher the bit depth, the better the resolution of depth calculations.