3dphotoshoot

view src/game.c @ 10:c71c477521ca

converting to GLES2 and C++
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 31 May 2015 00:40:26 +0300
parents d1b456d08713
children ad49e1f9b627
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include "opengl.h"
5 #include "game.h"
6 #include "camera.h"
7 #include "sdr.h"
9 static void draw_quad(float hsz, float vsz);
11 static int win_width, win_height;
12 static float win_aspect;
13 static int video_width, video_height;
14 static float video_aspect;
15 static unsigned int sdrprog;
16 static int aloc_vertex, aloc_texcoord, uloc_texmat, uloc_wmat, uloc_vmat, uloc_pmat;
18 static const char *vsdr_source =
19 "attribute vec4 attr_pos, attr_tex;\n"
20 "uniform mat4 world_matrix, proj_matrix, tex_matrix;\n"
21 "varying vec4 tex_coords;\n"
22 "void main()\n"
23 "{\n"
24 "\tmat4 mvp = proj_matrix * world_matrix;\n"
25 "\tgl_Position = mvp * attr_pos;\n"
26 "\ttex_coords = tex_matrix * attr_tex;\n"
27 "}\n";
29 static const char *psdr_source =
30 "#extension GL_OES_EGL_image_external : require\n"
31 "precision lowp float;\n"
32 "uniform samplerExternalOES tex;\n"
33 "varying vec4 tex_coords;\n"
34 "void main()\n"
35 "{\n"
36 "\tvec4 texel = texture2D(tex, tex_coords.xy);\n"
37 "\tgl_FragColor = vec4(texel.xyz, 1.0);\n"
38 "}\n";
41 int game_init(void)
42 {
43 unsigned int vsdr, psdr;
45 glEnable(GL_DEPTH_TEST);
46 glEnable(GL_CULL_FACE);
48 glClearColor(0.4, 0.4, 0.4, 1);
50 if(!(vsdr = create_vertex_shader(vsdr_source)))
51 return -1;
52 if(!(psdr = create_pixel_shader(psdr_source)))
53 return -1;
54 if(!(sdrprog = create_program_link(vsdr, psdr))) {
55 fprintf(stderr, "failed to create shader program\n");
56 return -1;
57 }
58 glUseProgram(sdrprog);
59 aloc_vertex = glGetAttribLocation(sdrprog, "attr_pos");
60 aloc_texcoord = glGetAttribLocation(sdrprog, "attr_tex");
61 uloc_texmat = glGetUniformLocation(sdrprog, "tex_matrix");
62 uloc_wmat = glGetUniformLocation(sdrprog, "world_matrix");
63 uloc_vmat = glGetUniformLocation(sdrprog, "view_matrix");
64 uloc_pmat = glGetUniformLocation(sdrprog, "proj_matrix");
66 cam_start_video();
67 cam_video_size(&video_width, &video_height);
68 if(video_height) {
69 video_aspect = (float)video_width / (float)video_height;
70 } else {
71 video_aspect = 1.0;
72 }
74 printf("started video %dx%d (aspect: %g)\n", video_width, video_height, video_aspect);
75 return 0;
76 }
78 void game_shutdown(void)
79 {
80 cam_shutdown();
81 free_program(sdrprog);
82 }
84 void game_display(unsigned long msec)
85 {
86 unsigned int tex;
87 const float *tex_matrix;
88 float xscale, yscale;
90 cam_update();
91 tex = cam_texture();
92 tex_matrix = cam_texture_matrix();
94 //float tsec = (float)msec / 1000.0f;
96 //glClearColor(sin(tsec * 10.0), cos(tsec * 10.0), -sin(tsec * 10.0), 1.0);
97 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
99 glUseProgram(sdrprog);
100 glUniformMatrix4fv(uloc_texmat, 1, 0, tex_matrix);
101 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
103 if(video_aspect > win_aspect) {
104 xscale = 1.0;
105 yscale = 1.0 / video_aspect;
106 } else {
107 xscale = video_aspect;
108 yscale = 1.0;
109 }
110 draw_quad(xscale, yscale);
111 }
113 static void draw_quad(float hsz, float vsz)
114 {
115 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
116 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
117 float xform[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
119 if(aloc_vertex == -1) {
120 return;
121 }
123 xform[0] = hsz;
124 xform[5] = vsz;
125 glUniformMatrix4fv(uloc_wmat, 1, 0, xform);
127 glEnableVertexAttribArray(aloc_vertex);
128 glVertexAttribPointer(aloc_vertex, 2, GL_FLOAT, 0, 0, varr);
129 if(aloc_texcoord) {
130 glEnableVertexAttribArray(aloc_texcoord);
131 glVertexAttribPointer(aloc_texcoord, 2, GL_FLOAT, 0, 0, tcarr);
132 }
134 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
136 glDisableVertexAttribArray(aloc_vertex);
137 if(aloc_texcoord) {
138 glDisableVertexAttribArray(aloc_texcoord);
139 }
140 }
142 void game_reshape(int x, int y)
143 {
144 win_width = x;
145 win_height = y;
146 win_aspect = y ? (float)x / (float)y : 1.0;
147 glViewport(0, 0, x, y);
149 /*glMatrixMode(GL_PROJECTION);
150 glLoadIdentity();
151 glScalef((float)win_height / (float)win_width, 1.0, 1.0);*/
152 }
154 void game_keyboard(int key, int pressed)
155 {
156 if(!pressed) return;
158 switch(key) {
159 case 27:
160 exit(0);
162 default:
163 break;
164 }
165 }
167 #define MAX_TOUCH_IDS 16
168 static struct {
169 int bnstate[8];
170 int prev_x, prev_y;
171 } mstate[MAX_TOUCH_IDS];
173 void game_mouse_button(int id, int bn, int pressed, int x, int y)
174 {
175 if(id >= MAX_TOUCH_IDS) return;
177 mstate[id].prev_x = x;
178 mstate[id].prev_y = y;
179 mstate[id].bnstate[bn] = pressed;
180 }
182 void game_mouse_motion(int id, int x, int y)
183 {
184 /*
185 int dx, dy, cx, cy;
187 if(id >= MAX_TOUCH_IDS) return;
189 cx = win_width / 2;
190 cy = win_height / 2;
192 dx = x - mstate[id].prev_x;
193 dy = y - mstate[id].prev_y;
194 mstate[id].prev_x = x;
195 mstate[id].prev_y = y;
197 if(!dx && !dy) return;
199 if(mouselook || mstate[id].bnstate[0]) {
200 player_turn(&player, dx * 0.5, dy * 0.5);
201 }
202 if(mstate[id].bnstate[2]) {
203 dbg_cam_dist += 0.1 * dy;
204 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
205 }
207 if(mouselook) {
208 warping_mouse = 1;
209 set_mouse_pos(cx, cy);
210 mstate[id].prev_x = cx;
211 mstate[id].prev_y = cy;
212 }
213 */
214 }