Changeset fd6f465 in opengl-game
- Timestamp:
- Jul 20, 2018, 1:42:00 AM (6 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 3effd81
- Parents:
- 9f9f9a7
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
laser.vert
r9f9f9a7 rfd6f465 1 1 #version 410 2 3 #define MAX_NUM_OBJECTS 1024 2 4 3 5 uniform mat4 view, proj; 4 6 7 layout (std140) uniform models { 8 mat4 model_mats[MAX_NUM_OBJECTS]; 9 }; 10 5 11 layout(location = 0) in vec3 vertex_position; 6 12 layout(location = 1) in vec2 vt; 13 layout(location = 2) in uint ubo_index; 7 14 8 15 out vec2 texture_coordinates; … … 10 17 11 18 void 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)); 13 20 texture_coordinates = vt; 14 21 -
new-game.cpp
r9f9f9a7 rfd6f465 46 46 * 47 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 48 61 */ 49 62 … … 125 138 void calculateObjectBoundingBox(SceneObject& obj); 126 139 127 void addLaserToScene( SceneObject& obj, vec3 start, vec3 end, vec3 color, GLfloat width);140 void addLaserToScene(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp); 128 141 129 142 void initializeBuffers( … … 163 176 void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo, 164 177 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, 166 179 GLuint colors_vbo, GLuint selected_colors_vbo, 167 180 SceneObject* selectedObject); … … 855 868 addObjectToSceneDuringInit(obj); 856 869 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); 866 872 867 873 vector<SceneObject>::iterator obj_it; … … 928 934 glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, 0, 0); 929 935 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 930 953 float cam_speed = 1.0f; 931 954 float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD; … … 965 988 GLuint ub_binding_point = 0; 966 989 990 // TODO: Replace test_loc and mat_loc with more descriptive names 967 991 GLuint view_test_loc = glGetUniformLocation(color_sp, "view"); 968 992 GLuint proj_test_loc = glGetUniformLocation(color_sp, "proj"); … … 976 1000 GLuint laser_proj_mat_loc = glGetUniformLocation(laser_sp, "proj"); 977 1001 GLuint laser_color_loc = glGetUniformLocation(laser_sp, "laser_color"); 1002 GLuint laser_sp_ub_index = glGetUniformBlockIndex(laser_sp, "models"); 1003 978 1004 979 1005 glUseProgram(color_sp); … … 983 1009 glUniformBlockBinding(color_sp, color_sp_ub_index, ub_binding_point); 984 1010 glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE); 1011 985 1012 986 1013 glUseProgram(texture_sp); … … 996 1023 glUniformMatrix4fv(laser_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat)); 997 1024 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 998 1029 999 1030 bool cam_moved = false; … … 1203 1234 renderScene(shaderBufferInfo, 1204 1235 color_sp, texture_sp, laser_sp, 1205 color_vao, texture_vao, 1236 color_vao, texture_vao, laser_vao, 1206 1237 colors_vbo, selected_colors_vbo, 1207 1238 selectedObject); … … 1551 1582 } 1552 1583 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) { 1584 void addLaserToScene(vec3 start, vec3 end, vec3 color, GLfloat width, GLuint laser_sp) { 1585 SceneObject obj = SceneObject(); 1555 1586 obj.id = objects.size(); // currently unused 1556 1587 obj.type = TYPE_LASER; 1588 obj.shader_program = laser_sp; 1557 1589 obj.deleted = false; 1558 1590 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(); 1563 1592 1564 1593 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, 1605 1612 }; 1606 1613 … … 1627 1634 1628 1635 obj.num_points = obj.points.size() / 3; 1636 obj.model_base = translate(mat4(1.0f), start); 1637 obj.model_transform = mat4(1.0f); 1629 1638 1630 1639 objects.push_back(obj); … … 1796 1805 glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.points.size() * sizeof(GLfloat), &obj.points[0]); 1797 1806 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 } 1801 1814 1802 1815 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 1803 1819 glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo); 1804 1820 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 1811 1822 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 1812 1823 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)); 1823 1829 1824 1830 bufferInfo->vbo_offset += obj.num_points; … … 1838 1844 void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo, 1839 1845 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, 1841 1847 GLuint colors_vbo, GLuint selected_colors_vbo, 1842 1848 SceneObject* selectedObject) { … … 1865 1871 1866 1872 glUseProgram(laser_sp); 1867 glBindVertexArray( texture_vao);1873 glBindVertexArray(laser_vao); 1868 1874 1869 1875 glDrawArrays(GL_TRIANGLES, shaderBufferInfo[laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset);
Note:
See TracChangeset
for help on using the changeset viewer.