Skip to content

Commit

Permalink
Frustsum Culling (#34)
Browse files Browse the repository at this point in the history
* apply transformation to bouding sphere

* working on frustsum, add right and real up to camera, add python script to calculate frustsum

* finish frustsum culling
  • Loading branch information
JulioJPinto authored May 25, 2024
1 parent 1a0918f commit 6bad640
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 111 deletions.
14 changes: 7 additions & 7 deletions engine/include/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
#define CAMERA_HPP

#include <string>

#include <glm/glm.hpp>
#include "Camera.hpp"
#include "utils.hpp"

class Camera {
public:
Point position;
Point lookAt;
Point up;
glm::vec3 position;
glm::vec3 lookAt;
glm::vec3 up;
glm::vec3 right;
glm::vec3 real_up;
int fov;
float near;
float far;

Camera();
Camera(Point position, Point lookAt, Point up, int fov, float near,
Camera(glm::vec3 position, glm::vec3 lookAt, glm::vec3 up, int fov, float near,
float far);

Camera(const Camera& other);

void changeVectors(Point position, Point lookAt, Point up);

// std::string toString();
};

Expand Down
4 changes: 2 additions & 2 deletions engine/include/curves.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TimeRotations {
TimeRotations();
TimeRotations(float time, float x, float y, float z);

void applyTimeRotation(float elapsed_time);
glm::mat4 applyTimeRotation(float elapsed_time);
};

class TimeTranslations {
Expand All @@ -35,7 +35,7 @@ class TimeTranslations {
TimeTranslations();
TimeTranslations(float time, bool align, std::vector<Point> curve);

void applyTimeTranslations(float elapsed_time);
glm::mat4 applyTimeTranslations(float elapsed_time);

void renderCatmullRomCurve();

Expand Down
38 changes: 31 additions & 7 deletions engine/include/frustsum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@
#define FRUSTSUM_CPP

#include <math.h>

#include <iostream>
#include <glm/glm.hpp>

#include "Window.hpp"
#include "Camera.hpp"


struct Plane {

float distance = 0.f;
glm::vec3 point = { 0.f, 0.f, 0.f };
glm::vec3 normal = { 0.f, 1.f, 0.f };
glm::vec4 abcd = { 0.f, 0.f, 0.f, 0.f };

Plane() = default;
Plane(const Plane& other) = default;
Plane(const glm::vec3& normal, float distance) : normal(normal), distance(distance) {}
Plane(const glm::vec3& normal, glm::vec3 point);

// rethink distance to point
float distanceToPoint(const glm::vec3& point) const {
return glm::dot(normal, point) + distance;
return abcd.x * point.x + abcd.y * point.y + abcd.z * point.z + abcd.w;
}


void printPlane() {
std::cout << "Point: " << point.x << " " << point.y << " " << point.z << std::endl;
std::cout << "Normal: " << normal.x << " " << normal.y << " " << normal.z << std::endl;
}


Expand All @@ -36,7 +44,22 @@ struct Frustsum {
Frustsum(const Frustsum& other) = default;
Frustsum(const Plane& nearFace, const Plane& farFace, const Plane& rightFace, const Plane& leftFace, const Plane& topFace, const Plane& bottomFace)
: nearFace(nearFace), farFace(farFace), rightFace(rightFace), leftFace(leftFace), topFace(topFace), bottomFace(bottomFace) {}
Frustsum(const Camera& cam);
Frustsum(const Camera& cam, const Window& window);

void printFrustsum() {
std::cout << "Near" << std::endl;
nearFace.printPlane();
std::cout << "Far" << std::endl;
farFace.printPlane();
std::cout << "right" << std::endl;
rightFace.printPlane();
std::cout << "left" << std::endl;
leftFace.printPlane();
std::cout << "top" << std::endl;
topFace.printPlane();
std::cout << "bottom" << std::endl;
bottomFace.printPlane();
}

};

Expand All @@ -49,8 +72,9 @@ struct BoundingSphere {
BoundingSphere() = default;
BoundingSphere(const BoundingSphere& other) = default;
BoundingSphere(const glm::vec3& center, float radius) : center(center), radius(radius) {}
BoundingSphere(std::vector<Vertex> points);

bool isInsideFrustsum(const Frustsum& frustsum) const;
bool isInsideFrustsum(const Frustsum& frustsum, glm::mat4 transformations) const;

};

Expand Down
3 changes: 1 addition & 2 deletions engine/include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ class Model {
Model(std::string filename, std::vector<Vertex> points);

void initModel();
void drawModel(const Frustsum& f);
void drawModel();
void setupModel();
bool loadTexture();
bool isInsideFrustsum(const Frustsum& frustsum) const;

std::vector<Vertex> getPoints();

Expand Down
18 changes: 9 additions & 9 deletions engine/src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
#include <string>

Camera::Camera() {
this->position = Point();
this->lookAt = Point();
this->up = Point();
this->position = glm::vec3();
this->lookAt = glm::vec3();
this->up = glm::vec3();
this->fov = 0;
this->near = 0;
this->far = 0;
}

Camera::Camera(Point position, Point lookAt, Point up, int fov, float near,
Camera::Camera(glm::vec3 position, glm::vec3 lookAt, glm::vec3 up, int fov, float near,
float far) {
this->position = position;
this->lookAt = lookAt;
this->up = up;

glm::vec3 direction = glm::normalize(position - lookAt);
this->right = glm::normalize(glm::cross(up, direction));
this->real_up = glm::normalize(glm::cross(direction, right));

this->fov = fov;
this->near = near;
this->far = far;
}

void Camera::changeVectors(Point position, Point lookAt, Point up) {
this->position = position;
this->lookAt = lookAt;
this->up = up;
}

Camera::Camera(const Camera& other) {
// Copy member variables from 'other' to the current object
Expand Down
23 changes: 15 additions & 8 deletions engine/src/curves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ TimeRotations::TimeRotations(float time, float x, float y, float z) {
this->z = z;
}

void TimeRotations::applyTimeRotation(float elapsed_time) {
glm::mat4 TimeRotations::applyTimeRotation(float elapsed_time) {
if (this->time == 0) {
return;
return glm::mat4(1.0f);
}
float angle = 360 * (elapsed_time / this->time);
glRotatef(angle, this->x, this->y, this->z);
return glm::rotate(glm::mat4(1.0f), glm::radians(angle), glm::vec3(this->x, this->y, this->z));
}

TimeTranslations::TimeTranslations() {
Expand Down Expand Up @@ -129,12 +129,12 @@ std::array<float, 16> TimeTranslations::rotationMatrix(Point x, Point y, Point z
}


void TimeTranslations::applyTimeTranslations(float elapsed_time) {
glm::mat4 TimeTranslations::applyTimeTranslations(float elapsed_time) {
if (this->time == 0) {
return;
return glm::mat4(1.0f);
}

// this->renderCatmullRomCurve();
this->renderCatmullRomCurve();

float time = elapsed_time / this->time;

Expand All @@ -143,14 +143,21 @@ void TimeTranslations::applyTimeTranslations(float elapsed_time) {
Point pos = position_dir.first;
Point dir = position_dir.second;

glTranslatef(pos.x, pos.y, pos.z);

glm::mat4 matrix = glm::mat4(1.0f);
matrix = glm::translate(matrix, glm::vec3(pos.x, pos.y, pos.z));

if (this->align) {
Point x = dir.normalize();
Point z = Point(x).cross(this->y_axis).normalize();
Point y = Point(z).cross(x).normalize();
glMultMatrixf(rotationMatrix(x, y, z).data());
std::array<float, 16> matrixR = this->rotationMatrix(x, y, z);
glm::mat4 rotation_matrix = glm::make_mat4(matrixR.data());

matrix *= rotation_matrix;
}

return matrix;
}

void TimeTranslations::renderCatmullRomCurve() {
Expand Down
Loading

0 comments on commit 6bad640

Please sign in to comment.