Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / 3D Theory & Graphics / Problem with depth rotation (Open GL) Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Nepher

May 08, 2005, 07:13 AM

I've been making this simple program where there is for example 4 cubes, which rotate around all axe's...this goes well for a half rotation around the axe, but when cubes that were below should go above they stay below and the 2 that were on top correctly become smaller(perspective) but still stay on top, so it looks pretty dump.

Down there is the code for init and drawing...All the help is welcome i've been struggling with this for a while! There might be some methods used twice or more, cause i've been trying them to different places, and dont mind commented lines :)

  1.  
  2. ...
  3. static void perspective (
  4.     GLfloat fovy,
  5.     GLfloat aspect,
  6.     GLfloat zNear,
  7.     GLfloat zFar)
  8. {
  9.     GLfloat xmin, xmax, ymin, ymax;
  10.        
  11.     ymax = zNear * (GLfloat)tan(fovy * 3.1415962f / 360.0);
  12.     ymin = -ymax;
  13.     xmin = ymin * aspect;
  14.     xmax = ymax * aspect;
  15.  
  16.     glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
  17. }
  18.  
  19. static void updateState (
  20.     int width,
  21.     int height)
  22. {
  23.     static const GLfloat light_position[]   = { -50.f, 50.f, 50.f, 0.f };
  24.     static const GLfloat light_ambient[]    = { 0.125f, 0.125f, 0.125f, 1.f };
  25.     static const GLfloat light_diffuse[]    = { 0.7f, 0.7f, 0.7f, 1.f };
  26.     static const GLfloat material_spec[]    = { 0.7f, 0.7f, 0.7f, 0.f };
  27.     static const GLfloat zero_vec4[]        = { 0.0f, 0.0f, 0.0f, 0.f };
  28.  
  29.     float aspect = height ? (float)width/(float)height : 1.0f;
  30.        
  31.         glViewport          (0, 0, width, height);
  32.     glScissor           (0, 0, width, height);
  33.  
  34.     glMatrixMode        (GL_MODELVIEW);
  35.     glLoadIdentity      ();
  36.  
  37.     glLightfv           (GL_LIGHT0, GL_POSITION, light_position);
  38.     glLightfv           (GL_LIGHT0, GL_AMBIENT, light_ambient);
  39.     glLightfv           (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  40.     glLightfv           (GL_LIGHT0, GL_SPECULAR, zero_vec4);
  41.     glMaterialfv        (GL_FRONT_AND_BACK, GL_SPECULAR, material_spec);
  42.  
  43.     glDisable           (GL_NORMALIZE);
  44.    
  45.  
  46.     glEnable            (GL_LIGHT0);
  47.     glEnable            (GL_COLOR_MATERIAL);
  48.     glEnable            (GL_CULL_FACE);
  49.        
  50.         glEnable                        (GL_DEPTH_TEST);
  51.  
  52.         glDepthFunc(GL_LEQUAL);
  53.         glDepthRangef(0,1);
  54.         glClearDepthf(1.0f);
  55.  
  56.         glEnable            (GL_LIGHTING);
  57.     glHint              (GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  58.     glShadeModel        (GL_FLAT);
  59.     glDisable           (GL_DITHER);
  60.     glClearColor        (0.1f, 0.2f, 0.4f, 0.f );
  61.                
  62.     glColor4x           (0x10000, 0, 0, 0);
  63.    
  64.        
  65.        
  66.         glEnableClientState (GL_VERTEX_ARRAY);
  67.     glEnableClientState (GL_NORMAL_ARRAY);
  68.  
  69.     glMatrixMode        (GL_PROJECTION);
  70.        
  71.  
  72.  
  73.     glLoadIdentity      ();
  74.  
  75.    
  76.  
  77.         perspective         (60.f, aspect, 0.1f, 100.f);
  78.  
  79. }
  80.  
  81.  
  82. static void init (void)
  83. {
  84.  
  85.  
  86.  
  87. #if defined(GL_OES_VERSION_1_1)
  88.    
  89.  
  90.         glBindBuffer(GL_ARRAY_BUFFER, s_vertexBufferID);
  91.     glBufferData(GL_ARRAY_BUFFER, sizeof(s_cubeVertices), s_cubeVertices, GL_STATIC_DRAW);
  92.  
  93.     glBindBuffer(GL_ARRAY_BUFFER, s_normalBufferID);
  94.     glBufferData(GL_ARRAY_BUFFER, sizeof(s_cubeNormals), s_cubeNormals, GL_STATIC_DRAW);
  95.  
  96.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s_indexBufferID);
  97.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(s_cubeIndices), s_cubeIndices, GL_STATIC_DRAW);
  98.  
  99. #endif
  100. }
  101.  
  102.  
  103. static void drawScene (
  104.     int width,
  105.     int height)
  106. {
  107.  
  108.          
  109. const double time = fmod(GetTickCount() / 1000.0, 360.0);
  110.  
  111.     updateState     (width, height);
  112.  
  113.  
  114.         glClear         (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  115.  
  116.         glMatrixMode    (GL_MODELVIEW);
  117.     glLoadIdentity  ();
  118.        
  119.  
  120.         glTranslatef    (0.f, 0.f, -30.f);
  121.  
  122.        
  123.         glRotatef       ((float)(time*29.77f*2), 1.0f,2.0f, 0.0f);
  124.  
  125.         glRotatef       ((float)(time*22.311f), -0.1f, 0.0f, -5.0f);
  126.         glTranslatef    (-0.f,-0.f,30.f);
  127.  
  128.  
  129.  
  130.         glBindBuffer    (GL_ARRAY_BUFFER, s_vertexBufferID);
  131.     glVertexPointer (3, GL_BYTE, 0, 0);
  132.  
  133.     glBindBuffer    (GL_ARRAY_BUFFER, s_normalBufferID);
  134.     glNormalPointer (GL_BYTE, 0, 0);
  135.    
  136.         glBindBuffer    (GL_ELEMENT_ARRAY_BUFFER, s_indexBufferID);
  137.         glPushMatrix();
  138.         glTranslatef    (3.f,0.f,-20);
  139.  
  140.  
  141.  
  142. //      glRotatef       ((float)(time*29.77f), 1.0f, 2.0f, 0.0f);
  143. //      glRotatef       ((float)(time*22.311f), -0.1f, 0.0f, -5.0f);
  144.  
  145.  
  146.         glDrawElements(GL_TRIANGLES, 6*6, GL_UNSIGNED_BYTE, 0);
  147.  
  148.         glPopMatrix();
  149.  
  150.  
  151. glPushMatrix();
  152.  
  153.        
  154.         glTranslatef    (3.f,8.f,-20);
  155.        
  156.  
  157. //      glRotatef       ((float)(time*29.77f), 1.0f, 2.0f, 0.0f);
  158. //      glRotatef       ((float)(time*22.311f), -0.1f, 0.0f, -5.0f);
  159.  
  160.         glDrawElements(GL_TRIANGLES, 6*6, GL_UNSIGNED_BYTE, 0);
  161.  
  162.         glPopMatrix();
  163.        
  164. glPushMatrix();
  165.  
  166.         glTranslatef    (-5.f,0.f,-20);
  167.  
  168. //      glRotatef       ((float)(time*29.77f), 1.0f, 2.0f, 0.0f);
  169. //      glRotatef       ((float)(time*22.311f), -0.1f, 0.0f, -5.0f);
  170.  
  171.         glDrawElements(GL_TRIANGLES, 6*6, GL_UNSIGNED_BYTE, 0);
  172.  
  173.         glPopMatrix();
  174.  
  175.  
  176.  
  177. glPushMatrix();
  178.  
  179.  
  180.  
  181.         glTranslatef    (-5.f,8.f,-20);
  182.  
  183.  
  184.  
  185. //      glRotatef       ((float)(time*29.77f), 1.0f, 2.0f, 0.0f);
  186. //      glRotatef       ((float)(time*22.311f), -0.1f, 0.0f, -5.0f);
  187.  
  188.  
  189.         glDrawElements(GL_TRIANGLES, 6*6, GL_UNSIGNED_BYTE, 0);
  190.  
  191.         glPopMatrix();
  192.  
  193. ....

 
Nepher

May 08, 2005, 12:00 PM

Please someone help me...

 
fman256

May 08, 2005, 02:34 PM

How do you want the cubes to rotate? If you want them to rotate in a natural way(always around one axis), you should only be doing one rotation.

 
This thread contains 3 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.