This commit is contained in:
2024-01-14 11:04:54 -05:00
parent 2d8074e16d
commit 402874e23e
13 changed files with 70 additions and 68 deletions

View File

@@ -2,5 +2,5 @@
#include <types/camera.h>
namespace Occlusion {
void frustumCull(Camera* camera);
bool frustumCull(Camera* camera, Entity* entity);
}

View File

@@ -1,7 +0,0 @@
#pragma once
#include <types/vertex.h>
#include <types/entity.h>
namespace BoundingBox {
VertexArray calculateAABB(VertexArray* vArray);
}

View File

@@ -0,0 +1,7 @@
#pragma once
#include <types/vertex.h>
#include <types/entity.h>
namespace Collision {
VertexArray calculateBoundingBox(VertexArray* vArray);
}

View File

@@ -22,7 +22,7 @@ public:
//Angle GetRotation() const { return glm::eulerAngles(); }
//void SetRotation(Angle&const rhs);
bool draw = true;
bool occluded = false;
bool occluded();
bool collidable = true;
void destruct() {

View File

@@ -2,7 +2,7 @@
#include <iostream>
#include <cmath>
#include <types/collision/axisAlignedBoundingBox.h>
#include <types/collision/collision.h>
#include "moby.h"
#include "entityList.h"
#include "../engine/engine.h"
@@ -43,15 +43,19 @@ public:
this->angle.y = 64*engine->frameDelta;
//this->angle.x = 1;
geometry.rotate(angle);
boundingBox = BoundingBox::calculateAABB(&geometry);
boundingBox = Collision::calculateBoundingBox(&geometry);
}
void render() override {
glColor3f(0.75,0.75,0.75);
glPushMatrix();
glTranslatef(position.x ,position.y,position.z);
geometry.draw();
glPopMatrix();
glPushMatrix();
glColor3f(1,0,0);
boundingBox.drawWireframe();
glTranslatef(position.x ,position.y,position.z);
boundingBox.draw();
glPopMatrix();
}
};

View File

@@ -19,9 +19,5 @@ public:
void scale(GLfloat multiplier);
void rotate(LinearAlgebra::Vector3 angle);
void load(const std::string& filename);
//Wrong
void draw();
void drawWireframe();
};

View File

