Program to rotate things simultaneously


I created this accidentally while playing about with some openGl commands. The perspective projection of the objects makes them behave this way. This won’t happen if you use the ortho command.


#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>

int a[3]={10,10,10}, b[3]={10,-10,10},
c[3]={-10,-10,10}, d[3]={-10,10,10},
e[3]={10,10,-10}, f[3]={10,-10,-10},
g[3]={-10,-10,-10}, h[3]={-10,10,-10};

float angle=0.5f;

void drawcube(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);

glMatrixMode(GL_MODELVIEW);
glRotatef(angle, 0.0, 0.66, 0.12);
glBegin(GL_POLYGON);
glVertex3iv(a);
glVertex3iv(f);
glVertex3iv(c);
glVertex3iv(g);

glEnd();
glBegin(GL_LINE_LOOP);
for(int i=1;i<10000;i++)
{
float angle = 8*3.14*i/100;
glVertex2f(cos(angle), sin(angle));
}
glEnd();

glFlush();
glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 0x1B:
case ‘q’:
case ‘Q’:
exit(0);
break;
}
}

void mouse(int btn, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
if (btn == GLUT_LEFT_BUTTON)
angle = angle + 1.0f;
else if (btn == GLUT_RIGHT_BUTTON)
angle = angle – 1.0f;
else
angle = 0.0f;
}
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow(“Glut rotate”);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutDisplayFunc(drawcube);
glutIdleFunc(drawcube);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-30.0, 30.0, -30.0, 30.0, -30.0, 30.0);
//glRotatef(30.0, 1.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0, 0.0, 0.0);

glutMainLoop();
return(0);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s