Changeset fd6f465 in opengl-game


Ignore:
Timestamp:
Jul 20, 2018, 1:42:00 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
3effd81
Parents:
9f9f9a7
Message:

Change the laser rendering algorithm to draw lasers starting from the origin and then translate them into the correct position using the model matrix.

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • laser.vert

    r9f9f9a7 rfd6f465  
    11#version 410
     2
     3#define MAX_NUM_OBJECTS 1024
    24
    35uniform mat4 view, proj;
    46
     7layout (std140) uniform models {
     8  mat4 model_mats[MAX_NUM_OBJECTS];
     9};
     10
    511layout(location = 0) in vec3 vertex_position;
    612layout(location = 1) in vec2 vt;
     13layout(location = 2) in uint ubo_index;
    714
    815out vec2 texture_coordinates;
     
    1017
    1118void main() {
    12   position_eye = vec3(view * vec4(vertex_position, 1.0));
     19  position_eye = vec3(view * model_mats[ubo_index] * vec4(vertex_position, 1.0));
    1320  texture_coordinates = vt;
    1421
  • new-game.cpp

    r9f9f9a7 rfd6f465  
    4646 *
    4747 * 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
    4861 */
    4962
     
    125138void calculateObjectBoundingBox(SceneObject& obj);
    126139
    127 void addLaserToScene(SceneObject& obj, vec3 start, vec3 end, vec3 color, GLfloat width);
     140void addLaserToScene(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp);
    128141
    129142void initializeBuffers(
     
    163176void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    164177                  GLuint color_sp, GLuint texture_sp, GLuint laser_sp,
    165                   GLuint color_vao, GLuint texture_vao,
     178                  GLuint color_vao, GLuint texture_vao, GLuint laser_vao,
    166179                  GLuint colors_vbo, GLuint selected_colors_vbo,
    167180                  SceneObject* selectedObject);
     
    855868   addObjectToSceneDuringInit(obj);
    856869
    857    obj = SceneObject();
    858    obj.shader_program = laser_sp;
    859 
    860    addLaserToScene(obj, vec3(0.34f, -2.0f, 1.6f), vec3(0.34f, -2.0f, -3.0f), vec3(0.0f, 1.0f, 0.0f), 0.04f);
    861 
    862    obj = SceneObject();
    863    obj.shader_program = laser_sp;
    864 
    865    addLaserToScene(obj, vec3(-0.34f, -2.0f, 1.6f), vec3(-0.34f, -2.0f, -3.0f), vec3(0.0f, 1.0f, 0.0f), 0.04f);
     870   addLaserToScene(vec3(0.34f, -2.0f, 1.6f), vec3(0.34f, -2.0f, -3.0f), vec3(0.0f, 1.0f, 0.0f), 0.04f, laser_sp);
     871   addLaserToScene(vec3(-0.34f, -2.0f, 1.6f), vec3(-0.34f, -2.0f, -3.0f), vec3(0.0f, 1.0f, 0.0f), 0.04f, laser_sp);
    866872
    867873   vector<SceneObject>::iterator obj_it;
     
    928934   glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0);
    929935
     936   GLuint laser_vao = 0;
     937   glGenVertexArrays(1, &laser_vao);
     938   glBindVertexArray(laser_vao);
     939
     940   glEnableVertexAttribArray(0);
     941   glEnableVertexAttribArray(1);
     942   glEnableVertexAttribArray(2);
     943
     944   glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     945   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
     946
     947   glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
     948   glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
     949
     950   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
     951   glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, 0);
     952
    930953   float cam_speed = 1.0f;
    931954   float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
     
    965988   GLuint ub_binding_point = 0;
    966989
     990   // TODO: Replace test_loc and mat_loc with more descriptive names
    967991   GLuint view_test_loc = glGetUniformLocation(color_sp, "view");
    968992   GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj");
     
    9761000   GLuint laser_proj_mat_loc = glGetUniformLocation(laser_sp, "proj");
    9771001   GLuint laser_color_loc = glGetUniformLocation(laser_sp, "laser_color");
     1002   GLuint laser_sp_ub_index = glGetUniformBlockIndex(laser_sp, "models");
     1003
    9781004
    9791005   glUseProgram(color_sp);
     
    9831009   glUniformBlockBinding(color_sp, color_sp_ub_index, ub_binding_point);
    9841010   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
     1011
    9851012
    9861013   glUseProgram(texture_sp);
     
    9961023   glUniformMatrix4fv(laser_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
    9971024   glUniform3f(laser_color_loc, 0.2f, 1.0f, 0.2f);
     1025
     1026   glUniformBlockBinding(laser_sp, laser_sp_ub_index, ub_binding_point);
     1027   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
     1028
    9981029
    9991030   bool cam_moved = false;
     
    12031234            renderScene(shaderBufferInfo,
    12041235               color_sp, texture_sp, laser_sp,
    1205                color_vao, texture_vao,
     1236               color_vao, texture_vao, laser_vao,
    12061237               colors_vbo, selected_colors_vbo,
    12071238               selectedObject);
     
    15511582}
    15521583
    1553 // currently only works correctly for lasers oriented along the z axis
    1554 void addLaserToScene(SceneObject& obj, vec3 start, vec3 end, vec3 color, GLfloat width) {
     1584void addLaserToScene(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) {
     1585   SceneObject obj = SceneObject();
    15551586   obj.id = objects.size(); // currently unused
    15561587   obj.type = TYPE_LASER;
     1588   obj.shader_program = laser_sp;
    15571589   obj.deleted = false;
    15581590
    1559    // I really need to create a direction vector and add/subtract that from start and end
    1560    // to get the right coords
    1561    // Also need to multiply the width times a vector perpendicular to it
    1562    vec3 dir = end - start;
     1591   float length = (end-start).length();
    15631592
    15641593   obj.points = {
    1565       start.x + width / 2, start.y, start.z - width / 2,
    1566       start.x - width / 2, start.y, start.z - width / 2,
    1567       start.x - width / 2, start.y, start.z,
    1568       start.x + width / 2, start.y, start.z - width / 2,
    1569       start.x - width / 2, start.y, start.z,
    1570       start.x + width / 2, start.y, start.z,
    1571       end.x + width / 2,   end.y,   end.z + width / 2,
    1572       end.x - width / 2,   end.y,   end.z + width / 2,
    1573       start.x - width / 2, start.y, start.z - width / 2,
    1574       end.x + width / 2,   end.y,   end.z + width / 2,
    1575       start.x - width / 2, start.y, start.z - width / 2,
    1576       start.x + width / 2, start.y, start.z - width / 2,
    1577       end.x + width / 2,   end.y,   end.z,
    1578       end.x - width / 2,   end.y,   end.z,
    1579       end.x - width / 2,   end.y,   end.z + width / 2,
    1580       end.x + width / 2,   end.y,   end.z,
    1581       end.x - width / 2,   end.y,   end.z + width / 2,
    1582       end.x + width / 2,   end.y,   end.z + width / 2,
    1583    };
    1584 
    1585    vec3 end_color = vec3(1.0f, 0.0f, 0.0f); // temporary
    1586    obj.colors = {
    1587       end_color.x, end_color.y, end_color.z,
    1588       end_color.x, end_color.y, end_color.z,
    1589       end_color.x, end_color.y, end_color.z,
    1590       end_color.x, end_color.y, end_color.z,
    1591       end_color.x, end_color.y, end_color.z,
    1592       end_color.x, end_color.y, end_color.z,
    1593       color.x, color.y, color.z,
    1594       color.x, color.y, color.z,
    1595       color.x, color.y, color.z,
    1596       color.x, color.y, color.z,
    1597       color.x, color.y, color.z,
    1598       color.x, color.y, color.z,
    1599       end_color.x, end_color.y, end_color.z,
    1600       end_color.x, end_color.y, end_color.z,
    1601       end_color.x, end_color.y, end_color.z,
    1602       end_color.x, end_color.y, end_color.z,
    1603       end_color.x, end_color.y, end_color.z,
    1604       end_color.x, end_color.y, end_color.z,
     1594       width / 2, 0.0f, -width / 2,
     1595      -width / 2, 0.0f, -width / 2,
     1596      -width / 2, 0.0f, 0.0f,
     1597       width / 2, 0.0f, -width / 2,
     1598      -width / 2, 0.0f, 0.0f,
     1599       width / 2, 0.0f, 0.0f,
     1600       width / 2, 0.0f, -length + width / 2,
     1601      -width / 2, 0.0f, -length + width / 2,
     1602      -width / 2, 0.0f, -width / 2,
     1603       width / 2, 0.0f, -length + width / 2,
     1604      -width / 2, 0.0f, -width / 2,
     1605       width / 2, 0.0f, -width / 2,
     1606       width / 2, 0.0f, -length,
     1607      -width / 2, 0.0f, -length,
     1608      -width / 2, 0.0f, -length + width / 2,
     1609       width / 2, 0.0f, -length,
     1610      -width / 2, 0.0f, -length + width / 2,
     1611       width / 2, 0.0f, -length + width / 2,
    16051612   };
    16061613
     
    16271634
    16281635   obj.num_points = obj.points.size() / 3;
     1636   obj.model_base = translate(mat4(1.0f), start);
     1637   obj.model_transform = mat4(1.0f);
    16291638
    16301639   objects.push_back(obj);
     
    17961805   glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.points.size() * sizeof(GLfloat), &obj.points[0]);
    17971806
    1798    glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    1799    glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]);
    1800 
     1807   glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
     1808   glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]);
     1809
     1810   glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
     1811   for (int i = 0; i < obj.num_points; i++) {
     1812      glBufferSubData(GL_ARRAY_BUFFER, (obj.vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &obj.ubo_offset);
     1813   }
    18011814
    18021815   if (obj.type != TYPE_LASER) {
     1816      glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
     1817      glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.colors.size() * sizeof(GLfloat), &obj.colors[0]);
     1818
    18031819      glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    18041820      glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.selected_colors.size() * sizeof(GLfloat), &obj.selected_colors[0]);
    1805    }
    1806 
    1807    glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    1808    glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]);
    1809 
    1810    if (obj.type != TYPE_LASER) {
     1821
    18111822      glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    18121823      glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.normals.size() * sizeof(GLfloat), &obj.normals[0]);
    1813 
    1814       glBindBuffer(GL_ARRAY_BUFFER, model_mat_idx_vbo);
    1815       for (int i = 0; i < obj.num_points; i++) {
    1816          glBufferSubData(GL_ARRAY_BUFFER, (obj.vertex_vbo_offset + i) * sizeof(GLuint), sizeof(GLuint), &obj.ubo_offset);
    1817       }
    1818 
    1819       obj.model_mat = obj.model_transform * obj.model_base;
    1820       glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    1821       glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
    1822    }
     1824   }
     1825
     1826   obj.model_mat = obj.model_transform * obj.model_base;
     1827   glBindBuffer(GL_UNIFORM_BUFFER, ubo);
     1828   glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
    18231829
    18241830   bufferInfo->vbo_offset += obj.num_points;
     
    18381844void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    18391845                  GLuint color_sp, GLuint texture_sp, GLuint laser_sp,
    1840                   GLuint color_vao, GLuint texture_vao,
     1846                  GLuint color_vao, GLuint texture_vao, GLuint laser_vao,
    18411847                  GLuint colors_vbo, GLuint selected_colors_vbo,
    18421848                  SceneObject* selectedObject) {
     
    18651871
    18661872   glUseProgram(laser_sp);
    1867    glBindVertexArray(texture_vao);
     1873   glBindVertexArray(laser_vao);
    18681874
    18691875   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset);
Note: See TracChangeset for help on using the changeset viewer.