-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathARDriver.cc
462 lines (392 loc) · 11.7 KB
/
ARDriver.cc
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright 2009 Isis Innovation Limited
#define GL_GLEXT_PROTOTYPES 1
#include "ARDriver.h"
#include "Map.h"
#include "Games.h"
#include <cvd/image_io.h>
namespace PTAMM {
using namespace GVars3;
using namespace CVD;
using namespace std;
static bool CheckFramebufferStatus();
/**
* Constructor
* @param cam Reference to the camera
* @param irFrameSize the size of the frame
* @param glw the glwindow
* @param map the current map
*/
ARDriver::ARDriver(const ATANCamera &cam, ImageRef irFrameSize, GLWindow2 &glw, Map &map)
:mCamera(cam), mGLWindow(glw), mpMap( &map )
{
mirFrameSize = irFrameSize;
mCamera.SetImageSize(mirFrameSize);
mbInitialised = false;
}
/**
* Initialize the AR driver
*/
void ARDriver::Init()
{
mbInitialised = true;
mirFBSize = GV3::get<ImageRef>("ARDriver.FrameBufferSize", ImageRef(1200,900), SILENT);
glGenTextures(1, &mnFrameTex);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,mnFrameTex);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0,
GL_RGBA, mirFrameSize.x, mirFrameSize.y, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
MakeFrameBuffer();
try {
CVD::img_load(mLostOverlay, "ARData/Overlays/searching.png");
}
catch(CVD::Exceptions::All err) {
cerr << "Failed to load searching image " << "\"ARData/Overlays/searching.png\"" << ": " << err.what << endl;
}
}
/**
* Reset the game and the frame counter
*/
void ARDriver::Reset()
{
if(mpMap->pGame) {
mpMap->pGame->Reset();
}
mnCounter = 0;
}
/**
* Render the AR composite image
* @param imFrame The camera frame
* @param se3CfromW The camera position
* @param bLost Is the camera lost
*/
void ARDriver::Render(Image<Rgb<CVD::byte> > &imFrame, SE3<> se3CfromW, bool bLost)
{
if(!mbInitialised)
{
Init();
Reset();
};
mse3CfromW = se3CfromW;
mnCounter++;
// Upload the image to our frame texture
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mnFrameTex);
glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,
0, 0, 0,
mirFrameSize.x, mirFrameSize.y,
GL_RGB,
GL_UNSIGNED_BYTE,
imFrame.data());
// Set up rendering to go the FBO, draw undistorted video frame into BG
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,mnFrameBuffer);
CheckFramebufferStatus();
glViewport(0,0,mirFBSize.x,mirFBSize.y);
DrawFBBackGround();
glClearDepth(1);
glClear(GL_DEPTH_BUFFER_BIT);
// Set up 3D projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//only draw 3d stuff if not lost.
if(!bLost)
{
glMultMatrix(mCamera.MakeUFBLinearFrustumMatrix(0.005, 100));
glMultMatrix(se3CfromW);
DrawFadingGrid();
if(mpMap->pGame) {
mpMap->pGame->Draw3D( mGLWindow, *mpMap, se3CfromW);
}
}
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_BLEND);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set up for drawing 2D stuff:
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
DrawDistortedFB();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
mGLWindow.SetupViewport();
mGLWindow.SetupVideoOrtho();
mGLWindow.SetupVideoRasterPosAndZoom();
//2d drawing
if(!bLost)
{
if(mpMap->pGame) {
mpMap->pGame->Draw2D(mGLWindow, *mpMap);
}
}
else
{
//draw the lost ar overlays
glEnable(GL_BLEND);
glRasterPos2i( ( mGLWindow.size().x - mLostOverlay.size().x )/2,
( mGLWindow.size().y - mLostOverlay.size().y )/2 );
glDrawPixels(mLostOverlay);
glDisable(GL_BLEND);
}
}
/**
* Make the frame buffer
*/
void ARDriver::MakeFrameBuffer()
{
// Needs nvidia drivers >= 97.46
cout << " ARDriver: Creating FBO... ";
glGenTextures(1, &mnFrameBufferTex);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB,mnFrameBufferTex);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0,
GL_RGBA, mirFBSize.x, mirFBSize.y, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLuint DepthBuffer;
glGenRenderbuffersEXT(1, &DepthBuffer);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, DepthBuffer);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, mirFBSize.x, mirFBSize.y);
glGenFramebuffersEXT(1, &mnFrameBuffer);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mnFrameBuffer);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_RECTANGLE_ARB, mnFrameBufferTex, 0);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, DepthBuffer);
CheckFramebufferStatus();
cout << " .. created FBO." << endl;
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
/**
* check the status of the frame buffer
*/
static bool CheckFramebufferStatus()
{
GLenum n;
n = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(n == GL_FRAMEBUFFER_COMPLETE_EXT)
return true; // All good
cout << "glCheckFrameBufferStatusExt returned an error." << endl;
return false;
}
/**
* Draw the background (the image from the camera)
*/
void ARDriver::DrawFBBackGround()
{
static bool bFirstRun = true;
static GLuint nList;
mGLWindow.SetupUnitOrtho();
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mnFrameTex);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glDisable(GL_POLYGON_SMOOTH);
glDisable(GL_BLEND);
// Cache the cpu-intesive projections in a display list..
if(bFirstRun)
{
bFirstRun = false;
nList = glGenLists(1);
glNewList(nList, GL_COMPILE_AND_EXECUTE);
glColor3f(1,1,1);
// How many grid divisions in the x and y directions to use?
int nStepsX = 24; // Pretty arbitrary..
int nStepsY = (int) (nStepsX * ((double) mirFrameSize.x / mirFrameSize.y)); // Scaled by aspect ratio
if(nStepsY < 2)
nStepsY = 2;
for(int ystep = 0; ystep< nStepsY; ystep++)
{
glBegin(GL_QUAD_STRIP);
for(int xstep = 0; xstep <= nStepsX; xstep++)
for(int yystep = ystep; yystep<=ystep+1; yystep++) // Two y-coords in one go - magic.
{
Vector<2> v2Iter;
v2Iter[0] = (double) xstep / nStepsX;
v2Iter[1] = (double) yystep / nStepsY;
// If this is a border quad, draw a little beyond the
// outside of the frame, this avoids strange jaggies
// at the edge of the reconstructed frame later:
if(xstep == 0 || yystep == 0 || xstep == nStepsX || yystep == nStepsY)
for(int i=0; i<2; i++)
v2Iter[i] = v2Iter[i] * 1.02 - 0.01;
Vector<2> v2UFBDistorted = v2Iter;
Vector<2> v2UFBUnDistorted = mCamera.UFBLinearProject(mCamera.UFBUnProject(v2UFBDistorted));
glTexCoord2d(v2UFBDistorted[0] * mirFrameSize.x, v2UFBDistorted[1] * mirFrameSize.y);
glVertex(v2UFBUnDistorted);
}
glEnd();
}
glEndList();
}
else
glCallList(nList);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
}
/**
* Draw the distorted frame buffer
*/
void ARDriver::DrawDistortedFB()
{
static bool bFirstRun = true;
static GLuint nList;
mGLWindow.SetupViewport();
mGLWindow.SetupUnitOrtho();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, mnFrameBufferTex);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glDisable(GL_POLYGON_SMOOTH);
glDisable(GL_BLEND);
if(bFirstRun)
{
bFirstRun = false;
nList = glGenLists(1);
glNewList(nList, GL_COMPILE_AND_EXECUTE);
// How many grid divisions in the x and y directions to use?
int nStepsX = 24; // Pretty arbitrary..
int nStepsY = (int) (nStepsX * ((double) mirFrameSize.x / mirFrameSize.y)); // Scaled by aspect ratio
if(nStepsY < 2)
nStepsY = 2;
glColor3f(1,1,1);
for(int ystep = 0; ystep<nStepsY; ystep++)
{
glBegin(GL_QUAD_STRIP);
for(int xstep = 0; xstep<=nStepsX; xstep++)
for(int yystep = ystep; yystep<=ystep + 1; yystep++) // Two y-coords in one go - magic.
{
Vector<2> v2Iter;
v2Iter[0] = (double) xstep / nStepsX;
v2Iter[1] = (double) yystep / nStepsY;
Vector<2> v2UFBDistorted = v2Iter;
Vector<2> v2UFBUnDistorted = mCamera.UFBLinearProject(mCamera.UFBUnProject(v2UFBDistorted));
glTexCoord2d(v2UFBUnDistorted[0] * mirFBSize.x, (1.0 - v2UFBUnDistorted[1]) * mirFBSize.y);
glVertex(v2UFBDistorted);
}
glEnd();
}
glEndList();
}
else
glCallList(nList);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
}
/**
* Draw the fading grid
*/
void ARDriver::DrawFadingGrid()
{
double dStrength;
if(mnCounter >= 60)
return;
if(mnCounter < 30)
dStrength = 1.0;
dStrength = (60 - mnCounter) / 30.0;
glColor4f(1,1,1,dStrength);
int nHalfCells = 8;
if(mnCounter < 8)
nHalfCells = mnCounter + 1;
int nTot = nHalfCells * 2 + 1;
Vector<3> aaVertex[17][17];
for(int i=0; i<nTot; i++)
for(int j=0; j<nTot; j++)
{
Vector<3> v3;
v3[0] = (i - nHalfCells) * 0.1;
v3[1] = (j - nHalfCells) * 0.1;
v3[2] = 0.0;
aaVertex[i][j] = v3;
}
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth(2);
for(int i=0; i<nTot; i++)
{
glBegin(GL_LINE_STRIP);
for(int j=0; j<nTot; j++)
glVertex(aaVertex[i][j]);
glEnd();
glBegin(GL_LINE_STRIP);
for(int j=0; j<nTot; j++)
glVertex(aaVertex[j][i]);
glEnd();
};
}
/**
* What to do when the user clicks on the screen.
* Calculates the 3d postion of the click on the plane
* and passes info to a game, if there is one.
* @param nButton the button pressed
* @param irWin the window x, y location
*/
void ARDriver::HandleClick(int nButton, ImageRef irWin )
{
//The window may have been resized, so want to work out the coords based on the orignal image size
Vector<2> v2VidCoords = mGLWindow.VidFromWinCoords( irWin );
Vector<2> v2UFBCoords;
#ifdef WIN32
Vector<2> v2PlaneCoords; v2PlaneCoords[0] = numeric_limits<double>::quiet_NaN(); v2PlaneCoords[1] = numeric_limits<double>::quiet_NaN();
#else
Vector<2> v2PlaneCoords; v2PlaneCoords[0] = NAN; v2PlaneCoords[1] = NAN;
#endif
Vector<3> v3RayDirn_W;
// Work out image coords 0..1:
v2UFBCoords[0] = (v2VidCoords[0] + 0.5) / mCamera.GetImageSize()[0];
v2UFBCoords[1] = (v2VidCoords[1] + 0.5) / mCamera.GetImageSize()[1];
// Work out plane coords:
Vector<2> v2ImPlane = mCamera.UnProject(v2VidCoords);
Vector<3> v3C = unproject(v2ImPlane);
Vector<4> v4C = unproject(v3C);
SE3<> se3CamInv = mse3CfromW.inverse();
Vector<4> v4W = se3CamInv * v4C;
double t = se3CamInv.get_translation()[2];
double dDistToPlane = -t / (v4W[2] - t);
if(v4W[2] -t <= 0) // Clicked the wrong side of the horizon?
{
v4C.slice<0,3>() *= dDistToPlane;
Vector<4> v4Result = se3CamInv * v4C;
v2PlaneCoords = v4Result.slice<0,2>(); // <--- result
}
// Ray dirn:
v3RayDirn_W = v4W.slice<0,3>() - se3CamInv.get_translation();
normalize(v3RayDirn_W);
if(mpMap->pGame) {
mpMap->pGame->HandleClick(v2VidCoords, v2UFBCoords, v3RayDirn_W, v2PlaneCoords, nButton);
}
}
/**
* Handle the user pressing a key
* @param sKey the key the user pressed.
*/
void ARDriver::HandleKeyPress( std::string sKey )
{
if(mpMap && mpMap->pGame ) {
mpMap->pGame->HandleKeyPress( sKey );
}
}
/**
* Load a game by name.
* @param sName Name of the game
*/
void ARDriver::LoadGame(std::string sName)
{
if(mpMap->pGame)
{
delete mpMap->pGame;
mpMap->pGame = NULL;
}
mpMap->pGame = LoadAGame( sName, "");
if( mpMap->pGame ) {
mpMap->pGame->Init();
}
}
/**
* Advance the game logic
*/
void ARDriver::AdvanceLogic()
{
if(mpMap->pGame) {
mpMap->pGame->Advance();
}
}
}