Changeset 7ee66ea in opengl-game for new-game.cpp
- Timestamp:
- Aug 17, 2017, 2:30:31 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 201e2f8
- Parents:
- 9d22ee9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r9d22ee9 r7ee66ea 2 2 3 3 #include "logger.h" 4 5 #include <glm/mat4x4.hpp> // glm::mat4 6 #include <glm/gtc/matrix_transform.hpp> 7 #include <glm/gtc/type_ptr.hpp> 4 8 5 9 #include <GL/glew.h> … … 9 13 #include <iostream> 10 14 #include <fstream> 15 16 #define _USE_MATH_DEFINES 11 17 #include <cmath> 12 18 13 19 using namespace std; 20 using namespace glm; 21 22 #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 14 23 15 24 GLuint loadShader(GLenum type, string file); … … 87 96 -0.5f, -0.5f, 0.0f, 88 97 0.5f, -0.5f, 0.0f, 98 0.5f, -0.5f, 0.0f, 99 -0.5f, -0.5f, 0.0f, 100 0.0f, 0.5f, 0.0f, 89 101 }; 90 102 … … 93 105 0.0, 0.0, 1.0, 94 106 0.0, 1.0, 0.0, 95 }; 96 97 GLfloat model[] = { 107 0.0, 1.0, 0.0, 108 0.0, 0.0, 1.0, 109 1.0, 0.0, 0.0, 110 }; 111 112 GLfloat model_mat[] = { 98 113 1.0f, 0.0f, 0.0f, 0.0f, // column 1 99 114 0.0f, 1.0f, 0.0f, 0.0f, // column 2 … … 132 147 glLinkProgram(shader_program); 133 148 134 GLint location = glGetUniformLocation(shader_program, "model");135 136 149 float speed = 1.0f; 137 150 float last_position = 0.0f; 151 152 float cam_speed = 1.0f; 153 float cam_yaw_speed = 30.0f*ONE_DEG_IN_RAD; 154 155 float cam_pos[] = {0.0f, 0.0f, 2.0f}; 156 float cam_yaw = 0.0f; 157 158 mat4 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2])); 159 mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f)); 160 mat4 view_mat = R*T; 161 162 float near = 0.1f; 163 float far = 100.0f; 164 float fov = 67.0f * ONE_DEG_IN_RAD; 165 float aspect = (float)width / (float)height; 166 167 float range = tan(fov * 0.5f) * near; 168 float Sx = near / (range * aspect); 169 float Sy = near / range; 170 float Sz = -(far + near) / (far - near); 171 float Pz = -(2.0f * far * near) / (far - near); 172 173 float proj_mat[] = { 174 Sx, 0.0f, 0.0f, 0.0f, 175 0.0f, Sy, 0.0f, 0.0f, 176 0.0f, 0.0f, Sz, -1.0f, 177 0.0f, 0.0f, Pz, 0.0f, 178 }; 179 180 GLint model_mat_loc = glGetUniformLocation(shader_program, "model"); 181 GLint view_mat_loc = glGetUniformLocation(shader_program, "view"); 182 GLint proj_mat_loc = glGetUniformLocation(shader_program, "proj"); 183 184 glUseProgram(shader_program); 185 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, model_mat); 186 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 187 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, proj_mat); 188 189 bool cam_moved = false; 138 190 139 191 double previous_seconds = glfwGetTime(); … … 147 199 } 148 200 201 /* 149 202 model[12] = last_position + speed*elapsed_seconds; 150 203 last_position = model[12]; 151 glUseProgram(shader_program); 152 glUniformMatrix4fv(location, 1, GL_FALSE, model); 204 */ 153 205 154 206 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 155 207 glBindVertexArray(vao); 156 208 157 glDrawArrays(GL_TRIANGLES, 0, 3); 209 // Each point is made of 3 floats 210 int numPoints = (sizeof(points) / sizeof(float)) / 3; 211 glDrawArrays(GL_TRIANGLES, 0, numPoints); 158 212 159 213 glfwPollEvents(); … … 162 216 if (GLFW_PRESS == glfwGetKey(window, GLFW_KEY_ESCAPE)) { 163 217 glfwSetWindowShouldClose(window, 1); 218 } 219 220 float dist = cam_speed * elapsed_seconds; 221 if (glfwGetKey(window, GLFW_KEY_A)) { 222 cam_pos[0] -= cos(cam_yaw*ONE_DEG_IN_RAD)*dist; 223 cam_pos[2] += sin(cam_yaw*ONE_DEG_IN_RAD)*dist; 224 cam_moved = true; 225 } 226 if (glfwGetKey(window, GLFW_KEY_D)) { 227 cam_pos[0] += cos(cam_yaw)*dist; 228 cam_pos[2] -= sin(cam_yaw)*dist; 229 cam_moved = true; 230 } 231 if (glfwGetKey(window, GLFW_KEY_W)) { 232 cam_pos[0] -= sin(cam_yaw)*dist; 233 cam_pos[2] -= cos(cam_yaw)*dist; 234 cam_moved = true; 235 } 236 if (glfwGetKey(window, GLFW_KEY_S)) { 237 cam_pos[0] += sin(cam_yaw)*dist; 238 cam_pos[2] += cos(cam_yaw)*dist; 239 cam_moved = true; 240 } 241 if (glfwGetKey(window, GLFW_KEY_LEFT)) { 242 cam_yaw += cam_yaw_speed * elapsed_seconds; 243 cam_moved = true; 244 } 245 if (glfwGetKey(window, GLFW_KEY_RIGHT)) { 246 cam_yaw -= cam_yaw_speed * elapsed_seconds; 247 cam_moved = true; 248 } 249 if (cam_moved) { 250 T = translate(mat4(), vec3(-cam_pos[0], -cam_pos[1], -cam_pos[2])); 251 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f)); 252 view_mat = R*T; 253 254 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 255 cam_moved = false; 164 256 } 165 257 }
Note:
See TracChangeset
for help on using the changeset viewer.