summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 7825a2dca9d796dbb15712fbd664bbc372a6f990 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// A simple OpenGL graph renderer.
// -- norly.

#include <stdlib.h>
#include <GL/glut.h>

#include "graph.h"
#include "glancillary.h"


enum MOUSE_MODES {
  MOUSE_IDLE,
  MOUSE_ROTATING,
  MOUSE_TRANSLATING,
  MOUSE_TRANSLATING2,
  MOUSE_DRAGGING
};

enum MOUSE_MODES mouse_mode = MOUSE_IDLE;
int mouse_x, mouse_y;
GLfloat cameramat[16];

// The graph itself.
grGraph *g = NULL;




/****************************
 * GLUT window event handlers
 ***************************/

void on_display(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // Set up the camera
  glLoadMatrixf(cameramat);

  // Draw the old example objects
  glaDrawExample();

  // Draw the graph
  graphDrawGL(g);

  glFlush();
  glutSwapBuffers();
}

void on_reshape(int w, int h)
{
  // Set up the Viewport transformation
  glViewport(0, 0, (GLsizei) w, (GLsizei) h);

  // Set up the Projection transformation
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(90, (GLfloat)w/(GLfloat)h, 0.0, 5.0);

  // Switch to Model/view transformation for drawing objects
  glMatrixMode(GL_MODELVIEW);
}




/****************************
 * GLUT input event handlers
 ***************************/

void on_keyboard(unsigned char key, int x, int y)
{
  switch (key)
  {
    case 27:
    case 'Q':
    case 'q':
      exit(0);
      break;
    case 'a':
      glaCameraTranslatef(cameramat, 1.0, 0.0, 0.0);
      break;
    case 'd':
      glaCameraTranslatef(cameramat, -1.0, 0.0, 0.0);
      break;
    case 'w':
      glaCameraTranslatef(cameramat, 0.0, 0.0, 1.0);
      break;
    case 's':
      glaCameraTranslatef(cameramat, 0.0, 0.0, -1.0);
      break;
    case '=':
      //glScalef(1.5, 1.5, 1.5);
      break;
    case '-':
      //glScalef(1/1.5, 1/1.5, 1/1.5);
      break;
  }
  glutPostRedisplay();
}

void on_specialKeyboard(int key, int x, int y)
{
  switch (key)
  {
    case GLUT_KEY_LEFT:
      glaCameraRotatef(cameramat, -15.0, 0.0, 1.0, 0.0);
      break;
    case GLUT_KEY_UP:
      glaCameraTranslatef(cameramat, 0.0, -1.0, 0.0);
      break;
    case GLUT_KEY_RIGHT:
      glaCameraRotatef(cameramat, 15.0, 0.0, 1.0, 0.0);
      break;
    case GLUT_KEY_DOWN:
      glaCameraTranslatef(cameramat, 0.0, 1.0, 0.0);
      break;
  }
  glutPostRedisplay();
}



void on_mousebutton(int button, int state, int x, int y)
{
  if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
  {
    if (mouse_mode != MOUSE_IDLE)
      return;

    switch(glutGetModifiers())
    {
      case 0:   // When no modifiers are pressed
        //mouse_mode = MOUSE_DRAGGING;
        //glaSelect(x, y);
        //break;
      case GLUT_ACTIVE_SHIFT:
        mouse_mode = MOUSE_ROTATING;
        break;
      case GLUT_ACTIVE_SHIFT | GLUT_ACTIVE_CTRL:
        mouse_mode = MOUSE_TRANSLATING;
        break;
      case GLUT_ACTIVE_CTRL:
        mouse_mode = MOUSE_TRANSLATING2;
        break;
    }

    mouse_x = x;
    mouse_y = y;
  }
  else if (state == GLUT_UP)
    mouse_mode = MOUSE_IDLE;
}

void on_mousemove(int x, int y)
{
  int dx, dy;

  if (mouse_mode != MOUSE_IDLE)
  {
    dx = x - mouse_x;
    dy = y - mouse_y;
    mouse_x = x;
    mouse_y = y;

    glPushMatrix();
      glLoadIdentity();
      switch(mouse_mode)
      {
        case MOUSE_TRANSLATING:
          glTranslatef((0.1) * dx, (-0.1) * dy, 0.0);
          break;
        case MOUSE_TRANSLATING2:
          glRotatef(dx, 0.0, 1.0, 0.0);
          glTranslatef(0.0, 0.0, (-0.1) * dy);
          break;
        case MOUSE_ROTATING:
          glRotatef(dx, 0.0, 1.0, 0.0);
          glRotatef(dy, 1.0, 0.0, 0.0);
          break;
        case MOUSE_DRAGGING:
        case MOUSE_IDLE:
          break;
      }
      glMultMatrixf(cameramat);
      glGetFloatv(GL_MODELVIEW_MATRIX, cameramat);
    glPopMatrix();
  }
  glutPostRedisplay();
}




/****************************
 * Main function
 ***************************/

int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
  glutInitWindowSize (500, 500);
  glutInitWindowPosition (100, 100);
  glutCreateWindow (argv[0]);

  // Init graph and GL
  g = grAlloc(1000, 6000);
  srand(0);
  grGenRandom(g);
  glaInit ();

  // Init camera matrix
  glMatrixMode(GL_MODELVIEW);
  gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  glGetFloatv(GL_MODELVIEW_MATRIX, cameramat);
  glLoadIdentity();

  // Register callbacks
  glutDisplayFunc(on_display);
  glutReshapeFunc(on_reshape);
  glutKeyboardFunc(on_keyboard);
  glutSpecialFunc(on_specialKeyboard);
  glutMouseFunc(on_mousebutton);
  glutMotionFunc(on_mousemove);

  glutMainLoop();

  grFree(g);
  return 0;
}