3dphotoshoot

view src/game.cc @ 24:2712c5da2e00

getting sensor input (hack)
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 16 Jun 2015 06:17:59 +0300
parents d7fe157c402d
children ac80210d5fbe
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include <assert.h>
5 #include "opengl.h"
6 #include "game.h"
7 #include "camera.h"
8 #include "sanegl.h"
9 #include "texture.h"
10 #include "shader.h"
11 #include "text.h"
12 #include "vmath/vmath.h"
14 static void draw_quad(float hsz, float vsz);
16 int win_width, win_height;
18 static float win_aspect;
19 static int video_width, video_height;
20 static float video_aspect;
21 static struct texture *test_tex;
23 static SdrProg *sdr_cam, *sdr_tex;
25 static Quaternion qrot;
26 static Vector3 trans;
28 extern "C" int game_init(void)
29 {
30 //glEnable(GL_DEPTH_TEST);
31 //glEnable(GL_CULL_FACE);
33 glClearColor(0.4, 0.4, 0.4, 1);
35 if(!(sdr_cam = get_sdrprog("sdr/vertex.glsl", "sdr/android_cam_preview.p.glsl"))) {
36 return -1;
37 }
38 if(!(sdr_tex = get_sdrprog("sdr/vertex.glsl", "sdr/tex.p.glsl"))) {
39 return -1;
40 }
42 if(!(test_tex = get_texture("data/opengl.png"))) {
43 return -1;
44 }
46 cam_start_video();
47 cam_video_size(&video_width, &video_height);
48 if(video_height) {
49 video_aspect = (float)video_width / (float)video_height;
50 } else {
51 video_aspect = 1.0;
52 }
54 printf("started video %dx%d (aspect: %g)\n", video_width, video_height, video_aspect);
55 return 0;
56 }
58 extern "C" void game_shutdown(void)
59 {
60 cam_shutdown();
61 delete sdr_cam;
62 delete sdr_tex;
63 }
65 extern "C" void game_display(unsigned long msec)
66 {
67 unsigned int tex;
68 const float *tex_matrix;
69 float xscale, yscale;
71 cam_update();
72 tex = cam_texture();
73 tex_matrix = cam_texture_matrix();
75 //float tsec = (float)msec / 1000.0f;
77 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
79 gl_matrix_mode(GL_MODELVIEW);
80 gl_load_identity();
81 gl_matrix_mode(GL_TEXTURE);
82 gl_load_matrixf(tex_matrix);
84 sdr_cam->bind();
85 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
87 if(video_aspect > win_aspect) {
88 xscale = 1.0;
89 yscale = 1.0 / video_aspect;
90 } else {
91 xscale = video_aspect;
92 yscale = 1.0;
93 }
94 draw_quad(xscale, yscale);
96 gl_matrix_mode(GL_TEXTURE);
97 gl_load_identity();
98 gl_translatef(0, 1, 0);
99 gl_scalef(1, -1, 1);
100 gl_matrix_mode(GL_MODELVIEW);
101 gl_load_identity();
102 gl_scalef((float)test_tex->width / (float)test_tex->height, 1, 1);
104 sdr_tex->bind();
105 glBindTexture(GL_TEXTURE_2D, test_tex->texid);
107 glEnable(GL_BLEND);
108 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
109 draw_quad(0.5, 0.5);
110 glDisable(GL_BLEND);
112 gl_matrix_mode(GL_TEXTURE);
113 gl_load_identity();
116 // print the rotation quaternion
117 text_color(1, 1, 1, 1);
118 text_position(0, 0);
119 text_printf("translation (% .3f, % .3f, % .3f)", trans.x, trans.y, trans.z);
120 text_position(0, 1);
121 text_printf("Rotation quat ([% 1.3f, % 1.3f, % 1.3f], % 1.3f)", qrot.v.x, qrot.v.y, qrot.v.z, qrot.s);
122 }
124 static void draw_quad(float hsz, float vsz)
125 {
126 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
127 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
129 gl_matrix_mode(GL_MODELVIEW);
130 gl_push_matrix();
131 gl_scalef(hsz, vsz, 1);
133 if(SdrProg::active) {
134 gl_apply_xform(SdrProg::active->get_globj());
135 }
137 glEnableVertexAttribArray(SDR_ATTR_VERTEX);
138 glEnableVertexAttribArray(SDR_ATTR_TEXCOORD);
139 glVertexAttribPointer(SDR_ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr);
140 glVertexAttribPointer(SDR_ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr);
142 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
144 glDisableVertexAttribArray(SDR_ATTR_VERTEX);
145 glDisableVertexAttribArray(SDR_ATTR_TEXCOORD);
147 gl_pop_matrix();
148 }
150 extern "C" void game_reshape(int x, int y)
151 {
152 win_width = x;
153 win_height = y;
154 win_aspect = y ? (float)x / (float)y : 1.0;
155 glViewport(0, 0, x, y);
157 gl_matrix_mode(GL_PROJECTION);
158 gl_load_identity();
159 gl_scalef((float)win_height / (float)win_width, 1, 1);
160 }
162 extern "C" void game_keyboard(int key, int pressed)
163 {
164 if(!pressed) return;
166 switch(key) {
167 case 27:
168 exit(0);
170 default:
171 break;
172 }
173 }
175 #define MAX_TOUCH_IDS 16
176 static struct {
177 int bnstate[8];
178 int prev_x, prev_y;
179 } mstate[MAX_TOUCH_IDS];
181 extern "C" void game_mouse_button(int id, int bn, int pressed, int x, int y)
182 {
183 if(id >= MAX_TOUCH_IDS) return;
185 mstate[id].prev_x = x;
186 mstate[id].prev_y = y;
187 mstate[id].bnstate[bn] = pressed;
188 }
190 extern "C" void game_mouse_motion(int id, int x, int y)
191 {
192 /*
193 int dx, dy, cx, cy;
195 if(id >= MAX_TOUCH_IDS) return;
197 cx = win_width / 2;
198 cy = win_height / 2;
200 dx = x - mstate[id].prev_x;
201 dy = y - mstate[id].prev_y;
202 mstate[id].prev_x = x;
203 mstate[id].prev_y = y;
205 if(!dx && !dy) return;
207 if(mouselook || mstate[id].bnstate[0]) {
208 player_turn(&player, dx * 0.5, dy * 0.5);
209 }
210 if(mstate[id].bnstate[2]) {
211 dbg_cam_dist += 0.1 * dy;
212 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
213 }
215 if(mouselook) {
216 warping_mouse = 1;
217 set_mouse_pos(cx, cy);
218 mstate[id].prev_x = cx;
219 mstate[id].prev_y = cy;
220 }
221 */
222 }
224 void game_6dof_translation(float dx, float dy, float dz)
225 {
226 trans.x = dx;
227 trans.y = dy;
228 trans.z = dz;
229 }
231 void game_6dof_rotation(float qx, float qy, float qz, float qw)
232 {
233 qrot.v.x = qx;
234 qrot.v.y = qy;
235 qrot.v.z = qz;
236 qrot.s = qw;
237 }