-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstarter_app.cpp
More file actions
356 lines (269 loc) · 9.29 KB
/
starter_app.cpp
File metadata and controls
356 lines (269 loc) · 9.29 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
#include "starter_app.h"
#include <system/platform.h>
#include <graphics/sprite_renderer.h>
#include <graphics/texture.h>
#include <graphics/mesh.h>
#include <graphics/primitive.h>
#include <assets/png_loader.h>
#include <graphics/image_data.h>
#include <graphics/font.h>
#include <graphics/scene.h>
#include <maths/vector2.h>
#include <input/input_manager.h>
#include <input/sony_controller_input_manager.h>
#include <input/keyboard.h>
#include <maths/math_utils.h>
#include <graphics/renderer_3d.h>
#include "VirtualSystem.h"
#include "VirtualSonySample.h"
#include <vector>
StarterApp::StarterApp(gef::Platform& platform) :
Application(platform),
sprite_renderer_(NULL),
font_(NULL),
renderer_3d_(NULL)
{
}
bool StarterApp::sampleIsMarkerFound ( int idx )
{
return virtualSystem_->IsMarkerFound(idx);
}
void StarterApp::sampleGetTransform ( int idx, gef::Matrix44* mat )
{
virtualSystem_->GetMarkerTransform ( idx, mat );
}
void StarterApp::Init()
{
sprite_renderer_ = gef::SpriteRenderer::Create(platform_);
renderer_3d_ = gef::Renderer3D::Create(platform_);
input_manager_ = gef::InputManager::Create(platform_);
primitive_builder_ = new PrimitiveBuilder ( platform_ );
InitFont();
SetupCamera();
SetupLights();
virtualSystem_ = VirtualSystem::Create ();
virtualSystem_->Init ( primitive_builder_ );
////////////////////////////////////////////////////////////
// GAME LOGIC //////////////
testObject_ = new GameObject;
testObject_->position_ = gef::Vector4 ( 0.0f, 0.0f, 0.0f );
//testObject_->scale_ = gef::Vector4 ( 0.05f, 0.05f, 0.05f );
testObject_->scale_ = gef::Vector4 ( 1.0f, 1.0f, 1.0f );
//testObject_->set_mesh ( primitive_builder_->GetDefaultCubeMesh () );
testObject_->set_mesh ( primitive_builder_->CreateBoxMesh ( gef::Vector4 ( 0.059f, 0.059f, 0.001f ), gef::Vector4 ( 0.0f, 0.0f, 0.0f ) ) );
//////////////////////////////////////////////////////////
// initialise sony framework
sampleInitialize ();
smartInitialize ();
// reset marker tracking
AppData* dat = sampleUpdateBegin ();
smartTrackingReset ();
sampleUpdateEnd ( dat );
///////////////////////////////////////////////////////
}
void StarterApp::CleanUp()
{
//////////////////////////////////////////////////////////
smartRelease ();
sampleRelease ();
////////////////////////////////////////////////////////////
// GAME LOGIC //////////////
delete testObject_;
testObject_ = NULL;
//////////////////////////////////////////////////////////
virtualSystem_->CleanUp ();
delete virtualSystem_;
virtualSystem_ = NULL;
delete primitive_builder_;
primitive_builder_ = NULL;
CleanUpFont();
delete input_manager_;
input_manager_ = NULL;
delete sprite_renderer_;
sprite_renderer_ = NULL;
delete renderer_3d_;
renderer_3d_ = NULL;
}
bool StarterApp::Update(float frame_time)
{
fps_ = 1.0f / frame_time;
// read input devices
if (input_manager_)
{
input_manager_->Update ();
// controller input
gef::SonyControllerInputManager* controller_manager = input_manager_->controller_input ();
if (controller_manager)
{
}
// keyboard input
gef::Keyboard* keyboard = input_manager_->keyboard ();
virtualSystem_->ProcessKeyboardInput ( keyboard, frame_time );
}
virtualSystem_->Update ( frame_time );
//////////////////////////////////////////////////////////
AppData* dat = sampleUpdateBegin ();
// use the tracking library to try and find markers
smartUpdate ( dat->currentImage );
sampleUpdateEnd ( dat );
////////////////////////////////////////////////////////////
// GAME LOGIC //////////////
//// check to see if a particular marker can be found
//if (sampleIsMarkerFound ( 0 ))
//{
// // marker is being tracked, get its transform
// gef::Matrix44 marker_transform;
// sampleGetTransform (
// 0,
// &marker_transform );
// // set the transform of the 3D mesh instance to draw on
// // top of the marker
// gef::Matrix44 trans = marker_transform;
// gef::Matrix44 scale;
// scale.Scale ( gef::Vector4 (0.1f, 0.1f, 0.1f ) );
// gef::Vector4 position = trans.GetTranslation ();
// position.set_y ( position.y () + 0.15f );
// trans = scale * trans;
// trans.SetTranslation ( position );
// testObject_->set_transform ( trans );
//}
for (int i = 0; i < 6; i++)
{
if (sampleIsMarkerFound ( i ))
{
// marker is being tracked, get its transform
gef::Matrix44 marker_transform;
sampleGetTransform ( i, &marker_transform );
// set the transform of the 3D mesh instance to draw on
// top of the marker
gef::Vector4 position = marker_transform.GetTranslation ();
position.set_y ( position.y () + 0.10f );
marker_transform.SetTranslation ( position );
testObject_->set_transform ( marker_transform );
}
}
//////////////////////////////////////////////////////////
return true;
}
void StarterApp::Render()
{
//////////////////////////////////////////////////////////
AppData* dat = sampleRenderBegin ();
gef::Matrix44 projection_matrix;
projection_matrix = platform_.PerspectiveProjectionFov(camera_fov_, (float)platform_.width() / (float)platform_.height(), near_plane_, far_plane_);
renderer_3d_->set_projection_matrix(projection_matrix);
// draw meshes here
renderer_3d_->Begin();
virtualSystem_->Render ( renderer_3d_ );
gef::Matrix44 view_matrix;
view_matrix.SetIdentity ();
renderer_3d_->set_view_matrix ( view_matrix );
////////////////////////////////////////////////////////////
// GAME LOGIC //////////////
renderer_3d_->DrawMesh ( *testObject_ );
////////////////////////////////////////////////////////////
renderer_3d_->End();
// setup the sprite renderer, but don't clear the frame buffer
// draw 2D sprites here
sprite_renderer_->Begin(false);
DrawHUD();
sprite_renderer_->End();
sampleRenderEnd ();
//////////////////////////////////////////////////////////
}
void StarterApp::InitFont()
{
font_ = new gef::Font(platform_);
font_->Load("comic_sans");
}
void StarterApp::CleanUpFont()
{
delete font_;
font_ = NULL;
}
void StarterApp::DrawHUD()
{
if(font_)
{
// display frame rate
font_->RenderText(sprite_renderer_, gef::Vector4(850.0f, 510.0f, -0.9f), 1.0f, 0xffffffff, gef::TJ_LEFT, "FPS: %.1f", fps_);
}
}
void StarterApp::SetupLights()
{
gef::PointLight default_point_light;
default_point_light.set_colour(gef::Colour(0.7f, 0.7f, 1.0f, 1.0f));
default_point_light.set_position(gef::Vector4(-300.0f, -500.0f, 100.0f));
gef::Default3DShaderData& default_shader_data = renderer_3d_->default_shader_data();
default_shader_data.set_ambient_light_colour(gef::Colour(0.5f, 0.5f, 0.5f, 1.0f));
default_shader_data.AddPointLight(default_point_light);
}
void StarterApp::SetupCamera()
{
// initialise the camera settings
camera_eye_ = gef::Vector4(5.0f, 5.0f, 215.0f);
camera_lookat_ = gef::Vector4(0.0f, 0.0f, 0.0f);
camera_up_ = gef::Vector4(0.0f, 1.0f, 0.0f);
camera_fov_ = gef::DegToRad(45.0f);
near_plane_ = 0.01f;
far_plane_ = 100.f;
}
void StarterApp::UpdateCamera ()
{
// initialise the camera settings
camera_eye_ = gef::Vector4 ( 1.5f, 1.0f, 1.5f );
camera_lookat_ = gef::Vector4 ( 0.0f, 0.0f, 0.0f );
camera_up_ = gef::Vector4 ( 0.0f, 1.0f, 0.0f );
camera_fov_ = gef::DegToRad ( 45.0f );
near_plane_ = 0.01f;
far_plane_ = 1000.f;
}
gef::Mesh* StarterApp::GetFirstMesh ( gef::Scene* scene )
{
gef::Mesh* mesh = NULL;
if (scene)
{
// now check to see if there is any mesh data in the file, if so lets create a mesh from it
if (scene->mesh_data.size () > 0)
mesh = scene->CreateMesh ( platform_, scene->mesh_data.front () );
}
return mesh;
}
void StarterApp::ReadSceneAndAssignFirstMesh ( const char* filename, gef::Scene** scene, gef::Mesh** mesh )
{
gef::Scene* scn = new gef::Scene;
scn->ReadSceneFromFile ( platform_, filename );
// we do want to render the data stored in the scene file so lets create the materials from the material data present in the scene file
scn->CreateMaterials ( platform_ );
*mesh = GetFirstMesh ( scn );
*scene = scn;
}
bool StarterApp::IsColliding_SphereToSphere ( const gef::MeshInstance& meshInstance1, const gef::MeshInstance& meshInstance2 )
{
gef::Sphere sphere1 = meshInstance1.mesh ()->bounding_sphere ();
gef::Sphere sphere2 = meshInstance2.mesh ()->bounding_sphere ();
gef::Sphere sphere1_transformed = sphere1.Transform ( meshInstance1.transform () );
gef::Sphere sphere2_transformed = sphere2.Transform ( meshInstance2.transform () );
gef::Vector4 offset = sphere1_transformed.position () - sphere2_transformed.position ();
float distance = sqrtf(offset.x() * offset.x() + offset.y() * offset.y() + offset.z() * offset.z());
float combined_radii = sphere1_transformed.radius () + sphere2_transformed.radius ();
return distance < combined_radii;
}
bool StarterApp::IsColliding_AABBToAABB ( const gef::MeshInstance& meshInstance1, const gef::MeshInstance& meshInstance2 )
{
gef::Aabb Aabb1 = meshInstance1.mesh ()->aabb ();
gef::Aabb Aabb2 = meshInstance2.mesh ()->aabb ();
gef::Aabb Aabb1_t = Aabb1.Transform ( meshInstance1.transform () );
gef::Aabb Aabb2_t = Aabb2.Transform ( meshInstance2.transform () );
if (Aabb1_t.max_vtx ().x () > Aabb2_t.min_vtx ().x () &&
Aabb1_t.min_vtx ().x () < Aabb2_t.max_vtx ().x () &&
Aabb1_t.max_vtx ().y () > Aabb2_t.min_vtx ().y () &&
Aabb1_t.min_vtx ().y () < Aabb2_t.max_vtx ().y () &&
Aabb1_t.max_vtx ().z () > Aabb2_t.min_vtx ().z () &&
Aabb1_t.min_vtx ().z () < Aabb2_t.max_vtx ().z ()
)
{
return true;
}
return false;
}