trying to make occlusion work O.O

This commit is contained in:
2024-01-12 10:28:28 -05:00
parent fa0c5e5de9
commit 02409f62f3
10 changed files with 77 additions and 17 deletions

View File

@@ -42,7 +42,7 @@ CPMAddPackage(
CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-3.zip
URL https://git.redacted.cc/josh/j3ml/archive/test.zip
)
# BSD 2-clause license.

View File

@@ -1,2 +1,6 @@
#pragma once
#include <types/camera.h>
namespace Occlusion {
void frustumCull(Camera* camera);
}

View File

@@ -1,9 +1,10 @@
#pragma once
#include "moby.h"
#include "player.h"
#include "../engine/engine.h"
#include "entityList.h"
#include <types/moby.h>
#include <types/player.h>
#include <engine/engine.h>
#include <types/entityList.h>
#include <GL/glu.h>
#include <J3ML/Geometry.h>
enum class CameraMode: uint8_t {
THIRD_PERSON = 0,
@@ -25,6 +26,7 @@ public:
bool takingScreenshot = false;
LinearAlgebra::Matrix4x4 GetViewMatrix();
Geometry::Frustum getFrustum();
void MoveForward(float dist);
void StrafeLeft(float dist);
void StrafeRight(float dist);

View File

@@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
#include <types/vector.h>
#include <types/vertex.h>
#include <J3ML/LinearAlgebra/Matrix4x4.h>
#include <J3ML/LinearAlgebra/Vector3.h>
@@ -12,6 +13,8 @@ public:
LinearAlgebra::Position position;//X Y Z
uint32_t ticksAlive; //At 64tps it'd take 776 days to overflow.
VertexArray geometry;
VertexArray boundingBox;
LinearAlgebra::Vector3 GetPos() const;
void SetPos(const LinearAlgebra::Vector3& rhs);
LinearAlgebra::Matrix4x4 GetMatrix() const;
@@ -19,6 +22,7 @@ public:
//Angle GetRotation() const { return glm::eulerAngles(); }
//void SetRotation(Angle&const rhs);
bool draw = true;
bool occluded = false;
bool collidable = true;
void destruct() {

View File

@@ -2,8 +2,6 @@
#include <types/entity.h>
#include <types/animation/scriptedMove.h>
#include <J3ML/LinearAlgebra/Vector3.h>
#include <types/vertex.h>
//Movable Object.
class Moby : public Entity {
@@ -15,8 +13,6 @@ public:
LinearAlgebra::Vector3 angle = {0,0,0}; //Pitch Yaw Roll, The orientation of the entity in the world,
LinearAlgebra::Vector3 velAngle = {0,0,0}; //The angle of an entities velocity.
LinearAlgebra::Vector3 Velocity;
VertexArray geometry;
VertexArray boundingBox;
LinearAlgebra::Vector3 upVector = {0.0f,1.0f,0.0f};
ScriptedMove sMove;
void move(LinearAlgebra::Vector3 a, float speed);

View File

@@ -39,8 +39,9 @@ public:
position = {0,-2,0};
}
//Rotate
this->angle.y = -255*engine->frameDelta;
//this->angle.z = 255*engine->frameDelta;
std::cout << "Occluded: " << occluded << std::endl;
this->angle.y = 255*engine->frameDelta;
//this->angle.x = 1;
geometry.rotate(angle);
boundingBox = BoundingBox::calculateAABB(&geometry);
}
@@ -52,6 +53,7 @@ public:
//glRotatef(angle.z,0.0f, 0.0f, 1.0f);
glTranslatef(position.x ,position.y,position.z);
geometry.draw();
boundingBox.draw();
glPopMatrix();
}
};

View File

