Thursday, June 30, 2011

OpenGL Translation/Rotation over C++ Example

#include < GL/glut.h >
float yRotationAngle = 0.0f;
float yLocation = 0.0f;

void display (void)
{
glClearColor(0.2f, 0.7f, 0.0f, 1.0f);

glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

// Push everything 10 units back into the scene
glTranslatef(0.0f, 0.0f, -10.0f);

// Translate our object along the y axis
glTranslatef(0.0f, yLocation, 0.0f);


// Rotate our object around the y axis
glRotatef(yRotationAngle, 0.0f, 1.0f, 0.0f);

// Render the cube
glutWireCube(2.0f);

// Flush buffer to window
glFlush();


yRotationAngle += 0.01f;

//Start opposite rotation if the angle >360 or < -360 if (yRotationAngle > 360.0f)
yRotationAngle -= 360.0f;
}

void reshape (int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}


int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("OpenGL Rotation and Translation");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}

No comments:

Post a Comment