vrheights

view src/game.cc @ 1:d06e4e24f922

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 24 Sep 2014 11:34:07 +0300
parents ccbd444939a1
children b49461618f61
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 create_rtarg(int x, int y);
9 static int next_pow2(int x);
11 static int win_width, win_height;
12 static unsigned int fb_tex;
13 static unsigned int fbo, fb_depth;
14 static int fb_xsz, fb_ysz;
15 static int fb_tex_xsz, fb_tex_ysz;
17 bool game_init()
18 {
19 init_opengl();
21 if(vr_init() == -1) {
22 return false;
23 }
25 glEnable(GL_DEPTH_TEST);
26 glEnable(GL_CULL_FACE);
28 int sdk_fb_xsz = vr_getf(VR_LEYE_XRES) + vr_getf(VR_REYE_XRES);
29 int sdk_fb_ysz = std::max(vr_getf(VR_LEYE_YRES), vr_getf(VR_REYE_YRES));
30 create_rtarg(sdk_fb_xsz, sdk_fb_ysz);
32 return true;
33 }
35 void game_cleanup()
36 {
37 vr_shutdown();
38 }
40 void game_update(long tm)
41 {
42 }
44 void game_display()
45 {
46 }
48 void game_reshape(int x, int y)
49 {
50 }
52 void game_keyboard(int key, bool pressed);
53 void game_mouse_button(int bn, bool state, int x, int y);
54 void game_mouse_motion(int x, int y);
57 static void create_rtarg(int x, int y)
58 {
59 if(x == fb_xsz && y == fb_ysz) {
60 return; // nothing changed
61 }
63 fb_xsz = x;
64 fb_ysz = y;
65 fb_tex_xsz = next_pow2(fb_xsz);
66 fb_tex_ysz = next_pow2(fb_ysz);
68 if(!fbo) {
69 glGenFramebuffers(1, &fbo);
71 glGenTextures(1, &fb_tex);
72 glBindTexture(GL_TEXTURE_2D, fb_tex);
73 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
76 glGenRenderbuffers(1, &fb_depth);
77 }
79 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
81 glBindTexture(GL_TEXTURE_2D, fb_tex);
82 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
83 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
85 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
86 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
88 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
89 glBindFramebuffer(GL_FRAMEBUFFER, 0);
90 }
92 static int next_pow2(int x)
93 {
94 x -= 1;
95 x |= x >> 1;
96 x |= x >> 2;
97 x |= x >> 4;
98 x |= x >> 8;
99 x |= x >> 16;
100 return x + 1;
101 }