Thread: OpenGL, glut, and rotating

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    2

    OpenGL, glut, and rotating

    Basically I want to do this:
    1)Draw a square in one corner of the window.
    2)Have the square move around the centre of the window.
    3)Have the square spin as it moves.

    I can go to (2) fine, but I'm having trouble with (3). Any rotation I set for the square makes it rotate around the window's centre, but I want it to rotate around its own center.
    I tried to translate to the square's centre (as I read somewhere) but translating in any direction moves the square as well.
    How can I make the square spin, without placing it in the window's centre? I think that if I knew that I'd be able to combine it with rotating around the centre.

    This is just making the square rotate:
    Code:
    #include <GL/glut.h>
    
    static GLfloat rotat=0.0;
    static GLint rotatq=0;
    
    void init(void);
    void display(void);
    void reshape(int w, int h);
    void spindisplay(void);
    void mouse(int button, int state, int x, int y);
    
    int main(int argc, char **argv)
    {	glutInit(&argc,argv);
    	glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
    	glutInitWindowSize(500,500);
    	glutInitWindowPosition(100,100);
    	glutCreateWindow("Moving squares");
    	init();
    	glutDisplayFunc(display);
    	glutReshapeFunc(reshape);
    	glutMouseFunc(mouse);
    	glutIdleFunc(spindisplay);
    	glutMainLoop();
    }
    
    void init(void)
    {	glClearColor(0.0,0.0,0.0,0.0);
    	glShadeModel(GL_FLAT);
    }
    
    void display(void)
    {	glClear(GL_COLOR_BUFFER_BIT);
    	glPushMatrix();
    	glRotatef(rotat,0.0,0.0,1.0);
    	glColor3f(0.0,0.0,1.0);
    	glRectf(-250.0,-250.0,-50.0,-50.0);
    	glPopMatrix();
    	glutSwapBuffers();
    }
    
    void reshape(int w, int h)
    {	glViewport(0,0,(GLsizei)w,(GLsizei)h);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	glOrtho(-250.0,250.0,-250.0,250.0,-1.0,1.0);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    }
    
    void spindisplay(void)
    {	if(rotatq==1)
    	{	rotat+=0.1;
    		if(rotat>360.0)
    			rotat-=360.0;
    	}
    	glutPostRedisplay();
    }
    
    void mouse(int button, int state, int x, int y)
    {	switch(button)
    	{	case GLUT_LEFT_BUTTON:
    			if(state==GLUT_DOWN)
    				rotatq=1;
    			break;
    		case GLUT_RIGHT_BUTTON:
    			if(state==GLUT_DOWN)
    				rotatq=0;
    			break;
    		default:
    			break;
    	}
    }
    I get the feeling I'm missing something very basic, but I don't know what that is...

    By the way, I'm using Ubuntu 6.10.

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    If I remember my OpenGL right(haven't used it much in months) you just translate to the location you want, then push the matrix, draw your square with what ever rotation, pop the matrix and call it good, but let me go write a test program and see... will be a good practice for me anyway.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    glRotef() alone will rotate by the specified amount of degrees about the specified axis with the point of rotation being the origin. So, to use a different point of rotation (the center of the square - which you need to calculate / know), one example could be:
    Code:
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(2.0, 3.0, 4.0);
    glRotatef(20.0, 0.0, 0.0, 1.0);
    glTranslatef(-2.0, -3.0, -4.0);
    This should rotate the points drawn next by 20 degrees about the line through the origin and the point (0, 0, 1) with a fixed point of (2, 3, 4).

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    2
    Quote Originally Posted by Wraithan
    If I remember my OpenGL right(haven't used it much in months) you just translate to the location you want, then push the matrix, draw your square with what ever rotation, pop the matrix and call it good, but let me go write a test program and see... will be a good practice for me anyway.
    That was it, I found it last night... Forgot to say it here though, sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. programming in openGL
    By garg in forum C Programming
    Replies: 1
    Last Post: 05-26-2009, 04:33 PM
  2. rotating around object (looat)
    By jabka in forum Game Programming
    Replies: 13
    Last Post: 06-18-2008, 05:02 PM