@@ -1 +1,44 @@
#include <engine/occlusion.h>
#include <engine/engine.h>
#include <types/entity.h>
#include <types/entityList.h>
#include <J3ML/Geometry.h>
#include <J3ML/LinearAlgebra/Vector3.h>
#include "J3ML/LinearAlgebra/Vector3.h"
void Occlusion::frustumCull(Camera* camera) {
Geometry::Frustum frustum = camera->getFrustum();
//TODO: Make the bounding box return this way.
for (auto e : entityList) {
VertexArray AABB;
// Transform the bounding box vertices to world space
for (auto& vertex : e->boundingBox.vertices) {
Vertex out{};
out.x = vertex.x + e->position.x;
out.y = vertex.y + e->position.y;
out.z = vertex.z + e->position.z;
AABB.vertices.push_back(out);
}
bool visible = true; // Initialize to true
// Check visibility for each vertex
for (auto& vertex : AABB.vertices) {
LinearAlgebra::Vector3 vertVector = { vertex.x, vertex.y, vertex.z };
if (LinearAlgebra::Vector3::Dot(vertVector - frustum.NearFace.Position, frustum.NearFace.Normal) < 0.0f ||
LinearAlgebra::Vector3::Dot(vertVector - frustum.FarFace.Position, frustum.FarFace.Normal) < 0.0f ||
LinearAlgebra::Vector3::Dot(vertVector - frustum.RightFace.Position, frustum.RightFace.Normal) < 0.0f ||
LinearAlgebra::Vector3::Dot(vertVector - frustum.LeftFace.Position, frustum.LeftFace.Normal) < 0.0f ||
LinearAlgebra::Vector3::Dot(vertVector - frustum.TopFace.Position, frustum.TopFace.Normal) < 0.0f ||
LinearAlgebra::Vector3::Dot(vertVector - frustum.BottomFace.Position, frustum.BottomFace.Normal) < 0.0f) {
visible = false;
break; // Break the loop when a vertex is outside the frustum
}
}
e->occluded = !visible;
}
}

View File

@@ -1,14 +1,12 @@
#include <types/camera.h>
#include <engine/occlusion.h>
// Even tho CLion reports this as const-qualifiable
// It is not in "intent" (obviously the gametick changes gamestate)
void Camera::update()
{
if (engine->debug && engine->tickCount %64 == 0) {
std::cout << "Camera:" << std::endl;
std::cout << "X: " << position.x << " Y: " << position.y << " Z: " << position.z << std::endl;
std::cout << "Pitch: " << angle.x << " Yaw: " << angle.y << " Roll: " << angle.z << std::endl;
}
}
@@ -79,6 +77,7 @@ void Camera::thirdPerson() {
this->angle.y = VectorMath::calcAngle(position,getPlayer()->position).y;
}
void Camera::pre_render() {
Occlusion::frustumCull(this);
modeBinds();
if (cameraMode == CameraMode::FREECAM)
freeCam();
@@ -108,6 +107,11 @@ void Camera::post_render() {
this->ticksAlive++;
}
Geometry::Frustum Camera::getFrustum() {
Geometry::Camera cam = {this->position,this->fAngle(),this->rAngle(),this->upVector};
return Geometry::CreateFrustumFromCamera(cam, engine->window->getSize()[0] / engine->window->getSize()[1], engine->fov, engine->nearPlane, engine->farPlane);
}
//TODO: Make it such that we don't have to do this.
Camera* getCamera() {
for (auto& e : entityList)

View File

@@ -16,7 +16,7 @@ LinearAlgebra::Vector3 Moby::simulateMove(LinearAlgebra::Vector3 a, float speed)
return p;
}
inline LinearAlgebra::Vector3 Moby::fAngle()
LinearAlgebra::Vector3 Moby::fAngle()
{
LinearAlgebra::Vector3 a;
a.x = (cos(Math::Radians(this->angle.y)) * cos(Math::Radians(this->angle.x)));

View File

@@ -1,7 +1,8 @@
#include <engine/engine.h>
#include <types/vertex.h>
#include <J3ML/LinearAlgebra/Quaternion.h>
#include <cmath>
#include "J3ML/LinearAlgebra/EulerAngle.h"
void VertexArray::scale(GLfloat multiplier) {
for (auto & vertex : vertices) {
@@ -60,6 +61,7 @@ void VertexArray::rotate(LinearAlgebra::Vector3 angle) {
for (auto& vertex : vertices) {
LinearAlgebra::Vector3 vert;
float length = sqrt(vertex.x * vertex.x + vertex.y * vertex.y + vertex.z * vertex.z);
vert.z = -vertex.x * std::sin(angleRad.y) +
vertex.y * std::sin(angleRad.x) * std::cos(angleRad.y) +
vertex.z * std::cos(angleRad.x) * std::cos(angleRad.y);
@@ -72,7 +74,10 @@ void VertexArray::rotate(LinearAlgebra::Vector3 angle) {
vertex.y * (std::cos(angleRad.x) * std::cos(angleRad.z) + std::sin(angleRad.x) * std::sin(angleRad.y) * std::sin(angleRad.z)) +
vertex.z * (std::sin(angleRad.x) * std::cos(angleRad.z) - std::cos(angleRad.x) * std::sin(angleRad.y) * std::sin(angleRad.z));
vert.Normalize();
vert = vert.Normalize();
vert.x *= length;
vert.y *= length;
vert.z *= length;
vertex.x = vert.x;
vertex.y = vert.y;
vertex.z = vert.z;