3dphotoshoot

view src/game.cc @ 21:4ca4e3c5a754

port to C++ completed, shader programs now use the SdrProg class
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 11 Jun 2015 04:56:33 +0300
parents c14613d27a3a
children d7fe157c402d
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"
12 static void draw_quad(float hsz, float vsz);
14 static int win_width, win_height;
15 static float win_aspect;
16 static int video_width, video_height;
17 static float video_aspect;
18 static struct texture *test_tex;
20 static SdrProg *sdr_cam, *sdr_tex;
22 extern "C" int game_init(void)
23 {
24 unsigned int vsdr, psdr_cam, psdr_tex;
26 //glEnable(GL_DEPTH_TEST);
27 glEnable(GL_CULL_FACE);
29 glClearColor(0.4, 0.4, 0.4, 1);
31 if(!(sdr_cam = get_sdrprog("sdr/vertex.glsl", "sdr/android_cam_preview.p.glsl"))) {
32 return -1;
33 }
34 if(!(sdr_tex = get_sdrprog("sdr/vertex.glsl", "sdr/tex.p.glsl"))) {
35 return -1;
36 }
38 if(!(test_tex = get_texture("data/opengl.png"))) {
39 return -1;
40 }
42 cam_start_video();
43 cam_video_size(&video_width, &video_height);
44 if(video_height) {
45 video_aspect = (float)video_width / (float)video_height;
46 } else {
47 video_aspect = 1.0;
48 }
50 printf("started video %dx%d (aspect: %g)\n", video_width, video_height, video_aspect);
51 return 0;
52 }
54 extern "C" void game_shutdown(void)
55 {
56 cam_shutdown();
57 delete sdr_cam;
58 delete sdr_tex;
59 }
61 extern "C" void game_display(unsigned long msec)
62 {
63 unsigned int tex;
64 const float *tex_matrix;
65 float xscale, yscale;
67 cam_update();
68 tex = cam_texture();
69 tex_matrix = cam_texture_matrix();
71 //float tsec = (float)msec / 1000.0f;
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
75 gl_matrix_mode(GL_MODELVIEW);
76 gl_load_identity();
77 gl_matrix_mode(GL_TEXTURE);
78 gl_load_matrixf(tex_matrix);
80 sdr_cam->bind();
81 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
83 if(video_aspect > win_aspect) {
84 xscale = 1.0;
85 yscale = 1.0 / video_aspect;
86 } else {
87 xscale = video_aspect;
88 yscale = 1.0;
89 }
90 draw_quad(xscale, yscale);
92 gl_matrix_mode(GL_TEXTURE);
93 gl_load_identity();
94 gl_translatef(0, 1, 0);
95 gl_scalef(1, -1, 1);
96 gl_matrix_mode(GL_MODELVIEW);
97 gl_load_identity();
98 gl_scalef((float)test_tex->width / (float)test_tex->height, 1, 1);
100 sdr_tex->bind();
101 glBindTexture(GL_TEXTURE_2D, test_tex->texid);
103 glEnable(GL_BLEND);
104 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
106 draw_quad(0.5, 0.5);
108 glDisable(GL_BLEND);
109 }
111 static void draw_quad(float hsz, float vsz)
112 {
113 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
114 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
116 gl_matrix_mode(GL_MODELVIEW);
117 gl_push_matrix();
118 gl_scalef(hsz, vsz, 1);
120 if(SdrProg::active) {
121 gl_apply_xform(SdrProg::active->get_globj());
122 }
124 glEnableVertexAttribArray(SDR_ATTR_VERTEX);
125 glEnableVertexAttribArray(SDR_ATTR_TEXCOORD);
126 glVertexAttribPointer(SDR_ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr);
127 glVertexAttribPointer(SDR_ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr);
129 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
131 glDisableVertexAttribArray(SDR_ATTR_VERTEX);
132 glDisableVertexAttribArray(SDR_ATTR_TEXCOORD);
134 gl_pop_matrix();
135 }
137 extern "C" void game_reshape(int x, int y)
138 {
139 win_width = x;
140 win_height = y;
141 win_aspect = y ? (float)x / (float)y : 1.0;
142 glViewport(0, 0, x, y);
144 gl_matrix_mode(GL_PROJECTION);
145 gl_load_identity();
146 gl_scalef((float)win_height / (float)win_width, 1, 1);
147 }
149 extern "C" void game_keyboard(int key, int pressed)
150 {
151 if(!pressed) return;
153 switch(key) {
154 case 27:
155 exit(0);
157 default:
158 break;
159 }
160 }
162 #define MAX_TOUCH_IDS 16
163 static struct {
164 int bnstate[8];
165 int prev_x, prev_y;
166 } mstate[MAX_TOUCH_IDS];
168 extern "C" void game_mouse_button(int id, int bn, int pressed, int x, int y)
169 {
170 if(id >= MAX_TOUCH_IDS) return;
172 mstate[id].prev_x = x;
173 mstate[id].prev_y = y;
174 mstate[id].bnstate[bn] = pressed;
175 }
177 extern "C" void game_mouse_motion(int id, int x, int y)
178 {
179 /*
180 int dx, dy, cx, cy;
182 if(id >= MAX_TOUCH_IDS) return;
184 cx = win_width / 2;
185 cy = win_height / 2;
187 dx = x - mstate[id].prev_x;
188 dy = y - mstate[id].prev_y;
189 mstate[id].prev_x = x;
190 mstate[id].prev_y = y;
192 if(!dx && !dy) return;
194 if(mouselook || mstate[id].bnstate[0]) {
195 player_turn(&player, dx * 0.5, dy * 0.5);
196 }
197 if(mstate[id].bnstate[2]) {
198 dbg_cam_dist += 0.1 * dy;
199 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
200 }
202 if(mouselook) {
203 warping_mouse = 1;
204 set_mouse_pos(cx, cy);
205 mstate[id].prev_x = cx;
206 mstate[id].prev_y = cy;
207 }
208 */
209 }