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.