conworlds

diff src/game.cc @ 7:bd8202d6d28d

some progress...
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 16:55:16 +0300
parents 3c36bc28c6c2
children 90abf4b93cc9
line diff
     1.1 --- a/src/game.cc	Thu Aug 21 01:08:03 2014 +0300
     1.2 +++ b/src/game.cc	Fri Aug 22 16:55:16 2014 +0300
     1.3 @@ -4,11 +4,13 @@
     1.4  #include "texture.h"
     1.5  #include "vr/vr.h"
     1.6  
     1.7 +static void game_render_eye(int eye);
     1.8  static void draw_scene();
     1.9  static bool setup_rtarg(int x, int y);
    1.10  
    1.11 -static Texture *rtarg[2];
    1.12 +static Texture *rtarg;
    1.13  static unsigned int fbo, rtarg_depth;
    1.14 +static int rtwidth, rtheight;
    1.15  
    1.16  static const float move_speed = 10.0f;
    1.17  
    1.18 @@ -20,6 +22,7 @@
    1.19  bool game_init()
    1.20  {
    1.21  	vr_init();
    1.22 +	vr_use_module_named("null");
    1.23  
    1.24  	glEnable(GL_DEPTH_TEST);
    1.25  	glEnable(GL_CULL_FACE);
    1.26 @@ -45,8 +48,7 @@
    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 +		delete rtarg;
    1.33  	}
    1.34  }
    1.35  
    1.36 @@ -83,22 +85,39 @@
    1.37  	cam.input_rotate(0, 0, roll);
    1.38  }
    1.39  
    1.40 -void game_render(int eye)
    1.41 +void game_render()
    1.42  {
    1.43 -	vr_begin(eye <= 0 ? VR_EYE_LEFT : VR_EYE_RIGHT);
    1.44 +	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    1.45 +	glClearColor(1, 0, 0, 1);
    1.46 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.47  
    1.48 +	glViewport(0, 0, rtwidth / 2.0, rtheight);
    1.49 +	vr_begin(VR_EYE_LEFT);
    1.50 +	game_render_eye(-1);
    1.51 +	vr_end();
    1.52 +
    1.53 +	glViewport(rtwidth / 2, 0, rtwidth / 2.0, rtheight);
    1.54 +	vr_begin(VR_EYE_RIGHT);
    1.55 +	game_render_eye(1);
    1.56 +	vr_end();
    1.57 +
    1.58 +	glBindFramebuffer(GL_FRAMEBUFFER, 0);
    1.59 +	vr_output_texture(rtarg->get_texture_id(), 0, 0, (float)rtwidth / (float)rtarg->get_width(),
    1.60 +		(float)rtheight / (float)rtarg->get_height());
    1.61 +}
    1.62 +
    1.63 +static void game_render_eye(int eye)
    1.64 +{
    1.65  	float mat[16];
    1.66  	Matrix4x4 view_matrix = cam.get_matrix().inverse();
    1.67  
    1.68 -	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.69 -
    1.70  	glMatrixMode(GL_PROJECTION);
    1.71  	glLoadIdentity();
    1.72 -	if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, mat)) {
    1.73 +	//if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
    1.74  		gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
    1.75 -	} else {
    1.76 -		glLoadTransposeMatrixf(mat);
    1.77 -	}
    1.78 +	/*} else {
    1.79 +		glLoadMatrixf(mat);
    1.80 +	}*/
    1.81  
    1.82  	glMatrixMode(GL_MODELVIEW);
    1.83  	if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
    1.84 @@ -109,8 +128,6 @@
    1.85  	glMultTransposeMatrixf(view_matrix[0]);
    1.86  
    1.87  	draw_scene();
    1.88 -
    1.89 -	vr_end();
    1.90  }
    1.91  
    1.92  void game_reshape(int x, int y)
    1.93 @@ -119,7 +136,20 @@
    1.94  	fb_width = x;
    1.95  	fb_height = y;
    1.96  
    1.97 -	setup_rtarg(x, y);
    1.98 +	int lxres = vr_get_opti(VR_OPT_LEYE_XRES);
    1.99 +	if(lxres) {
   1.100 +		int lyres = vr_get_opti(VR_OPT_LEYE_YRES);
   1.101 +		int rxres = vr_get_opti(VR_OPT_REYE_XRES);
   1.102 +		int ryres = vr_get_opti(VR_OPT_REYE_YRES);
   1.103 +
   1.104 +		rtwidth = lxres + rxres;
   1.105 +		rtheight = lyres > ryres ? lyres : ryres;
   1.106 +	} else {
   1.107 +		rtwidth = x;
   1.108 +		rtheight = y;
   1.109 +	}
   1.110 +
   1.111 +	setup_rtarg(rtwidth, rtheight);
   1.112  }
   1.113  
   1.114  void game_keyboard(int key, bool pressed, int x, int y)
   1.115 @@ -222,10 +252,7 @@
   1.116  	if(!fbo) {
   1.117  		glGenFramebuffers(1, &fbo);
   1.118  		glGenRenderbuffers(1, &rtarg_depth);
   1.119 -
   1.120 -		for(int i=0; i<2; i++) {
   1.121 -			rtarg[i] = new Texture;
   1.122 -		}
   1.123 +		rtarg = new Texture;
   1.124  	}
   1.125  
   1.126  	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
   1.127 @@ -234,17 +261,16 @@
   1.128  	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
   1.129  	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
   1.130  
   1.131 -	for(int i=0; i<2; i++) {
   1.132 -		rtarg[i] = new Texture;
   1.133 -		rtarg[i]->create2d(tex_width, tex_height);
   1.134 -		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
   1.135 -				GL_TEXTURE_2D, rtarg[i]->get_texture_id(), 0);
   1.136 -	}
   1.137 +	rtarg->create2d(tex_width, tex_height);
   1.138 +	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
   1.139 +		rtarg->get_texture_id(), 0);
   1.140  
   1.141  	if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
   1.142  		fprintf(stderr, "incomplete framebuffer!\n");
   1.143  		return false;
   1.144  	}
   1.145  	glBindFramebuffer(GL_FRAMEBUFFER, 0);
   1.146 +
   1.147 +	printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
   1.148  	return true;
   1.149  }