e79ae09266192d8a624d89431eb4da1bc8a290ff
[grGL.git] / src / main.c
1 // A simple OpenGL graph renderer.
2 // -- norly.
3
4 #include <stdlib.h>
5 #include <GL/glut.h>
6
7 #include "graph.h"
8 #include "glancillary.h"
9
10
11 enum MOUSE_MODES {
12   MOUSE_IDLE,
13   MOUSE_ROTATING,
14   MOUSE_TRANSLATING,
15   MOUSE_TRANSLATING2,
16   MOUSE_DRAGGING
17 };
18
19 enum MOUSE_MODES mouse_mode = MOUSE_IDLE;
20 int mouse_x, mouse_y;
21 GLfloat cameramat[16];
22
23 // The graph itself.
24 grGraph *g = NULL;
25
26
27
28
29 /****************************
30  * GLUT window event handlers
31  ***************************/
32
33 void on_display(void)
34 {
35   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
36
37   // Set up the camera
38   glLoadMatrixf(cameramat);
39
40   // Draw the old example objects
41   glaDrawExample();
42
43   // Draw the graph
44   graphDrawGL(g);
45
46   glFlush();
47   glutSwapBuffers();
48 }
49
50 void on_reshape(int w, int h)
51 {
52   // Set up the Viewport transformation
53   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
54
55   // Set up the Projection transformation
56   glMatrixMode(GL_PROJECTION);
57   glLoadIdentity();
58   gluPerspective(90, (GLfloat)w/(GLfloat)h, 0.0001, 1000.0);
59
60   // Switch to Model/view transformation for drawing objects
61   glMatrixMode(GL_MODELVIEW);
62 }
63
64
65
66
67 /****************************
68  * GLUT input event handlers
69  ***************************/
70
71 void on_keyboard(unsigned char key, int x, int y)
72 {
73   switch (key)
74   {
75     case 27:
76     case 'Q':
77     case 'q':
78       exit(0);
79       break;
80     case 'a':
81       glaCameraTranslatef(cameramat, 1.0, 0.0, 0.0);
82       break;
83     case 'd':
84       glaCameraTranslatef(cameramat, -1.0, 0.0, 0.0);
85       break;
86     case 'w':
87       glaCameraTranslatef(cameramat, 0.0, 0.0, 1.0);
88       break;
89     case 's':
90       glaCameraTranslatef(cameramat, 0.0, 0.0, -1.0);
91       break;
92     case '=':
93       //glScalef(1.5, 1.5, 1.5);
94       break;
95     case '-':
96       //glScalef(1/1.5, 1/1.5, 1/1.5);
97       break;
98   }
99   glutPostRedisplay();
100 }
101
102 void on_specialKeyboard(int key, int x, int y)
103 {
104   switch (key)
105   {
106     case GLUT_KEY_LEFT:
107       glaCameraRotatef(cameramat, -15.0, 0.0, 1.0, 0.0);
108       break;
109     case GLUT_KEY_UP:
110       glaCameraTranslatef(cameramat, 0.0, -1.0, 0.0);
111       break;
112     case GLUT_KEY_RIGHT:
113       glaCameraRotatef(cameramat, 15.0, 0.0, 1.0, 0.0);
114       break;
115     case GLUT_KEY_DOWN:
116       glaCameraTranslatef(cameramat, 0.0, 1.0, 0.0);
117       break;
118   }
119   glutPostRedisplay();
120 }
121
122
123
124 void on_mousebutton(int button, int state, int x, int y)
125 {
126   if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
127   {
128     if (mouse_mode != MOUSE_IDLE)
129       return;
130
131     switch(glutGetModifiers())
132     {
133       case 0:   // When no modifiers are pressed
134         //mouse_mode = MOUSE_DRAGGING;
135         //glaSelect(x, y);
136         //break;
137       case GLUT_ACTIVE_SHIFT:
138         mouse_mode = MOUSE_ROTATING;
139         break;
140       case GLUT_ACTIVE_SHIFT | GLUT_ACTIVE_CTRL:
141         mouse_mode = MOUSE_TRANSLATING;
142         break;
143       case GLUT_ACTIVE_CTRL:
144         mouse_mode = MOUSE_TRANSLATING2;
145         break;
146     }
147
148     mouse_x = x;
149     mouse_y = y;
150   }
151   else if (state == GLUT_UP)
152     mouse_mode = MOUSE_IDLE;
153 }
154
155 void on_mousemove(int x, int y)
156 {
157   int dx, dy;
158
159   if (mouse_mode != MOUSE_IDLE)
160   {
161     dx = x - mouse_x;
162     dy = y - mouse_y;
163     mouse_x = x;
164     mouse_y = y;
165
166     glPushMatrix();
167       glLoadIdentity();
168       switch(mouse_mode)
169       {
170         case MOUSE_TRANSLATING:
171           glTranslatef((0.1) * dx, (-0.1) * dy, 0.0);
172           break;
173         case MOUSE_TRANSLATING2:
174           glRotatef(dx, 0.0, 1.0, 0.0);
175           glTranslatef(0.0, 0.0, (-0.1) * dy);
176           break;
177         case MOUSE_ROTATING:
178           glRotatef(dx, 0.0, 1.0, 0.0);
179           glRotatef(dy, 1.0, 0.0, 0.0);
180           break;
181         case MOUSE_DRAGGING:
182         case MOUSE_IDLE:
183           break;
184       }
185       glMultMatrixf(cameramat);
186       glGetFloatv(GL_MODELVIEW_MATRIX, cameramat);
187     glPopMatrix();
188   }
189   glutPostRedisplay();
190 }
191
192
193
194
195 /****************************
196  * Main function
197  ***************************/
198
199 int main(int argc, char** argv)
200 {
201   glutInit(&argc, argv);
202   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
203   glutInitWindowSize (500, 500);
204   glutInitWindowPosition (100, 100);
205   glutCreateWindow (argv[0]);
206
207   // Init graph and GL
208   g = grAlloc(1000, 6000);
209   srand(0);
210   grGenRandom(g);
211   glaInit ();
212
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);
217   glLoadIdentity();
218
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);
226
227   glutMainLoop();
228
229   grFree(g);
230   return 0;
231 }