PDA

View Full Version : no depth in my scene, objects always visible



10-21-2004, 10:48 PM
hi all, I'm new to openGL and tried this simple solar system world, following the tutorial.
I then added DEPTH but still when one planet passes behind another, it's still visible and not hidden when it's behind it. what is the reason, what did i miss in my code? thanks for any help :)

code:
--------------------------

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

static int day =0, year = 0, hours=0, merc=0;
//e=eye c=center to look at o=orientation to origin
GLfloat ez = -8.0 , ey = 8.0 , ex= 0.0, cx=0.0, cy=0.0, cz=0.0, ox=0.0, oy=1.0, oz=1.0;
GLsizei height,width = 0;
int prevx, prevy, prevz = 0;

void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);

glLoadIdentity();
gluLookAt(ex,ey,ez,cx,cy,cz,ox,oy,oz);
glColor3f(1.0,0.0,0.0);
glScalef(10.0,1.0,1.0);
glutWireCube(1.0); //X RED
glScalef(0.1,10.0,1.0);
glColor3f(0.0,1.0,0.0);
glutWireCube(1.0); //Y GREEN
glScalef(1.0,0.1,1.0);
glPushMatrix();
glColor3f(1.0,1.0,1.0);
glutSolidSphere(1.0,20,16); //sun
glPushMatrix();
glRotatef((GLfloat)year,0.0,1.0,0.0);
glTranslatef(5.0,0.0,0.0); //earth
glRotatef((GLfloat)day,0.0,1.0,0.0);
glColor3f(0.0,0.0,1.0);
glutWireSphere(0.2,10,8);
glTranslatef(0.5,0.0,0.0);//moon
glPushMatrix();
glRotatef((GLfloat)hours,0.0,1.0,0.0);
glColor3f(0.0,1.0,0.0);
glutWireSphere(0.1,4,4);
glPopMatrix();
glPopMatrix();
glRotatef(30,1.0,0.0,0.0);
glRotatef((GLfloat)merc,0.0,1.0,0.0);
glTranslatef(2.0,0.0,0.0); // mercury
glColor3f(1.0,0.0,0.0);
glutSolidSphere(0.1,8,8);
glPushMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
day= (day+10) %360;
year = (year+3)% 360;
hours = (hours+1)%360;
merc = (merc+5)%360;
}

void reshape(int w, int h)
{
height = (GLsizei)h;
width = (GLsizei)w;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, width/height,1.0,100.0);
glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
switch(key) {
case 27: //ESC key
exit(0);
break;
default:
break;
}
}

void mousemove( int x, int y)
{
if(prevx==0 &amp;&amp; prevy==0)
{
prevx=x;
prevy=y;
}
if((prevy-y)>0)
{
ez+=0.1;
cz+=0.1;
}
else if((prevy-y)<0)
{
ez-=0.1;
cz-=0.1;
}

if((prevx-x)>0)
{
ex+=0.1;
cx+=0.1;
}
else if((prevx-x)<0)
{
ex-=0.1;
cx-=0.1;
}
prevx=x;
prevy=y;
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPT H);
glEnable(GL_DEPTH_TEST);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(&amp;display);
glutIdleFunc(&amp;display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMotionFunc(mousemove);
glutMainLoop();
return 0;
}

Sumpfratte
10-21-2004, 11:18 PM
Hello :) ,

you have made everything right, only the depth test is enabled at wrong place.
You should enable the depth test after creating the window otherwise the call has no effect.

Sumpfratte

10-21-2004, 11:34 PM
thanks a lot Sumpfratte, now it works effectively :)
I haven't thought about that point, well maybe coz i don't understand all 100% yet hehe ;)

Sumpfratte
10-21-2004, 11:49 PM
When I began with OpenGL I've made the same fault and it took several hours for me to find out the cause.

There was a simple rule I then found out for myself:

"Don't put a gl* command before glutCreateWindow". :D

Silkut
10-22-2004, 12:18 AM
I've made the same fault too, but with the alpha blending and Depth_test, it seems to be a frequent error..

G_gl
10-22-2004, 12:51 AM
hehe :)
i regged myself now <- newbie