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 && 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(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(&display);
glutIdleFunc(&display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMotionFunc(mousemove);
glutMainLoop();
return 0;
}