Dealing with incredibly annoying issue in rendering the BSPs

This commit is contained in:
connellpaxton 2024-02-19 11:58:04 -05:00
parent f2e78fbf76
commit ff771ba127
9 changed files with 63 additions and 39 deletions

View File

@ -356,7 +356,7 @@ void Renderer::draw() {
auto scissor = vk::Rect2D { auto scissor = vk::Rect2D {
.offset = {0, 0}, .offset = {0, 0},
.extent = swapchain->extent, .extent = win.getDimensions(),
}; };
/* no secondary command buffers (yet), so contents are passed inline */ /* no secondary command buffers (yet), so contents are passed inline */
@ -381,24 +381,27 @@ void Renderer::draw() {
uniform_buffer->upload(uni); uniform_buffer->upload(uni);
/*
command_buffer->bind(*terrain_pipeline);
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);
/*if (line_mode)
command_buffer->command_buffer.setPolygonModeEXT(vk::PolygonMode::eLine);
else
command_buffer->command_buffer.setPolygonModeEXT(vk::PolygonMode::eFill);*/
/*command_buffer->bind(*terrain_pipeline);
command_buffer->bind(terrain_pipeline->layout, terrain_pipeline->desc_set); command_buffer->bind(terrain_pipeline->layout, terrain_pipeline->desc_set);
command_buffer->bind(terrain.get()); command_buffer->bind(terrain.get());
command_buffer->command_buffer.drawIndexed(terrain->indices.size(), 1, 0, 0, 0); command_buffer->command_buffer.drawIndexed(terrain->indices.size(), 1, 0, 0, 0);*/
command_buffer->bind(*model_pipeline); /*command_buffer->bind(*model_pipeline);
command_buffer->bind(model_pipeline->layout, model_pipeline->desc_set); command_buffer->bind(model_pipeline->layout, model_pipeline->desc_set);
command_buffer->bind(models[0]); command_buffer->bind(models[0]);
command_buffer->command_buffer.drawIndexed(models[0]->indices.size(), 10, 0, 0, 0);*/ command_buffer->command_buffer.drawIndexed(models[0]->indices.size(), 10, 0, 0, 0);*/
bsp->load_indices(cam.pos); bsp->load_indices(cam.pos, visibility_testing);
command_buffer->bind(bsp.get()); command_buffer->bind(bsp.get());
command_buffer->command_buffer.setViewport(0, viewport);
command_buffer->command_buffer.setScissor(0, scissor);
command_buffer->command_buffer.drawIndexed(bsp->indices.size(), 1, 0, 0, 0); command_buffer->command_buffer.drawIndexed(bsp->indices.size(), 1, 0, 0, 0);
/* draw User Interface stuff */ /* draw User Interface stuff */

View File

@ -77,6 +77,8 @@ struct Renderer {
float speed = 1.0; float speed = 1.0;
bool running = true; bool running = true;
bool visibility_testing;
float tess_factor = 1.8f; float tess_factor = 1.8f;
float tess_edge_size = 20.0f; float tess_edge_size = 20.0f;
}; };

View File

@ -23,9 +23,10 @@ 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) { void BSP::load_indices(const glm::vec3& cam_pos, bool visibility_test) {
std::set<int> present_faces; std::set<int> present_faces;
std::vector<Face> visible_faces; std::vector<Face> visible_faces;
if (visibility_test) {
auto leaf_idx = determine_leaf(cam_pos); auto leaf_idx = determine_leaf(cam_pos);
if (leaf_idx == last_leaf) if (leaf_idx == last_leaf)
return; return;
@ -50,6 +51,9 @@ void BSP::load_indices(const glm::vec3& cam_pos) {
visible_faces.push_back(faces[idx]); visible_faces.push_back(faces[idx]);
} }
} }
} else {
visible_faces = faces;
}
for (auto& face : visible_faces) { for (auto& face : visible_faces) {
switch (face.type) { switch (face.type) {
@ -99,6 +103,13 @@ bool BSP::determine_visibility(int vis, int cluster) {
return !!(set & (1 << (cluster & 0x7))); return !!(set & (1 << (cluster & 0x7)));
} }
/* changes handedness by swapping z and y */
static inline void change_swizzle(glm::vec3& v) {
auto tmp = v.y;
v.y = v.z;
v.z = tmp;
}
BSP::BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname) : dev(dev), filename(fname) { BSP::BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname) : dev(dev), filename(fname) {
file_data = file::slurpb(fname); file_data = file::slurpb(fname);
Log::debug("File size: %zu\n", file_data.size()); Log::debug("File size: %zu\n", file_data.size());
@ -113,6 +124,10 @@ BSP::BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname)
copy_data(file_data.data(), entities, header->entities); copy_data(file_data.data(), entities, header->entities);
copy_data(file_data.data(), textures, header->textures); copy_data(file_data.data(), textures, header->textures);
copy_data(file_data.data(), planes, header->planes); copy_data(file_data.data(), planes, header->planes);
/* change swizzle */
for (auto& plane : planes) {
change_swizzle(plane.norm);
}
copy_data(file_data.data(), nodes, header->nodes); copy_data(file_data.data(), nodes, header->nodes);
copy_data(file_data.data(), leafs, header->leafs); copy_data(file_data.data(), leafs, header->leafs);
copy_data(file_data.data(), leaf_faces, header->leaf_faces); copy_data(file_data.data(), leaf_faces, header->leaf_faces);
@ -121,6 +136,12 @@ BSP::BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname)
copy_data(file_data.data(), brushes, header->brushes); copy_data(file_data.data(), brushes, header->brushes);
copy_data(file_data.data(), brush_sides, header->brush_sides); copy_data(file_data.data(), brush_sides, header->brush_sides);
copy_data(file_data.data(), vertices, header->vertices); copy_data(file_data.data(), vertices, header->vertices);
/* correct for handedness */
for (auto& vertex : vertices) {
change_swizzle(vertex.pos);
change_swizzle(vertex.norm);
}
copy_data(file_data.data(), mesh_vertices, header->mesh_vertices); copy_data(file_data.data(), mesh_vertices, header->mesh_vertices);
copy_data(file_data.data(), effects, header->effects); copy_data(file_data.data(), effects, header->effects);
copy_data(file_data.data(), faces, header->faces); copy_data(file_data.data(), faces, header->faces);
@ -136,7 +157,7 @@ BSP::BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname)
vertex_buffer->upload(vertices); vertex_buffer->upload(vertices);
/* set limit at 256Mi indices */ /* set limit at 256Mi indices */
index_buffer = std::make_unique<Buffer>(phys_dev, dev, 0x1000000 * sizeof(u32), index_buffer = std::make_unique<Buffer>(phys_dev, dev, 0x10000000 * sizeof(u32),
vk::BufferUsageFlagBits::eIndexBuffer, vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible vk::BufferUsageFlagBits::eIndexBuffer, vk::MemoryPropertyFlagBits::eHostCoherent | vk::MemoryPropertyFlagBits::eHostVisible
); );
} }

