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