From b9bd1859684e288ef17ca7b00f6c7be5f775cca1 Mon Sep 17 00:00:00 2001 From: connellpaxton Date: Sat, 24 Feb 2024 00:18:55 -0500 Subject: [PATCH] started adding texture (coord) support --- CMakeLists.txt | 2 +- Renderer/CommandBuffer.cpp | 1 - Renderer/Renderer.cpp | 6 ++-- Scene/BSP.cpp | 59 ++++++++++++++++++++++++------------- Scene/BSP.hpp | 16 ++++++---- assets/shaders/map/bsp.frag | 2 ++ assets/shaders/map/bsp.vert | 6 ++-- 7 files changed, 59 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a28d8b..6deee4a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) if(WIN32) -set(CMAKE_CXX_FLAGS "/O2") +set(CMAKE_CXX_FLAGS "") else() set(CMAKE_CXX_FLAGS "-D_DEBUG -Wall") endif() diff --git a/Renderer/CommandBuffer.cpp b/Renderer/CommandBuffer.cpp index 80a960c..7002521 100644 --- a/Renderer/CommandBuffer.cpp +++ b/Renderer/CommandBuffer.cpp @@ -85,7 +85,6 @@ void CommandBuffer::bind(Terrain* terrain) { void CommandBuffer::bind(HLBSP::BSP* bsp) { bind(*bsp->pipeline); bind(*bsp->vertex_buffer); - command_buffer.bindIndexBuffer(*bsp->index_buffer, 0, vk::IndexType::eUint32); bind(bsp->pipeline->layout, bsp->pipeline->desc_set); } diff --git a/Renderer/Renderer.cpp b/Renderer/Renderer.cpp index 654199a..4ec6d4a 100644 --- a/Renderer/Renderer.cpp +++ b/Renderer/Renderer.cpp @@ -345,12 +345,12 @@ void Renderer::draw() { command_buffer->command_buffer.setViewport(0, viewport); command_buffer->command_buffer.setScissor(0, scissor); - bsp->load_indices(cam.pos, visibility_testing, p * uni.view); + bsp->load_vertices(cam.pos, visibility_testing, p * uni.view); command_buffer->bind(bsp.get()); /*command_buffer->draw(bsp->vertices.size(), 1);*/ - command_buffer->command_buffer.drawIndexed(bsp->indices.size(), 1, 0, 0, 0); + command_buffer->draw(bsp->textured_vertices.size(), 1); - n_indices = bsp->indices.size(); + n_indices = bsp->textured_vertices.size(); if (show_bboxes) { command_buffer->bind(*box_pipeline); diff --git a/Scene/BSP.cpp b/Scene/BSP.cpp index 6bb5b47..d9da477 100644 --- a/Scene/BSP.cpp +++ b/Scene/BSP.cpp @@ -25,7 +25,14 @@ static inline void copy_data(void* file_data, std::vector& dst, Lump& lump) { std::memcpy(dst.data(), ((u8*)file_data) + lump.offset, lump.len); } -void BSP::load_indices(const glm::vec3& cam_pos, bool visibility_test, const glm::mat4& view) { +glm::vec2 calc_tex_coords(const glm::vec3& v, const TexInfo& t) { + return glm::vec2( + t.shift_s + glm::dot(v, t.shift_s_dir), + t.shift_t + glm::dot(v, t.shift_t_dir) + ); +} + +void BSP::load_vertices(const glm::vec3& cam_pos, bool visibility_test, const glm::mat4& view) { std::set present_faces; std::vector visible_faces; // if (visibility_test) { @@ -35,7 +42,7 @@ void BSP::load_indices(const glm::vec3& cam_pos, bool visibility_test, const glm auto fr_planes = frustum(view); if (leaf_idx == last_leaf) - index_buffer->upload(indices); + return; last_leaf = leaf_idx; auto& cam_leaf = leaves[leaf_idx]; @@ -76,18 +83,28 @@ void BSP::load_indices(const glm::vec3& cam_pos, bool visibility_test, const glm visible_faces = faces; } - indices.clear(); + textured_vertices.clear(); for (auto& face : visible_faces) { + auto& tex_info = tex_infos[face.tex_info_idx]; + for (i16 i = 1, j = 2; j < face.n_surf_edges; i++, j++) { - indices.push_back(face.first_surf_edge_idx); - indices.push_back(face.first_surf_edge_idx+i); - indices.push_back(face.first_surf_edge_idx+j); + textured_vertices.push_back(Vertex{ + .pos = processed_vertices[face.first_surf_edge_idx], + .uv = calc_tex_coords(processed_vertices[face.first_surf_edge_idx], tex_info), + }); + textured_vertices.push_back(Vertex{ + .pos = processed_vertices[face.first_surf_edge_idx+i], + .uv = calc_tex_coords(processed_vertices[face.first_surf_edge_idx+i], tex_info), + }); + textured_vertices.push_back(Vertex{ + .pos = processed_vertices[face.first_surf_edge_idx+j], + .uv = calc_tex_coords(processed_vertices[face.first_surf_edge_idx+j], tex_info), + }); } -// indices.push_back(get_index_from_surfedge(face.first_surf_edge_idx)); } - index_buffer->upload(indices); + vertex_buffer->upload(textured_vertices); } int BSP::get_index_from_surfedge(int surfedge) { @@ -179,7 +196,7 @@ BSP::BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname) Log::debug("Loading vertices\n"); copy_data(file_data.data(), vertices, header->vertices); for (auto& vertex : vertices) { - change_swizzle(vertex.pos); + change_swizzle(vertex); } Log::debug("Loading nodes\n"); @@ -216,10 +233,10 @@ BSP::BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname) Log::debug("Loading surfedges\n"); copy_data(file_data.data(), surfedges, header->surf_edges); - vertices_prime.reserve(surfedges.size()); - /* use this to build our vertices_prime, idea thanks to gzalo's HalfMapper */ + processed_vertices.reserve(surfedges.size()); + /* use this to build our processed_vertices, idea thanks to gzalo's HalfMapper */ for(const auto& s : surfedges) { - vertices_prime.push_back(vertices[edges[s > 0? s : -s].vertex_indices[s<=0]]); + processed_vertices.push_back(vertices[edges[s > 0? s : -s].vertex_indices[s<=0]]); } Log::debug("Loading models\n"); @@ -228,14 +245,14 @@ BSP::BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname) change_swizzle(model.bb_mins); change_swizzle(model.bb_maxes); } - - Log::debug("Creating vertex buffer of size %zu\n", vertices.size()); - vertex_buffer = std::make_unique>(phys_dev, dev, vertices_prime.size()); - vertex_buffer->upload(vertices_prime); - Log::debug("Creating index buffer\n"); - /* set limit at 256Mi indices */ - index_buffer = std::make_unique(phys_dev, dev, 100000 * sizeof(u32), - vk::BufferUsageFlagBits::eIndexBuffer, vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible - ); + size_t max_vertex_count = 0; + + for (const auto& face : faces) { + max_vertex_count += (face.n_surf_edges - 2) * 3; + } + + Log::debug("Creating vertex buffer of size %zu\n", max_vertex_count); + vertex_buffer = std::make_unique>(phys_dev, dev, max_vertex_count); + textured_vertices.reserve(max_vertex_count); } \ No newline at end of file diff --git a/Scene/BSP.hpp b/Scene/BSP.hpp index 1f128b9..dbaa3a2 100644 --- a/Scene/BSP.hpp +++ b/Scene/BSP.hpp @@ -28,6 +28,7 @@ namespace HLBSP { using rgba = glm::u8vec4; using vec3 = glm::vec3; + using vec2 = glm::vec2; using ivec3 = glm::vec<3, i16>; struct Header { @@ -83,6 +84,7 @@ namespace HLBSP { struct Vertex { vec3 pos; + vec2 uv; static inline std::vector attrs(uint32_t binding) { return std::vector { @@ -91,6 +93,11 @@ namespace HLBSP { .binding = binding, .format = vk::Format::eR32G32B32Sfloat, .offset = offsetof(Vertex, pos), + }, { + .location = 1, + .binding = binding, + .format = vk::Format::eR32G32Sfloat, + .offset = offsetof(Vertex, uv), } }; } @@ -190,7 +197,7 @@ namespace HLBSP { struct BSP { BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname); - void load_indices(const vec3& cam_pos, bool visibility_testing, const glm::mat4& view); + void load_vertices(const vec3& cam_pos, bool visibility_testing, const glm::mat4& view); int determine_leaf(vec3 cam_pos); bool determine_visibility(const Leaf& cam_leaf, const Leaf& leaf, const std::array& frustum, const vec3 box_verts[8]); int get_index_from_surfedge(int surfedge); @@ -204,8 +211,8 @@ namespace HLBSP { std::vector<::std::map<::std::string, std::string>> entities; std::vector planes; std::vector textures; - std::vector vertices; - std::vector vertices_prime; + std::vector vertices; + std::vector processed_vertices; /* skipping vis for now */ std::vector nodes; std::vector tex_infos; @@ -218,8 +225,7 @@ namespace HLBSP { std::vector surfedges; std::vector models; - std::vector indices; - std::unique_ptr index_buffer; + std::vector textured_vertices; std::unique_ptr> vertex_buffer; std::unique_ptr pipeline; /* to eliminate needless re-loading*/ diff --git a/assets/shaders/map/bsp.frag b/assets/shaders/map/bsp.frag index d88277f..13bd762 100644 --- a/assets/shaders/map/bsp.frag +++ b/assets/shaders/map/bsp.frag @@ -1,6 +1,8 @@ #version 460 core layout (location = 0) in vec4 color; +layout (location = 1) in vec2 texCoord; + layout (location = 0) out vec4 FragColor; layout (set = 0, binding = 0) uniform Matrices { diff --git a/assets/shaders/map/bsp.vert b/assets/shaders/map/bsp.vert index 48e5d2c..7009065 100644 --- a/assets/shaders/map/bsp.vert +++ b/assets/shaders/map/bsp.vert @@ -1,7 +1,9 @@ #version 460 core layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoord; layout (location = 0) out vec4 color; +layout (location = 1) out vec2 texCoord; layout (set = 0, binding = 0) uniform Matrices { mat4 view; @@ -24,10 +26,10 @@ vec4 unpackABGR(uint packedABGR) { return vec4(r,g,b,a); } - void main() { gl_Position = proj * view * vec4(aPos, 1.0); - color = vec4(normalize(aPos), 1.0); + color = vec4(aTexCoord, 0.0, 1.0); + texCoord = aTexCoord; } \ No newline at end of file