-
Notifications
You must be signed in to change notification settings - Fork 28
/
ARDriver.cc
288 lines (246 loc) · 8.07 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
// Copyright 2008 Isis Innovation Limited
#define GL_GLEXT_PROTOTYPES 1
#include "ARDriver.h"
using namespace GVars3;
using namespace CVD;
using namespace std;
static bool CheckFramebufferStatus();
ARDriver::ARDriver(const ATANCamera &cam, ImageRef irFrameSize, GLWindow2 &glw)
:mCamera(cam), mGLWindow(glw)
{
mirFrameSize = irFrameSize;
mCamera.SetImageSize(mirFrameSize);
mbInitialised = false;
}
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();
mGame.Init();
};
void ARDriver::Reset()
{
mGame.Reset();
mnCounter = 0;
}
void ARDriver::Render(Image<Rgb<byte> > &imFrame, SE3<> se3CfromW)
{
if(!mbInitialised)
{
Init();
Reset();
};
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();
glMultMatrix(mCamera.MakeUFBLinearFrustumMatrix(0.005, 100));
glMultMatrix(se3CfromW);
DrawFadingGrid();
mGame.DrawStuff(se3CfromW.inverse().get_translation());
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();
}
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);
}
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;
};
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);
}
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);
}
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();
};
};