Changeset d12d003 in opengl-game


Ignore:
Timestamp:
Mar 3, 2018, 1:24:52 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
0424bd8
Parents:
6f73e0c
Message:

Continue debugging object detection for mouse clicks

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r6f73e0c rd12d003  
    4343GLuint loadShaderProgram(string vertexShaderPath, string fragmentShaderPath);
    4444unsigned char* loadImage(string file_name, int* x, int* y);
     45
     46void printVector(string label, vec3 v);
     47
     48float NEAR_CLIP = 0.1f;
     49float FAR_CLIP = 100.0f;
    4550
    4651void glfw_error_callback(int error, const char* description) {
     
    139144      cout << "(" << i.x << "," << i.y << "," << i.z << ")" << endl;
    140145
    141       bool hit = insideTriangle(i, fp1, fp2, fp3);
    142       cout << (hit ? "true" : "false")  << endl;
    143 
    144       if (hit) {
    145          clicked = true;
    146       }
     146      clicked = insideTriangle(i, fp1, fp2, fp3);
     147      cout << (clicked ? "true" : "false")  << endl;
    147148   }
    148149}
     
    157158      float x = (2.0f*mouse_x) / width - 1.0f;
    158159      float y = 1.0f - (2.0f*mouse_y) / height;
     160
     161      //x = -.1f;
     162      x = -.25f;
     163      //x = -.5f;
     164
     165      y = .1f;
     166
    159167      cout << "x: " << x << ", y: " << y << endl;
    160168
     
    172180      // vec3 ray_world = normalize((inverse(view_mat) * ray_eye).xyz());
    173181
    174       vec4 ray_clip = vec4(x, y, 1.0f, 1.0f); // this should have a z equal to the near clipping plane
    175       vec3 ray_world = (inverse(view_mat) * ray_clip).xyz();
     182      //vec4 ray_clip = vec4(0.0f, 0.0f, NEAR_CLIP, 1.0f); // this should have a z equal to the near clipping plane
     183      vec4 ray_clip = vec4(x, y, 0.0f, 1.0f); // this should have a z equal to the near clipping plane
     184      vec4 ray_eye = ray_clip;
     185      vec3 ray_world = (inverse(view_mat) * ray_eye).xyz();
    176186
    177187      /* LATEST NOTES:
     
    184194       */
    185195
    186       // since ray_world is the end result we want anyway, we probably don't need to add cam_pos to
    187       // it, only to subtract it later
    188 
    189       vec3 click_point = cam_pos + ray_world;
    190 
    191       /* Now, we need to generate the constants for the equations describing
    192        * a 3D line:
    193        *   (x - x0) / a = (y - y0) / b = (z - z0) / c
    194        *
    195        * The line goes through the camera position, so
    196        * cam_pos = <x0, y0, z0>
    197        */
     196      printVector("Initial world ray:", ray_world);
     197
     198      // Theoretically, I should be able to change the z of the camera to whatever I want.
     199      // Figure out why I can't.
     200      // Remember that ray_world has to be a ray along the click line, not any point along it
     201      // vec3 cam_pos_temp = ray_world;
     202      // ray_world = vec3(0.0f, 0.0f, 1.0f);
     203
     204      vec3 cam_pos_temp = vec3(ray_world.xy(), 0.0f);
     205      ray_world = ray_world-cam_pos_temp;
    198206
    199207      // upper right corner is 1, 1 in opengl
    200208
    201       cout << "Converted -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;;
    202       cout << "Camera -> (" << cam_pos.x << "," << cam_pos.y << "," << cam_pos.z << ")" << endl;
    203       cout << "Click point -> (" << click_point.x << "," << click_point.y << "," << click_point.z << ")" << endl;
    204 
    205       float a = 1.0f;
    206       float b = a * (click_point.y - cam_pos.y) / (click_point.x - cam_pos.x);
    207       float c = a * (click_point.z - cam_pos.z) / (click_point.x - cam_pos.x);
    208 
    209       cout << "(x - " << cam_pos.x << ") / " << a << " = ";
    210       cout << "(y - " << cam_pos.y << ") / " << b << " = ";
    211       cout << "(z - " << cam_pos.z << ") / " << c << endl;;
    212 
    213       /* Now, we need to generate the constants for the equations describing
    214        * a 3D plane:
    215        * dx + ey +fz +g = 0
    216        */
     209      cout << "Ray clip -> (" << ray_clip.x << "," << ray_clip.y << "," << ray_clip.z << ")" << endl << endl;;
     210      cout << "Ray world -> (" << ray_world.x << "," << ray_world.y << "," << ray_world.z << ")" << endl << endl;;
     211      cout << "Camera -> (" << cam_pos_temp.x << "," << cam_pos_temp.y << "," << cam_pos_temp.z << ")" << endl;
    217212
    218213      vec3 fp1 = face_point1;
     
    225220      cout << "(" << fp3.x << ", " << fp3.y << ", " << fp3.z << ")" << endl;
    226221
    227       float pa = (fp2.y-fp1.y)*(fp3.z-fp1.z) - (fp3.y-fp1.y)*(fp2.z-fp1.z);
    228       float pb = (fp2.z-fp1.z)*(fp3.x-fp1.x) - (fp3.z-fp1.z)*(fp2.x-fp1.x);
    229       float pc = (fp2.x-fp1.x)*(fp3.y-fp1.y) - (fp3.x-fp1.x)*(fp2.y-fp1.y);
    230       float pd = -(pa*fp1.x+pb*fp1.y+pc*fp1.z);
    231 
    232       cout << pa << "x+" << pb << "y+" << pc << "z+" << pd << "=0" << endl;
    233 
    234       // get intersection
    235 
    236       // the intersection this computes is incorrect
    237       // it doesn't match the equation of the plane
    238       vec3 i;
    239       i.z = -cam_pos.z - pc*pd/(pa*a+pb*b);
    240       i.x = cam_pos.x + a * (i.z-cam_pos.z) / c;
    241       i.y = cam_pos.y + b * (i.z-cam_pos.z) / c;
    242 
    243       cout << "The holy grail?" << endl;
    244       cout << "(" << i.x << "," << i.y << "," << i.z << ")" << endl;
    245 
    246       bool hit = insideTriangle(i, fp1, fp2, fp3);
    247       cout << (hit ? "true" : "false")  << endl;
    248 
    249       if (hit) {
    250          clicked = true;
    251       }
     222      // LINE EQUATION:         P = O + Dt
     223      // O = cam_pos
     224      // D = ray_world
     225
     226      // PLANE EQUATION:        P dot n + d = 0  (n is the normal vector and d is the offset from the origin)
     227
     228      // Take the cross-product of two vectors on the plane to get the normal
     229      vec3 v1 = fp2 - fp1;
     230      vec3 v2 = fp3 - fp1;
     231
     232      vec3 normal = vec3(v1.y*v2.z-v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x);
     233      printVector("v1", v1);
     234      printVector("v2", v2);
     235      printVector("Cross", normal);
     236      cout << "Test theory: " << glm::dot(cam_pos_temp, normal) << endl;
     237      cout << "Test 2: " << glm::dot(ray_world, normal) << endl;
     238
     239      float d = -glm::dot(fp1, normal);
     240      cout << "d: " << d << endl;
     241
     242      float t = - (glm::dot(cam_pos_temp, normal) + d) / glm::dot(ray_world, normal);
     243      cout << "t: " << t << endl;
     244
     245      vec3 intersection = cam_pos_temp+t*ray_world;
     246      printVector("Intersection", intersection);
     247
     248      clicked = insideTriangle(intersection, fp1, fp2, fp3);
     249      cout << (clicked ? "true" : "false") << endl;
    252250   }
    253251}
     
    295293   }
    296294
    297    glfwSetMouseButtonCallback(window, mouse_button_callback);
     295   glfwSetMouseButtonCallback(window, mouse_button_callback_new);
    298296
    299297   glfwMakeContextCurrent(window);
     
    336334
    337335   GLfloat points[] = {
    338       0.0f,  0.5f, -0.001f,
    339      -0.5f, -0.5f,  0.0f,
    340       0.5f, -0.5f,  0.0f,
    341       0.5f, -0.5f,  0.0f,
    342      -0.5f, -0.5f,  0.0f,
    343       0.0f,  0.5f, -0.001f,
     336       0.0f,  0.5f,  0.0f,
     337      -0.5f, -0.5f,  0.0f,
     338       0.5f, -0.5f,  0.0f,
     339       0.5f, -0.5f,  0.0f,
     340      -0.5f, -0.5f,  0.0f,
     341       0.0f,  0.5f,  0.0f,
    344342   };
    345343   /*
    346344   GLfloat points[] = {
    347       0.0f,  1.0f,  0.0f,
    348      -1.0f,  0.0f,  0.0f,
    349       1.0f,  0.0f,  0.0f,
    350       1.0f,  0.0f,  0.0f,
    351      -1.0f,  0.0f,  0.0f,
    352       0.0f,  1.0f,  0.0f,
     345       0.0f,  1.0f,  0.0f,
     346      -1.0f,  0.0f,  0.0f,
     347       1.0f,  0.0f,  0.0f,
     348       1.0f,  0.0f,  0.0f,
     349      -1.0f,  0.0f,  0.0f,
     350       0.0f,  1.0f,  0.0f,
    353351   };
    354352   */
     
    381379
    382380   GLfloat points2[] = {
    383       0.5f,  0.5f,  0.0f,
    384      -0.5f,  0.5f,  0.0f,
    385      -0.5f, -0.5f,  0.0f,
    386       0.5f,  0.5f,  0.0f,
    387      -0.5f, -0.5f,  0.0f,
    388       0.5f, -0.5f,  0.0f,
     381       0.5f,  0.5f,  0.0f,
     382      -0.5f,  0.5f,  0.0f,
     383      -0.5f, -0.5f,  0.0f,
     384       0.5f,  0.5f,  0.0f,
     385      -0.5f, -0.5f,  0.0f,
     386       0.5f, -0.5f,  0.0f,
    389387   };
    390388
     
    485483   float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
    486484
    487    cam_pos = vec3(0.0f, 0.0f, 2.0f);
    488    //cam_pos = vec3(0.0f, 0.0f, 0.0f);
    489    float cam_yaw = 0.0f;
     485   //cam_pos = vec3(0.0f, 0.0f, 2.0f);
     486   cam_pos = vec3(0.0f, 0.0f, 0.0f);
     487   float cam_yaw = 45.0f * 2.0f * 3.14159f / 360.0f;
    490488
    491489   mat4 T = translate(mat4(), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
     
    497495   view_mat = R*T;
    498496
    499    float near = 0.1f;
    500    float far = 100.0f;
    501497   float fov = 67.0f * ONE_DEG_IN_RAD;
    502498   float aspect = (float)width / (float)height;
    503499
    504    float range = tan(fov * 0.5f) * near;
    505    float Sx = near / (range * aspect);
    506    float Sy = near / range;
    507    float Sz = -(far + near) / (far - near);
    508    float Pz = -(2.0f * far * near) / (far - near);
    509 
     500   float range = tan(fov * 0.5f) * NEAR_CLIP;
     501   float Sx = NEAR_CLIP / (range * aspect);
     502   float Sy = NEAR_CLIP / range;
     503   float Sz = -(FAR_CLIP + NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
     504   float Pz = -(2.0f * FAR_CLIP * NEAR_CLIP) / (FAR_CLIP - NEAR_CLIP);
     505
     506   /*
    510507   float proj_arr[] = {
    511508     Sx, 0.0f, 0.0f, 0.0f,
     
    514511     0.0f, 0.0f, Pz, 0.0f,
    515512   };
    516    /*
     513   */
    517514   float proj_arr[] = {
    518515     1.0f, 0.0f, 0.0f, 0.0f,
     
    521518     0.0f, 0.0f, 0.0f, 1.0f,
    522519   };
    523    */
    524520   proj_mat = make_mat4(proj_arr);
    525521
     
    536532   glUniformMatrix4fv(proj_test_loc, 1, GL_FALSE, value_ptr(proj_mat));
    537533
     534   /*
    538535   glUseProgram(shader_program2);
    539536   glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(model_mat2));
    540537   glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
     538   */
    541539
    542540   // glUniform1i(tex_loc, 0);
     
    703701   return x > 0.0f && y > 0.0f && x+y < 1.0f;
    704702}
     703
     704void printVector(string label, vec3 v) {
     705   cout << label << " -> (" << v.x << "," << v.y << "," << v.z << ")" << endl;
     706}
Note: See TracChangeset for help on using the changeset viewer.