vrheights

view src/game.cc @ 2:b49461618f61

starting point
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 25 Sep 2014 11:44:45 +0300
parents d06e4e24f922
children 316ec8250af2
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include <algorithm>
4 #include "opengl.h"
5 #include "game.h"
6 #include "goatvr.h"
8 static void draw_scene();
9 static void toggle_hmd_fullscr();
10 static void create_rtarg(int x, int y);
11 static int next_pow2(int x);
13 static int win_width, win_height;
14 static unsigned int fb_tex;
15 static unsigned int fbo, fb_depth;
16 static int fb_xsz, fb_ysz;
17 static int fb_tex_xsz, fb_tex_ysz;
19 bool game_init()
20 {
21 init_opengl();
23 if(vr_init() == -1) {
24 return false;
25 }
27 glEnable(GL_DEPTH_TEST);
28 glEnable(GL_CULL_FACE);
30 return true;
31 }
33 void game_cleanup()
34 {
35 vr_shutdown();
36 }
38 void game_update(long tm)
39 {
40 }
42 void game_display()
43 {
44 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
45 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
47 for(int i=0; i<2; i++) {
48 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
49 vr_begin(i);
51 float proj[16];
52 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
53 glMatrixMode(GL_PROJECTION);
54 glLoadMatrixf(proj);
55 }
57 glMatrixMode(GL_MODELVIEW);
59 float view[16];
60 vr_view_matrix(i, view);
61 glLoadMatrixf(view);
62 /* move the camera to the eye level of the user */
63 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
65 draw_scene();
67 vr_end();
68 }
70 glBindFramebuffer(GL_FRAMEBUFFER, 0);
71 glViewport(0, 0, win_width, win_height);
73 vr_swap_buffers();
74 assert(glGetError() == GL_NO_ERROR);
75 }
77 void game_reshape(int x, int y)
78 {
79 win_width = x;
80 win_height = y;
82 create_rtarg(vr_geti_def(VR_RENDER_XRES, x), vr_geti_def(VR_RENDER_YRES, y));
83 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
85 /* these might be overriden in VR mode (see game_display) */
86 glViewport(0, 0, x, y);
87 glMatrixMode(GL_PROJECTION);
88 glLoadIdentity();
89 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
90 }
92 void game_keyboard(int key, bool pressed)
93 {
94 if(pressed) {
95 switch(key) {
96 case 27:
97 exit_game();
98 break;
100 case 'f':
101 toggle_hmd_fullscr();
102 break;
104 case 'r':
105 vr_recenter();
106 break;
108 default:
109 break;
110 }
111 }
112 }
114 void game_mouse_button(int bn, bool state, int x, int y)
115 {
116 }
118 void game_mouse_motion(int x, int y)
119 {
120 }
122 static void draw_scene()
123 {
124 }
126 static void toggle_hmd_fullscr()
127 {
128 static bool fullscr;
129 static int prev_x, prev_y;
130 //static int prev_xsz, prev_ysz;
132 fullscr = !fullscr;
133 if(fullscr) {
134 /* entering fullscreen on the HMD */
135 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
136 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
137 if(xoffs != -1) {
138 get_window_pos(&prev_x, &prev_y);
139 move_window(xoffs, yoffs);
140 }
142 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
143 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
144 if(xsz != -1) {
145 //prev_xsz = win_width;
146 //prev_ysz = win_height;
147 resize_window(xsz, ysz);
148 }
149 enter_fullscreen();
151 } else {
152 /* leaving fullscreen */
153 leave_fullscreen();
154 /*move_window(prev_x, prev_y);
155 resize_window(prev_xsz, prev_ysz);*/
156 }
157 }
159 static void create_rtarg(int x, int y)
160 {
161 if(x == fb_xsz && y == fb_ysz) {
162 return; // nothing changed
163 }
165 fb_xsz = x;
166 fb_ysz = y;
167 fb_tex_xsz = next_pow2(fb_xsz);
168 fb_tex_ysz = next_pow2(fb_ysz);
170 if(!fbo) {
171 glGenFramebuffers(1, &fbo);
173 glGenTextures(1, &fb_tex);
174 glBindTexture(GL_TEXTURE_2D, fb_tex);
175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
176 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
178 glGenRenderbuffers(1, &fb_depth);
179 }
181 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
183 glBindTexture(GL_TEXTURE_2D, fb_tex);
184 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
185 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
187 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
188 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
189 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
191 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
192 glBindFramebuffer(GL_FRAMEBUFFER, 0);
193 }
195 static int next_pow2(int x)
196 {
197 x -= 1;
198 x |= x >> 1;
199 x |= x >> 2;
200 x |= x >> 4;
201 x |= x >> 8;
202 x |= x >> 16;
203 return x + 1;
204 }