Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Rotation ?

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    26

    Rotation ?

    What I'm trying to do simply is if x is pressed the rotation should be around x-axis and so on with y and z. This is the code
    Code :
     
     
    #  include <GL/glut.h>
     
     
    using namespace std;
     
    static int angle(0);
    static float x(0.0), y(0.0), z(0.0);
     
    // Drawing routine.
    void drawScene(void)
    {
       glClear(GL_COLOR_BUFFER_BIT);
       glColor3f(0.0, 0.0, 0.0);
       glLoadIdentity(); 
     
       // Modeling transformations.
       glTranslatef(0.0, 0.0, -15.0); 
       glScalef(1.0, 1.0, 1.0);
       glRotatef(angle, x, y, z);
     
       glutWireTeapot(5.0); 
     
       glFlush();
    }
     
    // Initialization routine.
    void setup(void) 
    {
       glClearColor(1.0, 1.0, 1.0, 0.0);  
    }
     
    // OpenGL window reshape routine.
    void resize(int w, int h)
    {
       glViewport(0, 0, (GLsizei)w, (GLsizei)h); 
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);
     
       glMatrixMode(GL_MODELVIEW);
    }
     
    // Keyboard input processing routine.
    void keyInput(unsigned char key, int x, int y)
    {
       switch(key) 
       {
          case 27:
             exit(0);
             break;
    	  case 'x': case 'X':
    	  {
    		 x = 1.0; y = 0.0; z = 0.0;
    		 angle += 25;
    		 if ( angle > 360 )
    			 angle = 0;
    	  }
    	  case 'y': case 'Y':
    	  {
    		 x = 0.0; y = 1.0; z = 0.0;
    		 angle += 25;
    		 if ( angle > 360 )
    			 angle = 0;
    	  }
    	  case 'z': case 'Z':
    	  {
    		 x = 0.0; y = 0.0; z = 1.0;
    		 angle += 25;
    		 if ( angle > 360 )
    			 angle = 0;
    	  }
          default:
             break;
       }
       glutPostRedisplay();
    }
     
    // Main routine.
    int main(int argc, char **argv) 
    {
       glutInit(&argc, argv);
       glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
       glutInitWindowSize(500, 500);
       glutInitWindowPosition(100, 100); 
       glutCreateWindow("box.cpp");
       setup(); 
       glutDisplayFunc(drawScene); 
       glutReshapeFunc(resize);  
       glutKeyboardFunc(keyInput);
       glutMainLoop(); 
     
       return 0;  
    }

    Why it doesn't work as I expected?

  2. #2
    Intern Contributor uwi2k2's Avatar
    Join Date
    Mar 2011
    Posts
    98

    break it !

    hi,

    thats simple...
    you are missing the breaks in the switch.

    Code :
          case 'x': case 'X':
          {
             x = 1.0; y = 0.0; z = 0.0;
             angle += 25;
             if ( angle > 360 )
                 angle = 0;
     
             break; // <<<<< MISSING !!!
          }

    so always the last ( z ) is happening.
    But when you change it the behavior for x and y won´t be what you expect ..
    why ? because of the order of translate rotate ...

    just play arround with the order and values of these lines to get a better feeling for it:
    Code :
       glTranslatef(0.0, 0.0, -15.0); 
       glScalef(1.0, 1.0, 1.0);
       glRotatef(angle, x, y, z);

    cu
    uwi
    uwi2k2 - OpenGL Trainer: www.opengl-trainer.com
    ---------------------------------------------------------
    Free Programming Helpline: www.programming-hotline.com
    ---------------------------------------------------------
    A Friday Night Programmer: www.uwiworld.net

  3. #3
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    177
    I might also suggest the small changes to your code shown below.
    You'll need a static angle variable for each axis (xangle, yangle, zangle).
    I'm guessing that even after you make these corrections the rotations still
    won't work as you hoped. Let us know.

    Code :
    case 'x':
    case 'X':
    {
       x = 1.0; y = 0.0; z = 0.0;
       xangle = (xangle + 25) % 360;
     
       break;
    }
    Last edited by Carmine; 05-10-2013 at 10:30 AM.

  4. #4
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    26
    @uwi2k2 & Carmine,
    thank you guys.

    @Carmine,
    What do you mean that I need angle variable for each axis. The code works only if I set for x glRotatef(angle, 1.0, 0.0, 0.0) and y glRotatef(angle, 0.0, 1.0, 0.0) and so is z. I mean without using variables inside glRotatef().
    Does this affect the function?

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    26
    Up up up up up

  6. #6
    Junior Member Newbie
    Join Date
    Apr 2013
    Posts
    26
    I've solved the problem and this is the final code

    Code :
    #include <iostream>
    #include <GL/glut.h>
     
     
    static int xangle(0), yangle(0), zangle(0);
     
     
    // Drawing routine.
    void drawScene(void)
    {
       glClear(GL_COLOR_BUFFER_BIT);
       glColor3f(0.0, 0.0, 0.0);
       glLoadIdentity(); 
     
       // Modeling transformations.
       glTranslatef(0.0, 0.0, -15.0); 
       glScalef(1.0, 1.0, 1.0);
       glRotatef(xangle, 1.0f, 0.0f, 0.0f);  // rotate about x
       glRotatef(yangle, 0.0f, 1.0f, 0.0f);  // rotate about y
       glRotatef(zangle, 0.0f, 0.0f, 1.0f);  // rotate about z
       glutWireTeapot(5.0); 
     
       glFlush();
    }
     
    // Initialization routine.
    void setup(void) 
    {
       glClearColor(1.0, 1.0, 1.0, 0.0);  
    }
     
    // OpenGL window reshape routine.
    void resize(int w, int h)
    {
       glViewport(0, 0, (GLsizei)w, (GLsizei)h); 
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       glFrustum(-5.0, 5.0, -5.0, 5.0, 5.0, 100.0);
       glMatrixMode(GL_MODELVIEW);
    }
     
    // Keyboard input processing routine.
    void keyInput(unsigned char key, int x, int y)
    {
       key = toupper(key);
       switch(key) 
       {
          case 27:
             exit(0);
             break;
    	  case 'X':
    	  {
    		 yangle = 0.0; zangle = 0.0;
    		 xangle += 25;
    		 if ( xangle > 360 )
    			 xangle = 0;
    		 break;
    	  }
    	  case 'Y':
    	  {
    		 xangle = 0.0; zangle = 0.0;
    		 yangle += 25;
    		 if ( yangle > 360 )
    			 yangle = 0;
    		 break;
    	  }
    	  case 'Z':
    	  {
    		 xangle = 0.0; yangle = 0.0;
    		 zangle += 25;
    		 if ( zangle > 360 )
    			 zangle = 0;
    		 break;
    	  }
          default:
             break;
       }
       glutPostRedisplay();
    }
     
    // Main routine.
    int main(int argc, char **argv) 
    {
       glutInit(&argc, argv);
       glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
       glutInitWindowSize(500, 500);
       glutInitWindowPosition(100, 100); 
       glutCreateWindow("box.cpp");
       setup(); 
       glutDisplayFunc(drawScene); 
       glutReshapeFunc(resize);  
       glutKeyboardFunc(keyInput);
       glutMainLoop(); 
     
       return 0;  
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •