symmetry
view src/main.cc @ 1:46fe847bba08
using goat3dgfx
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 25 Feb 2014 23:47:20 +0200 |
parents | a90a71a74f0b |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <vector>
5 #include <goat3dgfx/goat3dgfx.h>
7 using namespace goatgfx;
9 static bool init();
10 static void cleanup();
11 static void disp();
12 static void draw_scene(int which);
13 static void handle_input(float dt);
14 static void idle();
15 static void reshape(int x, int y);
16 static void keypress(unsigned char key, int x, int y);
17 static void keyrelease(unsigned char key, int x, int y);
18 static void mouse(int bn, int st, int x, int y);
19 static void motion(int x, int y);
21 static int win_width, win_height;
22 static float split_pos = 0.5;
23 static VRFpsCamera cam;
24 static std::vector<bool> keystate(256);
26 int main(int argc, char **argv)
27 {
28 glutInitWindowSize(1024, 768);
29 glutInit(&argc, argv);
30 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
31 glutCreateWindow("symmetry");
33 glutDisplayFunc(disp);
34 glutIdleFunc(idle);
35 glutReshapeFunc(reshape);
36 glutKeyboardFunc(keypress);
37 glutKeyboardUpFunc(keyrelease);
38 glutMouseFunc(mouse);
39 glutMotionFunc(motion);
41 if(!init()) {
42 return 1;
43 }
44 atexit(cleanup);
46 glutMainLoop();
47 return 0;
48 }
51 static bool init()
52 {
53 glewInit();
55 glEnable(GL_DEPTH_TEST);
56 glEnable(GL_CULL_FACE);
58 glEnable(GL_LIGHTING);
59 glEnable(GL_LIGHT0);
61 cam.input_move(0, 1.65, 0);
63 return true;
64 }
66 static void cleanup()
67 {
68 }
70 static void disp()
71 {
72 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
73 static unsigned int prev_msec;
74 float dt = (float)(msec - prev_msec) / 1000.0f;
75 prev_msec = msec;
77 handle_input(dt);
79 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
81 cam.use_inverse();
82 setup_gl_matrices();
84 float left_pixels = split_pos * win_width;
86 glEnable(GL_SCISSOR_TEST);
88 // draw left viewport
89 if(left_pixels > 0) {
90 glScissor(0, 0, left_pixels, win_height);
91 draw_scene(0);
92 }
94 // draw right viewport
95 if(left_pixels < win_width) {
96 glScissor(left_pixels, 0, win_width - left_pixels, win_height);
97 draw_scene(1);
98 }
100 glDisable(GL_SCISSOR_TEST);
102 glutSwapBuffers();
103 assert(glGetError() == GL_NO_ERROR);
104 }
106 static void draw_scene(int which)
107 {
108 glMatrixMode(GL_MODELVIEW);
110 float lpos[] = {-5, 20, 5, 1};
111 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
113 float color[][4] = {
114 {1.0, 0.3, 0.2, 1.0},
115 {0.2, 0.3, 1.0, 1.0}
116 };
118 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, color[which]);
120 glBegin(GL_QUADS);
121 glNormal3f(0, 1, 0);
122 glVertex3f(-20, 0, 20);
123 glVertex3f(20, 0, 20);
124 glVertex3f(20, 0, -20);
125 glVertex3f(-20, 0, -20);
126 glEnd();
128 for(int i=0; i<8; i++) {
129 float theta = 360.0 * (float)i / 8.0;
130 glPushMatrix();
131 glRotatef(theta, 0, 1, 0);
132 glTranslatef(0, 0, 10);
134 glTranslatef(0, 1, 0);
136 glFrontFace(GL_CW);
137 glutSolidTeapot(1.0);
138 glFrontFace(GL_CCW);
139 glPopMatrix();
140 }
141 }
143 static void handle_input(float dt)
144 {
145 Vector3 inpv;
146 float offs = dt * 2.0;
148 if(keystate['w'] || keystate['W']) {
149 inpv.z -= offs;
150 }
151 if(keystate['s'] || keystate['S']) {
152 inpv.z += offs;
153 }
154 if(keystate['d'] || keystate['D']) {
155 inpv.x += offs;
156 }
157 if(keystate['a'] || keystate['A']) {
158 inpv.x -= offs;
159 }
161 cam.input_move(inpv.x, inpv.y, inpv.z);
162 }
164 static void idle()
165 {
166 glutPostRedisplay();
167 }
169 static void reshape(int x, int y)
170 {
171 glViewport(0, 0, x, y);
173 Matrix4x4 proj;
174 proj.set_perspective(DEG_TO_RAD(50), (float)x / (float)y, 0.5, 500.0);
175 set_projection_matrix(proj);
177 win_width = x;
178 win_height = y;
179 }
181 static void keypress(unsigned char key, int x, int y)
182 {
183 keystate[key] = true;
185 switch(key) {
186 case 27:
187 exit(0);
189 case ' ':
190 split_pos = 0.5;
191 break;
192 }
193 }
195 static void keyrelease(unsigned char key, int x, int y)
196 {
197 keystate[key] = false;
198 }
200 static int prev_x, prev_y;
201 static bool bnstate[32];
203 static void mouse(int bn, int st, int x, int y)
204 {
205 prev_x = x;
206 prev_y = y;
207 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
208 }
210 static void motion(int x, int y)
211 {
212 int dx = x - prev_x;
213 int dy = y - prev_y;
214 prev_x = x;
215 prev_y = y;
217 if(!dx && !dy) {
218 return;
219 }
221 if(keystate[(int)'\b']) {
222 split_pos = (float)x / win_width;
223 return;
224 }
226 if(bnstate[0]) {
227 float dtheta_deg = dy * 0.5;
228 float dphi_deg = dx * 0.5;
230 cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0);
231 }
232 }