3dphotoshoot

view src/game.cc @ 27:3d082c566b53

fixed all the bugs, pc version works
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Jun 2015 04:32:25 +0300
parents a460b1e5af4a
children
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 glDisable(GL_DEPTH_TEST);
94 // draw video preview frame
95 gl_matrix_mode(GL_PROJECTION);
96 gl_push_matrix();
97 gl_load_identity();
98 gl_scalef((float)win_height / (float)win_width, 1, 1);
99 gl_matrix_mode(GL_MODELVIEW);
100 gl_load_identity();
102 if(video_aspect > win_aspect) {
103 xscale = 1.0;
104 yscale = 1.0 / video_aspect;
105 } else {
106 xscale = video_aspect;
107 yscale = 1.0;
108 }
109 gl_scalef(xscale, yscale, 1);
111 cam_draw_preview();
113 gl_matrix_mode(GL_PROJECTION);
114 gl_pop_matrix();
115 // done drawing preview
117 glEnable(GL_DEPTH_TEST);
119 gl_matrix_mode(GL_MODELVIEW);
120 gl_load_identity();
121 gl_translatef(0, 0, -6);
123 draw_rotation_gizmo();
125 // print the rotation quaternion
126 text_color(1, 1, 1, 1);
127 text_position(0, 0);
128 text_printf("translation (% .3f, % .3f, % .3f)", last_trans.x, last_trans.y, last_trans.z);
129 text_position(0, 1);
130 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);
131 }
133 static void draw_quad(float hsz, float vsz)
134 {
135 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
136 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
138 gl_matrix_mode(GL_MODELVIEW);
139 gl_push_matrix();
140 gl_scalef(hsz, vsz, 1);
142 if(SdrProg::active) {
143 gl_apply_xform(SdrProg::active->get_globj());
144 }
146 glEnableVertexAttribArray(SDR_ATTR_VERTEX);
147 glEnableVertexAttribArray(SDR_ATTR_TEXCOORD);
148 glVertexAttribPointer(SDR_ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr);
149 glVertexAttribPointer(SDR_ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr);
151 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
153 glDisableVertexAttribArray(SDR_ATTR_VERTEX);
154 glDisableVertexAttribArray(SDR_ATTR_TEXCOORD);
156 gl_pop_matrix();
157 }
159 static void draw_rotation_gizmo()
160 {
161 /*static const float axis[][3] = {
162 {1, 0, 0}, {0, 1, 0}, {0, 0, 1}
163 };*/
165 last_rot.normalize();
166 Matrix4x4 rmat = last_rot.get_rotation_matrix().transposed();
167 gl_matrix_mode(GL_MODELVIEW);
168 gl_push_matrix();
169 gl_mult_matrixf(rmat[0]);
171 sdr_debug->bind();
172 gl_apply_xform(sdr_debug->get_globj());
174 mesh->draw();
176 gl_pop_matrix();
177 }
179 static void draw_disc_wedge(float start, float end, int subdiv)
180 {
181 if(start > end) {
182 float tmp = start;
183 start = end;
184 end = tmp;
185 }
187 float arc_size = end - start;
188 subdiv = std::max<int>(subdiv * arc_size, 1);
189 int nverts = subdiv + 2;
191 float *varr = (float*)alloca(nverts * 3 * sizeof *varr);
192 float *vptr = varr;
194 // start with the center vertex
195 vptr[0] = vptr[1] = vptr[2] = 0;
196 vptr += 3;
198 // then add the arc vertices in sequence
199 float u = start;
200 float du = arc_size / (float)subdiv;
201 for(int i=0; i<subdiv + 1; i++) {
202 float angle = u * M_PI * 2.0;
203 vptr[0] = sin(angle);
204 vptr[1] = cos(angle);
205 vptr[2] = 0.0;
206 vptr += 3;
207 u += du;
208 }
210 glEnableVertexAttribArray(SDR_ATTR_VERTEX);
211 glVertexAttribPointer(SDR_ATTR_VERTEX, 3, GL_FLOAT, 0, 0, varr);
213 glDrawArrays(GL_TRIANGLE_FAN, 0, nverts);
215 glDisableVertexAttribArray(SDR_ATTR_VERTEX);
216 }
218 extern "C" void game_reshape(int x, int y)
219 {
220 win_width = x;
221 win_height = y;
222 win_aspect = y ? (float)x / (float)y : 1.0;
223 glViewport(0, 0, x, y);
225 gl_matrix_mode(GL_PROJECTION);
226 glu_perspective(50.0, win_aspect, 0.5, 500.0);
227 }
229 extern "C" void game_keyboard(int key, int pressed)
230 {
231 if(!pressed) return;
233 switch(key) {
234 case 27:
235 exit(0);
237 default:
238 break;
239 }
240 }
242 #define MAX_TOUCH_IDS 16
243 static struct {
244 int bnstate[8];
245 int prev_x, prev_y;
246 } mstate[MAX_TOUCH_IDS];
248 extern "C" void game_mouse_button(int id, int bn, int pressed, int x, int y)
249 {
250 if(id >= MAX_TOUCH_IDS) return;
252 mstate[id].prev_x = x;
253 mstate[id].prev_y = y;
254 mstate[id].bnstate[bn] = pressed;
255 }
257 extern "C" void game_mouse_motion(int id, int x, int y)
258 {
259 /*
260 int dx, dy, cx, cy;
262 if(id >= MAX_TOUCH_IDS) return;
264 cx = win_width / 2;
265 cy = win_height / 2;
267 dx = x - mstate[id].prev_x;
268 dy = y - mstate[id].prev_y;
269 mstate[id].prev_x = x;
270 mstate[id].prev_y = y;
272 if(!dx && !dy) return;
274 if(mouselook || mstate[id].bnstate[0]) {
275 player_turn(&player, dx * 0.5, dy * 0.5);
276 }
277 if(mstate[id].bnstate[2]) {
278 dbg_cam_dist += 0.1 * dy;
279 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
280 }
282 if(mouselook) {
283 warping_mouse = 1;
284 set_mouse_pos(cx, cy);
285 mstate[id].prev_x = cx;
286 mstate[id].prev_y = cy;
287 }
288 */
289 }
291 void game_6dof_translation(float dx, float dy, float dz)
292 {
293 last_trans.x = dx;
294 last_trans.y = dy;
295 last_trans.z = dz;
296 }
298 void game_6dof_rotation(float qx, float qy, float qz, float qw)
299 {
300 last_rot.v.x = qx;
301 last_rot.v.y = qy;
302 last_rot.v.z = qz;
303 last_rot.s = qw;
305 rot.rotate(last_rot);
306 }