#include int resx, resy; int omx,omy = -1; bool lmb, rmb, mmb = false; float xRot, yRot = 0; void initMain(void) { resx = 640; resy = 480; } void resize(int w, int h) { resx = w; resy = h; glViewport(0, 0, resx, resy); } void drawScene(void) { glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, resx/resy, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glTranslatef(0,0,-3); glRotatef(xRot,0,1,0); glRotatef(-yRot,1,0,0); glBegin(GL_TRIANGLES); glColor4f(1,0,0,1); glVertex3f(0,0.7,0); glColor4f(0,1,0,1); glVertex3f(-0.7,-0.7,0.7); glColor4f(0,0,1,1); glVertex3f(0.7,-0.7,0.7); glColor4f(1,0,0,1); glVertex3f(0, 0.7, 0); glColor4f(0,1,0,1); glVertex3f(-0.7, -0.7, -0.7); glColor4f(0,0,1,1); glVertex3f(0.7, -0.7, -0.7); glColor4f(1,0,0,1); glVertex3f(0, 0.7, 0); glColor4f(0,1,0,1); glVertex3f(0.7, -0.7, -0.7); glColor4f(0,0,1,1); glVertex3f(0.7, -0.7, 0.7); glColor4f(1,0,0,1); glVertex3f(0,0.7,0); glColor4f(0,1,0,1); glVertex3f(-0.7, -0.7, -0.7); glColor4f(0,0,1,1); glVertex3f(-0.7, -0.7, 0.7); glColor4f(1,0,0,1); glVertex3f(0.7,-0.7,0.7); glColor4f(0,1,0,1); glVertex3f(-0.7,-0.7, -0.7); glColor4f(0,0,1,1); glVertex3f(-0.7,-0.7,0.7); glColor4f(1,0,0,1); glVertex3f(0.7,-0.7,0.7); glColor4f(0,1,0,1); glVertex3f(-0.7,-0.7,-0.7); glColor4f(0,0,1,1); glVertex3f(0.7,-0.7,-0.7); glEnd(); glutSwapBuffers(); } void mouseButtons(int button, int state, int x, int y) { switch(button) { case GLUT_LEFT_BUTTON: if(state == GLUT_DOWN) lmb = true; else if(state == GLUT_UP) lmb = false; break; case GLUT_MIDDLE_BUTTON: if(state == GLUT_DOWN) mmb = true; else if(state == GLUT_UP) mmb = false; break; case GLUT_RIGHT_BUTTON: if(state == GLUT_DOWN) rmb = true; else if(state == GLUT_UP) rmb = false; break; } } void mouseMove(int x, int y) { if(omx == -1) omx = x; if(omy == -1) omy = y; if(omx != x) { xRot += omx - x; omx = x; } if(omy != y) { yRot += omy - y; omy = y; } } int main(int argc, char** argv) { initMain(); glutInit(&argc, argv); glutInitWindowSize(resx, resy); glutInitWindowPosition(0,0); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH); glutCreateWindow("glut"); glutReshapeFunc(resize); glutDisplayFunc(drawScene); glutIdleFunc(drawScene); glutMouseFunc(mouseButtons); glutMotionFunc(mouseMove); glutMainLoop(); return 0; }