rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@1
|
2 #include <stdlib.h>
|
nuclear@1
|
3 #include <assert.h>
|
nuclear@1
|
4 #include "opengl.h"
|
nuclear@1
|
5 #include "level.h"
|
nuclear@1
|
6 #include "camera.h"
|
nuclear@1
|
7
|
nuclear@1
|
8 void disp();
|
nuclear@1
|
9 void reshape(int x, int y);
|
nuclear@1
|
10 void keyb(unsigned char key, int x, int y);
|
nuclear@1
|
11 void mouse(int bn, int state, int x, int y);
|
nuclear@1
|
12 void motion(int x, int y);
|
nuclear@1
|
13
|
nuclear@1
|
14 static Level *level;
|
nuclear@1
|
15 static OrbitCamera cam;
|
nuclear@1
|
16
|
nuclear@1
|
17 int main(int argc, char **argv)
|
nuclear@1
|
18 {
|
nuclear@1
|
19 glutInit(&argc, argv);
|
nuclear@1
|
20 glutInitWindowSize(800, 600);
|
nuclear@1
|
21 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
|
nuclear@1
|
22 glutCreateWindow("prototype");
|
nuclear@1
|
23
|
nuclear@1
|
24 glutDisplayFunc(disp);
|
nuclear@1
|
25 glutReshapeFunc(reshape);
|
nuclear@1
|
26 glutKeyboardFunc(keyb);
|
nuclear@1
|
27 glutMouseFunc(mouse);
|
nuclear@1
|
28 glutMotionFunc(motion);
|
nuclear@1
|
29
|
nuclear@1
|
30 glewInit();
|
nuclear@1
|
31
|
nuclear@1
|
32 glEnable(GL_LIGHTING);
|
nuclear@1
|
33 glEnable(GL_LIGHT0);
|
nuclear@1
|
34 float ldir[] = {-1, 1, 2, 0};
|
nuclear@1
|
35 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
|
nuclear@1
|
36 glEnable(GL_NORMALIZE);
|
nuclear@1
|
37
|
nuclear@1
|
38 glEnable(GL_DEPTH_TEST);
|
nuclear@1
|
39 glEnable(GL_CULL_FACE);
|
nuclear@1
|
40 glEnable(GL_MULTISAMPLE);
|
nuclear@1
|
41
|
nuclear@1
|
42 level = new Level;
|
nuclear@1
|
43 if(!level->load("foobar")) {
|
nuclear@1
|
44 return 1;
|
nuclear@1
|
45 }
|
nuclear@1
|
46
|
nuclear@1
|
47 glutMainLoop();
|
nuclear@1
|
48 }
|
nuclear@1
|
49
|
nuclear@1
|
50 void disp()
|
nuclear@1
|
51 {
|
nuclear@1
|
52 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@1
|
53
|
nuclear@1
|
54 glMatrixMode(GL_MODELVIEW);
|
nuclear@1
|
55 glLoadIdentity();
|
nuclear@1
|
56 cam.use();
|
nuclear@1
|
57
|
nuclear@1
|
58 level->draw();
|
nuclear@1
|
59
|
nuclear@1
|
60 glutSwapBuffers();
|
nuclear@1
|
61 assert(glGetError() == GL_NO_ERROR);
|
nuclear@1
|
62 }
|
nuclear@1
|
63
|
nuclear@1
|
64 void reshape(int x, int y)
|
nuclear@1
|
65 {
|
nuclear@1
|
66 glViewport(0, 0, x, y);
|
nuclear@1
|
67 glMatrixMode(GL_PROJECTION);
|
nuclear@1
|
68 glLoadIdentity();
|
nuclear@1
|
69 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
|
nuclear@1
|
70 }
|
nuclear@1
|
71
|
nuclear@1
|
72 void keyb(unsigned char key, int x, int y)
|
nuclear@1
|
73 {
|
nuclear@1
|
74 switch(key) {
|
nuclear@1
|
75 case 27:
|
nuclear@1
|
76 exit(0);
|
nuclear@1
|
77 }
|
nuclear@1
|
78 }
|
nuclear@1
|
79
|
nuclear@1
|
80 static int prev_x, prev_y;
|
nuclear@1
|
81 static bool bnstate[32];
|
nuclear@1
|
82
|
nuclear@1
|
83 void mouse(int bn, int state, int x, int y)
|
nuclear@1
|
84 {
|
nuclear@1
|
85 prev_x = x;
|
nuclear@1
|
86 prev_y = y;
|
nuclear@1
|
87 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
|
nuclear@1
|
88 }
|
nuclear@1
|
89
|
nuclear@1
|
90 void motion(int x, int y)
|
nuclear@1
|
91 {
|
nuclear@1
|
92 int dx = x - prev_x;
|
nuclear@1
|
93 int dy = y - prev_y;
|
nuclear@1
|
94 prev_x = x;
|
nuclear@1
|
95 prev_y = y;
|
nuclear@1
|
96
|
nuclear@1
|
97 if(bnstate[0]) {
|
nuclear@1
|
98 cam.input_rotate(dx * 0.01, dy * 0.01, 0);
|
nuclear@1
|
99 glutPostRedisplay();
|
nuclear@1
|
100 }
|
nuclear@1
|
101 if(bnstate[2]) {
|
nuclear@1
|
102 cam.input_zoom(dy * 0.1);
|
nuclear@1
|
103 glutPostRedisplay();
|
nuclear@1
|
104 }
|
nuclear@1
|
105 }
|