Archived

This topic is now archived and is closed to further replies.

abvenia

Magical Water

Recommended Posts

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(); }

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

}

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
quote:
Original post by wAVaRiaN
I think I''m going to go sit in a corner and cry.


I saw it, and tried it.. thought I responded. Anyways; it didn''t work out, I imagine a mess up on my end. As I said, I revamped my code, actually drew the oxygen in between drawing the two hydrogens.(To make a point, ) With my new code; the depths worked as they should. Thanks for your help though!

Share this post


Link to post
Share on other sites