Changeset df652d5 in opengl-game


Ignore:
Timestamp:
Mar 17, 2018, 2:44:39 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
147ac6d
Parents:
de1d7f6
Message:

Create SceneObject and ObjectFace structs, a list of objects, and a list of faces, and start using them in the rendering and click detection code to better encapsulate objects in the scene.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rde1d7f6 rdf652d5  
    1919#include <string>
    2020#include <array>
     21#include <vector>
    2122
    2223using namespace std;
     
    2425
    2526#define ONE_DEG_IN_RAD (2.0 * M_PI) / 360.0 // 0.017444444
     27
     28struct SceneObject {
     29   mat4 model_mat;
     30   bool clicked;
     31};
     32
     33struct ObjectFace {
     34   unsigned int objectId;
     35   array<vec3, 3> points;
     36};
    2637
    2738const bool FULLSCREEN = false;
     
    3142vec3 cam_pos;
    3243
    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;
    38 
    39 bool clicked = false;
    4044int colors_i = 0;
    41 
    42 bool clicked_square = false;
    43 
    44 mat4 model_mat;
    45 mat4 model_mat2;
    4645
    4746mat4 view_mat;
    4847mat4 proj_mat;
    4948
    50 bool insideTriangle(vec3 p, array<vec3, 3> triangle);
     49vector<SceneObject> objects;
     50vector<ObjectFace> faces;
     51
     52bool insideTriangle(vec3 p, ObjectFace* face);
    5153
    5254GLuint loadShader(GLenum type, string file);
     
    6466
    6567void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
     68   /*
    6669   double mouse_x, mouse_y;
    6770   glfwGetCursorPos(window, &mouse_x, &mouse_y);
     
    8588      vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
    8689
    87       /* LATEST NOTES:
     90      / * LATEST NOTES:
    8891       *
    8992       * Normalizing the world ray caused issues, although it should make sense with the projection
     
    9295       * Now, I need to figure out the correct intersection test in 2D space
    9396       * Also, need to check that the global triangle points are correct
    94        */
     97       * /
    9598
    9699      // since ray_world is the end result we want anyway, we probably don't need to add cam_pos to
     
    99102      vec3 click_point = cam_pos + ray_world;
    100103
    101       /* Now, we need to generate the constants for the equations describing
     104      / * Now, we need to generate the constants for the equations describing
    102105       * a 3D line:
    103106       *   (x - x0) / a = (y - y0) / b = (z - z0) / c
     
    105108       * The line goes through the camera position, so
    106109       * cam_pos = <x0, y0, z0>
    107        */
     110       * /
    108111
    109112      // upper right corner is 1, 1 in opengl
     
    121124      cout << "(z - " << cam_pos.z << ") / " << c << endl;;
    122125
    123       /* Now, we need to generate the constants for the equations describing
     126      / * Now, we need to generate the constants for the equations describing
    124127       * a 3D plane:
    125128       * dx + ey +fz +g = 0
    126        */
     129       * /
    127130
    128131      vec3 fp1 = triangle_face[0];
     
    157160      cout << (clicked ? "true" : "false")  << endl;
    158161   }
     162   */
    159163}
    160164
     
    180184 *       -An array of 3 points representing the face
    181185 *       -The object struct represnting the object the face is a part of
     186 *
     187 *       -Really, all I need to pass in are the world ray and an ObjectFace reference
     188 *       -The world ray will first need to be multiplied by the view and projection matrices before being passed in
    182189 */
    183190
     
    185192   double mouse_x, mouse_y;
    186193   glfwGetCursorPos(window, &mouse_x, &mouse_y);
     194
     195   ObjectFace* face = &faces[0];
     196   SceneObject* obj = &objects[face->objectId];
    187197
    188198   if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
     
    207217      vec4 ray_clip = vec4(x, y, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane
    208218      vec4 ray_eye = ray_clip;
    209       vec3 ray_world = (inverse(model_mat) * inverse(view_mat) * ray_eye).xyz();
     219      vec3 ray_world = (inverse(obj->model_mat) * inverse(view_mat) * ray_eye).xyz();
    210220
    211221      /* LATEST NOTES:
     
    220230
    221231      vec4 cam_pos_origin = vec4(x, y, 0.0f, 1.0f);
    222       vec3 cam_pos_temp = (inverse(model_mat) * inverse(view_mat) * cam_pos_origin).xyz();
     232      vec3 cam_pos_temp = (inverse(obj->model_mat) * inverse(view_mat) * cam_pos_origin).xyz();
    223233
    224234      ray_world = ray_world-cam_pos_temp;
     
    228238      cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
    229239
    230       vec3 fp1 = triangle_face[0];
    231       vec3 fp2 = triangle_face[1];
    232       vec3 fp3 = triangle_face[2];
     240      vec3 fp1 = face->points[0];
     241      vec3 fp2 = face->points[1];
     242      vec3 fp3 = face->points[2];
    233243
    234244      cout << "Points on the plane" << endl;
     
    263273      printVector("Intersection", intersection);
    264274
    265       clicked = insideTriangle(intersection, triangle_face);
    266       cout << (clicked ? "true" : "false") << endl;
    267 
    268       clicked_square = !clicked_square;
     275      obj->clicked = insideTriangle(intersection, face);
     276      cout << (obj->clicked ? "true" : "false") << endl;
    269277   }
    270278}
     
    312320   }
    313321
     322   bool squareSelected = false;
     323
    314324   glfwSetMouseButtonCallback(window, mouse_button_callback_new);
    315325
     
    318328   glewInit();
    319329
     330   // Check if we might ever need this. If not, remove it.
    320331   // glViewport(0, 0, width*2, height*2);
    321332
     
    414425   // initialize global variables for click intersection tests
    415426
    416    colored_triangle = {
     427   mat4 T_model, R_model;
     428
     429   // triangle
     430   objects.push_back(SceneObject());
     431   objects[0].clicked = false;
     432
     433   /*
     434   mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
     435   */
     436   T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
     437   R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
     438   objects[0].model_mat = T_model*R_model;
     439
     440   faces.push_back(ObjectFace());
     441   faces[0].objectId = 0;
     442   faces[0].points = {
    417443      vec3(points[0], points[1], points[2]),
    418444      vec3(points[3], points[4], points[5]),
     
    420446   };
    421447
    422    square_triangle1 = {
     448   // square
     449   objects.push_back(SceneObject());
     450   objects[1].clicked = false;
     451
     452   // mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
     453   T_model = translate(mat4(), vec3(-0.5f, 0.0f, 0.0f));
     454   R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
     455   objects[1].model_mat = T_model*R_model;
     456
     457   faces.push_back(ObjectFace());
     458   faces[1].objectId = 1;
     459   faces[1].points = {
    423460      vec3(points2[0], points2[1], points2[2]),
    424461      vec3(points2[3], points2[4], points2[5]),
     
    426463   };
    427464
    428    square_triangle2 = {
     465   faces.push_back(ObjectFace());
     466   faces[2].objectId = 1;
     467   faces[2].points = {
    429468      vec3(points2[9], points2[10], points2[11]),
    430469      vec3(points2[12], points2[13], points2[14]),
    431470      vec3(points2[15], points2[16], points2[17]),
    432471   };
    433 
    434    triangle_face = colored_triangle;
    435 
    436    /*
    437    mat4 R_model = rotate(mat4(), 4.0f, vec3(0.0f, 1.0f, 0.0f));
    438    */
    439    mat4 T_model = translate(mat4(), vec3(0.5f, 0.0f, 0.0f));
    440    mat4 R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    441    model_mat = T_model*R_model;
    442 
    443    // mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
    444    mat4 T_model2 = translate(mat4(), vec3(-0.5f, 0.0f, 0.0f));
    445    mat4 R_model2 = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    446    model_mat2 = T_model2*R_model2;
    447472
    448473   GLuint points_vbo = 0;
     
    550575
    551576   glUseProgram(shader_program);
    552    glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(model_mat));
     577   glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
    553578   glUniformMatrix4fv(view_test_loc, 1, GL_FALSE, value_ptr(view_mat));
    554579   glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    555580
    556581   glUseProgram(shader_program2);
    557    glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
     582   glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
    558583   glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    559584   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
     
    571596      }
    572597
    573       if (clicked) {
     598      if (objects[0].clicked) {
    574599         glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    575600
     
    581606            colors_i = 0;
    582607         }
    583 
    584          clicked = false;
    585608      }
    586609
     
    596619      // this is temporary.
    597620      // 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));
     621      glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
    599622
    600623      glBindVertexArray(vao);
     
    602625      glDrawArrays(GL_TRIANGLES, 0, numPoints);
    603626
    604       if (clicked_square) {
     627      if (objects[1].clicked) {
     628         squareSelected = !squareSelected;
     629      }
     630
     631      if (squareSelected) {
    605632         glUseProgram(shader_program);
    606633
    607634         // this is temporary.
    608635         // 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));
     636         glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
    610637
    611638         glBindVertexArray(vao2);
     
    623650
    624651      glDrawArrays(GL_TRIANGLES, 0, numPoints2);
     652
     653      for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) {
     654         it->clicked = false;
     655      }
    625656
    626657      glfwPollEvents();
     
    724755}
    725756
    726 bool 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];
     757bool insideTriangle(vec3 p, ObjectFace* face) {
     758   vec3 v21 = face->points[1]- face->points[0];
     759   vec3 v31 = face->points[2]- face->points[0];
     760   vec3 pv1 = p- face->points[0];
    730761
    731762   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.