3dphotoshoot
view src/game.c @ 11:ad49e1f9b627
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 31 May 2015 06:02:08 +0300 |
parents | c71c477521ca |
children | aef7f51f6397 |
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 "sdr.h"
9 #include "sanegl.h"
11 static void draw_quad(float hsz, float vsz);
13 static int win_width, win_height;
14 static float win_aspect;
15 static int video_width, video_height;
16 static float video_aspect;
17 static unsigned int sdrprog;
18 static int aloc_vertex, aloc_texcoord, uloc_tex;
20 static const char *vsdr_source =
21 "attribute vec4 attr_vertex, attr_texcoord;\n"
22 "uniform mat4 matrix_modelview, matrix_projection, matrix_texture;\n"
23 "varying vec4 tex_coords;\n"
24 "void main()\n"
25 "{\n"
26 "\tmat4 mvp = matrix_projection * matrix_modelview;\n"
27 "\tgl_Position = mvp * attr_vertex;\n"
28 "\ttex_coords = matrix_texture * attr_texcoord;\n"
29 "}\n";
31 static const char *psdr_source =
32 "#extension GL_OES_EGL_image_external : require\n"
33 "precision mediump float;\n"
34 "uniform samplerExternalOES tex;\n"
35 "varying vec4 tex_coords;\n"
36 "void main()\n"
37 "{\n"
38 "\tvec4 texel = texture2D(tex, tex_coords.xy);\n"
39 "\tgl_FragColor = vec4(texel.xyz, 1.0);\n"
40 "}\n";
43 int game_init(void)
44 {
45 unsigned int vsdr, psdr;
47 glEnable(GL_DEPTH_TEST);
48 glEnable(GL_CULL_FACE);
50 glClearColor(0.4, 0.4, 0.4, 1);
52 if(!(vsdr = create_vertex_shader(vsdr_source)))
53 return -1;
54 assert(glGetError() == GL_NO_ERROR);
55 if(!(psdr = create_pixel_shader(psdr_source)))
56 return -1;
57 assert(glGetError() == GL_NO_ERROR);
58 if(!(sdrprog = create_program_link(vsdr, psdr))) {
59 fprintf(stderr, "failed to create shader program\n");
60 return -1;
61 }
62 glUseProgram(sdrprog);
63 aloc_vertex = glGetAttribLocation(sdrprog, "attr_vertex");
64 aloc_texcoord = glGetAttribLocation(sdrprog, "attr_texcoord");
65 uloc_tex = glGetUniformLocation(sdrprog, "tex");
67 cam_start_video();
68 cam_video_size(&video_width, &video_height);
69 if(video_height) {
70 video_aspect = (float)video_width / (float)video_height;
71 } else {
72 video_aspect = 1.0;
73 }
75 printf("started video %dx%d (aspect: %g)\n", video_width, video_height, video_aspect);
76 return 0;
77 }
79 void game_shutdown(void)
80 {
81 cam_shutdown();
82 free_program(sdrprog);
83 }
85 void game_display(unsigned long msec)
86 {
87 unsigned int tex;
88 const float *tex_matrix;
89 float xscale, yscale;
91 cam_update();
92 tex = cam_texture();
93 tex_matrix = cam_texture_matrix();
95 //float tsec = (float)msec / 1000.0f;
97 //glClearColor(sin(tsec * 10.0), cos(tsec * 10.0), -sin(tsec * 10.0), 1.0);
98 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
100 gl_matrix_mode(GL_MODELVIEW);
101 gl_load_identity();
102 gl_matrix_mode(GL_TEXTURE);
103 gl_load_matrixf(tex_matrix);
105 glUseProgram(sdrprog);
106 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
107 if(uloc_tex >= 0) {
108 glUniform1i(uloc_tex, 0);
109 }
111 if(video_aspect > win_aspect) {
112 xscale = 1.0;
113 yscale = 1.0 / video_aspect;
114 } else {
115 xscale = video_aspect;
116 yscale = 1.0;
117 }
118 draw_quad(xscale, yscale);
119 }
121 static void draw_quad(float hsz, float vsz)
122 {
123 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
124 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
126 if(aloc_vertex == -1) {
127 return;
128 }
130 gl_matrix_mode(GL_MODELVIEW);
131 gl_push_matrix();
132 gl_scalef(hsz, vsz, 1);
134 gl_apply_xform(sdrprog);
136 glEnableVertexAttribArray(aloc_vertex);
137 glVertexAttribPointer(aloc_vertex, 2, GL_FLOAT, 0, 0, varr);
138 if(aloc_texcoord != -1) {
139 glEnableVertexAttribArray(aloc_texcoord);
140 glVertexAttribPointer(aloc_texcoord, 2, GL_FLOAT, 0, 0, tcarr);
141 }
143 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
145 glDisableVertexAttribArray(aloc_vertex);
146 if(aloc_texcoord != -1) {
147 glDisableVertexAttribArray(aloc_texcoord);
148 }
150 gl_pop_matrix();
151 }
153 void game_reshape(int x, int y)
154 {
155 win_width = x;
156 win_height = y;
157 win_aspect = y ? (float)x / (float)y : 1.0;
158 glViewport(0, 0, x, y);
160 gl_matrix_mode(GL_PROJECTION);
161 gl_load_identity();
162 gl_scalef((float)win_height / (float)win_width, 1, 1);
163 }
165 void game_keyboard(int key, int pressed)
166 {
167 if(!pressed) return;
169 switch(key) {
170 case 27:
171 exit(0);
173 default:
174 break;
175 }
176 }
178 #define MAX_TOUCH_IDS 16
179 static struct {
180 int bnstate[8];
181 int prev_x, prev_y;
182 } mstate[MAX_TOUCH_IDS];
184 void game_mouse_button(int id, int bn, int pressed, int x, int y)
185 {
186 if(id >= MAX_TOUCH_IDS) return;
188 mstate[id].prev_x = x;
189 mstate[id].prev_y = y;
190 mstate[id].bnstate[bn] = pressed;
191 }
193 void game_mouse_motion(int id, int x, int y)
194 {
195 /*
196 int dx, dy, cx, cy;
198 if(id >= MAX_TOUCH_IDS) return;
200 cx = win_width / 2;
201 cy = win_height / 2;
203 dx = x - mstate[id].prev_x;
204 dy = y - mstate[id].prev_y;
205 mstate[id].prev_x = x;
206 mstate[id].prev_y = y;
208 if(!dx && !dy) return;
210 if(mouselook || mstate[id].bnstate[0]) {
211 player_turn(&player, dx * 0.5, dy * 0.5);
212 }
213 if(mstate[id].bnstate[2]) {
214 dbg_cam_dist += 0.1 * dy;
215 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
216 }
218 if(mouselook) {
219 warping_mouse = 1;
220 set_mouse_pos(cx, cy);
221 mstate[id].prev_x = cx;
222 mstate[id].prev_y = cy;
223 }
224 */
225 }