vrseasons

diff src/game.cc @ 0:393ef1143c9c

VR seasons
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 07 Apr 2015 11:16:56 +0300
parents
children 65c2e37c48b2
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/game.cc	Tue Apr 07 11:16:56 2015 +0300
     1.3 @@ -0,0 +1,137 @@
     1.4 +#include <assert.h>
     1.5 +#include "opengl.h"
     1.6 +#include "game.h"
     1.7 +#include <stdio.h>
     1.8 +#include <goatvr.h>
     1.9 +#include "main.h"
    1.10 +#include "scene.h"
    1.11 +#include "timer.h"
    1.12 +#include "rtarg.h"
    1.13 +#include "opt.h"
    1.14 +
    1.15 +static bool vr_mode = true;
    1.16 +static int win_width, win_height;
    1.17 +static RenderTarget vr_rtarg;
    1.18 +
    1.19 +bool game_init(int argc, char **argv)
    1.20 +{
    1.21 +	if(!parse_args(argc, argv)) {
    1.22 +		return false;
    1.23 +	}
    1.24 +
    1.25 +	init_gl();
    1.26 +
    1.27 +	if(vr_mode) {
    1.28 +		if(vr_init() == -1) {
    1.29 +			return false;
    1.30 +		}
    1.31 +
    1.32 +		win_width = vr_geti_def(VR_DISPLAY_WIDTH, 1280);
    1.33 +		win_height = vr_geti_def(VR_DISPLAY_HEIGHT, 800);
    1.34 +		resize_window(win_width, win_height);
    1.35 +
    1.36 +		int rt_width = vr_geti_def(VR_RENDER_XRES, win_width);
    1.37 +		int rt_height = vr_geti_def(VR_RENDER_YRES, win_height);
    1.38 +		vr_rtarg.create(rt_width, rt_height);
    1.39 +	}
    1.40 +
    1.41 +	return true;
    1.42 +}
    1.43 +
    1.44 +void game_shutdown()
    1.45 +{
    1.46 +	vr_shutdown();
    1.47 +}
    1.48 +
    1.49 +void game_draw()
    1.50 +{
    1.51 +	static unsigned long prev_msec;
    1.52 +	unsigned long msec = get_msec();
    1.53 +	float dt = (float)(msec - prev_msec) / 1000.0;
    1.54 +
    1.55 +	update_scene(msec, dt);
    1.56 +
    1.57 +	if(vr_mode) {
    1.58 +		set_render_target(&vr_rtarg);
    1.59 +
    1.60 +		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.61 +
    1.62 +		for(int i=0; i<2; i++) {
    1.63 +			vr_begin(i);
    1.64 +
    1.65 +			glViewport(i == VR_EYE_LEFT ? 0 : vr_rtarg.get_width() / 2, 0, vr_rtarg.get_width() / 2, vr_rtarg.get_height());
    1.66 +
    1.67 +			glMatrixMode(GL_PROJECTION);
    1.68 +			float proj[16];
    1.69 +			if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
    1.70 +				glLoadMatrixf(proj);
    1.71 +			} else {
    1.72 +				glLoadIdentity();
    1.73 +				gluPerspective(50.0, (float)win_width / 2.0 / (float)win_height, 0.5, 500.0);
    1.74 +			}
    1.75 +
    1.76 +			float view[16];
    1.77 +			vr_view_matrix(i, view);
    1.78 +
    1.79 +			glMatrixMode(GL_MODELVIEW);
    1.80 +			glLoadMatrixf(view);
    1.81 +			glTranslatef(0, -vr_getf(VR_EYE_HEIGHT), 0);
    1.82 +
    1.83 +			draw_scene();
    1.84 +
    1.85 +			vr_end();
    1.86 +		}
    1.87 +
    1.88 +		set_render_target(0);
    1.89 +		glViewport(0, 0, win_width, win_height);
    1.90 +
    1.91 +		vr_swap_buffers();
    1.92 +
    1.93 +	} else {
    1.94 +		glMatrixMode(GL_MODELVIEW);
    1.95 +		glLoadIdentity();
    1.96 +
    1.97 +		draw_scene();
    1.98 +
    1.99 +		swap_buffers();
   1.100 +	}
   1.101 +
   1.102 +	assert(glGetError() == GL_NO_ERROR);
   1.103 +}
   1.104 +
   1.105 +void game_reshape(int x, int y)
   1.106 +{
   1.107 +	glViewport(0, 0, x, y);
   1.108 +
   1.109 +	if(vr_mode) {
   1.110 +		vr_rtarg.resize(x, y);
   1.111 +
   1.112 +		float umax = (float)vr_rtarg.get_width() / (float)vr_rtarg.get_tex_width();
   1.113 +		float vmax = (float)vr_rtarg.get_height() / (float)vr_rtarg.get_tex_height();
   1.114 +		vr_output_texture(vr_rtarg.get_texture(), 0, 0, umax, vmax);
   1.115 +	}
   1.116 +}
   1.117 +
   1.118 +void game_keyboard(int key, bool press)
   1.119 +{
   1.120 +	if(press) {
   1.121 +		switch(key) {
   1.122 +		case 27:
   1.123 +			quit();
   1.124 +
   1.125 +		case ' ':
   1.126 +			if(vr_mode) {
   1.127 +				vr_recenter();
   1.128 +			}
   1.129 +			break;
   1.130 +		}
   1.131 +	}
   1.132 +}
   1.133 +
   1.134 +void game_mbutton(int bn, bool press, int x, int y)
   1.135 +{
   1.136 +}
   1.137 +
   1.138 +void game_mmotion(int x, int y)
   1.139 +{
   1.140 +}