Changeset 19c9338 in opengl-game


Ignore:
Timestamp:
Mar 9, 2018, 3:55:46 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
de1d7f6
Parents:
64a70f4
git-author:
Dmitry Portnoy <dmp1488@…> (03/09/18 03:43:05)
git-committer:
Dmitry Portnoy <dmp1488@…> (03/09/18 03:55:46)
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 r19c9338  
    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 * Eventually, maybe also want to store a reference to the correct shader
     168 * or whatever other info I need to properly render it
     169 *
     170 * Have an array of face structs
     171 * Each face struct has
     172 *    -an object index indicating which object it is a part of
     173 *    -an array of three points
     174 *
     175 * The mouse button callback will:
     176 *    -Set all selected flags in the objects array to false
     177 *    -iterate through the faces array
     178 *    -For each face, it will call faceClicked() with the following params:
     179 *       -Probably a world ray created from the mouse click coordinates
     180 *       -An array of 3 points representing the face
     181 *       -The object struct represnting the object the face is a part of
     182 */
    152183
    153184void mouse_button_callback_new(GLFWwindow* window, int button, int action, int mods) {
     
    161192      float y = 1.0f - (2.0f*mouse_y) / height;
    162193
    163       //x = -.1f;
    164       //x = -.25f;
    165       //x = -.5f;
    166 
    167       //y = .1f;
    168 
    169194      cout << "x: " << x << ", y: " << y << endl;
    170 
    171       // CHECK: Looks good up to here
    172195
    173196      // Since the projection matrix gets applied before the view matrix,
     
    182205      // vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
    183206
    184       //vec4 ray_clip = vec4(0.0f, 0.0f, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane
    185207      vec4 ray_clip = vec4(x, y, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane
    186208      vec4 ray_eye = ray_clip;
    187       vec3 ray_world = (inverse(view_mat) * ray_eye).xyz();
     209      vec3 ray_world = (inverse(model_mat) * inverse(view_mat) * ray_eye).xyz();
    188210
    189211      /* LATEST NOTES:
     
    191213       * Normalizing the world ray caused issues, although it should make sense with the projection
    192214       * matrix, since the z coordinate has meaning there.
     215       * Plus, we really want to normalize it only once we recompute it below as the difference of two points,
     216       * although doing so shouldn't effect the results. Check the book to see if there is a good reason for doing so.
    193217       */
    194218
     
    196220
    197221      vec4 cam_pos_origin = vec4(x, y, 0.0f, 1.0f);
    198       vec3 cam_pos_temp = (inverse(view_mat) * cam_pos_origin).xyz();
     222      vec3 cam_pos_temp = (inverse(model_mat) * inverse(view_mat) * cam_pos_origin).xyz();
    199223
    200224      ray_world = ray_world-cam_pos_temp;
     
    204228      cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
    205229
    206       vec3 fp1 = face_point1;
    207       vec3 fp2 = face_point2;
    208       vec3 fp3 = face_point3;
     230      vec3 fp1 = triangle_face[0];
     231      vec3 fp2 = triangle_face[1];
     232      vec3 fp3 = triangle_face[2];
    209233
    210234      cout << "Points on the plane" << endl;
     
    239263      printVector("Intersection", intersection);
    240264
    241       clicked = insideTriangle(intersection, fp1, fp2, fp3);
     265      clicked = insideTriangle(intersection, triangle_face);
    242266      cout << (clicked ? "true" : "false") << endl;
    243267
     
    328352   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    329353
    330    /*
    331354   GLfloat points[] = {
    332355       0.0f,  0.5f,  0.0f,
     
    337360       0.0f,  0.5f,  0.0f,
    338361   };
    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]);
    354362
    355363   GLfloat colors[] = {
     
    374382   int numPoints = (sizeof(points) / sizeof(float)) / 3;
    375383
    376    /*
    377384   GLfloat points2[] = {
    378385      0.5f,  0.5f,  0.0f,
     
    383390      0.5f, -0.5f,  0.0f,
    384391   };
    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    };
    395392
    396393   GLfloat colors2[] = {
     
    415412   int numPoints2 = (sizeof(points2) / sizeof(float)) / 3;
    416413
     414   // initialize global variables for click intersection tests
     415
     416   colored_triangle = {
     417      vec3(points[0], points[1], points[2]),
     418      vec3(points[3], points[4], points[5]),
     419      vec3(points[6], points[7], points[8]),
     420   };
     421
     422   square_triangle1 = {
     423      vec3(points2[0], points2[1], points2[2]),
     424      vec3(points2[3], points2[4], points2[5]),
     425      vec3(points2[6], points2[7], points2[8]),
     426   };
     427
     428   square_triangle2 = {
     429      vec3(points2[9], points2[10], points2[11]),
     430      vec3(points2[12], points2[13], points2[14]),
     431      vec3(points2[15], points2[16], points2[17]),
     432   };
     433
     434   triangle_face = colored_triangle;
     435
    417436   /*
    418    mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
    419437   mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
    420438   */
    421    mat4 T_model = translate(mat4(), vec3(0.0f, 0.0f, 0.0f));
     439   mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
    422440   mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    423    mat4 model_mat = T_model*R_model;
     441   model_mat = T_model*R_model;
    424442
    425443   // 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));
     444   mat4 T_model2 = translate(mat4(), vec3(-0.5f, 0.0f, 0.0f));
    427445   mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    428    mat4 model_mat2 = T_model2*R_model2;
     446   model_mat2 = T_model2*R_model2;
    429447
    430448   GLuint points_vbo = 0;
     
    523541   proj_mat = make_mat4(proj_arr);
    524542
     543   GLint model_test_loc = glGetUniformLocation(shader_program, "model");
     544   GLint view_test_loc = glGetUniformLocation(shader_program, "view");
     545   GLint proj_test_loc = glGetUniformLocation(shader_program, "proj");
     546
    525547   GLint model_mat_loc = glGetUniformLocation(shader_program2, "model");
    526548   GLint view_mat_loc = glGetUniformLocation(shader_program2, "view");
    527549   GLint proj_mat_loc = glGetUniformLocation(shader_program2, "proj");
    528550
    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 
    533551   glUseProgram(shader_program);
    534552   glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
     553   glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
    535554   glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    536555
    537556   glUseProgram(shader_program2);
    538557   glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
     558   glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    539559   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    540 
    541    // glUniform1i(tex_loc, 0);
    542560
    543561   bool cam_moved = false;
     
    575593
    576594      glUseProgram(shader_program);
    577       glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
     595
     596      // this is temporary.
     597      // It's needed to offset the code for the recoloring of the square working during click detection
     598      glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
    578599
    579600      glBindVertexArray(vao);
     
    583604      if (clicked_square) {
    584605         glUseProgram(shader_program);
    585          glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
     606
     607         // this is temporary.
     608         // It's needed to get the recoloring of the square working during click detection
     609         glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat2));
    586610
    587611         glBindVertexArray(vao2);
     
    591615      } else {
    592616         glUseProgram(shader_program2);
    593          glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    594617
    595618         glBindVertexArray(vao2);
     
    701724}
    702725
    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;
     726bool insideTriangle(vec3 p, array<vec3,3> triangle) {
     727   vec3 v21 = triangle[1]-triangle[0];
     728   vec3 v31 = triangle[2]-triangle[0];
     729   vec3 pv1 = p-triangle[0];
    707730
    708731   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.