oculus1

view src/vr.cc @ 12:d797639e0234

moving on to the distortion... not correct yet
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Sep 2013 10:14:29 +0300
parents 3265970a7315
children 464e1d135d68
line source
1 #include <stdio.h>
2 #include <GL/glew.h>
3 #include "vr.h"
4 #include "vr_impl.h"
6 static const char *sdr_src =
7 "uniform sampler2D tex;\n"
8 "uniform vec2 lens_center;\n"
9 "uniform vec2 scr_center;\n"
10 "uniform vec2 scale;\n"
11 "uniform vec2 scale_in;\n"
12 "uniform vec4 warp_param;\n"
13 "\n"
14 "vec2 warp(in vec2 pt)\n"
15 "{\n"
16 " vec2 theta = (pt - lens_center) * scale_in;\n"
17 " float rsq = length(theta);\n"
18 " vec2 rvec = theta * (warp_param.x + warp_param.y * rsq +\n"
19 " warp_param.z * rsq * rsq +\n"
20 " warp_param.w * rsq * rsq * rsq);\n"
21 " return lens_center + scale * rvec;\n"
22 "}\n"
23 "\n"
24 "void main()\n"
25 "{\n"
26 " vec2 tc = warp(gl_TexCoord[0].xy);\n"
27 " gl_FragColor.rgb = texture2D(tex, tc).rgb;\n"
28 " gl_FragColor.a = 1.0;\n"
29 "}\n";
31 static bool init_ovr();
32 static bool init_sdr();
34 VRContext vr_ctx;
35 static unsigned int sdrprog;
37 extern "C" int vr_init(enum vr_init_mode mode)
38 {
39 glewInit();
41 if(!init_ovr()) {
42 return -1;
43 }
45 if(!init_sdr()) {
46 return -1;
47 }
49 return 0;
50 }
52 extern "C" void vr_shutdown(void)
53 {
54 //System::Destroy();
55 }
57 static bool init_ovr()
58 {
59 LogMaskConstants log_level = LogMask_All;
60 // initialize Oculus SDK
61 const char *logenv = getenv("VR_LOGLEVEL");
62 if(logenv) {
63 switch(atoi(logenv)) {
64 case 0:
65 log_level = LogMask_None;
66 break;
67 case 1:
68 log_level = LogMask_Regular;
69 break;
70 case 2:
71 default:
72 log_level = LogMask_All;
73 break;
74 }
75 }
77 System::Init(Log::ConfigureDefaultLog(log_level));
78 if(!(vr_ctx.ovr_devman = DeviceManager::Create())) {
79 fprintf(stderr, "failed to create OVR device manager\n");
80 return false;
81 }
83 // create the display device
84 HMDInfo info;
85 if(!(vr_ctx.ovr_hmd_dev = vr_ctx.ovr_devman->EnumerateDevices<HMDDevice>().CreateDevice())) {
86 fprintf(stderr, "no oculus rift devices found\n");
87 } else {
88 if(vr_ctx.ovr_hmd_dev->GetDeviceInfo(&info)) {
89 printf("oculus device info:\n");
90 printf(" name: %s\n", info.DisplayDeviceName);
91 printf(" ipd: %f\n", info.InterpupillaryDistance);
92 printf(" distortion: %f %f %f %f\n", info.DistortionK[0],
93 info.DistortionK[1], info.DistortionK[2], info.DistortionK[3]);
94 }
96 // calculate and store viewing parameters
97 float vhalfsz = info.VScreenSize * 0.5;
98 vr_ctx.info.fov = 2.0 * atan(vhalfsz / info.EyeToScreenDistance);
100 vr_ctx.info.width = info.HResolution;
101 vr_ctx.info.height = info.VResolution;
102 vr_ctx.info.aspect = (float)vr_ctx.info.width / (float)vr_ctx.info.height;
104 vr_ctx.info.ipd = info.InterpupillaryDistance;
105 for(int i=0; i<4; i++) {
106 vr_ctx.info.distort[i] = info.DistortionK[i];
107 }
109 vr_ctx.info.lens_center = info.LensSeparationDistance / info.HScreenSize;
110 }
112 // get the sensor device
113 if(vr_ctx.ovr_hmd_dev) {
114 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_hmd_dev->GetSensor())) {
115 fprintf(stderr, "failed to get oculus sensor device\n");
116 }
117 } else {
118 if(!(vr_ctx.ovr_sensor_dev = vr_ctx.ovr_devman->EnumerateDevices<SensorDevice>().CreateDevice())) {
119 fprintf(stderr, "failed to get oculus sensor device\n");
120 }
121 }
123 if(vr_ctx.ovr_sensor_dev) {
124 SensorInfo sinfo;
125 if(vr_ctx.ovr_sensor_dev->GetDeviceInfo(&sinfo)) {
126 printf("oculus sensor device info:\n");
127 printf(" name: %s\n", sinfo.ProductName);
128 }
130 vr_ctx.ovr_sfusion.AttachToSensor(vr_ctx.ovr_sensor_dev);
131 }
132 return true;
133 }
135 static bool init_sdr()
136 {
137 int status;
139 unsigned int sdr = glCreateShader(GL_FRAGMENT_SHADER);
140 glShaderSource(sdr, 1, &sdr_src, 0);
141 glCompileShader(sdr);
142 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
143 if(!status) {
144 fprintf(stderr, "failed to compile distortion shader\n");
145 return false;
146 }
148 sdrprog = glCreateProgram();
149 glAttachShader(sdrprog, sdr);
150 glLinkProgram(sdrprog);
151 if(!status) {
152 fprintf(stderr, "failed to link distortion shader program\n");
153 glDeleteShader(sdr);
154 return false;
155 }
157 int loc;
159 glUseProgram(sdrprog);
161 if((loc = glGetUniformLocation(sdrprog, "tex")) != -1) {
162 glUniform1i(loc, 0);
163 }
164 if((loc = glGetUniformLocation(sdrprog, "lens_center")) != -1) {
165 glUniform2f(loc, 0.5, 0.5);
166 }
167 if((loc = glGetUniformLocation(sdrprog, "scr_center")) != -1) {
168 glUniform2f(loc, 0, 0);
169 }
170 if((loc = glGetUniformLocation(sdrprog, "scale")) != -1) {
171 glUniform2f(loc, 1, 1);
172 }
173 if((loc = glGetUniformLocation(sdrprog, "scale_in")) != -1) {
174 glUniform2f(loc, 1, 1);
175 }
176 if((loc = glGetUniformLocation(sdrprog, "warp_param")) != -1) {
177 glUniform4f(loc, vr_ctx.info.distort[0], vr_ctx.info.distort[1],
178 vr_ctx.info.distort[2], vr_ctx.info.distort[3]);
179 }
181 return true;
182 }
184 extern "C" int vr_get_width(void)
185 {
186 return vr_ctx.info.width;
187 }
189 extern "C" int vr_get_height(void)
190 {
191 return vr_ctx.info.height;
192 }
194 extern "C" float vr_get_fov(void)
195 {
196 return vr_ctx.info.fov;
197 }
199 extern "C" float vr_get_aspect(void)
200 {
201 return vr_ctx.info.aspect;
202 }
204 extern "C" void vr_set_eyedist(float ipd)
205 {
206 vr_ctx.info.ipd = ipd;
207 }
209 extern "C" float vr_get_eyedist(void)
210 {
211 return vr_ctx.info.ipd;
212 }
214 extern "C" void vr_set_distort(const float *coef)
215 {
216 memcpy(vr_ctx.info.distort, coef, sizeof vr_ctx.info.distort);
217 }
219 extern "C" void vr_get_distort(float *coef)
220 {
221 memcpy(coef, vr_ctx.info.distort, sizeof vr_ctx.info.distort);
222 }
224 extern "C" void vr_set_prediction_sec(float dt)
225 {
226 vr_ctx.ovr_sfusion.SetPrediction(dt);
227 }
229 extern "C" float vr_get_prediction_sec(void)
230 {
231 return vr_ctx.ovr_sfusion.GetPredictionDelta();
232 }
234 extern "C" void vr_get_translation(float *offs)
235 {
236 // current oculus devkit doesn't do translation
237 offs[0] = offs[1] = offs[2] = 0.0f;
238 }
240 extern "C" void vr_get_rotation(float *quat)
241 {
242 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
243 quat[0] = oq.x;
244 quat[1] = oq.y;
245 quat[2] = oq.z;
246 quat[3] = oq.w;
247 }
249 extern "C" void vr_get_rotation_euler(float *euler)
250 {
251 Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
252 oq.GetEulerAngles<Axis_Y, Axis_X, Axis_Z>(euler + 1, euler, euler + 2);
253 }
255 extern "C" void vr_draw_eye(unsigned int tex, int eye)
256 {
257 static const float rects[3][4] = {
258 {-1, -1, 1, 1},
259 {-1, -1, 0, 1},
260 {0, -1, 1, 1}
261 };
262 static const float offs_scale[3] = {0.0, -1.0, 1.0};
264 glPushAttrib(GL_ENABLE_BIT);
265 glDisable(GL_DEPTH_TEST);
266 glDisable(GL_LIGHTING);
267 glEnable(GL_TEXTURE_2D);
269 glMatrixMode(GL_MODELVIEW);
270 glPushMatrix();
271 glLoadIdentity();
273 glMatrixMode(GL_PROJECTION);
274 glPushMatrix();
275 glLoadIdentity();
277 glUseProgram(sdrprog);
279 int loc;
280 if((loc = glGetUniformLocation(sdrprog, "lens_center")) != -1) {
281 float lens_center = 0.5 - vr_ctx.info.lens_center * offs_scale[eye];
282 glUniform2f(loc, lens_center, 0.5);
283 printf("lens_center = %f\n", lens_center);
284 }
286 glBindTexture(GL_TEXTURE_2D, tex);
287 glBegin(GL_QUADS);
288 glColor4f(1, 1, 1, 1);
289 glTexCoord2f(0, 0); glVertex2f(rects[eye][0], rects[eye][1]);
290 glTexCoord2f(1, 0); glVertex2f(rects[eye][2], rects[eye][1]);
291 glTexCoord2f(1, 1); glVertex2f(rects[eye][2], rects[eye][3]);
292 glTexCoord2f(0, 1); glVertex2f(rects[eye][0], rects[eye][3]);
293 glEnd();
295 glUseProgram(0);
297 glPopMatrix();
298 glMatrixMode(GL_MODELVIEW);
299 glPopMatrix();
301 glPopAttrib();
302 }