-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneModel.cpp
More file actions
366 lines (299 loc) · 9.63 KB
/
SceneModel.cpp
File metadata and controls
366 lines (299 loc) · 9.63 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
///////////////////////////////////////////////////
//
// Hamish Carr
// October, 2023
//
// ------------------------
// SceneModel.cpp
// ------------------------
//
// The model of the scene
//
//
///////////////////////////////////////////////////
#include "SceneModel.h"
#include <math.h>
#include <Quaternion.h>
#include <cmath>
#include <algorithm>
#define blendingFrames 5
#define moveSpeed 0.3
// three local variables with the hardcoded file names
const char* flatLandModelName = "./models/flatland.dem";
const char* stripeLandModelName = "./models/stripeland.dem";
const char* rollingLandModelName = "./models/rollingland.dem";
const char* sphereModelName = "./models/spheroid.face";
const char* dodecahedronModelName = "./models/dodecahedron.face";
// four type of character's animation
const char* motionBvhStand = "./models/stand.bvh";
const char* motionBvhRun = "./models/fast_run.bvh";
// the speed of camera movement
const float cameraSpeed = 5.0;
// this is 24 fps nominal speed
const float frameTime = 0.0416667;
const Homogeneous4 sunDirection(0.5, -0.5, 0.3, 0.0);
const GLfloat groundColour[4] = { 0.2, 0.5, 0.2, 1.0 };
const GLfloat ballColour[4] = { 0.6, 0.6, 0.6, 1.0 };
const GLfloat characterColour[4] = { 1.0, 1.0, 0.0, 1.0 };
const GLfloat sunAmbient[4] = { 0.1, 0.1, 0.1, 1.0 };
const GLfloat sunDiffuse[4] = { 0.7, 0.7, 0.7, 1.0 };
const GLfloat blackColour[4] = { 0.0, 0.0, 0.0, 1.0 };
const float gravity = -9.8f;
const float elasticity = 0.6f;
const Cartesian3 originPos(5.f, 0.f, 10.f);
// constructor
SceneModel::SceneModel()
{ // constructor
// load landscape models from files
flatLandModel.ReadFileTerrainData(flatLandModelName, 3);
stripeLandModel.ReadFileTerrainData(stripeLandModelName, 3);
rollingLandModel.ReadFileTerrainData(rollingLandModelName, 3);
// set the reference for the terrain model to use
this->activeLandModel = &flatLandModel;
// load BVH models
standMotion.ReadFileBVH(motionBvhStand);
fast_runMotion.ReadFileBVH(motionBvhRun);
// load models
sphereModel.ReadFileIndexedFace(sphereModelName);
dodeModel.ReadFileIndexedFace(dodecahedronModelName);
this->activeCollisionModel = &sphereModel;
// initialize quat sets
GetquatStand();
GetquatRunt(0, quatRun0);
// initialize origin motion
this->activeBVHMotion = &standMotion;
flag = 3;
flagCheck = 1;
blendingframeNumber = 0;
this->globalPosition = Cartesian3(0.f, 0.f, 0.f);
// initialize posision of model
this->modelPosition = originPos;
// set the initial view matrix
viewMatrix = Matrix4::Translate(Cartesian3(0.0, 15.0, -10.0));
// and set the frame number to 0
frameNumber = 0;
// call the reset routine to initialise the ball position
ResetPhysics();
} // constructor
// routine that updates the scene for the next frame
void SceneModel::Update()
{ // Update()
Render();
} // Update()
// routine to tell the scene to render itself
void SceneModel::Render()
{ // Render()
// enable Z-buffering
glEnable(GL_DEPTH_TEST);
// set lighting parameters
glShadeModel(GL_FLAT);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, sunAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, sunDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, blackColour);
glLightfv(GL_LIGHT0, GL_EMISSION, blackColour);
// background is sky-blue
glClearColor(0.7, 0.7, 1.0, 1.0);
// clear the buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set the modelview matrix
glMatrixMode(GL_MODELVIEW);
// start with the identity
glLoadIdentity();
// add the final rotation from z-up to z-backwords
glRotatef(-90.0, 1.0, 0.0, 0.0);
// now compute the view matrix by combining camera translation & rotation
columnMajorMatrix columnMajorViewMatrix = viewMatrix.columnMajor();
glMultMatrixf(columnMajorViewMatrix.coordinates);
// set the light position
glLightfv(GL_LIGHT0, GL_POSITION, &(sunDirection.x));
// and set a material colour for the ground
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, groundColour);
glMaterialfv(GL_FRONT, GL_SPECULAR, blackColour);
glMaterialfv(GL_FRONT, GL_EMISSION, blackColour);
// render the terrain
activeLandModel->Render();
//and set a material colour for character
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, characterColour);
glPushMatrix();
// update the vertical position of character
globalPosition.z = activeLandModel->getHeight(globalPosition.x, globalPosition.y);
glTranslatef(globalPosition.x, globalPosition.y, globalPosition.z);
switch (flag)
{
// play stand2run blending
case 1:
activeBVHMotion->blendingRender(blendingFrames, blendingframeNumber++, quatStand, quatRun0);
if (blendingframeNumber == blendingFrames)
{
// Manually execute case 3 logic
activeBVHMotion->Render(frameNumber);
frameNumber = (frameNumber + 1) % activeBVHMotion->frame_count;
flag = 3;
blendingframeNumber = 0;
}
break;
// play run2stand blending
case 2:
activeBVHMotion->blendingRender(blendingFrames, blendingframeNumber++, quatlast, quatStand);
if (blendingframeNumber == blendingFrames)
{
// Manually execute case 3 logic
activeBVHMotion->Render(frameNumber);
frameNumber = (frameNumber + 1) % activeBVHMotion->frame_count;
flag = 3;
blendingframeNumber = 0;
}
break;
// normal
case 3:
// render the skeleton
activeBVHMotion->Render(frameNumber);
//update frameNumber
frameNumber = (frameNumber + 1) % activeBVHMotion->frame_count;
}
glPopMatrix();
// render collision model
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, ballColour);
velocity.z += gravity * frameTime;
modelPosition.x += velocity.x * frameTime;
modelPosition.y += velocity.y * frameTime;
modelPosition.z += velocity.z * frameTime;
// simple collision test
if (modelPosition.z - 1 <= activeLandModel->getHeight(modelPosition.x, modelPosition.y)) {
Cartesian3 normal = this->activeLandModel->getNormal(modelPosition.x, modelPosition.y).unit();
modelPosition.z = 1 + activeLandModel->getHeight(modelPosition.x, modelPosition.y);
float dotProduct = velocity.dot(normal);
Cartesian3 velocityNormal = normal * dotProduct;
Cartesian3 velocityTangent = velocity - velocityNormal;
velocity = velocityTangent - velocityNormal * elasticity;
std::cout << velocity << std::endl;
}
glPushMatrix();
glTranslatef(modelPosition.x, modelPosition.y, modelPosition.z);
activeCollisionModel->Render();
glPopMatrix();
// collision test
CollisionTest();
} // Render()
// character control events: W for forward
void SceneModel::EventCharacterForward()
{ // EventCharacterForward()
if (flagCheck)
{
flag = 1;
flagCheck = false;
}
this->activeBVHMotion->forward = true;
this->activeBVHMotion = &fast_runMotion;
globalPosition.x += moveSpeed;
} // EventCharacterForward()
// character control events: S for backward
void SceneModel::EventCharacterBackward()
{ // EventCharacterBackward()
if (flagCheck)
{
flag = 1;
flagCheck = false;
}
this->activeBVHMotion->forward = false;
this->activeBVHMotion = &fast_runMotion;
globalPosition.x -= moveSpeed;
} // EventCharacterBackward()
// character control events: release
void SceneModel::EventCharacterStand()
{
// find last frame of run motion
GetquatRunt(frameNumber, quatlast);
flagCheck = true;
this->flag = 2;
frameNumber = 0;
this->activeBVHMotion = &standMotion;
}
void SceneModel::ResetGame()
{ // ResetGame()
//while ()
//{
// if (this->activeBVHMotion->forward = true)
// {
// EventCharacterForward();
// }
// else
// {
// EventCharacterBackward();
// }
//}
this->ResetPhysics();
} // ResetGame()
// routine to reset the simulation
void SceneModel::ResetPhysics()
{ // ResetPhysics()
modelPosition = originPos;
velocity = Cartesian3();
std::cout << "Resetting Physics." << std::endl;
} // ResetPhysics()
// routine to switch between flat land and rolling land
void SceneModel::SwitchLand()
{ // SwitchLand()
// toggle between terrains
if (activeLandModel == &flatLandModel)
activeLandModel = &stripeLandModel;
else if (activeLandModel == &stripeLandModel)
activeLandModel = &rollingLandModel;
else if (activeLandModel == &rollingLandModel)
activeLandModel = &flatLandModel;
ResetPhysics();
} // SwitchLand()
// routine to switch between sphere and dodecahedron
void SceneModel::SwitchModel()
{ // SwitchModel()
if (activeCollisionModel == &sphereModel)
activeCollisionModel = &dodeModel;
else if (activeCollisionModel == &dodeModel)
activeCollisionModel = &sphereModel;
// and reset the physics
ResetPhysics();
} // SwitchModel()
// routine to get stand quaternion set
void SceneModel::GetquatStand()
{
for (int i = 0; i < standMotion.boneRotations[0].size(); i++)
{
quatStand.push_back(standMotion.euler2quat(
standMotion.boneRotations[0][i].x,
standMotion.boneRotations[0][i].y,
standMotion.boneRotations[0][i].z)
);
}
}
// routine to get run quaternion set at frame t
void SceneModel::GetquatRunt(int t, std::vector<Quaternion> & quatSet)
{
// reset quatSet
quatSet.clear();
for (int i = 0; i < fast_runMotion.boneRotations[0].size(); i++)
{
quatSet.push_back(fast_runMotion.euler2quat(
fast_runMotion.boneRotations[t][i].x,
fast_runMotion.boneRotations[t][i].y,
fast_runMotion.boneRotations[t][i].z)
);
}
}
// collision test between model and character
void SceneModel::CollisionTest()
{
// distance between sphere center and cylinder axis
float dis = std::sqrt((globalPosition.x - modelPosition.x) * (globalPosition.x - modelPosition.x) +
(globalPosition.y - modelPosition.y) * (globalPosition.y - modelPosition.y));
if (dis < 1.f + 0.5f)
{
float z_closest = std::clamp(modelPosition.z, globalPosition.z, globalPosition.z + 5);
if (abs(z_closest - modelPosition.z) < 1.f)
{
collisionCount++;
std::cout << "collision "<< collisionCount <<" happened!" << std::endl;
}
}
}