Changeset bc6d8f6 in opengl-game for new-game.cpp
- Timestamp:
- Mar 9, 2018, 3:43:05 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- de1d7f6
- Parents:
- 64a70f4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
new-game.cpp
r64a70f4 rbc6d8f6 18 18 #include <cmath> 19 19 #include <string> 20 #include <array> 20 21 21 22 using namespace std; … … 30 31 vec3 cam_pos; 31 32 32 vec3 face_point1, face_point2, face_point3; 33 array<vec3, 3> triangle_face; 34 35 array<vec3,3> colored_triangle; 36 array<vec3, 3> square_triangle1; 37 array<vec3, 3> square_triangle2; 33 38 34 39 bool clicked = false; … … 37 42 bool clicked_square = false; 38 43 44 mat4 model_mat; 45 mat4 model_mat2; 46 39 47 mat4 view_mat; 40 48 mat4 proj_mat; 41 49 42 bool insideTriangle(vec3 p, vec3 v1, vec3 v2, vec3 v3);50 bool insideTriangle(vec3 p, array<vec3, 3> triangle); 43 51 44 52 GLuint loadShader(GLenum type, string file); … … 118 126 */ 119 127 120 vec3 fp1 = face_point1;121 vec3 fp2 = face_point2;122 vec3 fp3 = face_point3;128 vec3 fp1 = triangle_face[0]; 129 vec3 fp2 = triangle_face[1]; 130 vec3 fp3 = triangle_face[2]; 123 131 124 132 cout << "Points on the plane" << endl; … … 146 154 cout << "(" << i.x << "," << i.y << "," << i.z << ")" << endl; 147 155 148 clicked = insideTriangle(i, fp1, fp2, fp3);156 clicked = insideTriangle(i, triangle_face); 149 157 cout << (clicked ? "true" : "false") << endl; 150 158 } 151 159 } 160 161 /* REFACTORING PLAN: 162 * 163 * Have an array of object structs 164 * Each object struct has: 165 * -a model matrix 166 * -a selected boolean 167 * 168 * Have an array of face structs 169 * Each face struct has 170 * -an object index indicating which object it is a part of 171 * -an array of three points 172 * 173 * The mouse button callback will: 174 * -Set all selected flags in the objects array to false 175 * -iterate through the faces array 176 * -For each face, it will call faceClicked() with the following params: 177 * -An array of 3 points representing the face 178 * -The object struct represnting the object the face is a part of 179 */ 152 180 153 181 void mouse_button_callback_new(GLFWwindow* window, int button, int action, int mods) { … … 160 188 float x = (2.0f*mouse_x) / width - 1.0f; 161 189 float y = 1.0f - (2.0f*mouse_y) / height; 162 163 //x = -.1f;164 //x = -.25f;165 //x = -.5f;166 167 //y = .1f;168 190 169 191 cout << "x: " << x << ", y: " << y << endl; … … 182 204 // vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz()); 183 205 184 //vec4 ray_clip = vec4(0.0f, 0.0f, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane185 206 vec4 ray_clip = vec4(x, y, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane 186 207 vec4 ray_eye = ray_clip; 187 vec3 ray_world = (inverse( view_mat) * ray_eye).xyz();208 vec3 ray_world = (inverse(model_mat) * inverse(view_mat) * ray_eye).xyz(); 188 209 189 210 /* LATEST NOTES: … … 191 212 * Normalizing the world ray caused issues, although it should make sense with the projection 192 213 * matrix, since the z coordinate has meaning there. 214 * Plus, we really want to normalize it only once we recompute it below as the difference of two points, 215 * although doing so shouldn't effect the results. Check the book to see if there is a good reason for doing so. 193 216 */ 194 217 … … 196 219 197 220 vec4 cam_pos_origin = vec4(x, y, 0.0f, 1.0f); 198 vec3 cam_pos_temp = (inverse( view_mat) * cam_pos_origin).xyz();221 vec3 cam_pos_temp = (inverse(model_mat) * inverse(view_mat) * cam_pos_origin).xyz(); 199 222 200 223 ray_world = ray_world-cam_pos_temp; … … 204 227 cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl; 205 228 206 vec3 fp1 = face_point1;207 vec3 fp2 = face_point2;208 vec3 fp3 = face_point3;229 vec3 fp1 = triangle_face[0]; 230 vec3 fp2 = triangle_face[1]; 231 vec3 fp3 = triangle_face[2]; 209 232 210 233 cout << "Points on the plane" << endl; … … 239 262 printVector("Intersection", intersection); 240 263 241 clicked = insideTriangle(intersection, fp1, fp2, fp3);264 clicked = insideTriangle(intersection, triangle_face); 242 265 cout << (clicked ? "true" : "false") << endl; 243 266 … … 328 351 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 329 352 330 /*331 353 GLfloat points[] = { 332 354 0.0f, 0.5f, 0.0f, … … 337 359 0.0f, 0.5f, 0.0f, 338 360 }; 339 */340 341 GLfloat points[] = {342 0.5f, 0.5f, 0.0f,343 0.0f, -0.5f, 0.0f,344 1.0f, -0.5f, 0.0f,345 1.0f, -0.5f, 0.0f,346 0.0f, -0.5f, 0.0f,347 0.5f, 0.5f, 0.0f,348 };349 350 // initialize global variables for click intersection test351 face_point1 = vec3(points[0], points[1], points[2]);352 face_point2 = vec3(points[3], points[4], points[5]);353 face_point3 = vec3(points[6], points[7], points[8]);354 361 355 362 GLfloat colors[] = { … … 374 381 int numPoints = (sizeof(points) / sizeof(float)) / 3; 375 382 376 /*377 383 GLfloat points2[] = { 378 384 0.5f, 0.5f, 0.0f, … … 383 389 0.5f, -0.5f, 0.0f, 384 390 }; 385 */386 387 GLfloat points2[] = {388 0.0f, 0.5f, 0.0f,389 -1.0f, 0.5f, 0.0f,390 -1.0f, -0.5f, 0.0f,391 0.0f, 0.5f, 0.0f,392 -1.0f, -0.5f, 0.0f,393 0.0f, -0.5f, 0.0f,394 };395 391 396 392 GLfloat colors2[] = { … … 415 411 int numPoints2 = (sizeof(points2) / sizeof(float)) / 3; 416 412 413 // initialize global variables for click intersection tests 414 415 colored_triangle = { 416 vec3(points[0], points[1], points[2]), 417 vec3(points[3], points[4], points[5]), 418 vec3(points[6], points[7], points[8]), 419 }; 420 421 square_triangle1 = { 422 vec3(points2[0], points2[1], points2[2]), 423 vec3(points2[3], points2[4], points2[5]), 424 vec3(points2[6], points2[7], points2[8]), 425 }; 426 427 square_triangle2 = { 428 vec3(points2[9], points2[10], points2[11]), 429 vec3(points2[12], points2[13], points2[14]), 430 vec3(points2[15], points2[16], points2[17]), 431 }; 432 433 triangle_face = colored_triangle; 434 417 435 /* 418 mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));419 436 mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f)); 420 437 */ 421 mat4 T_model = translate(mat4(), vec3(0. 0f, 0.0f, 0.0f));438 mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f)); 422 439 mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 423 m at4 model_mat = T_model*R_model;440 model_mat = T_model*R_model; 424 441 425 442 // mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f)); 426 mat4 T_model2 = translate(mat4(), vec3( 0.0f, 0.0f, 0.0f));443 mat4 T_model2 = translate(mat4(), vec3(-0.5f, 0.0f, 0.0f)); 427 444 mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f)); 428 m at4 model_mat2 = T_model2*R_model2;445 model_mat2 = T_model2*R_model2; 429 446 430 447 GLuint points_vbo = 0; … … 523 540 proj_mat = make_mat4(proj_arr); 524 541 542 GLint model_test_loc = glGetUniformLocation(shader_program, "model"); 543 GLint view_test_loc = glGetUniformLocation(shader_program, "view"); 544 GLint proj_test_loc = glGetUniformLocation(shader_program, "proj"); 545 525 546 GLint model_mat_loc = glGetUniformLocation(shader_program2, "model"); 526 547 GLint view_mat_loc = glGetUniformLocation(shader_program2, "view"); 527 548 GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj"); 528 549 529 GLint model_test_loc = glGetUniformLocation(shader_program, "model");530 GLint view_test_loc = glGetUniformLocation(shader_program, "view");531 GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");532 533 550 glUseProgram(shader_program); 534 551 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat)); 552 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat)); 535 553 glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat)); 536 554 537 555 glUseProgram(shader_program2); 538 556 glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2)); 557 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat)); 539 558 glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat)); 540 541 // glUniform1i(tex_loc, 0);542 559 543 560 bool cam_moved = false; … … 575 592 576 593 glUseProgram(shader_program); 577 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat)); 594 595 // this is temporary. 596 // It's needed to offset the code for the recoloring of the square working during click detection 597 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat)); 578 598 579 599 glBindVertexArray(vao); … … 583 603 if (clicked_square) { 584 604 glUseProgram(shader_program); 585 glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat)); 605 606 // this is temporary. 607 // It's needed to get the recoloring of the square working during click detection 608 glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat2)); 586 609 587 610 glBindVertexArray(vao2); … … 591 614 } else { 592 615 glUseProgram(shader_program2); 593 glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));594 616 595 617 glBindVertexArray(vao2); … … 701 723 } 702 724 703 bool insideTriangle(vec3 p, vec3 v1, vec3 v2, vec3 v3) {704 vec3 v21 = v2-v1;705 vec3 v31 = v3-v1;706 vec3 pv1 = p- v1;725 bool insideTriangle(vec3 p, array<vec3,3> triangle) { 726 vec3 v21 = triangle[1]-triangle[0]; 727 vec3 v31 = triangle[2]-triangle[0]; 728 vec3 pv1 = p-triangle[0]; 707 729 708 730 float y = (pv1.y*v21.x - pv1.x*v21.y) / (v31.y*v21.x - v31.x*v21.y);
Note:
See TracChangeset
for help on using the changeset viewer.