View File

@ -207,7 +207,7 @@ namespace Q3BSP {
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 glm::vec3& cam_pos); void load_indices(const glm::vec3& cam_pos, bool visibility_testing);
int determine_leaf(glm::vec3 cam_pos); int determine_leaf(glm::vec3 cam_pos);
bool determine_visibility(int vis, int cluster); bool determine_visibility(int vis, int cluster);

View File

@ -11,7 +11,7 @@
#include <Scene/Camera.hpp> #include <Scene/Camera.hpp>
UI::UI(Renderer* ren) : info{ .flycam = ren->flycam, .time = ren->time, .cam = ren->cam, .tess_factor = ren->tess_factor, .tess_edge_size = ren->tess_edge_size }, dev(ren->dev) { UI::UI(Renderer* ren) : info{ .flycam = ren->flycam, .visibility_testing = ren->visibility_testing, .time = ren->time, .cam = ren->cam, .tess_factor = ren->tess_factor, .tess_edge_size = ren->tess_edge_size }, dev(ren->dev) {
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
ImGui::CreateContext(); ImGui::CreateContext();
@ -92,6 +92,7 @@ void UI::newFrame() {
ImGui::Text("FPS: %f", info.fps); ImGui::Text("FPS: %f", info.fps);
ImGui::Text("Time: %f", info.time); ImGui::Text("Time: %f", info.time);
ImGui::Checkbox("Fly Camera", &info.flycam); ImGui::Checkbox("Fly Camera", &info.flycam);
ImGui::Checkbox("Visibility Testing", &info.visibility_testing);
ImGui::SliderFloat("Tessellation Factor", &info.tess_factor, 0.1, 10.0); ImGui::SliderFloat("Tessellation Factor", &info.tess_factor, 0.1, 10.0);
ImGui::SliderFloat("Edge Size", &info.tess_edge_size, 0.0, 40.0); ImGui::SliderFloat("Edge Size", &info.tess_edge_size, 0.0, 40.0);

View File

@ -12,6 +12,7 @@ struct UI {
struct UI_Info { struct UI_Info {
float fps = 0.0; float fps = 0.0;
bool& flycam; bool& flycam;
bool& visibility_testing;
float& time; float& time;
/* camera stuff */ /* camera stuff */
Camera& cam; Camera& cam;

View File

@ -32,14 +32,8 @@ vec4 unpackABGR(uint packedABGR) {
void main() { void main() {
mat4 zup_to_yup = mat4(
1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0
);
gl_Position = proj * view * zup_to_yup * vec4(aPos, 1.0); gl_Position = proj * view * vec4(aPos, 1.0);
texCoord = aTexCoord; texCoord = aTexCoord;
norm = aNorm; norm = aNorm;
color = unpackABGR(aColor); color = unpackABGR(aColor);

Binary file not shown.

View File

@ -28,7 +28,7 @@ int main(int argc, char* argv[]) {
while (in->queue.size()) { while (in->queue.size()) {
/* take event from front of queue, then process it */ /* take event from front of queue, then process it */
const auto event = in->queue.front(); const InputEvent event = in->queue.front();
in->queue.pop(); in->queue.pop();
switch (event.tag) { switch (event.tag) {
case InputEvent::Tag::eRESIZE: case InputEvent::Tag::eRESIZE:
@ -71,6 +71,8 @@ int main(int argc, char* argv[]) {
ren.speed *= 10.0; ren.speed *= 10.0;
} else if (event.key.key == GLFW_KEY_Y && event.key.state == GLFW_PRESS) { } else if (event.key.key == GLFW_KEY_Y && event.key.state == GLFW_PRESS) {
ren.speed /= 10.0; ren.speed /= 10.0;
} else if (event.key.key == GLFW_KEY_V && event.key.state == GLFW_PRESS) {
ren.visibility_testing = !ren.visibility_testing;
} }
break; break;
} }