rev |
line source |
nuclear@16
|
1 #include <stdio.h>
|
nuclear@16
|
2 #include <stdlib.h>
|
nuclear@16
|
3 #include <algorithm>
|
nuclear@16
|
4 #include <goat3dgfx/goat3dgfx.h>
|
nuclear@16
|
5 #include <vmath/vmath.h>
|
nuclear@16
|
6
|
nuclear@16
|
7 using namespace goatgfx;
|
nuclear@16
|
8
|
nuclear@16
|
9 static bool init();
|
nuclear@16
|
10 static void cleanup();
|
nuclear@16
|
11 static void display();
|
nuclear@16
|
12 static void reshape(int x, int y);
|
nuclear@16
|
13 static void keyboard(unsigned char key, int x, int y);
|
nuclear@16
|
14 static void mouse(int bn, int st, int x, int y);
|
nuclear@16
|
15 static void motion(int x, int y);
|
nuclear@16
|
16 static bool parse_args(int argc, char **argv);
|
nuclear@16
|
17
|
nuclear@16
|
18 static float cam_theta, cam_phi;
|
nuclear@16
|
19 static const char *scene_filename;
|
nuclear@16
|
20 static Scene *scn;
|
nuclear@16
|
21
|
nuclear@16
|
22 int main(int argc, char **argv)
|
nuclear@16
|
23 {
|
nuclear@16
|
24 glutInit(&argc, argv);
|
nuclear@16
|
25 glutInitWindowSize(800, 600);
|
nuclear@16
|
26 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
|
nuclear@16
|
27 glutCreateWindow("viewscn");
|
nuclear@16
|
28
|
nuclear@16
|
29 glutDisplayFunc(display);
|
nuclear@16
|
30 glutReshapeFunc(reshape);
|
nuclear@16
|
31 glutKeyboardFunc(keyboard);
|
nuclear@16
|
32 glutMouseFunc(mouse);
|
nuclear@16
|
33 glutMotionFunc(motion);
|
nuclear@16
|
34
|
nuclear@16
|
35 if(!parse_args(argc, argv)) {
|
nuclear@16
|
36 return 1;
|
nuclear@16
|
37 }
|
nuclear@16
|
38
|
nuclear@16
|
39 if(!init()) {
|
nuclear@16
|
40 return 1;
|
nuclear@16
|
41 }
|
nuclear@16
|
42 atexit(cleanup);
|
nuclear@16
|
43
|
nuclear@16
|
44 glutMainLoop();
|
nuclear@16
|
45 return 0;
|
nuclear@16
|
46 }
|
nuclear@16
|
47
|
nuclear@16
|
48 static bool init()
|
nuclear@16
|
49 {
|
nuclear@16
|
50 glewInit();
|
nuclear@16
|
51
|
nuclear@16
|
52 glEnable(GL_DEPTH_TEST);
|
nuclear@16
|
53 glEnable(GL_CULL_FACE);
|
nuclear@16
|
54
|
nuclear@16
|
55 scn = new Scene;
|
nuclear@16
|
56 if(!scn->load(scene_filename)) {
|
nuclear@16
|
57 fatal_log("failed to load scene: %s\n", scene_filename);
|
nuclear@16
|
58 return false;
|
nuclear@16
|
59 }
|
nuclear@16
|
60
|
nuclear@16
|
61 return true;
|
nuclear@16
|
62 }
|
nuclear@16
|
63
|
nuclear@16
|
64 static void cleanup()
|
nuclear@16
|
65 {
|
nuclear@16
|
66 delete scn;
|
nuclear@16
|
67 }
|
nuclear@16
|
68
|
nuclear@16
|
69 static void display()
|
nuclear@16
|
70 {
|
nuclear@16
|
71 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@16
|
72
|
nuclear@16
|
73 Matrix4x4 view_matrix;
|
nuclear@16
|
74 view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0);
|
nuclear@16
|
75 view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0);
|
nuclear@16
|
76 set_view_matrix(view_matrix);
|
nuclear@16
|
77
|
nuclear@16
|
78 setup_gl_matrices();
|
nuclear@16
|
79
|
nuclear@16
|
80 scn->draw();
|
nuclear@16
|
81
|
nuclear@16
|
82 glutSwapBuffers();
|
nuclear@16
|
83 CHECKGLERR;
|
nuclear@16
|
84 }
|
nuclear@16
|
85
|
nuclear@16
|
86 static void reshape(int x, int y)
|
nuclear@16
|
87 {
|
nuclear@16
|
88 glViewport(0, 0, x, y);
|
nuclear@16
|
89
|
nuclear@16
|
90 Matrix4x4 proj;
|
nuclear@16
|
91 proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
|
nuclear@16
|
92 set_projection_matrix(proj);
|
nuclear@16
|
93 }
|
nuclear@16
|
94
|
nuclear@16
|
95 static void keyboard(unsigned char key, int x, int y)
|
nuclear@16
|
96 {
|
nuclear@16
|
97 switch(key) {
|
nuclear@16
|
98 case 27:
|
nuclear@16
|
99 exit(0);
|
nuclear@16
|
100 }
|
nuclear@16
|
101 }
|
nuclear@16
|
102
|
nuclear@16
|
103 static bool bnstate[16];
|
nuclear@16
|
104 static int prev_x, prev_y;
|
nuclear@16
|
105
|
nuclear@16
|
106 static void mouse(int bn, int st, int x, int y)
|
nuclear@16
|
107 {
|
nuclear@16
|
108 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
|
nuclear@16
|
109 prev_x = x;
|
nuclear@16
|
110 prev_y = y;
|
nuclear@16
|
111 }
|
nuclear@16
|
112
|
nuclear@16
|
113 static void motion(int x, int y)
|
nuclear@16
|
114 {
|
nuclear@16
|
115 int dx = x - prev_x;
|
nuclear@16
|
116 int dy = y - prev_y;
|
nuclear@16
|
117 prev_x = x;
|
nuclear@16
|
118 prev_y = y;
|
nuclear@16
|
119
|
nuclear@16
|
120 if(!dx && !dy) return;
|
nuclear@16
|
121
|
nuclear@16
|
122 if(bnstate[0]) {
|
nuclear@16
|
123 cam_theta += dx * 0.5;
|
nuclear@16
|
124 cam_phi += dy * 0.5;
|
nuclear@16
|
125 cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
|
nuclear@16
|
126 glutPostRedisplay();
|
nuclear@16
|
127 }
|
nuclear@16
|
128 }
|
nuclear@16
|
129
|
nuclear@16
|
130 static bool parse_args(int argc, char **argv)
|
nuclear@16
|
131 {
|
nuclear@16
|
132 for(int i=1; i<argc; i++) {
|
nuclear@16
|
133 if(argv[i][0] == '-' && argv[i][2] == 0) {
|
nuclear@16
|
134 switch(argv[i][1]) {
|
nuclear@16
|
135 case 'h':
|
nuclear@16
|
136 default:
|
nuclear@16
|
137 printf("usage: %s <filename>\n", argv[i]);
|
nuclear@16
|
138 exit(0);
|
nuclear@16
|
139 }
|
nuclear@16
|
140 } else {
|
nuclear@16
|
141 if(scene_filename) {
|
nuclear@16
|
142 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
|
nuclear@16
|
143 return false;
|
nuclear@16
|
144 }
|
nuclear@16
|
145 scene_filename = argv[i];
|
nuclear@16
|
146 }
|
nuclear@16
|
147 }
|
nuclear@16
|
148
|
nuclear@16
|
149 if(!scene_filename) {
|
nuclear@16
|
150 fprintf(stderr, "you must specify the scene file to view\n");
|
nuclear@16
|
151 return false;
|
nuclear@16
|
152 }
|
nuclear@16
|
153
|
nuclear@16
|
154 return true;
|
nuclear@16
|
155 }
|