3dphotoshoot
view src/game.c @ 9:d1b456d08713
texture matrix and video size from JNI
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 25 May 2015 05:27:26 +0300 |
parents | 9fc7d52f578d |
children | c71c477521ca |
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"
8 static void draw_quad(float hsz, float vsz);
10 static int win_width, win_height;
11 static float win_aspect;
12 static int video_width, video_height;
13 static float video_aspect;
16 int game_init(void)
17 {
18 glEnable(GL_DEPTH_TEST);
19 glEnable(GL_CULL_FACE);
20 //glEnable(GL_LIGHTING);
22 glClearColor(0.4, 0.4, 0.4, 1);
24 cam_start_video();
25 cam_video_size(&video_width, &video_height);
26 if(video_height) {
27 video_aspect = (float)video_width / (float)video_height;
28 } else {
29 video_aspect = 1.0;
30 }
32 printf("started video %dx%d (aspect: %g)\n", video_width, video_height, video_aspect);
33 return 0;
34 }
36 void game_shutdown(void)
37 {
38 cam_shutdown();
39 }
41 void game_display(unsigned long msec)
42 {
43 unsigned int tex;
44 const float *tex_matrix;
45 float xscale, yscale;
47 cam_update();
48 tex = cam_texture();
49 tex_matrix = cam_texture_matrix();
51 //float tsec = (float)msec / 1000.0f;
53 //glClearColor(sin(tsec * 10.0), cos(tsec * 10.0), -sin(tsec * 10.0), 1.0);
54 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
56 glMatrixMode(GL_MODELVIEW);
57 glLoadIdentity();
59 if(tex) {
60 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
61 glEnable(GL_TEXTURE_EXTERNAL_OES);
63 glMatrixMode(GL_TEXTURE);
64 glLoadMatrixf(tex_matrix);
65 glMatrixMode(GL_MODELVIEW);
66 }
68 if(video_aspect > win_aspect) {
69 xscale = 1.0;
70 yscale = 1.0 / video_aspect;
71 } else {
72 xscale = video_aspect;
73 yscale = 1.0;
74 }
75 draw_quad(xscale, yscale);
77 if(tex) {
78 glDisable(GL_TEXTURE_EXTERNAL_OES);
80 glMatrixMode(GL_TEXTURE);
81 glLoadIdentity();
82 glMatrixMode(GL_MODELVIEW);
83 }
84 }
86 static void draw_quad(float hsz, float vsz)
87 {
88 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
89 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
90 static const float colarr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
92 glPushMatrix();
93 glScalef(hsz, vsz, 1);
95 glEnableClientState(GL_VERTEX_ARRAY);
96 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
97 glEnableClientState(GL_COLOR_ARRAY);
99 glVertexPointer(2, GL_FLOAT, 0, varr);
100 glTexCoordPointer(2, GL_FLOAT, 0, tcarr);
101 glColorPointer(4, GL_FLOAT, 0, colarr);
103 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
105 glDisableClientState(GL_VERTEX_ARRAY);
106 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
107 glDisableClientState(GL_COLOR_ARRAY);
109 glPopMatrix();
110 }
112 void game_reshape(int x, int y)
113 {
114 win_width = x;
115 win_height = y;
116 win_aspect = y ? (float)x / (float)y : 1.0;
117 glViewport(0, 0, x, y);
119 glMatrixMode(GL_PROJECTION);
120 glLoadIdentity();
121 glScalef((float)win_height / (float)win_width, 1.0, 1.0);
122 }
124 void game_keyboard(int key, int pressed)
125 {
126 if(!pressed) return;
128 switch(key) {
129 case 27:
130 exit(0);
132 default:
133 break;
134 }
135 }
137 #define MAX_TOUCH_IDS 16
138 static struct {
139 int bnstate[8];
140 int prev_x, prev_y;
141 } mstate[MAX_TOUCH_IDS];
143 void game_mouse_button(int id, int bn, int pressed, int x, int y)
144 {
145 if(id >= MAX_TOUCH_IDS) return;
147 mstate[id].prev_x = x;
148 mstate[id].prev_y = y;
149 mstate[id].bnstate[bn] = pressed;
150 }
152 void game_mouse_motion(int id, int x, int y)
153 {
154 /*
155 int dx, dy, cx, cy;
157 if(id >= MAX_TOUCH_IDS) return;
159 cx = win_width / 2;
160 cy = win_height / 2;
162 dx = x - mstate[id].prev_x;
163 dy = y - mstate[id].prev_y;
164 mstate[id].prev_x = x;
165 mstate[id].prev_y = y;
167 if(!dx && !dy) return;
169 if(mouselook || mstate[id].bnstate[0]) {
170 player_turn(&player, dx * 0.5, dy * 0.5);
171 }
172 if(mstate[id].bnstate[2]) {
173 dbg_cam_dist += 0.1 * dy;
174 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
175 }
177 if(mouselook) {
178 warping_mouse = 1;
179 set_mouse_pos(cx, cy);
180 mstate[id].prev_x = cx;
181 mstate[id].prev_y = cy;
182 }
183 */
184 }