FIXED THE DAMN FRUSTUM CULLING (I forgot to swap y & z vertices in the bounding boxes).

More like frustratum culling, amirite?
This commit is contained in:
connellpaxton 2024-02-20 23:57:39 -05:00
parent dc8e5a5483
commit bc865c969d
5 changed files with 35 additions and 14 deletions

View File

@ -79,10 +79,11 @@ void Input::handleMovementKeys(Renderer& ren) {
glm::vec3 forward; glm::vec3 forward;
if (ren.flycam) if (ren.flycam)
forward = glm::vec3(glm::sin(ren.cam.theta)*glm::cos(ren.cam.phi), glm::cos(ren.cam.theta), glm::sin(ren.cam.theta)*glm::sin(ren.cam.phi)); forward = glm::normalize(glm::vec3(glm::sin(ren.cam.theta)*glm::cos(ren.cam.phi), glm::cos(ren.cam.theta), glm::sin(ren.cam.theta)*glm::sin(ren.cam.phi)));
else else {
forward = glm::vec3(glm::cos(ren.cam.phi), 0.0, glm::sin(ren.cam.phi)); forward = glm::normalize(glm::vec3(glm::cos(ren.cam.phi), 0.0, glm::sin(ren.cam.phi)));
const auto right = glm::cross(forward, glm::vec3(0.0, 1.0, 0.0)); }
const auto right = glm::normalize(glm::cross(forward, glm::vec3(0.0, 1.0, 0.0)));
auto speed = glfwGetKey(in, GLFW_KEY_LEFT_SHIFT)? 2.0f : 1.0f; auto speed = glfwGetKey(in, GLFW_KEY_LEFT_SHIFT)? 2.0f : 1.0f;
speed *= ren.speed; speed *= ren.speed;
@ -143,7 +144,7 @@ void Input::handleCursorMovement(Renderer& ren, double x, double y) {
if (io.WantCaptureMouse) if (io.WantCaptureMouse)
return; return;
if (!ren.capture_mouse) { if (ren.in_menu) {
io.AddMousePosEvent(x, y); io.AddMousePosEvent(x, y);
return; return;
} }

View File

@ -72,7 +72,7 @@ struct Renderer {
float fps; float fps;
bool capture_mouse = false; bool in_menu = false;
bool flycam = false; bool flycam = false;
/* time speed */ /* time speed */
float time = 0.0; float time = 0.0;

View File

@ -127,7 +127,8 @@ bool BSP::determine_visibility(const Leaf& cam_leaf, const Leaf& leaf, const std
} }
/* changes handedness by swapping z and y */ /* changes handedness by swapping z and y */
static inline void change_swizzle(glm::vec3& v) { template<typename T>
static inline void change_swizzle(T& v) {
auto tmp = v.y; auto tmp = v.y;
v.y = v.z; v.y = v.z;
v.z = tmp; v.z = tmp;
@ -152,10 +153,22 @@ BSP::BSP(vk::PhysicalDevice phys_dev, vk::Device dev, const std::string& fname)
change_swizzle(plane.norm); change_swizzle(plane.norm);
} }
copy_data(file_data.data(), nodes, header->nodes); copy_data(file_data.data(), nodes, header->nodes);
for (auto& node : nodes) {
change_swizzle(node.bb_mins);
change_swizzle(node.bb_maxes);
}
copy_data(file_data.data(), leafs, header->leafs); copy_data(file_data.data(), leafs, header->leafs);
for (auto& leaf : leafs) {
change_swizzle(leaf.bb_mins);
change_swizzle(leaf.bb_maxes);
}
copy_data(file_data.data(), leaf_faces, header->leaf_faces); copy_data(file_data.data(), leaf_faces, header->leaf_faces);
copy_data(file_data.data(), leaf_brushes, header->leaf_brushes); copy_data(file_data.data(), leaf_brushes, header->leaf_brushes);
copy_data(file_data.data(), models, header->models); copy_data(file_data.data(), models, header->models);
for (auto& model : models) {
change_swizzle(model.bb_mins);
change_swizzle(model.bb_maxes);
}
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);

View File

@ -56,11 +56,17 @@ int main(int argc, char* argv[]) {
case InputEvent::Tag::eBUTTON: case InputEvent::Tag::eBUTTON:
break; break;
case InputEvent::Tag::eKEY: case InputEvent::Tag::eKEY:
if (event.key.key == GLFW_KEY_ESCAPE && event.key.state == GLFW_PRESS) {
ren.in_menu = !ren.in_menu;
in->setCursor(ren.in_menu);
break;
}
if (ren.in_menu)
break;
if (event.key.key == GLFW_KEY_Q) { if (event.key.key == GLFW_KEY_Q) {
return 0; return 0;
} else if (event.key.key == GLFW_KEY_ESCAPE && event.key.state == GLFW_PRESS) {
ren.capture_mouse = !ren.capture_mouse;
in->setCursor(!ren.capture_mouse);
} else if (event.key.key == GLFW_KEY_R && event.key.state == GLFW_PRESS) { } else if (event.key.key == GLFW_KEY_R && event.key.state == GLFW_PRESS) {
ren.time = 0; ren.time = 0;
} else if (event.key.key == GLFW_KEY_C && event.key.state == GLFW_PRESS) { } else if (event.key.key == GLFW_KEY_C && event.key.state == GLFW_PRESS) {

View File

@ -20,20 +20,21 @@ static glm::vec4 extract_plane(const glm::mat4& mat, int col) {
/* extracts frustum from projection matrix */ /* extracts frustum from projection matrix */
static std::array<glm::vec4, 6> frustum(const glm::mat4& mat) { static std::array<glm::vec4, 6> frustum(const glm::mat4& mat) {
/* Left, Right, Top, Bottom, Back, Front */ /* Left, Right, Top, Bottom, Back, Front */
return std::array<glm::vec4, 6> { std::array<glm::vec4, 6> ret; /* {
extract_plane(mat, 1), extract_plane(mat, 1),
extract_plane(mat, -1), extract_plane(mat, -1),
extract_plane(mat, 2), extract_plane(mat, 2),
extract_plane(mat, -2), extract_plane(mat, -2),
extract_plane(mat, 3), extract_plane(mat, 3),
extract_plane(mat, -3), extract_plane(mat, -3),
}; };*/
/*for (size_t i = 0; i < 3; i++) for (size_t i = 0; i < 3; i++)
for (size_t j = 0; j < 4; j++) for (size_t j = 0; j < 4; j++)
ret[i * 2][j] = mat[j].w + mat[j][i], ret[i * 2][j] = mat[j].w + mat[j][i],
ret[i * 2 + 1][j] = mat[j].w - mat[j][i];*/ ret[i * 2 + 1][j] = mat[j].w - mat[j][i];
return ret;
} }
static bool box_in_frustum(const std::array<glm::vec4, 6>& frustum, const glm::vec3 box_verts[8]) { static bool box_in_frustum(const std::array<glm::vec4, 6>& frustum, const glm::vec3 box_verts[8]) {