-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskymap_ocean_cat_wander_island.cpp
418 lines (356 loc) · 11.1 KB
/
skymap_ocean_cat_wander_island.cpp
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
/*********************************************************
Version 1.2
Code provided by Gustavo Carneiro, Anthony Dick
for the course
COMP SCI 3014/7090 Computer Graphics
School of Computer Science
University of Adelaide
Permission is granted for anyone to copy, use, modify, or distribute this
program and accompanying programs and documents for any purpose, provided
this copyright notice is retained and prominently displayed, along with
a note saying that the original programs are available from the aforementioned
course web page.
The programs and documents are distributed without any warranty, express or
implied. As the programs were written for research purposes only, they have
not been tested to the degree that would be advisable in any important
application. All use of these programs is entirely at the user's own risk.
*********************************************************/
/**
* Skymap
*/
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <string.h>
#include <unistd.h>
#include <GL/glew.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "shader.h"
#include "skybox.h"
#include "InputState.h"
#include "Camera.h"
#include "Ocean.h"
#include "cat.h"
#include "wander.h"
#include "Island.h"
#include "Audio.h"
#define VALS_PER_VERT 3
#define VALS_PER_TEX 2
#define VALS_PER_COLOUR 4
#define CUBE_NUM_TRIS 12 // number of triangles in a cube (2 per face)
#define CUBE_NUM_VERTICES 8 // number of vertices in a cube`
// A cube
unsigned int cubeVaoHandle;
// Our shader program
GLuint programID;
unsigned int cubeMapTextureHandle;
// An accumulation of the input for our camera rotation
float xRot = 0.0f;
float yRot = 0.0f;
int winX = 640;
int winY = 480;
//
// Switch between orthographic and perspective camera.
// Also ensure the cube stays the same shape if window resized
//
/**
* Sets the shader uniforms and generic attributes
* @param program, Shader program object to use
*/
Skybox* m_skybox;
InputState Input;
Camera* m_camera;
Ocean* m_ocean;
Cat* m_cat;
SteeringCat *m_catSteer;
Island *m_Island;
AudioLoop *m_Audio;
float strafe_speed;
float travel_speed;
float groundPlane = 1.0f;
int setup() {
m_camera = new Camera();
travel_speed=0.0f;
strafe_speed=0.0f;
m_camera->PositionCamera(-10.0f, 5.0f, 20.0f, 0.42f, 0.105f);
//m_camera->SetSpeed(0.0f);
m_camera->SetPerspective(glm::radians(53.13f), (float) winX / winY, 0.1f, 10000.0f );
m_skybox = new Skybox(
"san_francisco/posx.jpg",
"san_francisco/posy.jpg",
"san_francisco/posz.jpg",
"san_francisco/negx.jpg",
"san_francisco/negy.jpg",
"san_francisco/negz.jpg",
10.0f
);
m_ocean = new Ocean(
"san_francisco/posx.jpg",
"san_francisco/posy.jpg",
"san_francisco/posz.jpg",
"san_francisco/negx.jpg",
"san_francisco/negy.jpg",
"san_francisco/negz.jpg",
0.0f,
0.0f,
0.0f,
1000.0f
);
/*
public:
Cat( const char* in_skeleton,
const char* in_mesh,
const char* in_walk_F_anim,
const char* in_walk_L_anim,
const char* in_walk_R_anim,
const char* in_material,
const char* in_tex_albedo,
float in_cycle_duration,
float in_scale);
*/
glm::mat4 initialLocation = glm::mat4();
m_cat = new Cat(
"jnx.csf",
"jnx.cmf",
"walk.caf",
"walk_L.caf",
"walk_R.caf",
"jnx.crf",
"test.tga",
1.33333333f,
0.1f,
initialLocation);
glm::vec3 initialPosition=glm::vec3(0.0,0.0,0.0);
glm::vec3 initialVelocity=glm::vec3(0.5,0.0,0.0);
float initialMass =5.0;
m_catSteer = new SteeringCat (initialPosition,
initialVelocity,
initialMass);
/*
Island::Island (const char* heightMap,
float scalex,
float scaley,
float scalez,
float posx,
float posy,
float posz
*/
const char* hieightMap = "islandHieght.tga";
m_Island = new Island (
"islandHieght.tga",
40.0,
1.0,
1.0,
0.0,
groundPlane,
0.0
);
m_Audio = new AudioLoop("Intro.mp3");
m_Audio->playSoundOnce();
return 0; // return success
}
const int TIMERMSECS = 400;
float animation_time = 0;
const float animation_step = 0.03f;
void animate(int id){
float time_elapsed = TIMERMSECS/1000.0;
float current_step = animation_step* time_elapsed;
animation_time += current_step;
glutPostRedisplay();
travel_speed*=0.8f;
strafe_speed*=0.8f;
}
#define ANG_SCALE 28.0f;
/**
* Renders a frame of the state and shaders we have set up to the window
*/
void render() {
m_camera->MoveCamera(travel_speed);
m_camera->Strafe(strafe_speed);
m_camera->SetViewByMouse(Input.deltaX, Input.deltaY);
// GL state
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutTimerFunc(17,animate,0);
m_catSteer->update();
m_skybox->projectionMatrix = m_camera->GetProjectionMatrix();
m_skybox->viewMatrix = m_camera->GetRotationMatrix();
m_skybox->render();
m_ocean->projectionMatrix = m_camera->GetProjectionMatrix();
m_ocean->viewMatrix = m_camera->GetViewMatrix();
m_ocean->cameraPosition = m_camera->GetPosition();
m_Island->projectionMatrix = m_camera->GetProjectionMatrix();
m_Island->viewMatrix = m_camera->GetViewMatrix();
m_Island->cameraPosition = m_camera->GetPosition();
m_Island->render();
m_cat->viewMatrix = m_camera->GetViewMatrix();
m_cat->projectionMatrix = m_camera->GetProjectionMatrix();
m_cat->location = glm::translate(glm::mat4(),glm::vec3(0.0,groundPlane,0.0))*m_catSteer->getTransform();
m_cat->direction = m_catSteer->getAngularVelocity()*ANG_SCALE;
//std::cout << m_cat->direction << std::endl;
m_cat->onIdle();
m_cat->renderModel();
#ifdef DEBUG
std::cout << "Position Camera:" << m_ocean->cameraPosition.x << "," << m_ocean->cameraPosition.y << "," << m_ocean->cameraPosition.z << "," << std::endl;
#endif
m_ocean->render();
glFlush();
glutSwapBuffers();
#ifdef DEBUG
std::cout << "Yaw:" << m_camera->GetYaw() << std::endl;
std::cout << "Pitch:" << m_camera->GetPitch() << std::endl;
std::cout << "Position: " <<
m_camera->GetPosition()[0] << "," <<
m_camera->GetPosition()[1] << "," <<
m_camera->GetPosition()[2] << "," << std::endl;
#endif
}
float STRAFE_INC=0.15;
float FORWARD_INC=0.15;
void SpecialFunc( int key, int x, int y )
{
switch(key)
{
case GLUT_KEY_LEFT:
yRot -= 2.0;
strafe_speed += -STRAFE_INC;
break;
case GLUT_KEY_RIGHT:
yRot += 2.0;
strafe_speed += STRAFE_INC;
break;
case GLUT_KEY_UP:
xRot += 2.0;
travel_speed += FORWARD_INC;
break;
case GLUT_KEY_DOWN:
xRot -= 2.0;
travel_speed += -FORWARD_INC;
break;
}
glutPostRedisplay();
}
/**
* Called while a keyboard key press is detected
* This GLUT functions is not OpenGL specific, but allows interactivity to our programs
* @param key, the keyboard key that made the event
* @param x, not used
* @param y, not used
*/
void keyboardDown(unsigned char key, int x, int y) {
// We simply check the key argument against characters we care about, in this case A and D
switch(key)
{
case 27: // escape key pressed
exit(0);
break;
case 'd': // Toggle depth test
if ( glIsEnabled( GL_DEPTH_TEST ) ) {
glDisable( GL_DEPTH_TEST );
}
else {
glEnable( GL_DEPTH_TEST );
}
glutPostRedisplay();
break;
glutPostRedisplay();
break;
}
}
void reshapeFunc( int x, int y )
{
winX = x;
winY = y;
glViewport(0, 0, (GLsizei)x, (GLsizei)y); // Set our viewport to the size of our window
m_camera->SetPerspective(glm::radians(53.13f), (float) winX / winY, 0.05f, 100.0f );
}
void MouseFunc(int button, int state, int x, int y)
{
// Update a global set of variables
// to avoid cluttering up the callbacks
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
Input.lMousePressed = true;
Input.prevX = x;
Input.prevY = y;
}
else
Input.lMousePressed = false;
Input.deltaX = 0.0f;
Input.deltaY = 0.0f;
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN){
Input.rMousePressed = true;
Input.prevX = x;
Input.prevY = y;
}
else
Input.rMousePressed = false;
Input.deltaX = 0.0f;
Input.deltaY = 0.0f;
break;
default:
break;
}
}
void MotionFunc(int x, int y)
{
int xDiff = x - Input.prevX;
int yDiff = y - Input.prevY;
Input.deltaX += xDiff;
Input.deltaY += yDiff;
Input.prevX = x;
Input.prevY = y;
}
/**
* Program entry. Sets up OpenGL state, GLSL Shaders and GLUT window and function call backs
* Takes no arguments
*/
int main(int argc, char **argv) {
std::cout << "Controls: arrow keys rotate cube\n";
// Set up GLUT window
glutInit(&argc, argv);
glutInitWindowPosition(100, 0);
glutInitWindowSize(winX, winY);
#ifdef __APPLE__
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_3_2_CORE_PROFILE | GLUT_MULTISAMPLE);
#else
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
#endif
glutCreateWindow("Cat Picnic");
glClearColor(0.2f,0.2f,0.2f,1.0f);
/*
// Initialize GLEW
glewExperimental = true; // Needed for core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
programID = LoadShaders("sky.vert", "sky.frag");
if (programID == 0)
return 1;
glUseProgram(programID);
*/
if (setup() !=0)
return 1;
// Here we set a new function callback which is the GLUT handling of keyboard input
glutKeyboardFunc(keyboardDown);
glutSpecialFunc(SpecialFunc);
glutMouseFunc(MouseFunc);
glutMotionFunc(MotionFunc);
glutDisplayFunc(render);
glutReshapeFunc(reshapeFunc);
glutMainLoop();
return 0;
}