vrheights

changeset 1:d06e4e24f922

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 24 Sep 2014 11:34:07 +0300
parents ccbd444939a1
children b49461618f61
files src/game.cc
diffstat 1 files changed, 75 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/src/game.cc	Mon Sep 22 18:36:24 2014 +0300
     1.2 +++ b/src/game.cc	Wed Sep 24 11:34:07 2014 +0300
     1.3 @@ -1,8 +1,19 @@
     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 @@ -13,6 +24,12 @@
    1.24  
    1.25  	glEnable(GL_DEPTH_TEST);
    1.26  	glEnable(GL_CULL_FACE);
    1.27 +
    1.28 +	int sdk_fb_xsz = vr_getf(VR_LEYE_XRES) + vr_getf(VR_REYE_XRES);
    1.29 +	int sdk_fb_ysz = std::max(vr_getf(VR_LEYE_YRES), vr_getf(VR_REYE_YRES));
    1.30 +	create_rtarg(sdk_fb_xsz, sdk_fb_ysz);
    1.31 +
    1.32 +	return true;
    1.33  }
    1.34  
    1.35  void game_cleanup()
    1.36 @@ -20,10 +37,65 @@
    1.37  	vr_shutdown();
    1.38  }
    1.39  
    1.40 -void game_update(long tm);
    1.41 -void game_display();
    1.42 -void game_reshape(int x, int y);
    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 +}