Changeset 3effd81 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Jul 25, 2018, 2:44:09 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
42e642d
Parents:
fd6f465
Message:

Change addLaserToScene() to position the laser between the correct endpoints and rotate it towards the camera so perceived width is constant.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rfd6f465 r3effd81  
    3333using namespace std;
    3434using namespace glm;
    35 
    36 /* LASERS TODO:
    37  * -Allow lasers that face any direction
    38  * -Make the lasers rotate to always face the camera
    39  * -Use textures to draw lasers
    40  *    -The textures should be grayscale and have transparency
    41  *    -The laser shader should take an input color to blend with the texture to give the lasers color
    42  *    -DONE
    43  * -The lasers should be SceneObjects and drawn like all other objects
    44  *    -DONE
    45  * -Make lasers shoot from the ends of the ship's wings when the user presses a button and disappear after a second or so
    46  *
    47  * NOTE: Asteroid movement currently depends on framerate, fix this in a generic/reusable way
    48  */
    49 
    50 /* LASER POSITIONING ALGORITHM (to be implemented)
    51  * -Determine the length of the laser based on the start and end points
    52  * -Create a laser (long thin rectangle) of that length and the appropriate width
    53  *  along the z axis, facing up, and with its start point at the origin (to make sure rotations happen around that point)
    54  * -Determine the line from the camera that intersects the line the laser should lie on at a 90 degree angle
    55  * -The angle between that line and the laser's normal should be the angle needed to rotate the laser so it faces the camera
    56  * -Now, create the transformation matrix for the laser to correctly position it
    57  *   -First, rotate along the z axis by the angle calculated above
    58  *   -Then, determine the correct angles and rotate around the y and x axes to make the laser point in the right direction
    59  *   -Finally, translate the laser to its correct position by getting the difference between the camera position
    60  *    and the laser start point
    61  */
    6235
    6336enum State {
     
    229202
    230203ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
     204
     205/*
     206* TODO: Make lasers shoot from the ends of the ship's wings when the user presses a button and disappear after a second or so
     207* TODO: Asteroid movement currently depends on framerate, fix this in a generic/reusable way
     208*/
    231209
    232210int main(int argc, char* argv[]) {
     
    408386   shaderBufferInfo[texture_sp] = BufferInfo();
    409387   shaderBufferInfo[laser_sp] = BufferInfo();
     388
     389   cam_pos = vec3(0.0f, 0.0f, 2.0f);
     390   float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
     391   float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f;
    410392
    411393   SceneObject obj;
     
    958940   // glm::perspective can create the projection matrix
    959941
    960    cam_pos = vec3(0.0f, 0.0f, 2.0f);
    961    //cam_pos = vec3(-2.1f, -1.5f, -1.5f); // Good position for checking ship faces
    962    float cam_yaw = 0.0f * 2.0f * 3.14159f / 360.0f;
    963    float cam_pitch = -50.0f * 2.0f * 3.14159f / 360.0f;
    964 
    965942   mat4 T = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
    966943   mat4 yaw_mat = rotate(mat4(1.0f), -cam_yaw, vec3(0.0f, 1.0f, 0.0f));
     
    969946   view_mat = R*T;
    970947
     948   // TODO: Create a function to construct the projection matrix
     949   // (Maybe I should just use glm::perspective, after making sure it matches what I have now)
    971950   float fov = 67.0f * ONE_DEG_IN_RAD;
    972951   float aspect = (float)width / (float)height;
     
    12751254      vec4 ray_world = inverse(view_mat) * ray_eye;
    12761255
    1277       vec4 cam_pos_temp = vec4(cam_pos, 1.0f);
    1278 
    12791256      vec4 click_point;
    12801257      vec3 closest_point = vec3(0.0f, 0.0f, -FAR_CLIP); // Any valid point will be closer than the far clipping plane, so initial value to that
     
    12901267                  vec3(it->points[p_idx + 6], it->points[p_idx + 7], it->points[p_idx + 8]),
    12911268               },
    1292                &*it, ray_world, cam_pos_temp, click_point
     1269               &*it, ray_world, vec4(cam_pos, 1.0f), click_point
    12931270            )) {
    12941271               click_point = view_mat * click_point;
     
    15821559}
    15831560
     1561/* LASER RENDERING/POSITIONING ALGORITHM
     1562 * -Draw a thin rectangle for the laser beam, using the specified width and endpoints
     1563 * -Texture the beam with a grayscale partially transparent image
     1564 * -In the shader, blend the image with a color to support lasers of different colors
     1565 *
     1566 * The flat part of the textured rectangle needs to always face the camera, so the laser's width is constant
     1567 * This is done as follows:
     1568* -Determine the length of the laser based on the start and end points
     1569* -Draw a rectangle along the z-axis and rotated upwards along the y-axis, with the correct final length and width
     1570* -Rotate the beam around the z-axis by the correct angle, sot that in its final position, the flat part faces the camera
     1571* -Rotate the beam along the x-axis and then along the y-axis and then translate it to put it into its final position
     1572*/
    15841573void addLaserToScene(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) {
    15851574   SceneObject obj = SceneObject();
     
    15891578   obj.deleted = false;
    15901579
    1591    float length = (end-start).length();
     1580   vec3 ray = end - start;
     1581   float length = glm::length(ray);
    15921582
    15931583   obj.points = {
     
    16341624
    16351625   obj.num_points = obj.points.size() / 3;
    1636    obj.model_base = translate(mat4(1.0f), start);
     1626
     1627   float xAxisRotation = asin(ray.y / length);
     1628   float yAxisRotation = atan2(-ray.x, -ray.z);
     1629
     1630   vec3 normal(rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) *
     1631               rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) *
     1632               vec4(0.0f, 1.0f, 0.0f, 1.0f));
     1633
     1634   // To project point P onto line AB:
     1635   // projection = A + dot(AP,AB) / dot(AB,AB) * AB
     1636   vec3 projOnLaser = start + glm::dot(cam_pos-start, ray) / (length*length) * ray;
     1637   vec3 laserToCam = cam_pos - projOnLaser;
     1638
     1639   float zAxisRotation = -atan2(glm::dot(glm::cross(normal, laserToCam), glm::normalize(ray)), glm::dot(normal, laserToCam));
     1640
     1641   obj.model_base = mat4(1.0f);
     1642   obj.model_base = rotate(mat4(1.0f), zAxisRotation, vec3(0.0f, 0.0f, 1.0f)) * obj.model_base;
     1643   obj.model_base = rotate(mat4(1.0f), xAxisRotation, vec3(1.0f, 0.0f, 0.0f)) * obj.model_base;
     1644   obj.model_base = rotate(mat4(1.0f), yAxisRotation, vec3(0.0f, 1.0f, 0.0f)) * obj.model_base;
     1645   obj.model_base = translate(mat4(1.0f), start) * obj.model_base;
    16371646   obj.model_transform = mat4(1.0f);
    16381647
     
    17371746      shaderBufferInfo[shaderIt->first].ubo_base = lastShaderUboCount * 2;
    17381747
     1748      /*
    17391749      cout << "shader: " << shaderIt->first << endl;
    17401750      cout << "point counts: " << shaderCounts[shaderIt->first] << endl;
     
    17421752      cout << "vbo_base: " << shaderBufferInfo[shaderIt->first].vbo_base << endl;
    17431753      cout << "ubo_base: " << shaderBufferInfo[shaderIt->first].ubo_base << endl;
     1754      */
    17441755
    17451756      shaderBufferInfo[shaderIt->first].vbo_offset = 0;
Note: See TracChangeset for help on using the changeset viewer.