started adding texture (coord) support
This commit is contained in:
parent
8a806b8c65
commit
b9bd185968
@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD 20)
|
|||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
set(CMAKE_CXX_FLAGS "/O2")
|
set(CMAKE_CXX_FLAGS "")
|
||||||
else()
|
else()
|
||||||
set(CMAKE_CXX_FLAGS "-D_DEBUG -Wall")
|
set(CMAKE_CXX_FLAGS "-D_DEBUG -Wall")
|
||||||
endif()
|
endif()
|
||||||
|
|||||||
@ -85,7 +85,6 @@ void CommandBuffer::bind(Terrain* terrain) {
|
|||||||
void CommandBuffer::bind(HLBSP::BSP* bsp) {
|
void CommandBuffer::bind(HLBSP::BSP* bsp) {
|
||||||
bind(*bsp->pipeline);
|
bind(*bsp->pipeline);
|
||||||
bind(*bsp->vertex_buffer);
|
bind(*bsp->vertex_buffer);
|
||||||
command_buffer.bindIndexBuffer(*bsp->index_buffer, 0, vk::IndexType::eUint32);
|
|
||||||
bind(bsp->pipeline->layout, bsp->pipeline->desc_set);
|
bind(bsp->pipeline->layout, bsp->pipeline->desc_set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -345,12 +345,12 @@ void Renderer::draw() {
|
|||||||
command_buffer->command_buffer.setViewport(0, viewport);
|
command_buffer->command_buffer.setViewport(0, viewport);
|
||||||
command_buffer->command_buffer.setScissor(0, scissor);
|
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->bind(bsp.get());
|
||||||
/*command_buffer->draw(bsp->vertices.size(), 1);*/
|
/*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) {
|
if (show_bboxes) {
|
||||||
command_buffer->bind(*box_pipeline);
|
command_buffer->bind(*box_pipeline);
|
||||||
|
|||||||
@ -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);
|
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::set<int> present_faces;
|
||||||
std::vector<Face> visible_faces;
|
std::vector<Face> visible_faces;
|
||||||
// if (visibility_test) {
|
// 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);
|
auto fr_planes = frustum(view);
|
||||||
|
|
||||||
if (leaf_idx == last_leaf)
|
if (leaf_idx == last_leaf)
|
||||||
index_buffer->upload(indices);
|
return;
|
||||||
|
|
||||||
last_leaf = leaf_idx;
|
last_leaf = leaf_idx;
|
||||||
auto& cam_leaf = leaves[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;
|
visible_faces = faces;
|
||||||
}
|
}
|
||||||
|
|
||||||
indices.clear();
|
textured_vertices.clear();
|
||||||
|
|
||||||
for (auto& face : visible_faces) {
|
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++) {
|
for (i16 i = 1, j = 2; j < face.n_surf_edges; i++, j++) {
|
||||||
indices.push_back(face.first_surf_edge_idx);
|
textured_vertices.push_back(Vertex{
|
||||||
indices.push_back(face.first_surf_edge_idx+i);
|
.pos = processed_vertices[face.first_surf_edge_idx],
|
||||||
indices.push_back(face.first_surf_edge_idx+j);
|
.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) {
|
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");
|
Log::debug("Loading vertices\n");
|
||||||
copy_data(file_data.data(), vertices, header->vertices);
|
copy_data(file_data.data(), vertices, header->vertices);
|
||||||
for (auto& vertex : vertices) {
|
for (auto& vertex : vertices) {
|
||||||
change_swizzle(vertex.pos);
|
change_swizzle(vertex);
|
||||||
}
|
}
|
||||||
|
|
||||||
Log::debug("Loading nodes\n");
|
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");
|
Log::debug("Loading surfedges\n");
|
||||||
copy_data(file_data.data(), surfedges, header->surf_edges);
|
copy_data(file_data.data(), surfedges, header->surf_edges);
|
||||||
vertices_prime.reserve(surfedges.size());
|
processed_vertices.reserve(surfedges.size());
|
||||||
/* use this to build our vertices_prime, idea thanks to gzalo's HalfMapper */
|
/* use this to build our processed_vertices, idea thanks to gzalo's HalfMapper */
|
||||||
for(const auto& s : surfedges) {
|
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");
|
Log::debug("Loading models\n");
|
||||||
@ -229,13 +246,13 @@ BSP::BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname)
|
|||||||
change_swizzle(model.bb_maxes);
|
change_swizzle(model.bb_maxes);
|
||||||
}
|
}
|
||||||
|
|
||||||
Log::debug("Creating vertex buffer of size %zu\n", vertices.size());
|
size_t max_vertex_count = 0;
|
||||||
vertex_buffer = std::make_unique<GeneralVertexBuffer<Vertex>>(phys_dev, dev, vertices_prime.size());
|
|
||||||
vertex_buffer->upload(vertices_prime);
|
|
||||||
|
|
||||||
Log::debug("Creating index buffer\n");
|
for (const auto& face : faces) {
|
||||||
/* set limit at 256Mi indices */
|
max_vertex_count += (face.n_surf_edges - 2) * 3;
|
||||||
index_buffer = std::make_unique<Buffer>(phys_dev, dev, 100000 * sizeof(u32),
|
}
|
||||||
vk::BufferUsageFlagBits::eIndexBuffer, vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible
|
|
||||||
);
|
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);
|
||||||
}
|
}
|
||||||
@ -28,6 +28,7 @@ namespace HLBSP {
|
|||||||
using rgba = glm::u8vec4;
|
using rgba = glm::u8vec4;
|
||||||
|
|
||||||
using vec3 = glm::vec3;
|
using vec3 = glm::vec3;
|
||||||
|
using vec2 = glm::vec2;
|
||||||
using ivec3 = glm::vec<3, i16>;
|
using ivec3 = glm::vec<3, i16>;
|
||||||
|
|
||||||
struct Header {
|
struct Header {
|
||||||
@ -83,6 +84,7 @@ namespace HLBSP {
|
|||||||
|
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
vec3 pos;
|
vec3 pos;
|
||||||
|
vec2 uv;
|
||||||
|
|
||||||
static inline std::vector<vk::VertexInputAttributeDescription> attrs(uint32_t binding) {
|
static inline std::vector<vk::VertexInputAttributeDescription> attrs(uint32_t binding) {
|
||||||
return std::vector<vk::VertexInputAttributeDescription> {
|
return std::vector<vk::VertexInputAttributeDescription> {
|
||||||
@ -91,6 +93,11 @@ namespace HLBSP {
|
|||||||
.binding = binding,
|
.binding = binding,
|
||||||
.format = vk::Format::eR32G32B32Sfloat,
|
.format = vk::Format::eR32G32B32Sfloat,
|
||||||
.offset = offsetof(Vertex, pos),
|
.offset = offsetof(Vertex, pos),
|
||||||
|
}, {
|
||||||
|
.location = 1,
|
||||||
|
.binding = binding,
|
||||||
|
.format = vk::Format::eR32G32Sfloat,
|
||||||
|
.offset = offsetof(Vertex, uv),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -190,7 +197,7 @@ namespace HLBSP {
|
|||||||
|
|
||||||
struct BSP {
|
struct BSP {
|
||||||
BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname);
|
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);
|
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]);
|
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);
|
int get_index_from_surfedge(int surfedge);
|
||||||
@ -204,8 +211,8 @@ namespace HLBSP {
|
|||||||
std::vector<::std::map<::std::string, std::string>> entities;
|
std::vector<::std::map<::std::string, std::string>> entities;
|
||||||
std::vector<Plane> planes;
|
std::vector<Plane> planes;
|
||||||
std::vector<MipTexture> textures;
|
std::vector<MipTexture> textures;
|
||||||
std::vector<Vertex> vertices;
|
std::vector<glm::vec3> vertices;
|
||||||
std::vector<Vertex> vertices_prime;
|
std::vector<glm::vec3> processed_vertices;
|
||||||
/* skipping vis for now */
|
/* skipping vis for now */
|
||||||
std::vector<Node> nodes;
|
std::vector<Node> nodes;
|
||||||
std::vector<TexInfo> tex_infos;
|
std::vector<TexInfo> tex_infos;
|
||||||
@ -218,8 +225,7 @@ namespace HLBSP {
|
|||||||
std::vector<Surfedge> surfedges;
|
std::vector<Surfedge> surfedges;
|
||||||
std::vector<Model> models;
|
std::vector<Model> models;
|
||||||
|
|
||||||
std::vector<u32> indices;
|
std::vector<Vertex> textured_vertices;
|
||||||
std::unique_ptr<Buffer> index_buffer;
|
|
||||||
std::unique_ptr<GeneralVertexBuffer<Vertex>> vertex_buffer;
|
std::unique_ptr<GeneralVertexBuffer<Vertex>> vertex_buffer;
|
||||||
std::unique_ptr<GraphicsPipeline> pipeline;
|
std::unique_ptr<GraphicsPipeline> pipeline;
|
||||||
/* to eliminate needless re-loading*/
|
/* to eliminate needless re-loading*/
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
#version 460 core
|
#version 460 core
|
||||||
|
|
||||||
layout (location = 0) in vec4 color;
|
layout (location = 0) in vec4 color;
|
||||||
|
layout (location = 1) in vec2 texCoord;
|
||||||
|
|
||||||
layout (location = 0) out vec4 FragColor;
|
layout (location = 0) out vec4 FragColor;
|
||||||
|
|
||||||
layout (set = 0, binding = 0) uniform Matrices {
|
layout (set = 0, binding = 0) uniform Matrices {
|
||||||
|
|||||||
@ -1,7 +1,9 @@
|
|||||||
#version 460 core
|
#version 460 core
|
||||||
layout (location = 0) in vec3 aPos;
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec2 aTexCoord;
|
||||||
|
|
||||||
layout (location = 0) out vec4 color;
|
layout (location = 0) out vec4 color;
|
||||||
|
layout (location = 1) out vec2 texCoord;
|
||||||
|
|
||||||
layout (set = 0, binding = 0) uniform Matrices {
|
layout (set = 0, binding = 0) uniform Matrices {
|
||||||
mat4 view;
|
mat4 view;
|
||||||
@ -24,10 +26,10 @@ vec4 unpackABGR(uint packedABGR) {
|
|||||||
return vec4(r,g,b,a);
|
return vec4(r,g,b,a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
|
||||||
gl_Position = proj * view * vec4(aPos, 1.0);
|
gl_Position = proj * view * vec4(aPos, 1.0);
|
||||||
|
|
||||||
color = vec4(normalize(aPos), 1.0);
|
color = vec4(aTexCoord, 0.0, 1.0);
|
||||||
|
texCoord = aTexCoord;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user