started adding texture (coord) support

This commit is contained in:
connellpaxton 2024-02-24 00:18:55 -05:00
parent 8a806b8c65
commit b9bd185968
7 changed files with 59 additions and 33 deletions

View File

@ -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()

View File

@ -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);
}

View File

@ -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);

View File

@ -25,7 +25,14 @@ static inline void copy_data(void* file_data, std::vector<T>& 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<int> present_faces;
std::vector<Face> 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<GeneralVertexBuffer<Vertex>>(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<Buffer>(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<GeneralVertexBuffer<Vertex>>(phys_dev, dev, max_vertex_count);
textured_vertices.reserve(max_vertex_count);
}

View File

@ -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<vk::VertexInputAttributeDescription> attrs(uint32_t binding) {
return std::vector<vk::VertexInputAttributeDescription> {
@ -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<glm::vec4, 6>& 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<Plane> planes;
std::vector<MipTexture> textures;
std::vector<Vertex> vertices;
std::vector<Vertex> vertices_prime;
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> processed_vertices;
/* skipping vis for now */
std::vector<Node> nodes;
std::vector<TexInfo> tex_infos;
@ -218,8 +225,7 @@ namespace HLBSP {
std::vector<Surfedge> surfedges;
std::vector<Model> models;
std::vector<u32> indices;
std::unique_ptr<Buffer> index_buffer;
std::vector<Vertex> textured_vertices;
std::unique_ptr<GeneralVertexBuffer<Vertex>> vertex_buffer;
std::unique_ptr<GraphicsPipeline> pipeline;
/* to eliminate needless re-loading*/

View File

@ -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 {

View File

@ -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;
}