vrheights
diff src/game.cc @ 2:b49461618f61
starting point
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 25 Sep 2014 11:44:45 +0300 |
parents | d06e4e24f922 |
children | 316ec8250af2 |
line diff
1.1 --- a/src/game.cc Wed Sep 24 11:34:07 2014 +0300 1.2 +++ b/src/game.cc Thu Sep 25 11:44:45 2014 +0300 1.3 @@ -1,101 +1,204 @@ 1.4 -#include <stdio.h> 1.5 -#include <assert.h> 1.6 -#include <algorithm> 1.7 -#include "opengl.h" 1.8 -#include "game.h" 1.9 -#include "goatvr.h" 1.10 - 1.11 -static void create_rtarg(int x, int y); 1.12 -static int next_pow2(int x); 1.13 - 1.14 -static int win_width, win_height; 1.15 -static unsigned int fb_tex; 1.16 -static unsigned int fbo, fb_depth; 1.17 -static int fb_xsz, fb_ysz; 1.18 -static int fb_tex_xsz, fb_tex_ysz; 1.19 - 1.20 -bool game_init() 1.21 -{ 1.22 - init_opengl(); 1.23 - 1.24 - if(vr_init() == -1) { 1.25 - return false; 1.26 - } 1.27 - 1.28 - glEnable(GL_DEPTH_TEST); 1.29 - glEnable(GL_CULL_FACE); 1.30 - 1.31 - int sdk_fb_xsz = vr_getf(VR_LEYE_XRES) + vr_getf(VR_REYE_XRES); 1.32 - int sdk_fb_ysz = std::max(vr_getf(VR_LEYE_YRES), vr_getf(VR_REYE_YRES)); 1.33 - create_rtarg(sdk_fb_xsz, sdk_fb_ysz); 1.34 - 1.35 - return true; 1.36 -} 1.37 - 1.38 -void game_cleanup() 1.39 -{ 1.40 - vr_shutdown(); 1.41 -} 1.42 - 1.43 -void game_update(long tm) 1.44 -{ 1.45 -} 1.46 - 1.47 -void game_display() 1.48 -{ 1.49 -} 1.50 - 1.51 -void game_reshape(int x, int y) 1.52 -{ 1.53 -} 1.54 - 1.55 -void game_keyboard(int key, bool pressed); 1.56 -void game_mouse_button(int bn, bool state, int x, int y); 1.57 -void game_mouse_motion(int x, int y); 1.58 - 1.59 - 1.60 -static void create_rtarg(int x, int y) 1.61 -{ 1.62 - if(x == fb_xsz && y == fb_ysz) { 1.63 - return; // nothing changed 1.64 - } 1.65 - 1.66 - fb_xsz = x; 1.67 - fb_ysz = y; 1.68 - fb_tex_xsz = next_pow2(fb_xsz); 1.69 - fb_tex_ysz = next_pow2(fb_ysz); 1.70 - 1.71 - if(!fbo) { 1.72 - glGenFramebuffers(1, &fbo); 1.73 - 1.74 - glGenTextures(1, &fb_tex); 1.75 - glBindTexture(GL_TEXTURE_2D, fb_tex); 1.76 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.77 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.78 - 1.79 - glGenRenderbuffers(1, &fb_depth); 1.80 - } 1.81 - 1.82 - glBindFramebuffer(GL_FRAMEBUFFER, fbo); 1.83 - 1.84 - glBindTexture(GL_TEXTURE_2D, fb_tex); 1.85 - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); 1.86 - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0); 1.87 - 1.88 - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz); 1.89 - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth); 1.90 - 1.91 - assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); 1.92 - glBindFramebuffer(GL_FRAMEBUFFER, 0); 1.93 -} 1.94 - 1.95 -static int next_pow2(int x) 1.96 -{ 1.97 - x -= 1; 1.98 - x |= x >> 1; 1.99 - x |= x >> 2; 1.100 - x |= x >> 4; 1.101 - x |= x >> 8; 1.102 - x |= x >> 16; 1.103 - return x + 1; 1.104 -} 1.105 +#include <stdio.h> 1.106 +#include <assert.h> 1.107 +#include <algorithm> 1.108 +#include "opengl.h" 1.109 +#include "game.h" 1.110 +#include "goatvr.h" 1.111 + 1.112 +static void draw_scene(); 1.113 +static void toggle_hmd_fullscr(); 1.114 +static void create_rtarg(int x, int y); 1.115 +static int next_pow2(int x); 1.116 + 1.117 +static int win_width, win_height; 1.118 +static unsigned int fb_tex; 1.119 +static unsigned int fbo, fb_depth; 1.120 +static int fb_xsz, fb_ysz; 1.121 +static int fb_tex_xsz, fb_tex_ysz; 1.122 + 1.123 +bool game_init() 1.124 +{ 1.125 + init_opengl(); 1.126 + 1.127 + if(vr_init() == -1) { 1.128 + return false; 1.129 + } 1.130 + 1.131 + glEnable(GL_DEPTH_TEST); 1.132 + glEnable(GL_CULL_FACE); 1.133 + 1.134 + return true; 1.135 +} 1.136 + 1.137 +void game_cleanup() 1.138 +{ 1.139 + vr_shutdown(); 1.140 +} 1.141 + 1.142 +void game_update(long tm) 1.143 +{ 1.144 +} 1.145 + 1.146 +void game_display() 1.147 +{ 1.148 + glBindFramebuffer(GL_FRAMEBUFFER, fbo); 1.149 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.150 + 1.151 + for(int i=0; i<2; i++) { 1.152 + glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz); 1.153 + vr_begin(i); 1.154 + 1.155 + float proj[16]; 1.156 + if(vr_proj_matrix(i, 0.5, 500.0, proj)) { 1.157 + glMatrixMode(GL_PROJECTION); 1.158 + glLoadMatrixf(proj); 1.159 + } 1.160 + 1.161 + glMatrixMode(GL_MODELVIEW); 1.162 + 1.163 + float view[16]; 1.164 + vr_view_matrix(i, view); 1.165 + glLoadMatrixf(view); 1.166 + /* move the camera to the eye level of the user */ 1.167 + glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0); 1.168 + 1.169 + draw_scene(); 1.170 + 1.171 + vr_end(); 1.172 + } 1.173 + 1.174 + glBindFramebuffer(GL_FRAMEBUFFER, 0); 1.175 + glViewport(0, 0, win_width, win_height); 1.176 + 1.177 + vr_swap_buffers(); 1.178 + assert(glGetError() == GL_NO_ERROR); 1.179 +} 1.180 + 1.181 +void game_reshape(int x, int y) 1.182 +{ 1.183 + win_width = x; 1.184 + win_height = y; 1.185 + 1.186 + create_rtarg(vr_geti_def(VR_RENDER_XRES, x), vr_geti_def(VR_RENDER_YRES, y)); 1.187 + vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz); 1.188 + 1.189 + /* these might be overriden in VR mode (see game_display) */ 1.190 + glViewport(0, 0, x, y); 1.191 + glMatrixMode(GL_PROJECTION); 1.192 + glLoadIdentity(); 1.193 + gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0); 1.194 +} 1.195 + 1.196 +void game_keyboard(int key, bool pressed) 1.197 +{ 1.198 + if(pressed) { 1.199 + switch(key) { 1.200 + case 27: 1.201 + exit_game(); 1.202 + break; 1.203 + 1.204 + case 'f': 1.205 + toggle_hmd_fullscr(); 1.206 + break; 1.207 + 1.208 + case 'r': 1.209 + vr_recenter(); 1.210 + break; 1.211 + 1.212 + default: 1.213 + break; 1.214 + } 1.215 + } 1.216 +} 1.217 + 1.218 +void game_mouse_button(int bn, bool state, int x, int y) 1.219 +{ 1.220 +} 1.221 + 1.222 +void game_mouse_motion(int x, int y) 1.223 +{ 1.224 +} 1.225 + 1.226 +static void draw_scene() 1.227 +{ 1.228 +} 1.229 + 1.230 +static void toggle_hmd_fullscr() 1.231 +{ 1.232 + static bool fullscr; 1.233 + static int prev_x, prev_y; 1.234 + //static int prev_xsz, prev_ysz; 1.235 + 1.236 + fullscr = !fullscr; 1.237 + if(fullscr) { 1.238 + /* entering fullscreen on the HMD */ 1.239 + int xoffs = vr_geti_def(VR_WIN_XOFFS, -1); 1.240 + int yoffs = vr_geti_def(VR_WIN_YOFFS, -1); 1.241 + if(xoffs != -1) { 1.242 + get_window_pos(&prev_x, &prev_y); 1.243 + move_window(xoffs, yoffs); 1.244 + } 1.245 + 1.246 + int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1); 1.247 + int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1); 1.248 + if(xsz != -1) { 1.249 + //prev_xsz = win_width; 1.250 + //prev_ysz = win_height; 1.251 + resize_window(xsz, ysz); 1.252 + } 1.253 + enter_fullscreen(); 1.254 + 1.255 + } else { 1.256 + /* leaving fullscreen */ 1.257 + leave_fullscreen(); 1.258 + /*move_window(prev_x, prev_y); 1.259 + resize_window(prev_xsz, prev_ysz);*/ 1.260 + } 1.261 +} 1.262 + 1.263 +static void create_rtarg(int x, int y) 1.264 +{ 1.265 + if(x == fb_xsz && y == fb_ysz) { 1.266 + return; // nothing changed 1.267 + } 1.268 + 1.269 + fb_xsz = x; 1.270 + fb_ysz = y; 1.271 + fb_tex_xsz = next_pow2(fb_xsz); 1.272 + fb_tex_ysz = next_pow2(fb_ysz); 1.273 + 1.274 + if(!fbo) { 1.275 + glGenFramebuffers(1, &fbo); 1.276 + 1.277 + glGenTextures(1, &fb_tex); 1.278 + glBindTexture(GL_TEXTURE_2D, fb_tex); 1.279 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 1.280 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.281 + 1.282 + glGenRenderbuffers(1, &fb_depth); 1.283 + } 1.284 + 1.285 + glBindFramebuffer(GL_FRAMEBUFFER, fbo); 1.286 + 1.287 + glBindTexture(GL_TEXTURE_2D, fb_tex); 1.288 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); 1.289 + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0); 1.290 + 1.291 + glBindRenderbuffer(GL_RENDERBUFFER, fb_depth); 1.292 + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz); 1.293 + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth); 1.294 + 1.295 + assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE); 1.296 + glBindFramebuffer(GL_FRAMEBUFFER, 0); 1.297 +} 1.298 + 1.299 +static int next_pow2(int x) 1.300 +{ 1.301 + x -= 1; 1.302 + x |= x >> 1; 1.303 + x |= x >> 2; 1.304 + x |= x >> 4; 1.305 + x |= x >> 8; 1.306 + x |= x >> 16; 1.307 + return x + 1; 1.308 +}