Store models & textures in a better way.

This commit is contained in:
2024-01-23 16:00:39 -05:00
parent 7c79e6ecc6
commit d7e798b221
13 changed files with 127 additions and 67 deletions

View File

@@ -50,6 +50,11 @@ CPMAddPackage(
URL https://git.redacted.cc/Redacted/SOIL/archive/v1.0.zip
)
#MIT License.
CPMAddPackage(
NAME uuid_v4
URL https://github.com/RedactedSoftware/uuid_v4/archive/refs/tags/v1.zip
)
add_library(Re3D SHARED ${SOURCES})
# Why god???
set_target_properties(Re3D PROPERTIES LINKER_LANGUAGE CXX)
@@ -66,8 +71,9 @@ include_directories(
${ReHardwareID_SOURCE_DIR}/include
${J3ML_SOURCE_DIR}/include
${SOIL_SOURCE_DIR}/include
${uuid_v4_SOURCE_DIR}/
)
target_link_libraries(Re3D_Demo PUBLIC Re3D ReWindowLibrary ReHardwareID J3ML soil ${OPENGL_LIBRARIES})
target_link_libraries(Re3D_Demo PUBLIC Re3D ReWindowLibrary ReHardwareID J3ML soil uuid_v4 ${OPENGL_LIBRARIES})

View File

@@ -40,17 +40,10 @@ public:
class World : public DataModel {
private:
VertexArray playerBaseGeometry;
Texture playerBaseTexture;
VertexArray skyboxGeometry;
Texture skyboxTexture;
public:
public:
std::string name;
World() : DataModel() {}
//TODO: Store these more elegantly of course.
VertexArray* getPlayerBaseGeometry();
VertexArray* getSkyboxGeometry();
GLuint getPlayerTextureID();
GLuint getSkyboxTextureID();
std::vector<VertexArray> geometryList;
std::vector<Texture> textureList;
};

View File

@@ -6,12 +6,15 @@
#include <J3ML/LinearAlgebra/Vector3.h>
class Entity {
private:
virtual void loadTexture() {};
virtual void loadGeometry() {};
protected:
Entity* parent;
std::vector<Entity*> children;
VertexArray* geometry = nullptr;
public:
virtual GLuint getTextureID() {};
virtual VertexArray* getGeometry() {};
std::vector<Entity*> GetChildren();
virtual void SetParent(Entity* parent);
bool IsDescendantOf(Entity* ancestor);
@@ -46,6 +49,7 @@ protected:
LinearAlgebra::Matrix4x4 coordinates;
LinearAlgebra::Position position; // X Y Z
public:
std::string uuid;
uint32_t ticksAlive; //At 64tps it'd take 776 days to overflow.
VertexArray boundingBox;
@@ -72,13 +76,7 @@ public:
}
Entity():
position(0,0,0),
ticksAlive(0)
{
children = std::vector<Entity*> ();
parent = nullptr;
}
Entity();
Entity(const Entity& rhs) = default; // Boilerplate: Copy Constructor
Entity(Entity&& rhs) = default; // Boilerplate: Move Constructor

View File

@@ -6,6 +6,9 @@
#include <types/moby.h>
class Player : public Moby {
private:
void loadTexture() override;
void loadGeometry() override;
public:
bool alive;
uint8_t health;
@@ -22,7 +25,6 @@ public:
void update(float elapsed) override;
void pre_render() override;
void render() override;
Player() {
geometry = engine->world->getPlayerBaseGeometry();
}
GLuint getTextureID() override;
VertexArray* getGeometry() override;
};

View File

@@ -4,13 +4,13 @@
#include <types/moby.h>
class Skybox : public Moby {
private:
void loadTexture() override;
void loadGeometry() override;
public:
GLuint getTextureID() override;
VertexArray* getGeometry() override;
void pre_render() override;
void render() override;
Skybox() {
geometry = engine->world->getSkyboxGeometry();
}
};

View File

@@ -2,11 +2,14 @@
#include <GL/gl.h>
#include <GL/glu.h>
#include <vector>
#include <string>
class Texture {
private:
public:
std::string name;
std::vector<std::string> usedBy;
void load(const char* file);
GLuint id;
};

