Changeset bc6d8f6 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Mar 9, 2018, 3:43:05 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
de1d7f6
Parents:
64a70f4
Message:

Restrucutre code to enable easier click testing of different triangles, start using 3d vec3 arrays to pass triangles around more conveniently, and make an algorithm for click testing all the triangles in the scene.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r64a70f4 rbc6d8f6  
    1818#include <cmath>
    1919#include <string>
     20#include <array>
    2021
    2122using namespace std;
     
    3031vec3 cam_pos;
    3132
    32 vec3 face_point1, face_point2, face_point3;
     33array<vec3, 3> triangle_face;
     34
     35array<vec3,3> colored_triangle;
     36array<vec3, 3> square_triangle1;
     37array<vec3, 3> square_triangle2;
    3338
    3439bool clicked = false;
     
    3742bool clicked_square = false;
    3843
     44mat4 model_mat;
     45mat4 model_mat2;
     46
    3947mat4 view_mat;
    4048mat4 proj_mat;
    4149
    42 bool insideTriangle(vec3 p, vec3 v1, vec3 v2, vec3 v3);
     50bool insideTriangle(vec3 p, array<vec3, 3> triangle);
    4351
    4452GLuint loadShader(GLenum type, string file);
     
    118126       */
    119127
    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];
    123131
    124132      cout << "Points on the plane" << endl;
     
    146154      cout << "(" << i.x << "," << i.y << "," << i.z << ")" << endl;
    147155
    148       clicked = insideTriangle(i, fp1, fp2, fp3);
     156      clicked = insideTriangle(i, triangle_face);
    149157      cout << (clicked ? "true" : "false")  << endl;
    150158   }
    151159}
     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 */
    152180
    153181void mouse_button_callback_new(GLFWwindow* window, int button, int action, int mods) {
     
    160188      float x = (2.0f*mouse_x) / width - 1.0f;
    161189      float y = 1.0f - (2.0f*mouse_y) / height;
    162 
    163       //x = -.1f;
    164       //x = -.25f;
    165       //x = -.5f;
    166 
    167       //y = .1f;
    168190
    169191      cout << "x: " << x << ", y: " << y << endl;
     
    182204      // vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
    183205
    184       //vec4 ray_clip = vec4(0.0f, 0.0f, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane
    185206      vec4 ray_clip = vec4(x, y, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane
    186207      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();
    188209
    189210      /* LATEST NOTES:
     
    191212       * Normalizing the world ray caused issues, although it should make sense with the projection
    192213       * 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.
    193216       */
    194217
     
    196219
    197220      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();
    199222
    200223      ray_world = ray_world-cam_pos_temp;
     
    204227      cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
    205228
    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];
    209232
    210233      cout << "Points on the plane" << endl;
     
    239262      printVector("Intersection", intersection);
    240263
    241       clicked = insideTriangle(intersection, fp1, fp2, fp3);
     264      clicked = insideTriangle(intersection, triangle_face);
    242265      cout << (clicked ? "true" : "false") << endl;
    243266
     
    328351   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    329352
    330    /*
    331353   GLfloat points[] = {
    332354       0.0f,  0.5f,  0.0f,
     
    337359       0.0f,  0.5f,  0.0f,
    338360   };
    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 test
    351    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]);
    354361
    355362   GLfloat colors[] = {
     
    374381   int numPoints = (sizeof(points) / sizeof(float)) / 3;
    375382
    376    /*
    377383   GLfloat points2[] = {
    378384      0.5f,  0.5f,  0.0f,
     
    383389      0.5f, -0.5f,  0.0f,
    384390   };
    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    };
    395391
    396392   GLfloat colors2[] = {
     
    415411   int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
    416412
     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
    417435   /*
    418    mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
    419436   mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
    420437   */
    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));
    422439   mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    423    mat4 model_mat = T_model*R_model;
     440   model_mat = T_model*R_model;
    424441
    425442   // 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));
    427444   mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    428    mat4 model_mat2 = T_model2*R_model2;
     445   model_mat2 = T_model2*R_model2;
    429446
    430447   GLuint points_vbo = 0;
     
    523540   proj_mat = make_mat4(proj_arr);
    524541
     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
    525546   GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
    526547   GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
    527548   GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
    528549
    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 
    533550   glUseProgram(shader_program);
    534551   glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
     552   glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
    535553   glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    536554
    537555   glUseProgram(shader_program2);
    538556   glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
     557   glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    539558   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    540 
    541    // glUniform1i(tex_loc, 0);
    542559
    543560   bool cam_moved = false;
     
    575592
    576593      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));
    578598
    579599      glBindVertexArray(vao);
     
    583603      if (clicked_square) {
    584604         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));
    586609
    587610         glBindVertexArray(vao2);
     
    591614      } else {
    592615         glUseProgram(shader_program2);
    593          glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    594616
    595617         glBindVertexArray(vao2);
     
    701723}
    702724
    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;
     725bool 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];
    707729
    708730   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.