vrheights

view src/game.cc @ 3:316ec8250af2

added the teapot code for testing
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 25 Sep 2014 17:13:23 +0300
parents b49461618f61
children 690ef7fa791f
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"
7 #include "teapot.h"
9 static void draw_scene();
10 static void toggle_hmd_fullscr();
11 static void create_rtarg(int x, int y);
12 static int next_pow2(int x);
14 static int win_width, win_height;
15 static unsigned int fb_tex;
16 static unsigned int fbo, fb_depth;
17 static int fb_xsz, fb_ysz;
18 static int fb_tex_xsz, fb_tex_ysz;
20 bool game_init()
21 {
22 init_opengl();
24 if(vr_init() == -1) {
25 return false;
26 }
28 glEnable(GL_DEPTH_TEST);
29 glEnable(GL_CULL_FACE);
30 glEnable(GL_LIGHTING);
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 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
47 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
49 for(int i=0; i<2; i++) {
50 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
51 vr_begin(i);
53 float proj[16];
54 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
55 glMatrixMode(GL_PROJECTION);
56 glLoadMatrixf(proj);
57 }
59 glMatrixMode(GL_MODELVIEW);
61 float view[16];
62 vr_view_matrix(i, view);
63 glLoadMatrixf(view);
64 /* move the camera to the eye level of the user */
65 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
67 draw_scene();
69 vr_end();
70 }
72 glBindFramebuffer(GL_FRAMEBUFFER, 0);
73 glViewport(0, 0, win_width, win_height);
75 vr_swap_buffers();
76 assert(glGetError() == GL_NO_ERROR);
77 }
79 void game_reshape(int x, int y)
80 {
81 win_width = x;
82 win_height = y;
84 create_rtarg(vr_geti_def(VR_RENDER_XRES, x), vr_geti_def(VR_RENDER_YRES, y));
85 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
87 /* these might be overriden in VR mode (see game_display) */
88 glViewport(0, 0, x, y);
89 glMatrixMode(GL_PROJECTION);
90 glLoadIdentity();
91 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
92 }
94 void game_keyboard(int key, bool pressed)
95 {
96 if(pressed) {
97 switch(key) {
98 case 27:
99 exit_game();
100 break;
102 case 'f':
103 toggle_hmd_fullscr();
104 break;
106 case 'r':
107 vr_recenter();
108 break;
110 default:
111 break;
112 }
113 }
114 }
116 void game_mouse_button(int bn, bool state, int x, int y)
117 {
118 }
120 void game_mouse_motion(int x, int y)
121 {
122 }
124 static void material(float r, float g, float b, float roughness)
125 {
126 float gloss = 1.0 - roughness;
127 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
128 float specular[] = {gloss, gloss, gloss, 1.0};
129 float shin = gloss * 128.0;
131 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
132 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
133 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
134 }
136 static void draw_scene()
137 {
138 float lpos[][4] = {
139 {-0.7, 0.7, 1, 0},
140 {1, 0, 1, 0}
141 };
142 float lcol[][4] = {
143 {0.9, 0.7, 0.6, 1},
144 {0.3, 0.4, 0.75, 1}
145 };
146 for(int i=0; i<2; i++) {
147 glEnable(GL_LIGHT0 + i);
148 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
149 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
150 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
151 }
153 glMatrixMode(GL_MODELVIEW);
155 material(1, 1, 1, 1);
156 glBegin(GL_QUADS);
157 glNormal3f(0, 1, 0);
158 glVertex3f(-10, 0, 10);
159 glVertex3f(10, 0, 10);
160 glVertex3f(10, 0, -10);
161 glVertex3f(-10, 0, -10);
162 glEnd();
164 material(1, 1, 1, 0.4);
165 glPushMatrix();
166 glTranslatef(0, 1.3, -10);
167 glFrontFace(GL_CW);
168 bezier_teapot(2.0);
169 glFrontFace(GL_CCW);
170 glPopMatrix();
171 }
173 static void toggle_hmd_fullscr()
174 {
175 static bool fullscr;
176 static int prev_x, prev_y;
177 //static int prev_xsz, prev_ysz;
179 fullscr = !fullscr;
180 if(fullscr) {
181 /* entering fullscreen on the HMD */
182 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
183 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
184 if(xoffs != -1) {
185 get_window_pos(&prev_x, &prev_y);
186 move_window(xoffs, yoffs);
187 }
189 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
190 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
191 if(xsz != -1) {
192 //prev_xsz = win_width;
193 //prev_ysz = win_height;
194 resize_window(xsz, ysz);
195 }
196 enter_fullscreen();
198 } else {
199 /* leaving fullscreen */
200 leave_fullscreen();
201 /*move_window(prev_x, prev_y);
202 resize_window(prev_xsz, prev_ysz);*/
203 }
204 }
206 static void create_rtarg(int x, int y)
207 {
208 if(x == fb_xsz && y == fb_ysz) {
209 return; // nothing changed
210 }
212 fb_xsz = x;
213 fb_ysz = y;
214 fb_tex_xsz = next_pow2(fb_xsz);
215 fb_tex_ysz = next_pow2(fb_ysz);
217 if(!fbo) {
218 glGenFramebuffers(1, &fbo);
220 glGenTextures(1, &fb_tex);
221 glBindTexture(GL_TEXTURE_2D, fb_tex);
222 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
223 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
225 glGenRenderbuffers(1, &fb_depth);
226 }
228 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
230 glBindTexture(GL_TEXTURE_2D, fb_tex);
231 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
232 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
234 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
235 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
236 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
238 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
239 glBindFramebuffer(GL_FRAMEBUFFER, 0);
240 }
242 static int next_pow2(int x)
243 {
244 x -= 1;
245 x |= x >> 1;
246 x |= x >> 2;
247 x |= x >> 4;
248 x |= x >> 8;
249 x |= x >> 16;
250 return x + 1;
251 }