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...

#include <cstdlib>
#include <stdlib.h>    // For malloc() etc.
#include <stdio.h>     // For printf(), fopen() etc.
#include <iostream>    // For cout
#include <math.h>      // For sin(), cos() etc.
#include <GL/glfw.h>   // For GLFW, OpenGL and GL

  //For animation if no joystick is preasent.
double angle=0.0;

void Draw( void ){
      int    width, height;  // Window dimensions
      double t;              // Time (in seconds)
      // Get current time
      t = glfwGetTime();
      // Get window size
      glfwGetWindowSize( &width, &height );

      // Make sure that height is non-zero to avoid division by zero
      height = height < 1 ? 1 : height;

      // Set viewport
      glViewport( 0, 0, width, height );
      // Clear color and depht buffers
      glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
      //glClearDepth(-10000);
      glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
      // Set up projection matrix
      glMatrixMode( GL_PROJECTION );    // Select projection matrix
      glLoadIdentity();                 // Start with an identity matrix
      glOrtho(-2,2,-2,2,-2,2);

      // Set up modelview matrix
      glMatrixMode( GL_MODELVIEW );     // Select modelview matrix
      glLoadIdentity();                 // Start with an identity matrix


    //  glRotated(30,0,1,0);
      //glRotated(30,1,0,0);
      
      
      if(glfwGetJoystickParam(0,GLFW_PRESENT)){
            float pos[4];
            int num = glfwGetJoystickPos(GLFW_JOYSTICK_1,pos,10);
            float scale = ((-pos[2])+1)/2;
            glScalef(scale,scale,scale);
            if(!(pos[0]<0.05&&pos[0]>-0.05)){
                                             glRotated(180*pos[0],1,0,0);
            }
            if(!(pos[1]<0.05&&pos[1]>-0.05)){
                                             glRotated(180*pos[1],0,1,0);
            }
            if(num<3){
                        if(!(pos[3]<0.05&&pos[3]>-0.05)){
                                             glRotated(180*pos[3],0,0,1);
                        }
            }
      } else {
             glRotated(angle,0,1,0);
             angle=angle+1;
      }
      glBegin(GL_QUADS);

                        glColor3f(1,0,0);

                        glVertex3f(1,1,1);
                        glVertex3f(-1,1,1);
                        glVertex3f(-1,-1,1);
                        glVertex3f(1,-1,1);

                        glColor3f(0,1,0);

                        glVertex3f(1,-1,-1);
                        glVertex3f(-1,-1,-1);
                        glVertex3f(-1,1,-1);
                        glVertex3f(1,1,-1);

/*                        glColor3f(0,0,1);

                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);

                        glColor3f(1,1,0);
                        
                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);

                        glColor3f(1,0,1);

                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);

                        glColor3f(0,1,1);
                        
                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);
                        glVertex3f(1,1,1);
                        */
      glEnd();
      glfwSwapBuffers();

  }
  //----------------------------------------------------------------------
  // main() - Program entry point
  //----------------------------------------------------------------------

  int main( int argc, char **argv )
  {
      int    ok;             // Flag telling if the window was opened
      int    running;        // Flag telling if the program is running
      // Initialize GLFW
      glfwInit();
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_DEPTH_BUFFER_BIT);
      glDisable(GL_CULL_FACE);
      glCullFace(GL_BACK);
      //glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
      // Open window
      ok = glfwOpenWindow(
          640, 480,          // Width and height of window
          8, 8, 8,           // Number of red, green, and blue bits for color buffer
          8,                 // Number of bits for alpha buffer
          24,                // Number of bits for depth buffer (Z-buffer)
          0,                 // Number of bits for stencil buffer
          GLFW_WINDOW        // We want a desktop window (could be GLFW_FULLSCREEN)
      );

      // If we could not open a window, exit now
      if( !ok )
      {
          glfwTerminate();
          return 0;
      }
      // Set window title
      glfwSetWindowTitle( "My OpenGL program" );

      // Enable sticky keys
      glfwEnable( GLFW_STICKY_KEYS );
      // Main rendering loop
      do
      {
          // Call our rendering function
          Draw();

          
          // Check if the escape key was pressed, or if the window was closed
          running = !glfwGetKey( GLFW_KEY_ESC ) &&
                    glfwGetWindowParam( GLFW_OPENED );
      }
      while( running );
      // Terminate GLFW
      glfwTerminate();
      // Exit program
      return 0;
  }

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.

Thanks, that worked, was that something I missed in the GLFW documentation?

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 =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.