Magical Water

Started by
10 comments, last by abvenia 19 years, 11 months ago
I coded a program which was supposed to create a water molecule like scene. The hydrogens rotate about the oxygen, but when they should be showing as in front of them; they appear behind them still; or at least are drawn over by the oxygen. I have the depth buffer enabled, as well as the Depthfunc... I will post the code; editied to attempt to save room. Thanks! int main(argc,argv) int argc; char *argv[]; { glutInit(&argc, argv); glShadeModel(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_CULL_FACE);glClearDepth(10.0); glutInitWindowPosition (100, 100);glutInitWindowSize(500, 500); glutCreateWindow("water"); glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGBA); glutDisplayFunc(Display); glutIdleFunc(Display); glutReshapeFunc(changeSize); glFrustum(-1.5, 1.5, -1.5, 1.5, 1.0, 10.0); glOrtho(-3.4, 3.4, -3.4, 3.4, -3.4, 3.4); glClearColor(0.6, 0.5, 0.5, 0.5); glutMainLoop(); return 0; } void Display(void) { glMatrixMode(GL_MODELVIEW); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glLoadIdentity(); glPushMatrix(); glColor3f(1.0,1.0,1.0); glTranslatef(0.0,0.0,-1.7); glRotatef(rmolZ,0.0,0.0,1.0); glRotatef(rmolY,0.0,1.0,0.0); glTranslatef(0.2,0.0,0.0); glutSolidSphere(0.05,24,24); glPopMatrix(); glLoadIdentity(); glPushMatrix(); glTranslatef(0.0,0.0,-1.7); glColor3f(1.0,1.0,1.0); glRotatef(rmolZ,0.0,0.0,1.0); glRotatef(rmolY,0.0,1.0,0.0); glTranslatef(-0.0651,0.189,0.0); glutSolidSphere(0.05,24,24); glPopMatrix(); glLoadIdentity(); glPushMatrix(); glTranslatef(0.0,0.0,-1.7); glColor3f(red,green,blue); glRotatef(rmolZ,0.0,0.0,1.0); glRotatef(rmolY,0.0,1.0,0.0); glutSolidSphere(0.05,24,24); glPopMatrix(); glEnd(); if(rmolY>-1080) rmolY-=3; else rmolZ-=5; if(rmolZ<-720) exit(0); glutSwapBuffers(); glFlush(); glutPostRedisplay( ); } void changeSize(int w, int h) { if(h == 0) h = 1; float ratio = 1.0* w / h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho (-1.0*(GLfloat)w/(GLfloat)h,1.0*(GLfloat)w/(GLfloat)h, -1.0, 1.0, -1.0, 1.0); glViewport(0, 0, w, h); gluPerspective(45,ratio,.1,100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,0.0,0.0,0.0,0.0,0.0f,1.0f,0.0f); glutPostRedisplay(); }
Advertisement
Two things seem strange to me:

1)
glClearDepth(10.0);
Usually, the depth buffer is cleared by 1.0, not 10.0

2)
glFrustum(-1.5, 1.5, -1.5, 1.5, 1.0, 10.0);
glOrtho(-3.4, 3.4, -3.4, 3.4, -3.4, 3.4);

Both glFrustum and glOrtho multiply the current perspective matrix by some projection matrix. I''ve never seen two perspective matrices concatenated like this. That should give unexpected results... Try using either glFrustum or glOrtho. And I''m not sure about that, but consider clearing the perspective matrix before, i.e.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.5, 1.5, -1.5, 1.5, 1.0, 10.0);
OR
glOrtho(-3.4, 3.4, -3.4, 3.4, -3.4, 3.4);

mmm, I included both because I am just learning via picking apart other programs.. I have commented out the glOrtho, added the glLoadIdentity.... as it occurs in Main. When I tried to add the glfustum near the beginning of the resize function; the models seemed very small for a split second, then the window was empty.. I removed that bit, and the program runs as normal; that is to say, still not applying depth visual properly. Thank you for your help.
Don''t forget to take into account the ordering in which the objects are drawn. Since you''re working with blending etc, you need to draw the objects that are going to be behind everything first. Or if you''re lazy, you can just do this:

void DrawScene(){

glDepthMask(GL_FALSE);
// Draw oxygen atom
// Draw hydrogen atoms
glDepthMask(GL_TRUE);

}
quote:Original post by wAVaRiaN
Don''t forget to take into account the ordering in which the objects are drawn. Since you''re working with blending etc, you need to draw the objects that are going to be behind everything first.


The Hydrogens are not always in front of the oxygen though; it rotates...
quote:Original post by abvenia
The Hydrogens are not always in front of the oxygen though; it rotates...


That''s why you have to depth-sort them (draw the nearest objects last).
quote:Original post by Tree Penguin
That''s why you have to depth-sort them (draw the nearest objects last).


Making just this change to the code results in the Hydrogens never being behind the oxygen; even when their rotation shows that they should be.




In any case, I''ve revamped the code; fixed my Matrices; and this fixed the problem, Thank you all for your help.
quote:Original post by abvenia
quote:Original post by Tree Penguin
That''s why you have to depth-sort them (draw the nearest objects last).


Making just this change to the code results in the Hydrogens never being behind the oxygen; even when their rotation shows that they should be.




In any case, I''ve revamped the code; fixed my Matrices; and this fixed the problem, Thank you all for your help.


In that case you are doing something wrong, when depth-sorting you should sort them from far to near, from the camera, not in world space.
quote:Original post by wAVaRiaN
Don''t forget to take into account the ordering in which the objects are drawn. Since you''re working with blending etc, you need to draw the objects that are going to be behind everything first. Or if you''re lazy, you can just do this:

void DrawScene(){

glDepthMask(GL_FALSE);
// Draw oxygen atom
// Draw hydrogen atoms
glDepthMask(GL_TRUE);

}


Did noone see the code I posted with this comment? You don''t need to do pre-depth testing using the above!
quote:Original post by wAVaRiaN
Did noone see the code I posted with this comment? You don't need to do pre-depth testing using the above!


did you not see this?

quote:
The Hydrogens are not always in front of the oxygen though; it rotates...


-me



[edited by - Palidine on May 19, 2004 7:11:50 PM]

This topic is closed to new replies.

Advertisement