Hi Everyone
I am trying to get the cylinder to rotate with the floor (cube). Unfortunately I cannot get this to work correctly. Please let me know if you can see where I have gone wrong?
Thanks a lot, my code is:
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glut.h>
GLfloat rotXAngle = 0.0;
GLfloat rotYAngle = 0.0;
GLfloat rotAngle = 0.0;
GLfloat fov = 60.0;
bool isRotating = false;
bool isReverse = false;
int width = 800;
int height = 800;
const float ar = (float)width / (float)height;
void IdleFunc()
{
if (isRotating)
{
if (isReverse)
{
rotAngle -= 0.5f;
}
else
{
rotAngle += 0.5f;
}
}
glutPostRedisplay();
}
void toggleAnimation()
{
if (isRotating){
isRotating = false;
}
else {
isRotating = true;
}
}
void changeRotation()
{
if (isRotating){
if (isReverse)
{
isReverse = false;
}
else
{
isReverse = true;
}
}
}
void KeyboardFunc(BYTE key, int x, int y)
{
switch (key)
{
case 27: // Escape key
exit(0);
break;
case 82: // r key
toggleAnimation();
break;
case 114: // R key
toggleAnimation();
break;
case 83: // s key
changeRotation();
break;
case 115: // S key
changeRotation();
break;
}
}
void zoomIn()
{
if (fov > 30)
fov -= 1.0;
}
void zoomOut()
{
if (fov < 90)
fov += 1.0;
}
void SpecialFunc(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_UP:
zoomIn();
break;
case GLUT_KEY_DOWN:
zoomOut();
break;
}
}
void demo_menu(int id)
{
switch (id)
{
case 1:
exit(0);
break;
case 2:
toggleAnimation();
break;
case 3:
changeRotation();
break;
case 4:
zoomIn();
break;
case 5:
zoomOut();
break;
}
glutPostRedisplay();
}
static void resize(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
gluPerspective(fov, ar, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void DrawCylinder(void)
{
GLUquadricObj *quadratic;
quadratic = gluNewQuadric();
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0f, 1.0f, -15.0f);
glRotatef(100.0f, 1.0f, 0.0f, 0.0f);
glRotatef(rotAngle, 0.0f, 1.0f, 0.0f);
glColor4f(0.5f, 0.0f, 0.0f, 1.0f);
gluCylinder(quadratic, 1.0f, 1.0f, 1.0, 32, 32);
glPopMatrix();
}
void DrawFloor(void)
{
glPushMatrix();
glLoadIdentity(); //resets drawing position
glTranslatef(0.0f, 0.0f, -15.0f);
glRotatef(10.0f, 1.0f, 0.0f, 0.0f);
glRotatef(rotAngle, 0.0f, 1.0f, 0.0f);
glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
glBegin(GL_QUADS);
glVertex3f(5.0f, 0.1f, -5.0f); // Top Right Of The Quad (Top)
glVertex3f(-5.0f, 0.1f, -5.0f); // Top Left Of The Quad (Top)
glVertex3f(-5.0f, 0.1f, 5.0f); // Bottom Left Of The Quad (Top)
glVertex3f(5.0f, 0.1f, 5.0f); // Bottom Right Of The Quad (Top)
glColor4f(0.1f, 0.1f, 0.1f, 1.0f);
glVertex3f(5.0f, -0.1f, 5.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-5.0f, -0.1f, 5.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-5.0f, -0.1f, -5.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f(5.0f, -0.1f, -5.0f); // Bottom Right Of The Quad (Bottom)
glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
glVertex3f(5.0f, 0.1f, 5.0f); // Top Right Of The Quad (Front)
glVertex3f(-5.0f, 0.1f, 5.0f); // Top Left Of The Quad (Front)
glVertex3f(-5.0f, -0.1f, 5.0f); // Bottom Left Of The Quad (Front)
glVertex3f(5.0f, -0.1f, 5.0f); // Bottom Right Of The Quad (Front)
glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
glVertex3f(5.0f, -0.1f, -5.0f); // Bottom Left Of The Quad (Back)
glVertex3f(-5.0f, -0.1f, -5.0f); // Bottom Right Of The Quad (Back)
glVertex3f(-5.0f, 0.1f, -5.0f); // Top Right Of The Quad (Back)
glVertex3f(5.0f, 0.1f, -5.0f); // Top Left Of The Quad (Back)
glColor4f(0.3f, 0.3f, 0.3f, 1.0f);
glVertex3f(-5.0f, 0.1f, 5.0f); // Top Right Of The Quad (Left)
glVertex3f(-5.0f, 0.1f, -5.0f); // Top Left Of The Quad (Left)
glVertex3f(-5.0f, -0.1f, -5.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-5.0f, -0.1f, 5.0f); // Bottom Right Of The Quad (Left)
glVertex3f(5.0f, 0.1f, -5.0f); // Top Right Of The Quad (Right)
glVertex3f(5.0f, 0.1f, 5.0f); // Top Left Of The Quad (Right)
glVertex3f(5.0f, -0.1f, 5.0f); // Bottom Left Of The Quad (Right)
glVertex3f(5.0f, -0.1f, -5.0f); // Bottom Right Of The Quad (Right)
glEnd(); // Done Drawing The Quad
glPopMatrix();
}
//--------------------------------------------------------------------------------------------
// DrawGLScene()
//--------------------------------------------------------------------------------------------
void DrawGLScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, ar, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Now draw and rotate objects..
DrawFloor();
DrawCylinder();
glutSwapBuffers();
}
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 100);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("My Animation");
glutSpecialFunc(SpecialFunc);
glutKeyboardFunc(KeyboardFunc);
glutReshapeFunc(resize);
glutDisplayFunc(DrawGLScene);
glutIdleFunc(IdleFunc);
glutCreateMenu(demo_menu);
glutAddMenuEntry("toggle animation", 2);
glutAddMenuEntry("change rotation", 3);
glutAddMenuEntry("zoom in", 4);
glutAddMenuEntry("zoom out", 5);
glutAddMenuEntry("quit", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop(); //transfer program to glut
return EXIT_SUCCESS;
}