943,526 Members | Top Members by Rank

Ad:
Jan 20th, 2009
0

OpenGL not depth buffering?

Expand Post »
Hi, I've been testing GLFW joystick, trying to write a simple program to rotate a cube with the joystick. The fact that I'm using GLFW may complicate matters for debugging, but it's the only OpenGL library with joystick support that i have found. I don't believe that it should be simple to port to GLUT, just comment out the GLFW calls...

when i rotate the cube some faces obscure other faces regardless of which way they are facing. so i simplified to two quads, one in front, and one in back, and it exhibits the same problem, when rotated to 180 the front face is still drawn in front of the back face. the origin is centered between them.

if you have a joystick, the axes are off, i didn't match them yet. if you don't have a joystick the two faces will rotate about the y axis in an animation.

I think my code is missing something simple, but I can't seem to figure it out. the code isn't short, but I've posted longer...

  1. #include <cstdlib>
  2. #include <stdlib.h> // For malloc() etc.
  3. #include <stdio.h> // For printf(), fopen() etc.
  4. #include <iostream> // For cout
  5. #include <math.h> // For sin(), cos() etc.
  6. #include <GL/glfw.h> // For GLFW, OpenGL and GL
  7.  
  8. //For animation if no joystick is preasent.
  9. double angle=0.0;
  10.  
  11. void Draw( void ){
  12. int width, height; // Window dimensions
  13. double t; // Time (in seconds)
  14. // Get current time
  15. t = glfwGetTime();
  16. // Get window size
  17. glfwGetWindowSize( &width, &height );
  18.  
  19. // Make sure that height is non-zero to avoid division by zero
  20. height = height < 1 ? 1 : height;
  21.  
  22. // Set viewport
  23. glViewport( 0, 0, width, height );
  24. // Clear color and depht buffers
  25. glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  26. //glClearDepth(-10000);
  27. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  28. // Set up projection matrix
  29. glMatrixMode( GL_PROJECTION ); // Select projection matrix
  30. glLoadIdentity(); // Start with an identity matrix
  31. glOrtho(-2,2,-2,2,-2,2);
  32.  
  33. // Set up modelview matrix
  34. glMatrixMode( GL_MODELVIEW ); // Select modelview matrix
  35. glLoadIdentity(); // Start with an identity matrix
  36.  
  37.  
  38. // glRotated(30,0,1,0);
  39. //glRotated(30,1,0,0);
  40.  
  41.  
  42. if(glfwGetJoystickParam(0,GLFW_PRESENT)){
  43. float pos[4];
  44. int num = glfwGetJoystickPos(GLFW_JOYSTICK_1,pos,10);
  45. float scale = ((-pos[2])+1)/2;
  46. glScalef(scale,scale,scale);
  47. if(!(pos[0]<0.05&&pos[0]>-0.05)){
  48. glRotated(180*pos[0],1,0,0);
  49. }
  50. if(!(pos[1]<0.05&&pos[1]>-0.05)){
  51. glRotated(180*pos[1],0,1,0);
  52. }
  53. if(num<3){
  54. if(!(pos[3]<0.05&&pos[3]>-0.05)){
  55. glRotated(180*pos[3],0,0,1);
  56. }
  57. }
  58. } else {
  59. glRotated(angle,0,1,0);
  60. angle=angle+1;
  61. }
  62. glBegin(GL_QUADS);
  63.  
  64. glColor3f(1,0,0);
  65.  
  66. glVertex3f(1,1,1);
  67. glVertex3f(-1,1,1);
  68. glVertex3f(-1,-1,1);
  69. glVertex3f(1,-1,1);
  70.  
  71. glColor3f(0,1,0);
  72.  
  73. glVertex3f(1,-1,-1);
  74. glVertex3f(-1,-1,-1);
  75. glVertex3f(-1,1,-1);
  76. glVertex3f(1,1,-1);
  77.  
  78. /* glColor3f(0,0,1);
  79.  
  80.   glVertex3f(1,1,1);
  81.   glVertex3f(1,1,1);
  82.   glVertex3f(1,1,1);
  83.   glVertex3f(1,1,1);
  84.  
  85.   glColor3f(1,1,0);
  86.  
  87.   glVertex3f(1,1,1);
  88.   glVertex3f(1,1,1);
  89.   glVertex3f(1,1,1);
  90.   glVertex3f(1,1,1);
  91.  
  92.   glColor3f(1,0,1);
  93.  
  94.   glVertex3f(1,1,1);
  95.   glVertex3f(1,1,1);
  96.   glVertex3f(1,1,1);
  97.   glVertex3f(1,1,1);
  98.  
  99.   glColor3f(0,1,1);
  100.  
  101.   glVertex3f(1,1,1);
  102.   glVertex3f(1,1,1);
  103.   glVertex3f(1,1,1);
  104.   glVertex3f(1,1,1);
  105.   */
  106. glEnd();
  107. glfwSwapBuffers();
  108.  
  109. }
  110. //----------------------------------------------------------------------
  111. // main() - Program entry point
  112. //----------------------------------------------------------------------
  113.  
  114. int main( int argc, char **argv )
  115. {
  116. int ok; // Flag telling if the window was opened
  117. int running; // Flag telling if the program is running
  118. // Initialize GLFW
  119. glfwInit();
  120. glEnable(GL_DEPTH_TEST);
  121. glEnable(GL_DEPTH_BUFFER_BIT);
  122. glDisable(GL_CULL_FACE);
  123. glCullFace(GL_BACK);
  124. //glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  125. // Open window
  126. ok = glfwOpenWindow(
  127. 640, 480, // Width and height of window
  128. 8, 8, 8, // Number of red, green, and blue bits for color buffer
  129. 8, // Number of bits for alpha buffer
  130. 24, // Number of bits for depth buffer (Z-buffer)
  131. 0, // Number of bits for stencil buffer
  132. GLFW_WINDOW // We want a desktop window (could be GLFW_FULLSCREEN)
  133. );
  134.  
  135. // If we could not open a window, exit now
  136. if( !ok )
  137. {
  138. glfwTerminate();
  139. return 0;
  140. }
  141. // Set window title
  142. glfwSetWindowTitle( "My OpenGL program" );
  143.  
  144. // Enable sticky keys
  145. glfwEnable( GLFW_STICKY_KEYS );
  146. // Main rendering loop
  147. do
  148. {
  149. // Call our rendering function
  150. Draw();
  151.  
  152.  
  153. // Check if the escape key was pressed, or if the window was closed
  154. running = !glfwGetKey( GLFW_KEY_ESC ) &&
  155. glfwGetWindowParam( GLFW_OPENED );
  156. }
  157. while( running );
  158. // Terminate GLFW
  159. glfwTerminate();
  160. // Exit program
  161. return 0;
  162. }
Similar Threads
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Jan 21st, 2009
0

Re: OpenGL not depth buffering?

It seems that GLFW doesn't properly initialize the opengl state machine until you call glfwOpenWindow.

Move the call to glEnable ( GL_DEPTH_TEST ); to somewhere after the call to glfwOpenWindow. It should work then.

You don't need to call glEnable ( GL_DEPTH_BUFFER_BIT ), at best it's meaningless and at worst it's trying to enable something random.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Jan 21st, 2009
0

Re: OpenGL not depth buffering?

Thanks, that worked, was that something I missed in the GLFW documentation?
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Jan 21st, 2009
0

Re: OpenGL not depth buffering?

Possibly. If I remember correctly GLUT (and SDL) are the same. Calling openwindow or equivalent provides the necessary 'startup arguments' to OpenGL (i.e. buffer sizes), so it's only then that the state machine can really be set-up, I guess =)
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Game Development Forum Timeline: i need help with making my game in DarkGDK
Next Thread in Game Development Forum Timeline: gaming mods





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC