Dealing with incredibly annoying issue in rendering the BSPs
This commit is contained in:
parent
f2e78fbf76
commit
ff771ba127
@ -356,7 +356,7 @@ void Renderer::draw() {
|
||||
|
||||
auto scissor = vk::Rect2D {
|
||||
.offset = {0, 0},
|
||||
.extent = swapchain->extent,
|
||||
.extent = win.getDimensions(),
|
||||
};
|
||||
|
||||
/* no secondary command buffers (yet), so contents are passed inline */
|
||||
@ -381,24 +381,27 @@ void Renderer::draw() {
|
||||
|
||||
uniform_buffer->upload(uni);
|
||||
|
||||
/*
|
||||
command_buffer->bind(*terrain_pipeline);
|
||||
command_buffer->command_buffer.setViewport(0, viewport);
|
||||
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.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(models[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->command_buffer.setViewport(0, viewport);
|
||||
command_buffer->command_buffer.setScissor(0, scissor);
|
||||
command_buffer->command_buffer.drawIndexed(bsp->indices.size(), 1, 0, 0, 0);
|
||||
|
||||
/* draw User Interface stuff */
|
||||
|
||||
@ -77,6 +77,8 @@ struct Renderer {
|
||||
float speed = 1.0;
|
||||
bool running = true;
|
||||
|
||||
bool visibility_testing;
|
||||
|
||||
float tess_factor = 1.8f;
|
||||
float tess_edge_size = 20.0f;
|
||||
};
|
||||
|
||||
@ -23,32 +23,36 @@ 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) {
|
||||
void BSP::load_indices(const glm::vec3& cam_pos, bool visibility_test) {
|
||||
std::set<int> present_faces;
|
||||
std::vector<Face> visible_faces;
|
||||
auto leaf_idx = determine_leaf(cam_pos);
|
||||
if (leaf_idx == last_leaf)
|
||||
return;
|
||||
if (visibility_test) {
|
||||
auto leaf_idx = determine_leaf(cam_pos);
|
||||
if (leaf_idx == last_leaf)
|
||||
return;
|
||||
|
||||
last_leaf = leaf_idx;
|
||||
auto& cam_leaf = leafs[leaf_idx];
|
||||
last_leaf = leaf_idx;
|
||||
auto& cam_leaf = leafs[leaf_idx];
|
||||
|
||||
|
||||
std::vector<Leaf> visible_leafs;
|
||||
for (auto& leaf : leafs) {
|
||||
if (determine_visibility(cam_leaf.cluster_idx, leaf.cluster_idx))
|
||||
visible_leafs.push_back(leaf);
|
||||
}
|
||||
|
||||
for (const auto& leaf : visible_leafs) {
|
||||
for (size_t i = 0; i < leaf.n_leaf_faces; i++) {
|
||||
auto idx = leaf_faces[leaf.first_leaf_face_idx + i].face_idx;
|
||||
if (present_faces.contains(idx))
|
||||
continue;
|
||||
|
||||
present_faces.insert(idx);
|
||||
visible_faces.push_back(faces[idx]);
|
||||
std::vector<Leaf> visible_leafs;
|
||||
for (auto& leaf : leafs) {
|
||||
if (determine_visibility(cam_leaf.cluster_idx, leaf.cluster_idx))
|
||||
visible_leafs.push_back(leaf);
|
||||
}
|
||||
|
||||
for (const auto& leaf : visible_leafs) {
|
||||
for (size_t i = 0; i < leaf.n_leaf_faces; i++) {
|
||||
auto idx = leaf_faces[leaf.first_leaf_face_idx + i].face_idx;
|
||||
if (present_faces.contains(idx))
|
||||
continue;
|
||||
|
||||
present_faces.insert(idx);
|
||||
visible_faces.push_back(faces[idx]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
visible_faces = faces;
|
||||
}
|
||||
|
||||
for (auto& face : visible_faces) {
|
||||
@ -99,6 +103,13 @@ bool BSP::determine_visibility(int vis, int cluster) {
|
||||
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) {
|
||||
file_data = file::slurpb(fname);
|
||||
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(), textures, header->textures);
|
||||
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(), leafs, header->leafs);
|
||||
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(), brush_sides, header->brush_sides);
|
||||
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(), effects, header->effects);
|
||||
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);
|
||||
|
||||
/* 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
|
||||
);
|
||||
}
|
||||
@ -207,7 +207,7 @@ namespace Q3BSP {
|
||||
|
||||
struct BSP {
|
||||
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);
|
||||
bool determine_visibility(int vis, int cluster);
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
|
||||
#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::CreateContext();
|
||||
|
||||
@ -92,6 +92,7 @@ void UI::newFrame() {
|
||||
ImGui::Text("FPS: %f", info.fps);
|
||||
ImGui::Text("Time: %f", info.time);
|
||||
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("Edge Size", &info.tess_edge_size, 0.0, 40.0);
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@ struct UI {
|
||||
struct UI_Info {
|
||||
float fps = 0.0;
|
||||
bool& flycam;
|
||||
bool& visibility_testing;
|
||||
float& time;
|
||||
/* camera stuff */
|
||||
Camera& cam;
|
||||
|
||||
@ -32,14 +32,8 @@ vec4 unpackABGR(uint packedABGR) {
|
||||
|
||||
|
||||
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;
|
||||
norm = aNorm;
|
||||
color = unpackABGR(aColor);
|
||||
|
||||
Binary file not shown.
@ -28,7 +28,7 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
while (in->queue.size()) {
|
||||
/* take event from front of queue, then process it */
|
||||
const auto event = in->queue.front();
|
||||
const InputEvent event = in->queue.front();
|
||||
in->queue.pop();
|
||||
switch (event.tag) {
|
||||
case InputEvent::Tag::eRESIZE:
|
||||
@ -71,6 +71,8 @@ int main(int argc, char* argv[]) {
|
||||
ren.speed *= 10.0;
|
||||
} else if (event.key.key == GLFW_KEY_Y && event.key.state == GLFW_PRESS) {
|
||||
ren.speed /= 10.0;
|
||||
} else if (event.key.key == GLFW_KEY_V && event.key.state == GLFW_PRESS) {
|
||||
ren.visibility_testing = !ren.visibility_testing;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user