rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@0
|
3 #include <math.h>
|
nuclear@11
|
4 #include <assert.h>
|
nuclear@0
|
5 #include "opengl.h"
|
nuclear@0
|
6 #include "game.h"
|
nuclear@7
|
7 #include "camera.h"
|
nuclear@11
|
8 #include "sanegl.h"
|
nuclear@17
|
9 #include "texture.h"
|
nuclear@21
|
10 #include "shader.h"
|
nuclear@7
|
11
|
nuclear@21
|
12 static void draw_quad(float hsz, float vsz);
|
nuclear@0
|
13
|
nuclear@0
|
14 static int win_width, win_height;
|
nuclear@9
|
15 static float win_aspect;
|
nuclear@9
|
16 static int video_width, video_height;
|
nuclear@9
|
17 static float video_aspect;
|
nuclear@17
|
18 static struct texture *test_tex;
|
nuclear@17
|
19
|
nuclear@21
|
20 static SdrProg *sdr_cam, *sdr_tex;
|
nuclear@0
|
21
|
nuclear@20
|
22 extern "C" int game_init(void)
|
nuclear@0
|
23 {
|
nuclear@17
|
24 unsigned int vsdr, psdr_cam, psdr_tex;
|
nuclear@10
|
25
|
nuclear@17
|
26 //glEnable(GL_DEPTH_TEST);
|
nuclear@0
|
27 glEnable(GL_CULL_FACE);
|
nuclear@0
|
28
|
nuclear@0
|
29 glClearColor(0.4, 0.4, 0.4, 1);
|
nuclear@8
|
30
|
nuclear@21
|
31 if(!(sdr_cam = get_sdrprog("sdr/vertex.glsl", "sdr/android_cam_preview.p.glsl"))) {
|
nuclear@10
|
32 return -1;
|
nuclear@10
|
33 }
|
nuclear@21
|
34 if(!(sdr_tex = get_sdrprog("sdr/vertex.glsl", "sdr/tex.p.glsl"))) {
|
nuclear@17
|
35 return -1;
|
nuclear@17
|
36 }
|
nuclear@17
|
37
|
nuclear@17
|
38 if(!(test_tex = get_texture("data/opengl.png"))) {
|
nuclear@17
|
39 return -1;
|
nuclear@17
|
40 }
|
nuclear@10
|
41
|
nuclear@8
|
42 cam_start_video();
|
nuclear@9
|
43 cam_video_size(&video_width, &video_height);
|
nuclear@9
|
44 if(video_height) {
|
nuclear@9
|
45 video_aspect = (float)video_width / (float)video_height;
|
nuclear@9
|
46 } else {
|
nuclear@9
|
47 video_aspect = 1.0;
|
nuclear@9
|
48 }
|
nuclear@9
|
49
|
nuclear@9
|
50 printf("started video %dx%d (aspect: %g)\n", video_width, video_height, video_aspect);
|
nuclear@0
|
51 return 0;
|
nuclear@0
|
52 }
|
nuclear@0
|
53
|
nuclear@20
|
54 extern "C" void game_shutdown(void)
|
nuclear@0
|
55 {
|
nuclear@8
|
56 cam_shutdown();
|
nuclear@21
|
57 delete sdr_cam;
|
nuclear@21
|
58 delete sdr_tex;
|
nuclear@0
|
59 }
|
nuclear@0
|
60
|
nuclear@20
|
61 extern "C" void game_display(unsigned long msec)
|
nuclear@0
|
62 {
|
nuclear@9
|
63 unsigned int tex;
|
nuclear@9
|
64 const float *tex_matrix;
|
nuclear@9
|
65 float xscale, yscale;
|
nuclear@9
|
66
|
nuclear@8
|
67 cam_update();
|
nuclear@9
|
68 tex = cam_texture();
|
nuclear@9
|
69 tex_matrix = cam_texture_matrix();
|
nuclear@7
|
70
|
nuclear@4
|
71 //float tsec = (float)msec / 1000.0f;
|
nuclear@0
|
72
|
nuclear@0
|
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
74
|
nuclear@11
|
75 gl_matrix_mode(GL_MODELVIEW);
|
nuclear@11
|
76 gl_load_identity();
|
nuclear@11
|
77 gl_matrix_mode(GL_TEXTURE);
|
nuclear@11
|
78 gl_load_matrixf(tex_matrix);
|
nuclear@11
|
79
|
nuclear@21
|
80 sdr_cam->bind();
|
nuclear@10
|
81 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
|
nuclear@7
|
82
|
nuclear@9
|
83 if(video_aspect > win_aspect) {
|
nuclear@9
|
84 xscale = 1.0;
|
nuclear@9
|
85 yscale = 1.0 / video_aspect;
|
nuclear@9
|
86 } else {
|
nuclear@9
|
87 xscale = video_aspect;
|
nuclear@9
|
88 yscale = 1.0;
|
nuclear@9
|
89 }
|
nuclear@21
|
90 draw_quad(xscale, yscale);
|
nuclear@17
|
91
|
nuclear@17
|
92 gl_matrix_mode(GL_TEXTURE);
|
nuclear@17
|
93 gl_load_identity();
|
nuclear@17
|
94 gl_translatef(0, 1, 0);
|
nuclear@17
|
95 gl_scalef(1, -1, 1);
|
nuclear@17
|
96 gl_matrix_mode(GL_MODELVIEW);
|
nuclear@17
|
97 gl_load_identity();
|
nuclear@17
|
98 gl_scalef((float)test_tex->width / (float)test_tex->height, 1, 1);
|
nuclear@17
|
99
|
nuclear@21
|
100 sdr_tex->bind();
|
nuclear@17
|
101 glBindTexture(GL_TEXTURE_2D, test_tex->texid);
|
nuclear@17
|
102
|
nuclear@17
|
103 glEnable(GL_BLEND);
|
nuclear@17
|
104 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
nuclear@17
|
105
|
nuclear@21
|
106 draw_quad(0.5, 0.5);
|
nuclear@17
|
107
|
nuclear@17
|
108 glDisable(GL_BLEND);
|
nuclear@7
|
109 }
|
nuclear@7
|
110
|
nuclear@21
|
111 static void draw_quad(float hsz, float vsz)
|
nuclear@7
|
112 {
|
nuclear@7
|
113 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
|
nuclear@9
|
114 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
|
nuclear@7
|
115
|
nuclear@11
|
116 gl_matrix_mode(GL_MODELVIEW);
|
nuclear@11
|
117 gl_push_matrix();
|
nuclear@11
|
118 gl_scalef(hsz, vsz, 1);
|
nuclear@11
|
119
|
nuclear@21
|
120 if(SdrProg::active) {
|
nuclear@21
|
121 gl_apply_xform(SdrProg::active->get_globj());
|
nuclear@21
|
122 }
|
nuclear@7
|
123
|
nuclear@21
|
124 glEnableVertexAttribArray(SDR_ATTR_VERTEX);
|
nuclear@21
|
125 glEnableVertexAttribArray(SDR_ATTR_TEXCOORD);
|
nuclear@21
|
126 glVertexAttribPointer(SDR_ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr);
|
nuclear@21
|
127 glVertexAttribPointer(SDR_ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr);
|
nuclear@7
|
128
|
nuclear@8
|
129 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
nuclear@7
|
130
|
nuclear@21
|
131 glDisableVertexAttribArray(SDR_ATTR_VERTEX);
|
nuclear@21
|
132 glDisableVertexAttribArray(SDR_ATTR_TEXCOORD);
|
nuclear@11
|
133
|
nuclear@11
|
134 gl_pop_matrix();
|
nuclear@0
|
135 }
|
nuclear@0
|
136
|
nuclear@20
|
137 extern "C" void game_reshape(int x, int y)
|
nuclear@0
|
138 {
|
nuclear@0
|
139 win_width = x;
|
nuclear@0
|
140 win_height = y;
|
nuclear@9
|
141 win_aspect = y ? (float)x / (float)y : 1.0;
|
nuclear@0
|
142 glViewport(0, 0, x, y);
|
nuclear@0
|
143
|
nuclear@11
|
144 gl_matrix_mode(GL_PROJECTION);
|
nuclear@11
|
145 gl_load_identity();
|
nuclear@11
|
146 gl_scalef((float)win_height / (float)win_width, 1, 1);
|
nuclear@0
|
147 }
|
nuclear@0
|
148
|
nuclear@20
|
149 extern "C" void game_keyboard(int key, int pressed)
|
nuclear@0
|
150 {
|
nuclear@0
|
151 if(!pressed) return;
|
nuclear@0
|
152
|
nuclear@0
|
153 switch(key) {
|
nuclear@0
|
154 case 27:
|
nuclear@0
|
155 exit(0);
|
nuclear@0
|
156
|
nuclear@0
|
157 default:
|
nuclear@0
|
158 break;
|
nuclear@0
|
159 }
|
nuclear@0
|
160 }
|
nuclear@0
|
161
|
nuclear@0
|
162 #define MAX_TOUCH_IDS 16
|
nuclear@0
|
163 static struct {
|
nuclear@0
|
164 int bnstate[8];
|
nuclear@0
|
165 int prev_x, prev_y;
|
nuclear@0
|
166 } mstate[MAX_TOUCH_IDS];
|
nuclear@0
|
167
|
nuclear@20
|
168 extern "C" void game_mouse_button(int id, int bn, int pressed, int x, int y)
|
nuclear@0
|
169 {
|
nuclear@0
|
170 if(id >= MAX_TOUCH_IDS) return;
|
nuclear@0
|
171
|
nuclear@0
|
172 mstate[id].prev_x = x;
|
nuclear@0
|
173 mstate[id].prev_y = y;
|
nuclear@0
|
174 mstate[id].bnstate[bn] = pressed;
|
nuclear@0
|
175 }
|
nuclear@0
|
176
|
nuclear@20
|
177 extern "C" void game_mouse_motion(int id, int x, int y)
|
nuclear@0
|
178 {
|
nuclear@0
|
179 /*
|
nuclear@0
|
180 int dx, dy, cx, cy;
|
nuclear@0
|
181
|
nuclear@0
|
182 if(id >= MAX_TOUCH_IDS) return;
|
nuclear@0
|
183
|
nuclear@0
|
184 cx = win_width / 2;
|
nuclear@0
|
185 cy = win_height / 2;
|
nuclear@0
|
186
|
nuclear@0
|
187 dx = x - mstate[id].prev_x;
|
nuclear@0
|
188 dy = y - mstate[id].prev_y;
|
nuclear@0
|
189 mstate[id].prev_x = x;
|
nuclear@0
|
190 mstate[id].prev_y = y;
|
nuclear@0
|
191
|
nuclear@0
|
192 if(!dx && !dy) return;
|
nuclear@0
|
193
|
nuclear@0
|
194 if(mouselook || mstate[id].bnstate[0]) {
|
nuclear@0
|
195 player_turn(&player, dx * 0.5, dy * 0.5);
|
nuclear@0
|
196 }
|
nuclear@0
|
197 if(mstate[id].bnstate[2]) {
|
nuclear@0
|
198 dbg_cam_dist += 0.1 * dy;
|
nuclear@0
|
199 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
|
nuclear@0
|
200 }
|
nuclear@0
|
201
|
nuclear@0
|
202 if(mouselook) {
|
nuclear@0
|
203 warping_mouse = 1;
|
nuclear@0
|
204 set_mouse_pos(cx, cy);
|
nuclear@0
|
205 mstate[id].prev_x = cx;
|
nuclear@0
|
206 mstate[id].prev_y = cy;
|
nuclear@0
|
207 }
|
nuclear@0
|
208 */
|
nuclear@0
|
209 }
|
nuclear@0
|
210
|