3dphotoshoot

view src/game.cc @ 22:d7fe157c402d

fonts
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 13 Jun 2015 05:32:07 +0300
parents 4ca4e3c5a754
children 2712c5da2e00
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"
13 static void draw_quad(float hsz, float vsz);
15 int win_width, win_height;
17 static float win_aspect;
18 static int video_width, video_height;
19 static float video_aspect;
20 static struct texture *test_tex;
22 static SdrProg *sdr_cam, *sdr_tex;
24 extern "C" int game_init(void)
25 {
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);
105 draw_quad(0.5, 0.5);
106 glDisable(GL_BLEND);
108 gl_matrix_mode(GL_TEXTURE);
109 gl_load_identity();
112 // draw some text
113 text_color(1, 1, 1, 1);
114 for(int i=0; i<25; i++) {
115 text_position(0, i);
116 text_printf("%d%s line of text", i, i == 1 ? "st" : (i == 2 ? "nd" : (i == 3 ? "rd" : "th")));
117 }
118 }
120 static void draw_quad(float hsz, float vsz)
121 {
122 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
123 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
125 gl_matrix_mode(GL_MODELVIEW);
126 gl_push_matrix();
127 gl_scalef(hsz, vsz, 1);
129 if(SdrProg::active) {
130 gl_apply_xform(SdrProg::active->get_globj());
131 }
133 glEnableVertexAttribArray(SDR_ATTR_VERTEX);
134 glEnableVertexAttribArray(SDR_ATTR_TEXCOORD);
135 glVertexAttribPointer(SDR_ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr);
136 glVertexAttribPointer(SDR_ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr);
138 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
140 glDisableVertexAttribArray(SDR_ATTR_VERTEX);
141 glDisableVertexAttribArray(SDR_ATTR_TEXCOORD);
143 gl_pop_matrix();
144 }
146 extern "C" void game_reshape(int x, int y)
147 {
148 win_width = x;
149 win_height = y;
150 win_aspect = y ? (float)x / (float)y : 1.0;
151 glViewport(0, 0, x, y);
153 gl_matrix_mode(GL_PROJECTION);
154 gl_load_identity();
155 gl_scalef((float)win_height / (float)win_width, 1, 1);
156 }
158 extern "C" void game_keyboard(int key, int pressed)
159 {
160 if(!pressed) return;
162 switch(key) {
163 case 27:
164 exit(0);
166 default:
167 break;
168 }
169 }
171 #define MAX_TOUCH_IDS 16
172 static struct {
173 int bnstate[8];
174 int prev_x, prev_y;
175 } mstate[MAX_TOUCH_IDS];
177 extern "C" void game_mouse_button(int id, int bn, int pressed, int x, int y)
178 {
179 if(id >= MAX_TOUCH_IDS) return;
181 mstate[id].prev_x = x;
182 mstate[id].prev_y = y;
183 mstate[id].bnstate[bn] = pressed;
184 }
186 extern "C" void game_mouse_motion(int id, int x, int y)
187 {
188 /*
189 int dx, dy, cx, cy;
191 if(id >= MAX_TOUCH_IDS) return;
193 cx = win_width / 2;
194 cy = win_height / 2;
196 dx = x - mstate[id].prev_x;
197 dy = y - mstate[id].prev_y;
198 mstate[id].prev_x = x;
199 mstate[id].prev_y = y;
201 if(!dx && !dy) return;
203 if(mouselook || mstate[id].bnstate[0]) {
204 player_turn(&player, dx * 0.5, dy * 0.5);
205 }
206 if(mstate[id].bnstate[2]) {
207 dbg_cam_dist += 0.1 * dy;
208 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
209 }
211 if(mouselook) {
212 warping_mouse = 1;
213 set_mouse_pos(cx, cy);
214 mstate[id].prev_x = cx;
215 mstate[id].prev_y = cy;
216 }
217 */
218 }