-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlayState.cpp
194 lines (140 loc) · 4.72 KB
/
PlayState.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
#include "PlayState.h"
GLuint program;
glm::mat4 projection;
GLuint objVAO;
int triangles;
Camera camera;
Maze myMaze;
Keyboard keys;
#define CELL_SIZE 100.0f
#define FOV 45.0f
#define NEAR_CLIP 1.0f
#define FAR_CLIP (100.0f*CELL_SIZE)
#define SENSITIVITY 0.9f
int width = 0;
int height = 0;
void PlayState::enter() {
std::cout << "Player at ST position. Reach FN and Press ESC!\n";
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
glutSetCursor(GLUT_CURSOR_NONE);
int vs = buildShader(GL_VERTEX_SHADER, (char *)"shaders/wall.vs");
int fs = buildShader(GL_FRAGMENT_SHADER, (char *)"shaders/wall.fs");
program = buildProgram(vs, fs, 0);
//dumpProgram(program, (char *)"Template"); // output information on the shader program
// init Maze
srand(time(NULL)); // seed random number generator
myMaze.generate(25, 25);
myMaze.print();
// init OpenGL
GLuint vbuffer;
GLuint ibuffer;
GLint vPosition;
glGenVertexArrays(1, &objVAO);
glBindVertexArray(objVAO);
// get the vertices and indices from myMaze
std::vector<glm::vec4> vecVertices = myMaze.getVertices(CELL_SIZE);
std::vector<GLuint> vecIndexes = myMaze.getIndexes();
triangles = vecIndexes.size()/3;
glGenBuffers(1, &vbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vbuffer);
glBufferData(GL_ARRAY_BUFFER, vecVertices.size()*sizeof(glm::vec4), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, vecVertices.size()*sizeof(glm::vec4), &vecVertices[0]);
glGenBuffers(1, &ibuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vecIndexes.size()*sizeof(GLuint), &vecIndexes[0], GL_STATIC_DRAW);
glUseProgram(program);
vPosition = glGetAttribLocation(program, "vPosition");
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vPosition);
// set play position
player.setPos(glm::vec3(1.5*CELL_SIZE, 0.5f*CELL_SIZE, 0.5f*CELL_SIZE));
}
void PlayState::leave() {
}
void PlayState::update() {
// update mouse
int deltaMovex = mouse.getx() - width/2;
float deltaMovey = mouse.gety() - height/2;
float mouseMoveRatiox = (float)deltaMovex/width;
float mouseMoveRatioy = (float)deltaMovey/height;
player.rotateRight(mouseMoveRatiox*SENSITIVITY);
player.rotateUp(-mouseMoveRatioy*SENSITIVITY);
mouse.warp(width/2, height/2);
// User input for player
if(keys.isDown('a'))
player.moveTowards(-player.getRightXZ());
if(keys.isDown('d'))
player.moveTowards(player.getRightXZ());
if(keys.isDown('w'))
player.moveTowards(player.getForwardXZ());
if(keys.isDown('s'))
player.moveTowards(-player.getForwardXZ());
if(keys.isDown('j'))
player.rotateRight(-player.ROT_SPEED);
if(keys.isDown('l'))
player.rotateRight(player.ROT_SPEED);
if(keys.isDown('i'))
player.rotateUp(player.ROT_SPEED);
if(keys.isDown('k'))
player.rotateUp(-player.ROT_SPEED);
// player step
glm::vec3 pos0 = player.getPos();
player.step(1000/60);
glm::vec3 pos = player.getPos();
glm::vec3 vel = player.getVel();
// collision
// x-axis
float PLAYER_RAD = 5.0f;
if(myMaze.isCellWall(floor(pos0.z/CELL_SIZE), floor((pos.x + PLAYER_RAD)/CELL_SIZE)) ||
myMaze.isCellWall(floor(pos0.z/CELL_SIZE), floor((pos.x - PLAYER_RAD)/CELL_SIZE))) {
vel.x = 0.0f;
pos.x = pos0.x;
}
if(myMaze.isCellWall(floor((pos.z + PLAYER_RAD)/CELL_SIZE), floor(pos0.x/CELL_SIZE)) ||
myMaze.isCellWall(floor((pos.z - PLAYER_RAD)/CELL_SIZE), floor(pos0.x/CELL_SIZE))) {
vel.z = 0.0f;
pos.z = pos0.z;
}
player.setPos(pos);
player.setVel(vel);
// update camera to player position
camera.setPosition(player.getPos());
camera.setForward(player.getForward());
camera.setUp(player.getUp());
}
void PlayState::render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
// Update view matrix
glm::mat4 view = camera.render();
int viewLoc = glGetUniformLocation(program, "modelView");
glUniformMatrix4fv(viewLoc, 1, 0, glm::value_ptr(view));
int projLoc = glGetUniformLocation(program, "projection");
glUniformMatrix4fv(projLoc, 1, 0, glm::value_ptr(projection));
glBindVertexArray(objVAO);
glDrawElements(GL_TRIANGLES, 3*triangles, GL_UNSIGNED_INT, NULL);
glutSwapBuffers();
}
void PlayState::changeSize(int w, int h) {
if (h == 0) h = 1; // Prevent a divide by zero, when window is too short
width = w;
height = h;
float ratio = 1.0 * w / h;
glViewport(0, 0, w, h);
projection = glm::perspective(FOV, ratio, NEAR_CLIP, FAR_CLIP);
}
void PlayState::keyboardDown(unsigned char key, int x, int y) {
// Exit on Esc
if(key == 27)
{
system("kill `ps | grep 'aplay'|awk '{print $1}'`") ;
glutLeaveMainLoop();
}
}
void PlayState::keyboardUp(unsigned char key, int x, int y) {
}
void PlayState::mousePress(int button, int state, int x, int y) {
}
void PlayState::mouseMove(int x, int y) {
}