-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
264 lines (205 loc) · 6.69 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "GL/freeglut.h"
#include "ScreenCapture.h"
#include "StereoCamera.h"
#include "Common/Types.h"
#include <stdio.h>
#include <OVR.h> //Oculus SDK
using namespace Common;
unsigned desktop_tex_id = 0;
// Left/right
unsigned ovrvision_tex_ids[2] = {0, 0};
// 15 inches
float world_screen_width_meters = 0.381f;
float world_meters_per_unit = world_screen_width_meters / 2.0f;
float desktop_ipd_adjust = 1.959;
float ovrvision_intra_ocular_offset_norm = 0.238f;
float desktop_z = -2.28;
float desktop_alpha = 0.9f;
int window_width = 1, window_height = 1;
// Oculus Rift stuff
OVR::DeviceManager* m_pManager;
OVR::HMDDevice* m_pHMD;
OVR::SensorDevice* m_pSensor;
OVR::SensorFusion* m_sFusion;
OVR::HMDInfo m_hmdInfo;
bool InitRift() {
OVR::System::Init();
m_pManager = OVR::DeviceManager::Create();
m_pHMD = m_pManager->EnumerateDevices<OVR::HMDDevice>().CreateDevice();
if (!m_pHMD)
return false;
m_pHMD->GetDeviceInfo(&m_hmdInfo);
m_pSensor = m_pHMD->GetSensor();
m_sFusion = new OVR::SensorFusion;
m_sFusion->AttachToSensor(m_pSensor);
return true;
}
Vec3f Multiply(Matrix4f const&matrix, Vec3f const&in) {
Vec4d ret = Vec4d(in.x, in.y, in.z, 0);
ret = matrix.Multiply(ret);
return Vec3f(ret.x, ret.y, ret.z);
}
void DrawDesktopEye(Vec3f const&eye,
Vec3f const&dir,
Vec3f const&up,
float screen_aspect,
float one_eye_aspect,
Vec2f const&tex_extent) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, // fov
//0.5f * float(m_hmdInfo.HResolution) / float(m_hmdInfo.VResolution),
one_eye_aspect,
0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Vec3f center = eye + dir;
gluLookAt(eye.x,eye.y,eye.z, // eye
center.x,center.y,center.z, // center
up.x,up.y,up.z // up
);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, desktop_tex_id);
glTranslatef(0,0,desktop_z);
glColor4f(1,1,1,desktop_alpha);
glBegin(GL_QUADS);
glTexCoord2f(0,tex_extent.y);
glVertex2f(-screen_aspect,-1);
glTexCoord2f(tex_extent.x,tex_extent.y);
glVertex2f(screen_aspect,-1);
glTexCoord2f(tex_extent.x, 0);
glVertex2f(screen_aspect,1);
glTexCoord2f(0,0);
glVertex2f(-screen_aspect,1);
glEnd();
}
void DrawCameraEye(float eye_x_coeff, GLuint tex_id, float screen_aspect, float image_aspect, Vec2f const&tex_extent) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1,1,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(ovrvision_intra_ocular_offset_norm * eye_x_coeff,0,0);
float y_scale = screen_aspect / image_aspect;
glScalef(1,y_scale,1);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, tex_id);
glColor4f(1,1,1,1);
glBegin(GL_QUADS);
glTexCoord2f(0,tex_extent.y);
glVertex2f(-1,-1);
glTexCoord2f(tex_extent.x,tex_extent.y);
glVertex2f(1,-1);
glTexCoord2f(tex_extent.x, 0);
glVertex2f(1,1);
glTexCoord2f(0,0);
glVertex2f(-1,1);
glEnd();
}
void display()
{
float screen_aspect;
Vec2f tex_extent;
if(!ScreenCapture_ToTexture(desktop_tex_id, &screen_aspect, &tex_extent))
{
fprintf(stderr, "Warning: ScreenCapture_ToTexture() failed\n");
return;
}
float camera_image_aspect;
Vec2f camera_tex_extent;
if(!StereoCamera_ToTextures(ovrvision_tex_ids[0], ovrvision_tex_ids[1], &camera_image_aspect, &camera_tex_extent))
{
fprintf(stderr, "Warning: StereoCamera_ToTextures() failed\n");
return;
}
float yaw; // yaw from Rift sensor in radians
float pitch; // pitch from Rift sensor in radians
float roll; // roll from Rift sensor in radians
m_sFusion->GetOrientation().GetEulerAngles<OVR::Axis_Y, OVR::Axis_X, OVR::Axis_Z>(&yaw, &pitch, &roll);
Matrix4f pitch_matrix = Matrix4f::MakeRotationMatrix(-pitch, Vec3f(1,0,0));
Matrix4f yaw_matrix = Matrix4f::MakeRotationMatrix(-yaw, Vec3f(0,1,0));
Matrix4f roll_matrix = Matrix4f::MakeRotationMatrix(-roll, Vec3f(0,0,1));
Vec3f dir(0,0,-1);
dir = Multiply(yaw_matrix, Multiply(pitch_matrix, Multiply(roll_matrix, dir)));
Vec3f up(0,1,0);
up = Multiply(yaw_matrix, Multiply(pitch_matrix, Multiply(roll_matrix, up)));
const float ipd_world_units = desktop_ipd_adjust * m_hmdInfo.InterpupillaryDistance / world_meters_per_unit;
Vec3f left_eye(-ipd_world_units / 2.0f,0,0);
Vec3f right_eye(ipd_world_units / 2.0f,0,0);
left_eye = Multiply(yaw_matrix, Multiply(pitch_matrix, Multiply(roll_matrix, left_eye)));
right_eye = Multiply(yaw_matrix, Multiply(pitch_matrix, Multiply(roll_matrix, right_eye)));
glClearColor(1,0,1,1);
glClear(GL_COLOR_BUFFER_BIT);
const float one_eye_aspect = 0.5f * window_width / window_height;
// Draw the desktop with the real world behind
glViewport(0,0,window_width / 2,window_height);
DrawCameraEye(1.0f, ovrvision_tex_ids[0], one_eye_aspect, camera_image_aspect, camera_tex_extent);
DrawDesktopEye(left_eye, dir, up, screen_aspect, one_eye_aspect, tex_extent);
glViewport(window_width / 2,0,window_width / 2,window_height);
DrawCameraEye(-1.0f, ovrvision_tex_ids[1], one_eye_aspect, camera_image_aspect, camera_tex_extent);
DrawDesktopEye(right_eye, dir, up, screen_aspect, one_eye_aspect, tex_extent);
glutSwapBuffers();
}
void idle()
{
// TODO: Limit FPS?
glutPostRedisplay();
}
void reshape(int w, int h)
{
window_width = w;
window_height = h;
}
void keyboard(unsigned char key, int x, int y)
{
if(key == '[')
ovrvision_intra_ocular_offset_norm -= 0.002f;
else if(key == ']')
ovrvision_intra_ocular_offset_norm += 0.002f;
else if(key == ';')
desktop_ipd_adjust -= 0.03f;
else if(key == '\'')
desktop_ipd_adjust += 0.03f;
else if(key == '=')
desktop_z -= 0.01f;
else if(key == '-')
desktop_z += 0.01f;
fprintf(stderr, "ovrvision_intra_ocular_offset_norm %f\n" , ovrvision_intra_ocular_offset_norm);
fprintf(stderr, "desktop_ipd_adjust %f\n", desktop_ipd_adjust);
fprintf(stderr, "desktop_z %f\n", desktop_z);
}
GLuint MakeTexture()
{
GLuint ret = 0;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &ret);
glBindTexture(GL_TEXTURE_2D, ret);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
return ret;
}
int main(int argc, char **argv)
{
if(!InitRift())
return 1;
if(!StereoCamera_Init())
return 1;
if(!ScreenCapture_Init())
return 1;
glutInit(&argc, argv);
glutInitWindowSize(800, 600);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("ARDesktop");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
desktop_tex_id = MakeTexture();
ovrvision_tex_ids[0] = MakeTexture();
ovrvision_tex_ids[1] = MakeTexture();
glutMainLoop();
return 0;
}