-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeapMain.cpp
More file actions
executable file
·412 lines (345 loc) · 10.2 KB
/
LeapMain.cpp
File metadata and controls
executable file
·412 lines (345 loc) · 10.2 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#pragma once
#define _CRT_SECURE_NO_DEPRECATE
// may need #include "stdafx.h" in visual studio
//#include "stdafx.h"
#include "ServerGame.h"
#include "ClientGame.h"
// used for multi-threading
#include <process.h>
void serverLoop(void *);
void clientLoop(void);
ServerGame * server;
ClientGame * client;
#include "LeapMain.h"
using namespace Leap;
/* Glasses filter types */
#define REDBLUE 1
#define REDGREEN 2
#define REDCYAN 3
#define BLUERED 4
#define GREENRED 5
#define CYANRED 6
int glassestype = REDCYAN;
#define DTOR 0.0174532925
#define RTOD 57.2957795
#define TWOPI 6.283185307179586476925287
#define PI 3.141592653589793238462643
#define PID2 1.570796326794896619231322
#define ESC 27
App app;
CAMERA camera;
bool fullscreen = false;
int debug = FALSE;
XYZ origin = { 0.0,0.0,0.0 };
int main(int argc, char** argv) {
LeapListener listener;
Controller controller;
controller.addListener(listener);
// initialize the client
//client = new ClientGame();
/* Set things (glut) up */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_ACCUM | GLUT_RGB | GLUT_DEPTH);
/* Create the window and handlers */
glutCreateWindow("Aliens vs. Player");
camera.screenwidth = 1920;
camera.screenheight = 1080;
glutReshapeWindow(camera.screenwidth, camera.screenheight);
if (fullscreen)
glutFullScreen();
glutIdleFunc(HandleDisplay);
glewExperimental = GL_TRUE;
if (0 != glewInit()) {
FAIL("Failed to initialize GLEW");
}
glGetError();
fprintf(stderr, "OpenGL %s\n", glGetString(GL_VERSION));
// http://paulbourke.net/stereographics/anaglyph/anaglyph.c
/* Set things (glut) up */
CreateEnvironment();
CameraHome(0);
app.initGl();
// Keep this process running until Enter is pressed
//std::cout << "Press Enter to quit..." << std::endl;
//std::cin.get();
glutMainLoop();
glfwTerminate();
controller.removeListener(listener);
return(0);
}
void LeapListener::onConnect(const Controller& controller) {
std::cout << "Connected" << std::endl;
}
void LeapListener::onFrame(const Controller& controller) {
const Frame frame = controller.frame();
if (frame.hands().count() > 0) {
showLeapHands = true;
leapHandPos = toGlm(frame.hands()[0].palmPosition());
}
else {
showLeapHands = true;
}
}
/*
This is the basic display callback routine
It creates the geometry, lighting, and viewing position
*/
void HandleDisplay(void)
{
int i, j;
XYZ r;
double dist, ratio, radians, scale, wd2, ndfl;
double left, right, top, bottom, near_ = 0.1, far_ = 10000;
/* Clip to avoid extreme stereo */
near_ = camera.focallength / 5;
/* Derive the the "right" vector */
CROSSPROD(camera.vd, camera.vu, r);
Normalise(&r);
r.x *= camera.eyesep / 2.0;
r.y *= camera.eyesep / 2.0;
r.z *= camera.eyesep / 2.0;
/* Misc stuff */
ratio = camera.screenwidth / (double)camera.screenheight;
radians = DTOR * camera.aperture / 2;
wd2 = near_ * tan(radians);
ndfl = near_ / camera.focallength;
/* Set the buffer for writing and reading */
glDrawBuffer(GL_BACK);
glReadBuffer(GL_BACK);
/* Clear things */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClear(GL_ACCUM_BUFFER_BIT); /* Not strictly necessary */
glViewport(0, 0, camera.screenwidth, camera.screenheight);
/* Left eye filter */
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
switch (glassestype) {
case REDBLUE:
case REDGREEN:
case REDCYAN:
glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
break;
case BLUERED:
glColorMask(GL_FALSE, GL_FALSE, GL_TRUE, GL_TRUE);
break;
case GREENRED:
glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_TRUE);
break;
case CYANRED:
glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
break;
}
/* Create the projection */
left = -ratio * wd2 + 0.5 * camera.eyesep * ndfl;
right = ratio * wd2 + 0.5 * camera.eyesep * ndfl;
top = wd2;
bottom = -wd2;
mat4 lProjection = glm::frustum(left, right, bottom, top, near_, far_);
/* Create the model */
mat4 lModelview = lookAt(vec3(camera.vp.x - r.x, camera.vp.y - r.y, camera.vp.z - r.z),
vec3(camera.vp.x - r.x + camera.vd.x,
camera.vp.y - r.y + camera.vd.y,
camera.vp.z - r.z + camera.vd.z),
vec3(camera.vu.x, camera.vu.y, camera.vu.z));
Render(lProjection, lModelview);
glFlush();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
/* Write over the accumulation buffer */
glAccum(GL_LOAD, 1.0); /* Could also use glAccum(GL_ACCUM,1.0); */
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Right eye filter */
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
switch (glassestype) {
case REDBLUE:
glColorMask(GL_FALSE, GL_FALSE, GL_TRUE, GL_TRUE);
break;
case REDGREEN:
glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_TRUE);
break;
case REDCYAN:
glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
break;
case BLUERED:
case GREENRED:
case CYANRED:
glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
break;
}
/* The projection */
left = -ratio * wd2 - 0.5 * camera.eyesep * ndfl;
right = ratio * wd2 - 0.5 * camera.eyesep * ndfl;
top = wd2;
bottom = -wd2;
mat4 rProjection = glm::frustum(left, right, bottom, top, near_, far_);
mat4 rModelview = lookAt(vec3(camera.vp.x + r.x, camera.vp.y + r.y, camera.vp.z + r.z),
vec3(camera.vp.x + r.x + camera.vd.x,
camera.vp.y + r.y + camera.vd.y,
camera.vp.z + r.z + camera.vd.z),
vec3(camera.vu.x, camera.vu.y, camera.vu.z));
Render(rProjection, rModelview);
glFlush();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
//std::cout << glm::to_string(lModelview) << std::endl;
/* Addin the new image and copy the result back */
glAccum(GL_ACCUM, 1.0);
glAccum(GL_RETURN, 1.0);
/* Let's look at it */
glutSwapBuffers();
}
void Render(const glm::mat4 & projection, const glm::mat4 & modelview) {
app.renderScene(projection, modelview);
//client->update();
}
/*
Move the camera to the home position
Or to a predefined stereo configuration
The model is assumed to be in a 10x10x10 cube
Centered at the origin
*/
void CameraHome(int mode)
{
camera.aperture = 60;
camera.pr = origin;
camera.vd.x = 0;
camera.vd.y = 0;
camera.vd.z = -1;
camera.vu.x = 0;
camera.vu.y = 3;
camera.vu.z = 0;
camera.vp.x = 0;
camera.vp.y = 0;
camera.vp.z = 12;
RotateCamera(0, 20, 0);
switch (mode) {
case 0:
case 2:
case 4:
camera.focallength = 10;
break;
case 1:
camera.focallength = 5;
break;
case 3:
camera.focallength = 15;
break;
}
/* Non stressful stereo setting */
camera.eyesep = camera.focallength / 30.0;
if (mode == 4)
camera.eyesep = 0;
}
/*
This is where global OpenGL/GLUT settings are made,
that is, things that will not change in time
*/
void CreateEnvironment(void)
{
int num[2];
glEnable(GL_DEPTH_TEST);
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POINT_SMOOTH);
glDisable(GL_POLYGON_SMOOTH);
glDisable(GL_DITHER);
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND); /* Not necessary but for bug in PS350 driver */
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth(1.0);
glPointSize(1.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glFrontFace(GL_CW);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearAccum(0.0, 0.0, 0.0, 0.0); /* The default */
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/*
In case you want to check what the colour depth of your
accumulation buffer is, hopefully it is 16 bits
*/
if (debug) {
glGetIntegerv(GL_ACCUM_RED_BITS, num);
fprintf(stderr, "Red bits: %d\n", num[0]);
glGetIntegerv(GL_ACCUM_GREEN_BITS, num);
fprintf(stderr, "Green bits: %d\n", num[0]);
glGetIntegerv(GL_ACCUM_BLUE_BITS, num);
fprintf(stderr, "Blue bits: %d\n", num[0]);
glGetIntegerv(GL_ACCUM_ALPHA_BITS, num);
fprintf(stderr, "Alpha bits: %d\n", num[0]);
}
}
/*
Rotate (ix,iy) or roll (iz) the camera about the focal point
ix,iy,iz are flags, 0 do nothing, +- 1 rotates in opposite directions
Correctly updating all camera attributes
*/
void RotateCamera(int ix, int iy, int iz)
{
XYZ vp, vu, vd;
XYZ right;
XYZ newvp, newr;
double radius, dd, radians;
double dx, dy, dz;
vu = camera.vu;
Normalise(&vu);
vp = camera.vp;
vd = camera.vd;
Normalise(&vd);
CROSSPROD(vd, vu, right);
Normalise(&right);
radians = 1 * PI / 180.0;
/* Handle the roll */
if (iz != 0) {
camera.vu.x += iz * right.x * radians;
camera.vu.y += iz * right.y * radians;
camera.vu.z += iz * right.z * radians;
Normalise(&camera.vu);
return;
}
/* Distance from the rotate point */
dx = camera.vp.x - camera.pr.x;
dy = camera.vp.y - camera.pr.y;
dz = camera.vp.z - camera.pr.z;
radius = sqrt(dx*dx + dy*dy + dz*dz);
/* Determine the new view point */
dd = radius * radians;
newvp.x = vp.x + dd * ix * right.x + dd * iy * vu.x - camera.pr.x;
newvp.y = vp.y + dd * ix * right.y + dd * iy * vu.y - camera.pr.y;
newvp.z = vp.z + dd * ix * right.z + dd * iy * vu.z - camera.pr.z;
Normalise(&newvp);
camera.vp.x = camera.pr.x + radius * newvp.x;
camera.vp.y = camera.pr.y + radius * newvp.y;
camera.vp.z = camera.pr.z + radius * newvp.z;
/* Determine the new right vector */
newr.x = camera.vp.x + right.x - camera.pr.x;
newr.y = camera.vp.y + right.y - camera.pr.y;
newr.z = camera.vp.z + right.z - camera.pr.z;
Normalise(&newr);
newr.x = camera.pr.x + radius * newr.x - camera.vp.x;
newr.y = camera.pr.y + radius * newr.y - camera.vp.y;
newr.z = camera.pr.z + radius * newr.z - camera.vp.z;
camera.vd.x = camera.pr.x - camera.vp.x;
camera.vd.y = camera.pr.y - camera.vp.y;
camera.vd.z = camera.pr.z - camera.vp.z;
Normalise(&camera.vd);
/* Determine the new up vector */
CROSSPROD(newr, camera.vd, camera.vu);
Normalise(&camera.vu);
}
/*
Normalise a vector
*/
void Normalise(XYZ *p)
{
double length;
length = sqrt(p->x * p->x + p->y * p->y + p->z * p->z);
if (length != 0) {
p->x /= length;
p->y /= length;
p->z /= length;
}
else {
p->x = 0;
p->y = 0;
p->z = 0;
}
}