3dphotoshoot

view src/game.cc @ 26:a460b1e5af4a

added GLUT frontend
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Jun 2015 03:55:05 +0300
parents ac80210d5fbe
children 3d082c566b53
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
5 #include <algorithm>
6 #include "opengl.h"
7 #include "game.h"
8 #include "camera.h"
9 #include "sanegl.h"
10 #include "texture.h"
11 #include "shader.h"
12 #include "text.h"
13 #include "vmath/vmath.h"
14 #include "mesh.h"
15 #include "meshgen.h"
17 static void draw_quad(float hsz, float vsz);
18 static void draw_rotation_gizmo();
19 static void draw_disc_wedge(float start, float end, int subdiv);
21 int win_width, win_height;
23 static float win_aspect;
24 static int video_width, video_height;
25 static float video_aspect;
26 static struct texture *test_tex;
28 static SdrProg *sdr_tex, *sdr_color, *sdr_debug;
30 static Quaternion last_rot;
31 static Vector3 last_trans;
33 static Quaternion rot;
34 static Vector3 rot_euler;
36 static Mesh *mesh;
38 extern "C" int game_init(void)
39 {
40 //glEnable(GL_DEPTH_TEST);
41 //glEnable(GL_CULL_FACE);
43 glClearColor(0.4, 0.4, 0.4, 1);
45 if(!(sdr_tex = get_sdrprog("sdr/vertex.glsl", "sdr/tex.p.glsl"))) {
46 return -1;
47 }
48 if(!(sdr_color = get_sdrprog("sdr/vertex.glsl", "sdr/color.p.glsl"))) {
49 return -1;
50 }
51 if(!(sdr_debug = get_sdrprog("sdr/vertex.glsl", "sdr/normvis.p.glsl"))) {
52 return -1;
53 }
55 if(!(test_tex = get_texture("data/opengl.png"))) {
56 return -1;
57 }
59 mesh = new Mesh;
60 gen_cylinder(mesh, 0.2, 1.0, 16, 1);
62 cam_start_video();
63 cam_video_size(&video_width, &video_height);
64 if(video_height) {
65 video_aspect = (float)video_width / (float)video_height;
66 } else {
67 video_aspect = 1.0;
68 }
70 printf("started video %dx%d (aspect: %g)\n", video_width, video_height, video_aspect);
71 return 0;
72 }
74 extern "C" void game_shutdown(void)
75 {
76 cam_shutdown();
77 delete sdr_tex;
78 }
80 extern "C" void game_display(unsigned long msec)
81 {
82 unsigned int tex;
83 float xscale, yscale;
85 cam_update();
86 tex = cam_texture();
88 //float tsec = (float)msec / 1000.0f;
90 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92 // draw video preview frame
93 gl_matrix_mode(GL_PROJECTION);
94 gl_push_matrix();
95 gl_load_identity();
96 gl_scalef((float)win_height / (float)win_width, 1, 1);
97 gl_matrix_mode(GL_MODELVIEW);
98 gl_load_identity();
100 if(video_aspect > win_aspect) {
101 xscale = 1.0;
102 yscale = 1.0 / video_aspect;
103 } else {
104 xscale = video_aspect;
105 yscale = 1.0;
106 }
107 gl_scalef(xscale, yscale, 1);
109 cam_draw_preview();
111 gl_matrix_mode(GL_PROJECTION);
112 gl_pop_matrix();
113 // done drawing preview
116 gl_matrix_mode(GL_MODELVIEW);
117 gl_load_identity();
118 gl_translatef(0, 0, -6);
120 draw_rotation_gizmo();
122 // print the rotation quaternion
123 text_color(1, 1, 1, 1);
124 text_position(0, 0);
125 text_printf("translation (% .3f, % .3f, % .3f)", last_trans.x, last_trans.y, last_trans.z);
126 text_position(0, 1);
127 text_printf("Rotation quat ([% 1.3f, % 1.3f, % 1.3f], % 1.3f)", last_rot.v.x, last_rot.v.y, last_rot.v.z, last_rot.s);
128 }
130 static void draw_quad(float hsz, float vsz)
131 {
132 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
133 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
135 gl_matrix_mode(GL_MODELVIEW);
136 gl_push_matrix();
137 gl_scalef(hsz, vsz, 1);
139 if(SdrProg::active) {
140 gl_apply_xform(SdrProg::active->get_globj());
141 }
143 glEnableVertexAttribArray(SDR_ATTR_VERTEX);
144 glEnableVertexAttribArray(SDR_ATTR_TEXCOORD);
145 glVertexAttribPointer(SDR_ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr);
146 glVertexAttribPointer(SDR_ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr);
148 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
150 glDisableVertexAttribArray(SDR_ATTR_VERTEX);
151 glDisableVertexAttribArray(SDR_ATTR_TEXCOORD);
153 gl_pop_matrix();
154 }
156 static void draw_rotation_gizmo()
157 {
158 /*static const float axis[][3] = {
159 {1, 0, 0}, {0, 1, 0}, {0, 0, 1}
160 };*/
162 last_rot.normalize();
163 Matrix4x4 rmat = last_rot.get_rotation_matrix().transposed();
164 gl_matrix_mode(GL_MODELVIEW);
165 gl_push_matrix();
166 gl_mult_matrixf(rmat[0]);
168 sdr_debug->bind();
169 gl_apply_xform(sdr_debug->get_globj());
171 mesh->draw();
173 gl_pop_matrix();
174 }
176 static void draw_disc_wedge(float start, float end, int subdiv)
177 {
178 if(start > end) {
179 float tmp = start;
180 start = end;
181 end = tmp;
182 }
184 float arc_size = end - start;
185 subdiv = std::max<int>(subdiv * arc_size, 1);
186 int nverts = subdiv + 2;
188 float *varr = (float*)alloca(nverts * 3 * sizeof *varr);
189 float *vptr = varr;
191 // start with the center vertex
192 vptr[0] = vptr[1] = vptr[2] = 0;
193 vptr += 3;
195 // then add the arc vertices in sequence
196 float u = start;
197 float du = arc_size / (float)subdiv;
198 for(int i=0; i<subdiv + 1; i++) {
199 float angle = u * M_PI * 2.0;
200 vptr[0] = sin(angle);
201 vptr[1] = cos(angle);
202 vptr[2] = 0.0;
203 vptr += 3;
204 u += du;
205 }
207 glEnableVertexAttribArray(SDR_ATTR_VERTEX);
208 glVertexAttribPointer(SDR_ATTR_VERTEX, 3, GL_FLOAT, 0, 0, varr);
210 glDrawArrays(GL_TRIANGLE_FAN, 0, nverts);
212 glDisableVertexAttribArray(SDR_ATTR_VERTEX);
213 }
215 extern "C" void game_reshape(int x, int y)
216 {
217 win_width = x;
218 win_height = y;
219 win_aspect = y ? (float)x / (float)y : 1.0;
220 glViewport(0, 0, x, y);
222 gl_matrix_mode(GL_PROJECTION);
223 glu_perspective(50.0, win_aspect, 0.5, 500.0);
224 }
226 extern "C" void game_keyboard(int key, int pressed)
227 {
228 if(!pressed) return;
230 switch(key) {
231 case 27:
232 exit(0);
234 default:
235 break;
236 }
237 }
239 #define MAX_TOUCH_IDS 16
240 static struct {
241 int bnstate[8];
242 int prev_x, prev_y;
243 } mstate[MAX_TOUCH_IDS];
245 extern "C" void game_mouse_button(int id, int bn, int pressed, int x, int y)
246 {
247 if(id >= MAX_TOUCH_IDS) return;
249 mstate[id].prev_x = x;
250 mstate[id].prev_y = y;
251 mstate[id].bnstate[bn] = pressed;
252 }
254 extern "C" void game_mouse_motion(int id, int x, int y)
255 {
256 /*
257 int dx, dy, cx, cy;
259 if(id >= MAX_TOUCH_IDS) return;
261 cx = win_width / 2;
262 cy = win_height / 2;
264 dx = x - mstate[id].prev_x;
265 dy = y - mstate[id].prev_y;
266 mstate[id].prev_x = x;
267 mstate[id].prev_y = y;
269 if(!dx && !dy) return;
271 if(mouselook || mstate[id].bnstate[0]) {
272 player_turn(&player, dx * 0.5, dy * 0.5);
273 }
274 if(mstate[id].bnstate[2]) {
275 dbg_cam_dist += 0.1 * dy;
276 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
277 }
279 if(mouselook) {
280 warping_mouse = 1;
281 set_mouse_pos(cx, cy);
282 mstate[id].prev_x = cx;
283 mstate[id].prev_y = cy;
284 }
285 */
286 }
288 void game_6dof_translation(float dx, float dy, float dz)
289 {
290 last_trans.x = dx;
291 last_trans.y = dy;
292 last_trans.z = dz;
293 }
295 void game_6dof_rotation(float qx, float qy, float qz, float qw)
296 {
297 last_rot.v.x = qx;
298 last_rot.v.y = qy;
299 last_rot.v.z = qz;
300 last_rot.s = qw;
302 rot.rotate(last_rot);
303 }