1 // A simple OpenGL graph renderer.
8 #include "glancillary.h"
19 enum MOUSE_MODES mouse_mode = MOUSE_IDLE;
21 GLfloat cameramat[16];
29 /****************************
30 * GLUT window event handlers
31 ***************************/
35 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
38 glLoadMatrixf(cameramat);
40 // Draw the old example objects
50 void on_reshape(int w, int h)
52 // Set up the Viewport transformation
53 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
55 // Set up the Projection transformation
56 glMatrixMode(GL_PROJECTION);
58 gluPerspective(90, (GLfloat)w/(GLfloat)h, 0.0001, 1000.0);
60 // Switch to Model/view transformation for drawing objects
61 glMatrixMode(GL_MODELVIEW);
67 /****************************
68 * GLUT input event handlers
69 ***************************/
71 void on_keyboard(unsigned char key, int x, int y)
81 glaCameraTranslatef(cameramat, 1.0, 0.0, 0.0);
84 glaCameraTranslatef(cameramat, -1.0, 0.0, 0.0);
87 glaCameraTranslatef(cameramat, 0.0, 0.0, 1.0);
90 glaCameraTranslatef(cameramat, 0.0, 0.0, -1.0);
93 //glScalef(1.5, 1.5, 1.5);
96 //glScalef(1/1.5, 1/1.5, 1/1.5);
102 void on_specialKeyboard(int key, int x, int y)
107 glaCameraRotatef(cameramat, -15.0, 0.0, 1.0, 0.0);
110 glaCameraTranslatef(cameramat, 0.0, -1.0, 0.0);
113 glaCameraRotatef(cameramat, 15.0, 0.0, 1.0, 0.0);
116 glaCameraTranslatef(cameramat, 0.0, 1.0, 0.0);
124 void on_mousebutton(int button, int state, int x, int y)
126 if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
128 if (mouse_mode != MOUSE_IDLE)
131 switch(glutGetModifiers())
133 case 0: // When no modifiers are pressed
134 //mouse_mode = MOUSE_DRAGGING;
137 case GLUT_ACTIVE_SHIFT:
138 mouse_mode = MOUSE_ROTATING;
140 case GLUT_ACTIVE_SHIFT | GLUT_ACTIVE_CTRL:
141 mouse_mode = MOUSE_TRANSLATING;
143 case GLUT_ACTIVE_CTRL:
144 mouse_mode = MOUSE_TRANSLATING2;
151 else if (state == GLUT_UP)
152 mouse_mode = MOUSE_IDLE;
155 void on_mousemove(int x, int y)
159 if (mouse_mode != MOUSE_IDLE)
170 case MOUSE_TRANSLATING:
171 glTranslatef((0.1) * dx, (-0.1) * dy, 0.0);
173 case MOUSE_TRANSLATING2:
174 glRotatef(dx, 0.0, 1.0, 0.0);
175 glTranslatef(0.0, 0.0, (-0.1) * dy);
178 glRotatef(dx, 0.0, 1.0, 0.0);
179 glRotatef(dy, 1.0, 0.0, 0.0);
185 glMultMatrixf(cameramat);
186 glGetFloatv(GL_MODELVIEW_MATRIX, cameramat);
195 /****************************
197 ***************************/
199 int main(int argc, char** argv)
201 glutInit(&argc, argv);
202 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
203 glutInitWindowSize (500, 500);
204 glutInitWindowPosition (100, 100);
205 glutCreateWindow (argv[0]);
208 g = grAlloc(1000, 6000);
213 // Init camera matrix
214 glMatrixMode(GL_MODELVIEW);
215 gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
216 glGetFloatv(GL_MODELVIEW_MATRIX, cameramat);
219 // Register callbacks
220 glutDisplayFunc(on_display);
221 glutReshapeFunc(on_reshape);
222 glutKeyboardFunc(on_keyboard);
223 glutSpecialFunc(on_specialKeyboard);
224 glutMouseFunc(on_mousebutton);
225 glutMotionFunc(on_mousemove);