oculus1
changeset 23:0c76f70fb7e9
merged
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 28 Sep 2013 04:13:33 +0300 |
parents | 251bc9e2e4a1 1b107de821c1 |
children | 8419d8a13cee |
files | src/main.cc |
diffstat | 4 files changed, 118 insertions(+), 24 deletions(-) [+] |
line diff
1.1 --- a/sdr/sdr.glsl Sat Sep 28 04:13:09 2013 +0300 1.2 +++ b/sdr/sdr.glsl Sat Sep 28 04:13:33 2013 +0300 1.3 @@ -2,6 +2,7 @@ 1.4 uniform float aspect, scale; 1.5 uniform float lens_center_offset; 1.6 uniform vec4 dist_factors; 1.7 +uniform vec2 tex_scale; 1.8 1.9 vec2 distort_texcoords(in vec2 tc); 1.10 float barrel_scale(float x, in vec4 k); 1.11 @@ -10,9 +11,9 @@ 1.12 { 1.13 vec2 tc = distort_texcoords(gl_TexCoord[0].xy); 1.14 1.15 - float vis = any(greaterThan(tc, vec2(1.0)) || lessThan(tc, vec2(0.0))) ? 0.0 : 1.0; 1.16 + float vis = any(greaterThan(tc, vec2(1.0))) || any(lessThan(tc, vec2(0.0))) ? 0.0 : 1.0; 1.17 1.18 - gl_FragColor.rgb = texture2D(tex, tc).rgb * vis; 1.19 + gl_FragColor.rgb = texture2D(tex, tc * tex_scale).rgb * vis; 1.20 gl_FragColor.a = 1.0; 1.21 } 1.22
2.1 --- a/src/main.cc Sat Sep 28 04:13:09 2013 +0300 2.2 +++ b/src/main.cc Sat Sep 28 04:13:33 2013 +0300 2.3 @@ -7,6 +7,12 @@ 2.4 #include "camera.h" 2.5 #include "sdr.h" 2.6 2.7 +#ifdef __APPLE__ 2.8 +#include <OpenGL/OpenGL.h> 2.9 +#include <OpenGL/CGLCurrent.h> 2.10 +#include <ApplicationServices/ApplicationServices.h> 2.11 +#endif 2.12 + 2.13 static bool init(); 2.14 static void cleanup(); 2.15 static void disp(); 2.16 @@ -26,6 +32,8 @@ 2.17 static void passive(int x, int y); 2.18 static void sball_rotate(int rx, int ry, int rz); 2.19 static bool parse_args(int argc, char **argv); 2.20 +static void printfps(); 2.21 +static int next_pow2(int x); 2.22 2.23 static VRFpsCamera cam; 2.24 static int width, height; 2.25 @@ -33,13 +41,15 @@ 2.26 2.27 static bool keystate[256]; 2.28 2.29 -static int rtarg_width, rtarg_height; 2.30 +static int rtarg_width, rtarg_height, rtarg_tex_width, rtarg_tex_height; 2.31 +static float tex_scale_x, tex_scale_y; 2.32 static unsigned int fbo, tex[2], zbuf; 2.33 2.34 static unsigned int teapot_sdr; 2.35 2.36 static bool fullscreen_pending; 2.37 2.38 + 2.39 int main(int argc, char **argv) 2.40 { 2.41 glutInitWindowSize(1280, 800); 2.42 @@ -102,7 +112,12 @@ 2.43 rtarg_width = (xsz + xsz / 2) / 2; 2.44 rtarg_height = ysz + ysz / 2; 2.45 2.46 - printf("render target: %dx%d\n", rtarg_width, rtarg_height); 2.47 + rtarg_tex_width = next_pow2(rtarg_width); 2.48 + rtarg_tex_height = next_pow2(rtarg_height); 2.49 + tex_scale_x = (float)rtarg_width / (float)rtarg_tex_width; 2.50 + tex_scale_y = (float)rtarg_height / (float)rtarg_tex_height; 2.51 + 2.52 + printf("render target: %dx%d (%dx%d)\n", rtarg_width, rtarg_height, rtarg_tex_width, rtarg_tex_height); 2.53 2.54 // create render targets for each eye 2.55 GLenum wrap_mode = GL_CLAMP_TO_EDGE; 2.56 @@ -117,31 +132,43 @@ 2.57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 2.58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_mode); 2.59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_mode); 2.60 - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, rtarg_width, rtarg_height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); 2.61 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, rtarg_tex_width, rtarg_tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); 2.62 } 2.63 2.64 // create the depth render buffer 2.65 - glGenRenderbuffers(1, &zbuf); 2.66 - glBindRenderbuffer(GL_RENDERBUFFER, zbuf); 2.67 - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, rtarg_width, rtarg_height); 2.68 + glGenRenderbuffersEXT(1, &zbuf); 2.69 + glBindRenderbufferEXT(GL_RENDERBUFFER, zbuf); 2.70 + glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, rtarg_tex_width, rtarg_tex_height); 2.71 2.72 // create the FBO 2.73 - glGenFramebuffers(1, &fbo); 2.74 - glBindFramebuffer(GL_FRAMEBUFFER, fbo); 2.75 - glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[0], 0); 2.76 - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, zbuf); 2.77 + glGenFramebuffersEXT(1, &fbo); 2.78 + glBindFramebufferEXT(GL_FRAMEBUFFER, fbo); 2.79 + glFramebufferTextureEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[0], 0); 2.80 + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, zbuf); 2.81 } 2.82 2.83 teapot_sdr = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl"); 2.84 2.85 + // enable vsync 2.86 +#ifdef __APPLE__ 2.87 + int swapint = 1; 2.88 + CGLContextObj ctx = CGLGetCurrentContext(); 2.89 + CGLSetParameter(ctx, kCGLCPSwapInterval, &swapint); 2.90 +#endif 2.91 + 2.92 +#ifdef __APPLE__ 2.93 + // this is required for glutWarpPointer to work correctly, otherwise after every 2.94 + // warp, mac os x freezes event processing for 1/4 second. 2.95 + CGSetLocalEventsSuppressionInterval(0.0); 2.96 +#endif 2.97 return true; 2.98 } 2.99 2.100 static void cleanup() 2.101 { 2.102 glDeleteTextures(2, tex); 2.103 - glDeleteRenderbuffers(1, &zbuf); 2.104 - glDeleteFramebuffers(1, &fbo); 2.105 + glDeleteRenderbuffersEXT(1, &zbuf); 2.106 + glDeleteFramebuffersEXT(1, &fbo); 2.107 vr_shutdown(); 2.108 } 2.109 2.110 @@ -189,6 +216,8 @@ 2.111 2.112 glutSwapBuffers(); 2.113 assert(glGetError() == GL_NO_ERROR); 2.114 + 2.115 + printfps(); 2.116 } 2.117 2.118 // display function used in VR mode 2.119 @@ -211,8 +240,8 @@ 2.120 glClearColor(0.1, 0.1, 0.1, 1.0); 2.121 2.122 // draw left view 2.123 - glBindFramebuffer(GL_FRAMEBUFFER, fbo); 2.124 - glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[0], 0); 2.125 + glBindFramebufferEXT(GL_FRAMEBUFFER, fbo); 2.126 + glFramebufferTextureEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[0], 0); 2.127 2.128 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 2.129 2.130 @@ -228,8 +257,8 @@ 2.131 2.132 2.133 // draw right view 2.134 - glBindFramebuffer(GL_FRAMEBUFFER, fbo); 2.135 - glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[1], 0); 2.136 + glBindFramebufferEXT(GL_FRAMEBUFFER, fbo); 2.137 + glFramebufferTextureEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[1], 0); 2.138 2.139 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 2.140 2.141 @@ -250,13 +279,15 @@ 2.142 glClearColor(0, 0, 0, 0); 2.143 glClear(GL_COLOR_BUFFER_BIT); 2.144 2.145 - vr_draw_eye(tex[0], VR_EYE_LEFT); 2.146 - vr_draw_eye(tex[1], VR_EYE_RIGHT); 2.147 + vr_draw_eye(VR_EYE_LEFT, tex[0], tex_scale_x, tex_scale_y); 2.148 + vr_draw_eye(VR_EYE_RIGHT, tex[1], tex_scale_x, tex_scale_y); 2.149 2.150 glutSwapBuffers(); 2.151 assert(glGetError() == GL_NO_ERROR); 2.152 2.153 glFinish(); 2.154 + 2.155 + printfps(); 2.156 } 2.157 2.158 static void draw_scene() 2.159 @@ -473,6 +504,21 @@ 2.160 if(!use_vr) { 2.161 rtarg_width = width; 2.162 rtarg_height = height; 2.163 + 2.164 + int new_tex_width = next_pow2(width); 2.165 + int new_tex_height = next_pow2(height); 2.166 + 2.167 + if(new_tex_width != rtarg_tex_width || new_tex_height != rtarg_tex_width) { 2.168 + // TODO 2.169 + exit(1); 2.170 + } 2.171 + 2.172 + rtarg_tex_width = new_tex_width; 2.173 + rtarg_tex_height = new_tex_height; 2.174 + 2.175 + tex_scale_x = (float)rtarg_width / (float)rtarg_tex_width; 2.176 + tex_scale_y = (float)rtarg_height / (float)rtarg_tex_height; 2.177 + 2.178 } 2.179 } 2.180 2.181 @@ -526,7 +572,7 @@ 2.182 int center_y = height / 2; 2.183 2.184 int dx = x - center_x; 2.185 - int dy = y - center_y; 2.186 + int dy = use_vr ? 0 : y - center_y; 2.187 2.188 if(use_vr && fullscreen) { 2.189 dy = 0; 2.190 @@ -541,8 +587,8 @@ 2.191 2.192 cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0); 2.193 2.194 + glutWarpPointer(center_x, center_y); 2.195 glutPostRedisplay(); 2.196 - glutWarpPointer(center_x, center_y); 2.197 } 2.198 2.199 static void sball_rotate(int rx, int ry, int rz) 2.200 @@ -568,3 +614,33 @@ 2.201 } 2.202 return true; 2.203 } 2.204 + 2.205 +static void printfps() 2.206 +{ 2.207 + static unsigned int last_upd, frames; 2.208 + unsigned int msec = glutGet(GLUT_ELAPSED_TIME); 2.209 + unsigned int dmsec = msec - last_upd; 2.210 + 2.211 + frames++; 2.212 + 2.213 + if(dmsec > 2000) { 2.214 + float dt = (float)dmsec / 1000.0f; 2.215 + 2.216 + printf("fps: %.2f \r", (float)frames / dt); 2.217 + fflush(stdout); 2.218 + 2.219 + frames = 0; 2.220 + last_upd = msec; 2.221 + } 2.222 +} 2.223 + 2.224 +static int next_pow2(int x) 2.225 +{ 2.226 + x--; 2.227 + x = (x >> 1) | x; 2.228 + x = (x >> 2) | x; 2.229 + x = (x >> 4) | x; 2.230 + x = (x >> 8) | x; 2.231 + x = (x >> 16) | x; 2.232 + return x + 1; 2.233 +}
3.1 --- a/src/vr.cc Sat Sep 28 04:13:09 2013 +0300 3.2 +++ b/src/vr.cc Sat Sep 28 04:13:33 2013 +0300 3.3 @@ -4,6 +4,7 @@ 3.4 #include "vr_impl.h" 3.5 #include "vr_sdr.h" 3.6 3.7 +static void init_ctx(); 3.8 static bool init_ovr(); 3.9 static bool init_sdr(); 3.10 3.11 @@ -14,6 +15,8 @@ 3.12 { 3.13 glewInit(); 3.14 3.15 + init_ctx(); 3.16 + 3.17 if(!init_ovr()) { 3.18 return -1; 3.19 } 3.20 @@ -31,6 +34,16 @@ 3.21 //System::Destroy(); 3.22 } 3.23 3.24 +static void init_ctx() 3.25 +{ 3.26 + vr_ctx.info.width = 1280; 3.27 + vr_ctx.info.height = 800; 3.28 + vr_ctx.info.fov = M_PI / 2.0; 3.29 + vr_ctx.info.aspect = (float)vr_ctx.info.width / (float)vr_ctx.info.height; 3.30 + vr_ctx.info.ipd = 0.035; 3.31 + vr_ctx.info.scale = 1.0; 3.32 +} 3.33 + 3.34 static bool init_ovr() 3.35 { 3.36 LogMaskConstants log_level = LogMask_All; 3.37 @@ -312,7 +325,7 @@ 3.38 oq.GetEulerAngles<Axis_Y, Axis_X, Axis_Z>(euler + 1, euler, euler + 2); 3.39 } 3.40 3.41 -extern "C" void vr_draw_eye(unsigned int tex, int eye) 3.42 +extern "C" void vr_draw_eye(int eye, unsigned int tex, float tex_scale_x, float tex_scale_y) 3.43 { 3.44 static const float rects[3][4] = { 3.45 {-1, -1, 1, 1}, 3.46 @@ -342,6 +355,10 @@ 3.47 float offset = vr_ctx.info.lens_center_offset * offs_scale[eye]; 3.48 glUniform1f(loc, offset); 3.49 } 3.50 + 3.51 + if((loc = glGetUniformLocation(sdrprog, "tex_scale")) != -1) { 3.52 + glUniform2f(loc, tex_scale_x, tex_scale_y); 3.53 + } 3.54 } 3.55 3.56 glBindTexture(GL_TEXTURE_2D, tex);
4.1 --- a/src/vr.h Sat Sep 28 04:13:09 2013 +0300 4.2 +++ b/src/vr.h Sat Sep 28 04:13:33 2013 +0300 4.3 @@ -54,7 +54,7 @@ 4.4 void vr_get_rotation_euler(float *euler); 4.5 4.6 /* OpenGL stuff */ 4.7 -void vr_draw_eye(unsigned int tex, int eye); 4.8 +void vr_draw_eye(int eye, unsigned int tex, float tex_scale_x, float tex_scale_y); 4.9 4.10 #ifdef __cplusplus 4.11 }