PDA

View Full Version : cannot understand the rotation



fatyuan
11-13-2013, 09:20 PM
Hi

I try to write something to show rotation, but I cannot understand why the whole rotating process is repeated again and again when I run the project.

I run it on Windows using codeblocks. Can anyone please help me?

Here is my code:


#include <GL/glut.h>
#include <stdlib.h>
#include <Windows.h>
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (-5.0, 5.0, -5.0, 5.0);
glMatrixMode (GL_MODELVIEW);
}

void drawSquare(void)
{
glBegin (GL_POLYGON);
glVertex2f (-1.0f,-1.0f);
glVertex2f (1.0f,-1.0f);
glVertex2f (1.0f, 1.0f);
// glVertex2f (0.0f, 1.2f);
glVertex2f (-1.0f,1.0f);
glEnd ( );
}

void rotate()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
drawSquare();
glFlush();
Sleep(1000);
glClear(GL_COLOR_BUFFER_BIT);
for(int i = 0; i < 30; i++)
{
glTranslatef(2.0 / 30, 3.0 / 30, 0.0);
glRotatef(1, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
drawSquare();
glFlush();
Sleep(100);
glClear(GL_COLOR_BUFFER_BIT);
}
}

int main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (100, 100);
glutInitWindowSize (600, 600);
glutCreateWindow ("Geometrical Transfromation");
init();
glutDisplayFunc (rotate);
glutMainLoop ( );
}

Ehsan Kamrani
11-14-2013, 02:45 AM
The method you have used is not suitable to show rotation. Use glut to set a timer and use double buffers. Then draw the quad including its translation and rotation once per frame.

Beside this point, you are not using glLoadIdentity() before glTranslate()/glRotate() command. Since OpenGL glare in object space, your 3D geometry is translated and rotated in each loop 'reiterative to its current translation/rotation status'.
Instead, use something like this:

void Render()
{
//Activate identity matrix
//load identity matrix
//translate
//use an incremental variable for glRotate
//Rotate the square using incremental variable
//draw the square
}