@@ -6,15 +6,13 @@
#include <J3ML/LinearAlgebra/Vector3.h>
#include "J3ML/LinearAlgebra/Vector3.h"
void Occlusion::frustumCull(Camera* camera) {
bool Occlusion::frustumCull(Camera* camera, Entity* entity) {
Geometry::Frustum frustum = camera->getFrustum();
//TODO: Make the bounding box return this way.
for (auto e : entityList) {
//TODO: Transform the BoundingBox to world space.
VertexArray AABB;
// Transform the bounding box vertices to world space
for (auto& vertex : e->boundingBox.vertices) {
for (auto& vertex : entity->boundingBox.vertices) {
Vertex out{};
out.x = vertex.x;
out.y = vertex.y;
@@ -22,23 +20,21 @@ void Occlusion::frustumCull(Camera* camera) {
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 };
//Returns "false" if the entity should not be occluded.
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
}
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) {
return false;
}
e->occluded = !visible;
}
return true;
}

View File

@@ -14,7 +14,6 @@ void Render::pre_render() {
engine->loadConfig();
auto *camera = new Camera();
storeEntity(camera);
// TODO:
getCamera()->position = {0.0f, -2.0f, -5.0f};
getCamera()->angle.y = 0.0f;
getCamera()->sMove.load("assets/scriptedMove/default.smov");
@@ -48,7 +47,7 @@ void Render::post_render() {
for (auto& e : entityList)
e->post_render();
//Resets all of the transformations for the next frame.
//Reset all the transformations for the next frame.
glPopMatrix();
glPushMatrix();
RWindow::glSwapBuffers();

View File

@@ -80,7 +80,6 @@ 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();

View File

@@ -1,9 +1,8 @@
#include <types/collision/axisAlignedBoundingBox.h>
#include "types/collision/collision.h"
#include <limits>
//This won't work correctly if you don't also rotate vArray by the rotation of the moby.
//TODO
VertexArray BoundingBox::calculateAABB(VertexArray* vArray) {
//This is in the local space.
VertexArray Collision::calculateBoundingBox(VertexArray* vArray) {
VertexArray output;
float minX = std::numeric_limits<float>::max();
float minY = std::numeric_limits<float>::max();
@@ -36,6 +35,7 @@ VertexArray BoundingBox::calculateAABB(VertexArray* vArray) {
output.vertices.push_back(Vertex(maxX, minY, maxZ));
output.vertices.push_back(Vertex(minX, maxY, maxZ));
output.vertices.push_back(Vertex(maxX, maxY, maxZ));
//std::cout << "Bounding Box: " << std::endl;
//for (auto vertex : output.vertices)
//std::cout << "X: " << vertex.x << "Y: " << vertex.y << "Z: " << vertex.z << std::endl;

View File

@@ -1,13 +1,19 @@
#include <types/entity.h>
#include <engine/occlusion.h>
//inline LinearAlgebra::Vector3 Entity::GetPos() const
//{ return LinearAlgebra::Vector3(coordinates[1]); }
//void Entity::SetPos(const LinearAlgebra::Vector3& rhs)
//{ coordinates[1] = LinearAlgebra::Vector4(rhs.x, rhs.y, rhs.z, 0); }
//glm::mat4 Entity::GetMatrix() const
//LinearAlgebra::Matrix4x4 Entity::GetMatrix() const
//{ return coordinates;}
void Entity::SetMatrix(const LinearAlgebra::Matrix4x4& rhs)
{ coordinates = rhs; }
bool Entity::occluded() {
if (!Occlusion::frustumCull(getCamera(), this))
return false;
return true;
}

View File

@@ -79,7 +79,6 @@ void Moby::doSMove() {
//LinearAlgebra::Vector3 a = VectorMath::calcAngle(this->position, scriptedMove.positions[scriptedMove.index]).movementAngle();
//move(a,scriptedMove.velocity);
//TODO: The angular velocity still isn't qccurate.
angle.x -= (angle.x - sMove.angles[sMove.index].x)*(sMove.velocity)*engine->frameDelta;
angle.y -= (angle.y -sMove.angles[sMove.index].y)*(sMove.velocity)*engine->frameDelta;
angle.z -= (angle.z - sMove.angles[sMove.index].z)*engine->frameDelta;

View File

@@ -1,6 +1,5 @@
#include <engine/engine.h>
#include <types/vertex.h>
#include <J3ML/LinearAlgebra/Quaternion.h>
#include <cmath>
#include "J3ML/LinearAlgebra/EulerAngle.h"
@@ -47,30 +46,34 @@ void VertexArray::load (const std::string& filename) {
//TODO: Render queue
void VertexArray::draw() {
glBegin(GL_TRIANGLES);
for (size_t i = 0; i < indices.size(); i += 3) {
glVertex3f(vertices[indices[i]].x, vertices[indices[i]].y, vertices[indices[i]].z);
glVertex3f(vertices[indices[i + 1]].x, vertices[indices[i + 1]].y, vertices[indices[i + 1]].z);
glVertex3f(vertices[indices[i + 2]].x, vertices[indices[i + 2]].y, vertices[indices[i + 2]].z);
if (!indices.empty()) {
glBegin(GL_TRIANGLES);
for (size_t i = 0; i < indices.size(); i += 3) {
glVertex3f(vertices[indices[i]].x, vertices[indices[i]].y, vertices[indices[i]].z);
glVertex3f(vertices[indices[i + 1]].x, vertices[indices[i + 1]].y, vertices[indices[i + 1]].z);
glVertex3f(vertices[indices[i + 2]].x, vertices[indices[i + 2]].y, vertices[indices[i + 2]].z);
}
glEnd();
}
//Render wireframe if we have no faces.
if (indices.empty()) {
glBegin(GL_LINES);
for (int i = 0; i < 4; ++i) {
glVertex3f(vertices[i].x, vertices[i].y, vertices[i].z);
glVertex3f(vertices[(i + 1) % 4].x, vertices[(i + 1) % 4].y, vertices[(i + 1) % 4].z);
glVertex3f(vertices[i + 4].x, vertices[i + 4].y, vertices[i + 4].z);
glVertex3f(vertices[((i + 1) % 4) + 4].x, vertices[((i + 1) % 4) + 4].y, vertices[((i + 1) % 4) + 4].z);
glVertex3f(vertices[i].x, vertices[i].y, vertices[i].z);
glVertex3f(vertices[i + 4].x, vertices[i + 4].y, vertices[i + 4].z);
}
glEnd();
}
glEnd();
}
void VertexArray::drawWireframe() {
glBegin(GL_LINES);
for (int i = 0; i < 4; ++i) {
glVertex3f(vertices[i].x, vertices[i].y, vertices[i].z);
glVertex3f(vertices[(i + 1) % 4].x, vertices[(i + 1) % 4].y, vertices[(i + 1) % 4].z);
glVertex3f(vertices[i + 4].x, vertices[i + 4].y, vertices[i + 4].z);
glVertex3f(vertices[((i + 1) % 4) + 4].x, vertices[((i + 1) % 4) + 4].y, vertices[((i + 1) % 4) + 4].z);
glVertex3f(vertices[i].x, vertices[i].y, vertices[i].z);
glVertex3f(vertices[i + 4].x, vertices[i + 4].y, vertices[i + 4].z);
}
glEnd();
}
//TODO: Fix gimbal lock when rotating in X or Z.
void VertexArray::rotate(LinearAlgebra::Vector3 angle) {
LinearAlgebra::Vector3 angleRad = LinearAlgebra::Vector3(Math::Radians(angle.x), Math::Radians(angle.y), Math::Radians(angle.z));