vrseasons

view 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 source
1 #include <assert.h>
2 #include "opengl.h"
3 #include "game.h"
4 #include <stdio.h>
5 #include <goatvr.h>
6 #include "main.h"
7 #include "scene.h"
8 #include "timer.h"
9 #include "rtarg.h"
10 #include "opt.h"
12 static bool vr_mode = true;
13 static int win_width, win_height;
14 static RenderTarget vr_rtarg;
16 bool game_init(int argc, char **argv)
17 {
18 if(!parse_args(argc, argv)) {
19 return false;
20 }
22 init_gl();
24 if(vr_mode) {
25 if(vr_init() == -1) {
26 return false;
27 }
29 win_width = vr_geti_def(VR_DISPLAY_WIDTH, 1280);
30 win_height = vr_geti_def(VR_DISPLAY_HEIGHT, 800);
31 resize_window(win_width, win_height);
33 int rt_width = vr_geti_def(VR_RENDER_XRES, win_width);
34 int rt_height = vr_geti_def(VR_RENDER_YRES, win_height);
35 vr_rtarg.create(rt_width, rt_height);
36 }
38 return true;
39 }
41 void game_shutdown()
42 {
43 vr_shutdown();
44 }
46 void game_draw()
47 {
48 static unsigned long prev_msec;
49 unsigned long msec = get_msec();
50 float dt = (float)(msec - prev_msec) / 1000.0;
52 update_scene(msec, dt);
54 if(vr_mode) {
55 set_render_target(&vr_rtarg);
57 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
59 for(int i=0; i<2; i++) {
60 vr_begin(i);
62 glViewport(i == VR_EYE_LEFT ? 0 : vr_rtarg.get_width() / 2, 0, vr_rtarg.get_width() / 2, vr_rtarg.get_height());
64 glMatrixMode(GL_PROJECTION);
65 float proj[16];
66 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
67 glLoadMatrixf(proj);
68 } else {
69 glLoadIdentity();
70 gluPerspective(50.0, (float)win_width / 2.0 / (float)win_height, 0.5, 500.0);
71 }
73 float view[16];
74 vr_view_matrix(i, view);
76 glMatrixMode(GL_MODELVIEW);
77 glLoadMatrixf(view);
78 glTranslatef(0, -vr_getf(VR_EYE_HEIGHT), 0);
80 draw_scene();
82 vr_end();
83 }
85 set_render_target(0);
86 glViewport(0, 0, win_width, win_height);
88 vr_swap_buffers();
90 } else {
91 glMatrixMode(GL_MODELVIEW);
92 glLoadIdentity();
94 draw_scene();
96 swap_buffers();
97 }
99 assert(glGetError() == GL_NO_ERROR);
100 }
102 void game_reshape(int x, int y)
103 {
104 glViewport(0, 0, x, y);
106 if(vr_mode) {
107 vr_rtarg.resize(x, y);
109 float umax = (float)vr_rtarg.get_width() / (float)vr_rtarg.get_tex_width();
110 float vmax = (float)vr_rtarg.get_height() / (float)vr_rtarg.get_tex_height();
111 vr_output_texture(vr_rtarg.get_texture(), 0, 0, umax, vmax);
112 }
113 }
115 void game_keyboard(int key, bool press)
116 {
117 if(press) {
118 switch(key) {
119 case 27:
120 quit();
122 case ' ':
123 if(vr_mode) {
124 vr_recenter();
125 }
126 break;
127 }
128 }
129 }
131 void game_mbutton(int bn, bool press, int x, int y)
132 {
133 }
135 void game_mmotion(int x, int y)
136 {
137 }