1 // A simple OpenGL graph renderer.
8 #include <OpenGL/glu.h>
17 #include "glancillary.h"
28 enum MOUSE_MODES mouse_mode = MOUSE_IDLE;
30 GLfloat cameramat[16];
38 /****************************
39 * GLUT window event handlers
40 ***************************/
44 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
47 glLoadMatrixf(cameramat);
49 // Draw the old example objects
59 void on_reshape(int w, int h)
61 // Set up the Viewport transformation
62 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
64 // Set up the Projection transformation
65 glMatrixMode(GL_PROJECTION);
67 gluPerspective(90, (GLfloat)w/(GLfloat)h, 0.0001, 1000.0);
69 // Switch to Model/view transformation for drawing objects
70 glMatrixMode(GL_MODELVIEW);
76 /****************************
77 * GLUT input event handlers
78 ***************************/
80 void on_keyboard(unsigned char key, int x, int y)
90 glaCameraTranslatef(cameramat, 1.0, 0.0, 0.0);
93 glaCameraTranslatef(cameramat, -1.0, 0.0, 0.0);
96 glaCameraTranslatef(cameramat, 0.0, 0.0, 1.0);
99 glaCameraTranslatef(cameramat, 0.0, 0.0, -1.0);
102 //glScalef(1.5, 1.5, 1.5);
105 //glScalef(1/1.5, 1/1.5, 1/1.5);
111 void on_specialKeyboard(int key, int x, int y)
116 glaCameraRotatef(cameramat, -15.0, 0.0, 1.0, 0.0);
119 glaCameraTranslatef(cameramat, 0.0, -1.0, 0.0);
122 glaCameraRotatef(cameramat, 15.0, 0.0, 1.0, 0.0);
125 glaCameraTranslatef(cameramat, 0.0, 1.0, 0.0);
133 void on_mousebutton(int button, int state, int x, int y)
135 if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
137 if (mouse_mode != MOUSE_IDLE)
140 switch(glutGetModifiers())
142 case 0: // When no modifiers are pressed
143 //mouse_mode = MOUSE_DRAGGING;
146 case GLUT_ACTIVE_SHIFT:
147 mouse_mode = MOUSE_ROTATING;
149 case GLUT_ACTIVE_SHIFT | GLUT_ACTIVE_CTRL:
150 mouse_mode = MOUSE_TRANSLATING;
152 case GLUT_ACTIVE_CTRL:
153 mouse_mode = MOUSE_TRANSLATING2;
160 else if (state == GLUT_UP)
161 mouse_mode = MOUSE_IDLE;
164 void on_mousemove(int x, int y)
168 if (mouse_mode != MOUSE_IDLE)
179 case MOUSE_TRANSLATING:
180 glTranslatef((0.1) * dx, (-0.1) * dy, 0.0);
182 case MOUSE_TRANSLATING2:
183 glRotatef(dx, 0.0, 1.0, 0.0);
184 glTranslatef(0.0, 0.0, (-0.1) * dy);
187 glRotatef(dx, 0.0, 1.0, 0.0);
188 glRotatef(dy, 1.0, 0.0, 0.0);
194 glMultMatrixf(cameramat);
195 glGetFloatv(GL_MODELVIEW_MATRIX, cameramat);
204 /****************************
206 ***************************/
208 int main(int argc, char** argv)
210 glutInit(&argc, argv);
211 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
212 glutInitWindowSize (500, 500);
213 glutInitWindowPosition (100, 100);
214 glutCreateWindow (argv[0]);
217 g = grAlloc(1000, 6000);
222 // Init camera matrix
223 glMatrixMode(GL_MODELVIEW);
224 gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
225 glGetFloatv(GL_MODELVIEW_MATRIX, cameramat);
228 // Register callbacks
229 glutDisplayFunc(on_display);
230 glutReshapeFunc(on_reshape);
231 glutKeyboardFunc(on_keyboard);
232 glutSpecialFunc(on_specialKeyboard);
233 glutMouseFunc(on_mousebutton);
234 glutMotionFunc(on_mousemove);