/*Rotating Shapes Created by Samuel Made possible because of The Pentabolis Project on Youtube Please give credit to original creators for more information go to sammysawesomesite.neocities.org/polygoncpp */ #include #include #include #include using namespace std; void display(); void reshape(int, int); void timer(int); void draw_square(float, float); void keyboardCallback(unsigned char key, int x, int y); void init() { glClearColor(0.0, 0.0, 0.0, 1.0); glEnable(GL_DEPTH_TEST); } int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowPosition(600,250); glutInitWindowSize(500,500); glutCreateWindow("3D polygonal renderer"); glutKeyboardFunc(keyboardCallback); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboardCallback); glutTimerFunc(0, timer, 0); init(); glutMainLoop(); return 0; } float direction = 0.0; float zang = 0.0; float xang = 0.0; float yang = 0.0; char lastKeyPressed = '\0'; void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(xang, yang, zang); glRotatef(direction, 0.0, 0.0, 0.0); //draw glBegin(GL_QUADS); //test //front glColor3f(1.0,0.0,0.0); glVertex3f(-1.0,1.0,1.0); glVertex3f(-1.0,-1.0,1.0); glVertex3f(1.0,-1.0,1.0); glVertex3f(1.0,1.0,1.0); //back glColor3f(0.5,0.0,0.0); glVertex3f(1.0,1.0,-1.0); glVertex3f(1.0,-1.0,-1.0); glVertex3f(-1.0,-1.0,-1.0); glVertex3f(-1.0,1.0,-1.0); //right glColor3f(0.7,0.0,0.0); glVertex3f(1.0,1.0,1.0); glVertex3f(1.0,-1.0,1.0); glVertex3f(1.0,-1.0,-1.0); glVertex3f(1.0,1.0,-1.0); //left glColor3f(0.7,0.0,0.0); glVertex3f(-1.0,1.0,-1.0); glVertex3f(-1.0,-1.0,-1.0); glVertex3f(-1.0,-1.0,1.0); glVertex3f(-1.0,1.0,1.0); //top glColor3f(0.9,0.0,0.0); glVertex3f(-1.0,1.0,-1.0); glVertex3f(-1.0,1.0,1.0); glVertex3f(1.0,1.0,1.0); glVertex3f(1.0,1.0,-1.0); //bottom glColor3f(0.2,0.0,0.0); glVertex3f(-1.0,-1.0,-1.0); glVertex3f(-1.0,-1.0,1.0); glVertex3f(1.0,-1.0,1.0); glVertex3f(1.0,-1.0,-1.0); glEnd(); glutSwapBuffers(); } void reshape(int w, int h) { glViewport(0,0, (GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60, 1, 2.0, 50.0); glMatrixMode(GL_MODELVIEW); } void timer(int) { glutPostRedisplay(); glutTimerFunc(1000/60, timer, 0); } void keyboardCallback(unsigned char key, int x, int y) { lastKeyPressed = key; switch(key) { case 27: exit(0); break; case 'd': xang -= 0.5; break; case 'a': xang += 0.5; break; case 'w': zang += 0.5; break; case 's': zang -= 0.5; break; case 'c': direction += 1.0; break; case 'x': direction -= 1.0; break; case ' ': yang -= 1.0; break; case 'e': yang += 1.0; break; case 'r': zang = 0.0; yang = 0.0; xang = 0.0; direction = 0.0; break; } }