Changeset c62eee6 in opengl-game
- Timestamp:
- Aug 25, 2017, 3:10:04 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 33a9664
- Parents:
- 485424b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r485424b rc62eee6 3 3 #include "stb_image.h" 4 4 5 #include <glm/mat4x4.hpp> // glm::mat4 5 #define GLM_SWIZZLE 6 #include <glm/mat4x4.hpp> 6 7 #include <glm/gtc/matrix_transform.hpp> 7 8 #include <glm/gtc/type_ptr.hpp> … … 21 22 22 23 #define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444 24 23 25 const bool FULLSCREEN = false; 26 int width = 640; 27 int height = 480; 28 29 vec3 cam_pos; 30 31 vec3 face_point1, face_point2, face_point3; 32 33 mat4 view_mat; 34 mat4 proj_mat; 24 35 25 36 GLuint loadShader(GLenum type, string file); … … 29 40 void glfw_error_callback(int error, const char* description) { 30 41 gl_log_err("GLFW ERROR: code %i msg: %s\n", error, description); 42 } 43 44 void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) { 45 double mouse_x, mouse_y; 46 glfwGetCursorPos(window, &mouse_x, &mouse_y); 47 48 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { 49 cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl; 50 51 float x = (2.0f*mouse_x) / width - 1.0f; 52 float y = 1.0f - (2.0f*mouse_y) / height; 53 54 vec4 ray_clip = vec4(x, y, -1.0f, 1.0f); 55 vec4 ray_eye = inverse(proj_mat) * ray_clip; 56 ray_eye = vec4(ray_eye.xy(), -1.0f, 0.0f); 57 vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz()); 58 59 vec3 click_point = cam_pos + ray_world; 60 61 /* Now, we need to generate the constants for the equations describing 62 * a 3D line: 63 * (x - x0) / a = (y - y0) / b = (z - z0) / c 64 * 65 * The line goes through the camera position, so 66 * cam_pos = <x0, y0, z0> 67 */ 68 69 cout << "Converted -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;; 70 cout << "Camera -> (" << cam_pos.x << "," << cam_pos.y << "," << cam_pos.z << ")" << endl; 71 cout << "Click point -> (" << click_point.x << "," << click_point.y << "," << click_point.z << ")" << endl; 72 73 float a = 1.0f; 74 float b = a * (click_point.y - cam_pos.y) / (click_point.x - cam_pos.x); 75 float c = a * (click_point.z - cam_pos.z) / (click_point.x - cam_pos.x); 76 77 cout << "(x - " << cam_pos.x << ") / " << a << " = "; 78 cout << "(y - " << cam_pos.y << ") / " << b << " = "; 79 cout << "(z - " << cam_pos.z << ") / " << c << endl;; 80 81 /* Now, we need to generate the constants for the equations describing 82 * a 3D plane: 83 * dx + ey +fz +g = 0 84 */ 85 86 cout << "Points on the plane" << endl; 87 cout << "(" << face_point1.x << "," << face_point1.y << "," << face_point1.z << ")" << endl; 88 cout << "(" << face_point2.x << "," << face_point2.y << "," << face_point2.z << ")" << endl; 89 cout << "(" << face_point3.x << "," << face_point3.y << "," << face_point3.z << ")" << endl; 90 91 // get intersection 92 } 31 93 } 32 94 … … 54 116 GLFWwindow* window = NULL; 55 117 56 int width = 640;57 int height = 480;58 59 118 if (FULLSCREEN) { 60 119 GLFWmonitor* mon = glfwGetPrimaryMonitor(); … … 75 134 return 1; 76 135 } 136 137 glfwSetMouseButtonCallback(window, mouse_button_callback); 138 77 139 glfwMakeContextCurrent(window); 78 140 glewExperimental = GL_TRUE; … … 122 184 }; 123 185 186 // initialize global variables for click intersection test 187 face_point1 = vec3(points[0], points[1], points[2]); 188 face_point2 = vec3(points[3], points[4], points[5]); 189 face_point3 = vec3(points[6], points[7], points[8]); 190 124 191 GLfloat colors[] = { 125 192 1.0, 0.0, 0.0, … … 166 233 int numPoints2 = (sizeof(points2) / sizeof(float)) / 3; 167 234 235 /* 168 236 mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f)); 169 237 mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f)); 238 */ 239 mat4 T_model = translate(mat4(), vec3(0.0f, 0.0f, 0.0f)); 240 mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 170 241 mat4 model_mat = T_model*R_model; 171 242 … … 234 305 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD; 235 306 236 float cam_pos[] = {0.0f, 0.0f, 2.0f};307 cam_pos = vec3(0.0f, 0.0f, 2.0f); 237 308 float cam_yaw = 0.0f; 238 309 239 mat4 T = translate(mat4(), vec3(-cam_pos [0], -cam_pos[1], -cam_pos[2]));310 mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z)); 240 311 mat4 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f)); 241 mat4view_mat = R*T;312 view_mat = R*T; 242 313 243 314 float near = 0.1f; … … 252 323 float Pz = -(2.0f * far * near) / (far - near); 253 324 254 float proj_ mat[] = {325 float proj_arr[] = { 255 326 Sx, 0.0f, 0.0f, 0.0f, 256 327 0.0f, Sy, 0.0f, 0.0f, … … 258 329 0.0f, 0.0f, Pz, 0.0f, 259 330 }; 331 proj_mat = make_mat4(proj_arr); 260 332 261 333 GLint model_mat_loc = glGetUniformLocation(shader_program2, "model"); … … 269 341 glUseProgram(shader_program); 270 342 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat)); 271 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, proj_mat);343 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat)); 272 344 273 345 glUseProgram(shader_program2); 274 346 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2)); 275 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, proj_mat);347 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat)); 276 348 277 349 // glUniform1i(tex_loc, 0); … … 319 391 float dist = cam_speed * elapsed_seconds; 320 392 if (glfwGetKey(window, GLFW_KEY_A)) { 321 cam_pos [0]-= cos(cam_yaw)*dist;322 cam_pos [2]+= sin(cam_yaw)*dist;393 cam_pos.x -= cos(cam_yaw)*dist; 394 cam_pos.z += sin(cam_yaw)*dist; 323 395 cam_moved = true; 324 396 } 325 397 if (glfwGetKey(window, GLFW_KEY_D)) { 326 cam_pos [0]+= cos(cam_yaw)*dist;327 cam_pos [2]-= sin(cam_yaw)*dist;398 cam_pos.x += cos(cam_yaw)*dist; 399 cam_pos.z -= sin(cam_yaw)*dist; 328 400 cam_moved = true; 329 401 } 330 402 if (glfwGetKey(window, GLFW_KEY_W)) { 331 cam_pos [0]-= sin(cam_yaw)*dist;332 cam_pos [2]-= cos(cam_yaw)*dist;403 cam_pos.x -= sin(cam_yaw)*dist; 404 cam_pos.z -= cos(cam_yaw)*dist; 333 405 cam_moved = true; 334 406 } 335 407 if (glfwGetKey(window, GLFW_KEY_S)) { 336 cam_pos [0]+= sin(cam_yaw)*dist;337 cam_pos [2]+= cos(cam_yaw)*dist;408 cam_pos.x += sin(cam_yaw)*dist; 409 cam_pos.z += cos(cam_yaw)*dist; 338 410 cam_moved = true; 339 411 } … … 347 419 } 348 420 if (cam_moved) { 349 T = translate(mat4(), vec3(-cam_pos [0], -cam_pos[1], -cam_pos[2]));421 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z)); 350 422 R = rotate(mat4(), -cam_yaw, vec3(0.0f, 1.0f, 0.0f)); 351 423 view_mat = R*T;
Note:
See TracChangeset
for help on using the changeset viewer.