3dphotoshoot

annotate 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
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <math.h>
nuclear@0 4 #include "opengl.h"
nuclear@0 5 #include "game.h"
nuclear@7 6 #include "camera.h"
nuclear@7 7
nuclear@8 8 static void draw_quad(float hsz, float vsz);
nuclear@0 9
nuclear@0 10 static int win_width, win_height;
nuclear@9 11 static float win_aspect;
nuclear@9 12 static int video_width, video_height;
nuclear@9 13 static float video_aspect;
nuclear@0 14
nuclear@0 15
nuclear@0 16 int game_init(void)
nuclear@0 17 {
nuclear@0 18 glEnable(GL_DEPTH_TEST);
nuclear@0 19 glEnable(GL_CULL_FACE);
nuclear@7 20 //glEnable(GL_LIGHTING);
nuclear@0 21
nuclear@0 22 glClearColor(0.4, 0.4, 0.4, 1);
nuclear@8 23
nuclear@8 24 cam_start_video();
nuclear@9 25 cam_video_size(&video_width, &video_height);
nuclear@9 26 if(video_height) {
nuclear@9 27 video_aspect = (float)video_width / (float)video_height;
nuclear@9 28 } else {
nuclear@9 29 video_aspect = 1.0;
nuclear@9 30 }
nuclear@9 31
nuclear@9 32 printf("started video %dx%d (aspect: %g)\n", video_width, video_height, video_aspect);
nuclear@0 33 return 0;
nuclear@0 34 }
nuclear@0 35
nuclear@0 36 void game_shutdown(void)
nuclear@0 37 {
nuclear@8 38 cam_shutdown();
nuclear@0 39 }
nuclear@0 40
nuclear@0 41 void game_display(unsigned long msec)
nuclear@0 42 {
nuclear@9 43 unsigned int tex;
nuclear@9 44 const float *tex_matrix;
nuclear@9 45 float xscale, yscale;
nuclear@9 46
nuclear@8 47 cam_update();
nuclear@9 48 tex = cam_texture();
nuclear@9 49 tex_matrix = cam_texture_matrix();
nuclear@7 50
nuclear@4 51 //float tsec = (float)msec / 1000.0f;
nuclear@0 52
nuclear@4 53 //glClearColor(sin(tsec * 10.0), cos(tsec * 10.0), -sin(tsec * 10.0), 1.0);
nuclear@0 54 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 55
nuclear@0 56 glMatrixMode(GL_MODELVIEW);
nuclear@0 57 glLoadIdentity();
nuclear@7 58
nuclear@7 59 if(tex) {
nuclear@7 60 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
nuclear@7 61 glEnable(GL_TEXTURE_EXTERNAL_OES);
nuclear@9 62
nuclear@9 63 glMatrixMode(GL_TEXTURE);
nuclear@9 64 glLoadMatrixf(tex_matrix);
nuclear@9 65 glMatrixMode(GL_MODELVIEW);
nuclear@8 66 }
nuclear@7 67
nuclear@9 68 if(video_aspect > win_aspect) {
nuclear@9 69 xscale = 1.0;
nuclear@9 70 yscale = 1.0 / video_aspect;
nuclear@9 71 } else {
nuclear@9 72 xscale = video_aspect;
nuclear@9 73 yscale = 1.0;
nuclear@9 74 }
nuclear@9 75 draw_quad(xscale, yscale);
nuclear@7 76
nuclear@8 77 if(tex) {
nuclear@7 78 glDisable(GL_TEXTURE_EXTERNAL_OES);
nuclear@9 79
nuclear@9 80 glMatrixMode(GL_TEXTURE);
nuclear@9 81 glLoadIdentity();
nuclear@9 82 glMatrixMode(GL_MODELVIEW);
nuclear@7 83 }
nuclear@7 84 }
nuclear@7 85
nuclear@8 86 static void draw_quad(float hsz, float vsz)
nuclear@7 87 {
nuclear@7 88 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
nuclear@9 89 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
nuclear@8 90 static const float colarr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
nuclear@7 91
nuclear@7 92 glPushMatrix();
nuclear@8 93 glScalef(hsz, vsz, 1);
nuclear@7 94
nuclear@7 95 glEnableClientState(GL_VERTEX_ARRAY);
nuclear@7 96 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@7 97 glEnableClientState(GL_COLOR_ARRAY);
nuclear@7 98
nuclear@7 99 glVertexPointer(2, GL_FLOAT, 0, varr);
nuclear@7 100 glTexCoordPointer(2, GL_FLOAT, 0, tcarr);
nuclear@8 101 glColorPointer(4, GL_FLOAT, 0, colarr);
nuclear@7 102
nuclear@8 103 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
nuclear@7 104
nuclear@7 105 glDisableClientState(GL_VERTEX_ARRAY);
nuclear@7 106 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@7 107 glDisableClientState(GL_COLOR_ARRAY);
nuclear@7 108
nuclear@7 109 glPopMatrix();
nuclear@0 110 }
nuclear@0 111
nuclear@0 112 void game_reshape(int x, int y)
nuclear@0 113 {
nuclear@0 114 win_width = x;
nuclear@0 115 win_height = y;
nuclear@9 116 win_aspect = y ? (float)x / (float)y : 1.0;
nuclear@0 117 glViewport(0, 0, x, y);
nuclear@0 118
nuclear@0 119 glMatrixMode(GL_PROJECTION);
nuclear@0 120 glLoadIdentity();
nuclear@0 121 glScalef((float)win_height / (float)win_width, 1.0, 1.0);
nuclear@0 122 }
nuclear@0 123
nuclear@0 124 void game_keyboard(int key, int pressed)
nuclear@0 125 {
nuclear@0 126 if(!pressed) return;
nuclear@0 127
nuclear@0 128 switch(key) {
nuclear@0 129 case 27:
nuclear@0 130 exit(0);
nuclear@0 131
nuclear@0 132 default:
nuclear@0 133 break;
nuclear@0 134 }
nuclear@0 135 }
nuclear@0 136
nuclear@0 137 #define MAX_TOUCH_IDS 16
nuclear@0 138 static struct {
nuclear@0 139 int bnstate[8];
nuclear@0 140 int prev_x, prev_y;
nuclear@0 141 } mstate[MAX_TOUCH_IDS];
nuclear@0 142
nuclear@0 143 void game_mouse_button(int id, int bn, int pressed, int x, int y)
nuclear@0 144 {
nuclear@0 145 if(id >= MAX_TOUCH_IDS) return;
nuclear@0 146
nuclear@0 147 mstate[id].prev_x = x;
nuclear@0 148 mstate[id].prev_y = y;
nuclear@0 149 mstate[id].bnstate[bn] = pressed;
nuclear@0 150 }
nuclear@0 151
nuclear@0 152 void game_mouse_motion(int id, int x, int y)
nuclear@0 153 {
nuclear@0 154 /*
nuclear@0 155 int dx, dy, cx, cy;
nuclear@0 156
nuclear@0 157 if(id >= MAX_TOUCH_IDS) return;
nuclear@0 158
nuclear@0 159 cx = win_width / 2;
nuclear@0 160 cy = win_height / 2;
nuclear@0 161
nuclear@0 162 dx = x - mstate[id].prev_x;
nuclear@0 163 dy = y - mstate[id].prev_y;
nuclear@0 164 mstate[id].prev_x = x;
nuclear@0 165 mstate[id].prev_y = y;
nuclear@0 166
nuclear@0 167 if(!dx && !dy) return;
nuclear@0 168
nuclear@0 169 if(mouselook || mstate[id].bnstate[0]) {
nuclear@0 170 player_turn(&player, dx * 0.5, dy * 0.5);
nuclear@0 171 }
nuclear@0 172 if(mstate[id].bnstate[2]) {
nuclear@0 173 dbg_cam_dist += 0.1 * dy;
nuclear@0 174 if(dbg_cam_dist < 0.0) dbg_cam_dist = 0.0;
nuclear@0 175 }
nuclear@0 176
nuclear@0 177 if(mouselook) {
nuclear@0 178 warping_mouse = 1;
nuclear@0 179 set_mouse_pos(cx, cy);
nuclear@0 180 mstate[id].prev_x = cx;
nuclear@0 181 mstate[id].prev_y = cy;
nuclear@0 182 }
nuclear@0 183 */
nuclear@0 184 }
nuclear@0 185