View File

@@ -16,6 +16,7 @@ struct Vertex {
class VertexArray {
public:
std::string name;
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
void scale(GLfloat multiplier);

View File

@@ -1,8 +1,6 @@
#include <engine/engine.h>
#include <engine/world.h>
#include <types/moby.h>
int DataModel::getEntityCount() const {
return this->children.size();
}
@@ -15,31 +13,6 @@ int DataModel::getMobyCount() const {
return count;
}
VertexArray* World::getPlayerBaseGeometry() {
if (playerBaseGeometry.vertices.empty())
playerBaseGeometry.load("assets/models/cube.obj");
return &playerBaseGeometry;
}
GLuint World::getPlayerTextureID() {
if (playerBaseTexture.id == 0)
playerBaseTexture.load("assets/textures/missing.png");
return playerBaseTexture.id;
}
VertexArray* World::getSkyboxGeometry() {
if (skyboxGeometry.vertices.empty())
skyboxGeometry.load("assets/models/sphere.obj");
return &skyboxGeometry;
}
GLuint World::getSkyboxTextureID() {
if (skyboxTexture.id == 0)
skyboxTexture.load("assets/textures/skybox.png");
return skyboxTexture.id;
}
void Entity::SetParent(Entity *parent) {
// hold a reference to this so it doesn't get collected as we're working with it
Entity *oldParent = this->parent;

View File

@@ -1,6 +1,8 @@
#include <types/entity.h>
#include <engine/occlusion.h>
#include <types/camera.h>
#include <uuid_v4.h>
//inline LinearAlgebra::Vector3 Entity::GetPos() const
//{ return LinearAlgebra::Vector3(coordinates[1]); }
@@ -31,3 +33,17 @@ LinearAlgebra::Vector3 Entity::GetPos() const {
void Entity::SetPos(const LinearAlgebra::Vector3 &rhs) {
position = rhs;
}
Entity::Entity() {
position = {0,0,0};
ticksAlive = 0;
children = std::vector<Entity*> ();
parent = nullptr;
//TODO
UUIDv4::UUIDGenerator<std::mt19937_64> uuidGenerator;
UUIDv4::UUID id = uuidGenerator.getUUID();
uuid = id.bytes();
}

View File

@@ -1,7 +1,7 @@
#include <types/player.h>
#include <types/collision/collision.h>
void Player::pre_render() {
boundingBox = Collision::calculateBoundingBox(geometry, angle);
boundingBox = Collision::calculateBoundingBox(getGeometry(), angle);
hVelocity = 2;
this->angle.y += 96*engine->frameDelta;
velAngle = angle;
@@ -17,10 +17,9 @@ void Player::render() {
glRotatef(angle.x,1.0f, 0.0f, 0.0f);
glRotatef(angle.y,0.0f, 1.0f, 0.0f);
glRotatef(angle.z,0.0f, 0.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, engine->world->getPlayerTextureID());
geometry->draw();
glBindTexture(GL_TEXTURE_2D, getTextureID());
getGeometry()->draw();
glBindTexture(GL_TEXTURE_2D, 0);
glPopMatrix();
glPushMatrix();
glColor3f(1,0,0);
@@ -42,3 +41,37 @@ LinearAlgebra::Vector3 Player::cameraPoint(float distance) {
void Player::update(float elapsed) {
}
GLuint Player::getTextureID() {
if (engine->world->textureList.empty())
loadTexture();
for (auto& texture : engine->world->textureList)
if (texture.name == "player")
return texture.id;
loadTexture();
return getTextureID();
}
void Player::loadTexture() {
Texture texture;
texture.name = "player";
texture.load("assets/textures/missing.png");
engine->world->textureList.push_back(texture);
}
VertexArray *Player::getGeometry() {
if (engine->world->geometryList.empty())
loadTexture();
for (auto& vArray : engine->world->geometryList)
if (vArray.name == "player")
return &vArray;
loadGeometry();
return getGeometry();
}
void Player::loadGeometry() {
VertexArray geometry;
geometry.name = "player";
geometry.load("assets/models/cube.obj");
engine->world->geometryList.push_back(geometry);
}

View File

@@ -4,7 +4,7 @@
void Skybox::pre_render() {
if (engine->frameCount == 1) {
geometry->scale(75);
getGeometry()->scale(75);
}
Camera* camera;
for (auto& e : engine->world->GetChildren())
@@ -17,8 +17,42 @@ void Skybox::render() {
glColor3f(0.75,0.75,0.75);
glPushMatrix();
glTranslatef(position.x ,position.y, position.z);
glBindTexture(GL_TEXTURE_2D, engine->world->getSkyboxTextureID());
geometry->draw();
glBindTexture(GL_TEXTURE_2D, getTextureID());
getGeometry()->draw();
glBindTexture(GL_TEXTURE_2D,0);
glPopMatrix();
}
GLuint Skybox::getTextureID() {
if (engine->world->textureList.empty())
loadTexture();
for (auto& texture : engine->world->textureList)
if (texture.name == "skybox")
return texture.id;
loadTexture();
return getTextureID();
}
void Skybox::loadTexture() {
Texture texture;
texture.name = "skybox";
texture.load("assets/textures/skybox.png");
engine->world->textureList.push_back(texture);
}
VertexArray* Skybox::getGeometry() {
if (engine->world->geometryList.empty())
loadTexture();
for (auto& vArray : engine->world->geometryList)
if (vArray.name == "skybox")
return &vArray;
loadGeometry();
return getGeometry();
}
void Skybox::loadGeometry() {
VertexArray geometry;
geometry.name = "skybox";
geometry.load("assets/models/sphere.obj");
engine->world->geometryList.push_back(geometry);
}

View File

@@ -6,7 +6,7 @@ void Texture::load(const char* file) {
if (id == 0)
{
printf("SOIL loading error: '%s'\n", SOIL_last_result());
printf("Couldn't Load Texture: '%s'\n", SOIL_last_result());
exit(0);
}
}

View File

@@ -113,6 +113,7 @@ void VertexArray::drawWireframe() {
glEnd();
}
//TODO: Convert EulerAngle to Quaternion before rotation.
void VertexArray::rotate(const LinearAlgebra::Vector3& angle) {
if (vertices.empty())
return;
@@ -122,11 +123,11 @@ void VertexArray::rotate(const LinearAlgebra::Vector3& angle) {
LinearAlgebra::Vector3 vert;
float length = sqrt(vertex.x * vertex.x + vertex.y * vertex.y + vertex.z * vertex.z);
vert.z = vertex.x * std::cos(angleRad.y) * std::cos(angleRad.z) +
vert.x = vertex.x * std::cos(angleRad.y) * std::cos(angleRad.z) +
vertex.y * (std::cos(angleRad.x) * std::sin(angleRad.z) - std::sin(angleRad.x) * std::sin(angleRad.y) * std::cos(angleRad.z)) +
vertex.z * (std::sin(angleRad.x) * std::sin(angleRad.z) + std::cos(angleRad.x) * std::sin(angleRad.y) * std::cos(angleRad.z));
vert.x = -vertex.x * std::sin(angleRad.y) +
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);
@@ -153,11 +154,11 @@ VertexArray VertexArray::rotate(VertexArray* vArray, const LinearAlgebra::Vector
LinearAlgebra::Vector3 vert;
float length = sqrt(vertex.x * vertex.x + vertex.y * vertex.y + vertex.z * vertex.z);
vert.z = vertex.x * std::cos(angleRad.y) * std::cos(angleRad.z) +
vert.x = vertex.x * std::cos(angleRad.y) * std::cos(angleRad.z) +
vertex.y * (std::cos(angleRad.x) * std::sin(angleRad.z) - std::sin(angleRad.x) * std::sin(angleRad.y) * std::cos(angleRad.z)) +
vertex.z * (std::sin(angleRad.x) * std::sin(angleRad.z) + std::cos(angleRad.x) * std::sin(angleRad.y) * std::cos(angleRad.z));
vert.x = -vertex.x * std::sin(angleRad.y) +
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);