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