-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathModelsGame.cc
426 lines (343 loc) · 8.97 KB
/
ModelsGame.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
// Copyright 2009 Isis Innovation Limited
//
// C++ Implementation: ModelsGame
//
// Description: An AR game for placing 3ds models in a map
//
// Author: Robert Castle <[email protected]>, (C) 2009
//
#include "ModelsGame.h"
#include "OpenGL.h"
#include "MapPoint.h"
#include "Map.h"
#include <cvd/gl_helpers.h>
#include <cvd/image_io.h>
#include <gvars3/instances.h>
namespace PTAMM {
using namespace CVD;
using namespace TooN;
using namespace GVars3;
class Map;
/**
* Constructor
*/
ModelsGame::ModelsGame()
: Game( "Models" ),
mData( Name() ),
mModelControls( mData )
{
Reset();
}
/**
* Destructor
*/
ModelsGame::~ModelsGame()
{
}
/**
* Reset the game.
*/
void ModelsGame::Reset()
{
mData.Reset();
}
/**
* Initialize the game.
*/
void ModelsGame::Init()
{
if( mbInitialised ) {
return;
}
bool bOk = false;
// initialize the model browser
bOk = mModelBrowser.Init( ImageRef(BROWSER_X, BROWSER_Y), "ARData/Overlays/model_browser.png");
if( !bOk ) {
return;
}
// initialize the model controls
mModelControls.Init();
// load the current model target
try {
Image<Rgba<CVD::byte> > imTarget;
CVD::img_load( imTarget, "ARData/Overlays/selected.png" );
glGenTextures( 1, &mnTargetTex);
glBindTexture( GL_TEXTURE_2D, mnTargetTex );
glTexImage2D(imTarget);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
}
catch(CVD::Exceptions::All err) {
cerr << "Failed to load target image " << "ARData/Overlays/selected.png" << ": " << err.what << endl;
return;
}
mbInitialised = true;
}
/**
* Draw the 3D components (the 3DS models)
* @param glWindow The GL Window
* @param map the current map
* @param se3CfromW The current camera position
*/
void ModelsGame::Draw3D( const GLWindow2 &glWindow, Map &map, SE3<> se3CfromW)
{
if( !mbInitialised ) {
Init();
}
if( mData.mbHideAR ) {
return;
}
mpMap = ↦
mse3CfW = se3CfromW;
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GLfloat tmp[]={0.5, 0.5, 0.5, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, tmp);
glLightfv(GL_LIGHT0, GL_DIFFUSE, tmp);
GLfloat tmp2[]={1.0, 0.0, 1.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, tmp2);
GLfloat tmp3[]={1.0, 1.0, 1.0, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tmp3);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrix(SE3<>());
// Display the map points?
if( !mData.mbHidePoints )
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPointSize(5);
glColor3f(1,1,1);
glBegin(GL_POINTS);
for( size_t ii = 0; ii < map.vpPoints.size(); ++ii )
{
MapPoint &p = *(map.vpPoints[ii]);
if( p.nSourceLevel != 0 ) {
continue;
}
glVertex( p.v3WorldPos );
}
glEnd();
}
// Draw the origin axes ?
if( GV3::get<int>("ModelsGame.DrawOrigin", "0", SILENT) ) {
_DrawAxes();
}
// Draw the models
std::vector<Model3DS*>::const_iterator itr;
for ( itr = mData.Models().begin(); itr != mData.Models().end(); ++itr )
{
(*itr)->Draw();
}
// Find the current model and draw the target
Model3DS * m = mData.CurrentModel();
if( m && !mData.mbHideControls )
{
glPushMatrix();
glMultMatrix( m->Pose() );
glScaled( m->Diameter(), m->Diameter(), m->Diameter() );
_DrawSelectedTarget();
glPopMatrix();
}
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
// A rather nasty hack to get around the window event limitations.
// Check to see if the user is holding the mouse button down and if
// it is on a repeating button, call it again.
if( mData.mDisplayState == ModelsGameData::ControlState &&
glWindow.IsMouseButtonPressed( CVD::GLWindow::BUTTON_LEFT ) ) {
mModelControls.HandlePressAndHold();
}
}
/**
* Draw the 2D components (menus etc)
* @param glWindow The GL Window
* @param map The current map
*/
void ModelsGame::Draw2D( const GLWindow2 &glWindow, Map &map)
{
glEnable(GL_BLEND);
switch(mData.mDisplayState)
{
case ModelsGameData::BrowserState:
mModelBrowser.Draw(glWindow); // draw the model browser
break;
case ModelsGameData::ControlState:
mModelControls.Draw(glWindow); // draw the controls
break;
case ModelsGameData::HiddenState: // nothing to draw
default:
//do nothing
break;
}
glDisable(GL_BLEND);
}
/**
* Handle a click from the user. The user will either be clicking
* on a button overlay or on the scene.
* @param v2VidCoords location in the image coords.
* @param v2UFB Location in the undistorted framebuffer coords
* @param v3RayDirnW Direction of the click ray
* @param v2Plane The in-plane location of the click
* @param nButton The button clicked
*/
void ModelsGame::HandleClick(Vector<2> v2VidCoords, Vector<2> v2UFB, Vector<3> v3RayDirnW, Vector<2> v2Plane, int nButton)
{
//select appropriate reponse for each overlay
switch(mData.mDisplayState)
{
case ModelsGameData::BrowserState:
_HandleModelBrowserActions( v2VidCoords );
break;
case ModelsGameData::ControlState:
if( !mModelControls.HandleClick( v2VidCoords, nButton ) && mData.mbSnapTo
&& !mData.mbHideControls && !mData.mbHideAR)
{
Model3DS *p = mData.CurrentModel();
if( p )
{
Vector<3> v3Loc = _SelectClosestMapPoint( v3RayDirnW );
p->MoveTo( v3Loc );
}
}
break;
default:
//do nothing
break;
}
}
/**
* Handle and act on the results from the user clicking on the model browser.
* @param v2VidCoords the video coordinates
* @return true if used the click
*/
bool ModelsGame::_HandleModelBrowserActions( Vector<2> v2VidCoords )
{
ModelBrowser::CLICK_STATUS cs = mModelBrowser.HandleClick( v2VidCoords );
if(cs == ModelBrowser::MODEL)
{
const ModelData * pModelData = mModelBrowser.GetSelectedModelData();
if( pModelData ) {
mData.AddModel( pModelData->sLocation, pModelData->sModelFile, pModelData->sName, pModelData->v3Rotation );
}
}
else if( cs == ModelBrowser::CANCEL )
{
if( mData.NumModels() != 0 ) {
mData.mDisplayState = ModelsGameData::ControlState;
}
}
else if( cs == ModelBrowser::OK ) {
}
else {
return false;
}
return true;
}
/**
* Select the closest map point to the ray from click
*/
Vector<3> ModelsGame::_SelectClosestMapPoint( Vector<3> v3RayDirnW )
{
if(!mpMap) {
return makeVector( 0, 0, 0 );
}
Vector<3> v3CamPos = mse3CfW.inverse().get_translation();
Vector<3> v3Best;
MapPoint* pBest = NULL;
double dBest = -999999.8;
for( size_t i = 0; i < mpMap->vpPoints.size(); i++ )
{
MapPoint &p = *(mpMap->vpPoints[i]);
if( p.nSourceLevel != 0 ) {
continue;
}
Vector<3> v3RayToPoint = p.v3WorldPos - v3CamPos;
normalize(v3RayToPoint);
if( (v3RayToPoint * v3RayDirnW) > dBest )
{
dBest = v3RayToPoint * v3RayDirnW;
pBest = &p;
}
}
return pBest->v3WorldPos;
}
/**
* Draw a coordinate frame
*/
void ModelsGame::_DrawAxes()
{
float w = 0;
glGetFloatv(GL_LINE_WIDTH, &w);
glLineWidth(5);
const float len = 0.5;
//draw axis
glBegin(GL_LINES);
glColor4f(1.0f,0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(len,0.0f,0.0f);
glColor4f(0.0f,1.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,len,0.0f);
glColor4f(0.0f,0.0f,1.0f,1.0f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,len);
glEnd();
glLineWidth(w);
}
/**
* Draw the selection target
*/
void ModelsGame::_DrawSelectedTarget()
{
glPushMatrix();
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_NORMALIZE);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glDisable(GL_LIGHTING);
//draw the selected icon
glColor4f(1,1,1,1);
glBindTexture ( GL_TEXTURE_2D , mnTargetTex );
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0); glVertex3f( 0.5, 0.5, 0.0);
glTexCoord2f(1.0,0.0); glVertex3f( 0.5, -0.5, 0.0);
glTexCoord2f(1.0,1.0); glVertex3f(-0.5, -0.5, 0.0);
glTexCoord2f(0.0,1.0); glVertex3f(-0.5, 0.5, 0.0);
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glPopMatrix();
}
/**
* Save the game to disk
* @param sMapPath Path to the map
* @return The file name
*/
std::string ModelsGame::Save(std::string sMapPath)
{
std::string sFileName = "ModelsGame.xml";
std::string sFilePath = sMapPath + "/" + sFileName;
mData.Save( sFilePath );
return sFileName;
}
/**
* Load a game from disk
* @param sDataPath The path to the game file.
*/
void ModelsGame::Load(std::string sDataPath)
{
mData.Load( sDataPath );
}
}