conworlds

diff src/game.cc @ 6:3c36bc28c6c2

more stuff in the vr test
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 21 Aug 2014 01:08:03 +0300
parents e6948e131526
children bd8202d6d28d
line diff
     1.1 --- a/src/game.cc	Wed Aug 20 16:34:43 2014 +0300
     1.2 +++ b/src/game.cc	Thu Aug 21 01:08:03 2014 +0300
     1.3 @@ -5,6 +5,10 @@
     1.4  #include "vr/vr.h"
     1.5  
     1.6  static void draw_scene();
     1.7 +static bool setup_rtarg(int x, int y);
     1.8 +
     1.9 +static Texture *rtarg[2];
    1.10 +static unsigned int fbo, rtarg_depth;
    1.11  
    1.12  static const float move_speed = 10.0f;
    1.13  
    1.14 @@ -24,6 +28,7 @@
    1.15  
    1.16  	glClearColor(0.1, 0.1, 0.1, 1);
    1.17  
    1.18 +
    1.19  	if(!floor_tex.load("data/tiles.png")) {
    1.20  		return false;
    1.21  	}
    1.22 @@ -36,9 +41,15 @@
    1.23  {
    1.24  	floor_tex.destroy();
    1.25  	vr_shutdown();
    1.26 +
    1.27 +	if(fbo) {
    1.28 +		glDeleteFramebuffers(1, &fbo);
    1.29 +		glDeleteRenderbuffers(1, &rtarg_depth);
    1.30 +		delete rtarg[0];
    1.31 +		delete rtarg[1];
    1.32 +	}
    1.33  }
    1.34  
    1.35 -
    1.36  void game_update(unsigned int msec)
    1.37  {
    1.38  	static unsigned int prev_msec;
    1.39 @@ -74,6 +85,8 @@
    1.40  
    1.41  void game_render(int eye)
    1.42  {
    1.43 +	vr_begin(eye <= 0 ? VR_EYE_LEFT : VR_EYE_RIGHT);
    1.44 +
    1.45  	float mat[16];
    1.46  	Matrix4x4 view_matrix = cam.get_matrix().inverse();
    1.47  
    1.48 @@ -96,6 +109,8 @@
    1.49  	glMultTransposeMatrixf(view_matrix[0]);
    1.50  
    1.51  	draw_scene();
    1.52 +
    1.53 +	vr_end();
    1.54  }
    1.55  
    1.56  void game_reshape(int x, int y)
    1.57 @@ -103,6 +118,8 @@
    1.58  	glViewport(0, 0, x, y);
    1.59  	fb_width = x;
    1.60  	fb_height = y;
    1.61 +
    1.62 +	setup_rtarg(x, y);
    1.63  }
    1.64  
    1.65  void game_keyboard(int key, bool pressed, int x, int y)
    1.66 @@ -195,3 +212,39 @@
    1.67  
    1.68  	glPopMatrix();
    1.69  }
    1.70 +
    1.71 +static bool setup_rtarg(int x, int y)
    1.72 +{
    1.73 +	int tex_width = next_pow2(x);
    1.74 +	int tex_height = next_pow2(y);
    1.75 +
    1.76 +	/* create render targets for each eye */
    1.77 +	if(!fbo) {
    1.78 +		glGenFramebuffers(1, &fbo);
    1.79 +		glGenRenderbuffers(1, &rtarg_depth);
    1.80 +
    1.81 +		for(int i=0; i<2; i++) {
    1.82 +			rtarg[i] = new Texture;
    1.83 +		}
    1.84 +	}
    1.85 +
    1.86 +	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    1.87 +
    1.88 +	glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
    1.89 +	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
    1.90 +	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
    1.91 +
    1.92 +	for(int i=0; i<2; i++) {
    1.93 +		rtarg[i] = new Texture;
    1.94 +		rtarg[i]->create2d(tex_width, tex_height);
    1.95 +		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
    1.96 +				GL_TEXTURE_2D, rtarg[i]->get_texture_id(), 0);
    1.97 +	}
    1.98 +
    1.99 +	if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
   1.100 +		fprintf(stderr, "incomplete framebuffer!\n");
   1.101 +		return false;
   1.102 +	}
   1.103 +	glBindFramebuffer(GL_FRAMEBUFFER, 0);
   1.104 +	return true;
   1.105 +}