Geometry & Textures no longer have names.

This commit is contained in:
2024-05-31 03:15:33 -04:00
parent b1c51a5afa
commit 6e88d585b8
18 changed files with 69 additions and 135 deletions

View File

@@ -9,9 +9,6 @@ if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
endif()
set(CMAKE_CXX_STANDARD 20)
if (WIN32)
set(CMAKE_CXX_FLAGS "-municode")
endif(WIN32)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
@@ -53,7 +50,7 @@ CPMAddPackage(
CPMAddPackage(
NAME JGL
URL https://git.redacted.cc/josh/JGL/archive/Release-12.zip
URL https://git.redacted.cc/josh/JGL/archive/Prerelease-13.zip
)
CPMAddPackage(
@@ -66,10 +63,12 @@ CPMAddPackage(
NAME uuid_v4
URL https://github.com/RedactedSoftware/uuid_v4/archive/refs/tags/v1.zip
)
CPMAddPackage(
NAME glad
URL https://git.redacted.cc/Redacted/glad/archive/v2.1.zip
URL https://git.redacted.cc/Redacted/glad/archive/v2.1ext_mt.zip
)
CPMAddPackage(
NAME archive
URL https://git.redacted.cc/Redacted/archive/archive/v1.0.zip

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
assets/fonts/modeseven.ttf Executable file

Binary file not shown.

View File

@@ -0,0 +1,7 @@
#pragma once
#include <typeinfo>
template <typename Source, typename Target>
bool instanceOf(Source* source, Target* target) {
return typeid(*source) == typeid(*target);
}

View File

@@ -10,13 +10,12 @@ class Entity;
class Texture {
public:
std::string name;
std::vector<Entity*> usedBy;
GLuint id = 0;
void load(const std::string& file, bool storeOnTextureList);
void load(Entity* entity, const std::string& file, bool storeOnTextureList);
virtual void erase();
void erase(Texture* texture) const;
Texture(std::string& name, const char* filePath, bool storeOnTextureList);
Texture(Entity* entity, const char *filePath, bool storeOnTextureList);
Texture() = default;
};
@@ -24,5 +23,5 @@ class MultiTexture : public Texture {
public:
void erase() override;
std::vector<Texture> multi;
MultiTexture(const std::string& name, const char* pathContainingTextures, bool storeOnTextureList);
MultiTexture(Entity* entity, const char* pathContainingTextures, bool storeOnTextureList);
};

View File

@@ -23,7 +23,6 @@ class VertexArray {
private:
OBB cachedOBB;
public:
std::string name;
std::vector<Vertex> vertices;
std::vector<unsigned int> indices;
std::vector<TextureCoordinate> texCoords;
@@ -31,17 +30,14 @@ public:
GLuint vbo = 0; //Vertices
GLuint ebo = 0; //Indices
GLuint tbo = 0; //Texture Coordinates
void rotate(const Vector3& angle); //SLOW.
/// Loads data from an OBJ mesh file to populate the VertexArray
/// @see LearnOpenGL::Mesh LearnOpenGL::Model J3ML::Geometry::TriangleMesh
void load(const std::string& filename);
[[nodiscard]] OBB getCachedOBB(const Matrix3x3& rotation) const;
[[nodiscard]] OBB getCachedOBB(const Vector3& rotation) const;
[[nodiscard]] OBB getCachedOBB(const EulerAngle& rotation) const;
static void erase(const VertexArray& vArray);
virtual void draw();
void drawWireframe();
explicit VertexArray(const std::string& filename);
explicit VertexArray(Entity* entity, const std::string& filename, bool storeOnGeometryList);
VertexArray() = default;
void erase(VertexArray* vArray) const;
void erase() const;
};

View File

@@ -26,5 +26,5 @@ Cube::Cube() {
}
void Cube::loadTexture() {
MultiTexture texture(name, texturePath.c_str(), true);
MultiTexture texture(this, texturePath.c_str(), true);
}

View File

@@ -78,10 +78,8 @@ void Engine::initGL() const {
glViewport(0,0,window_size.x,window_size.y);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_TEXTURE_2D);
glDepthMask(GL_TRUE);
glEnable(GL_CULL_FACE);
glEnable(GL_VERTEX_ARRAY);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
@@ -119,7 +117,7 @@ void Engine::initGL() const {
//TODO JGL rendering pass.
glDisable(GL_LIGHTING);
JGL::J3D::DrawString3D(JGL::Colors::Blue, "Example Text", {0.5f, 0, 0.5f}, 0.025f);
JGL::J3D::DrawString3D(JGL::Colors::White, std::to_string((int) framerate()), {0.5f, 0, 0.5f}, 0.0125f);
glEnable(GL_LIGHTING);
}
@@ -140,30 +138,28 @@ void Engine::initGL() const {
}
[[noreturn]] void Engine::renderLoop()
{
while (true) {
auto start = std::chrono::high_resolution_clock::now();
renderPass();
auto stop = std::chrono::high_resolution_clock::now();
{
while (true) {
auto start = std::chrono::high_resolution_clock::now();
renderPass();
auto stop = std::chrono::high_resolution_clock::now();
auto dT = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
float dT = std::chrono::duration<float, std::micro>(stop - start).count();
bool inFocus = engine->window->getFlag(RWindowFlags::IN_FOCUS);
if (dT < 1000.0f && inFocus) {
auto remaining = std::chrono::microseconds(static_cast<int>(1000.0f - dT));
std::this_thread::sleep_for(remaining);
dT += std::chrono::duration<float, std::micro>(remaining).count();
}
// Decrease framerate when focus is lost.
if (!inFocus) {
auto remaining = std::chrono::microseconds(static_cast<int>(41666.67f - dT));
std::this_thread::sleep_for(remaining);
dT += std::chrono::duration<float, std::micro>(remaining).count();
}
engine->frameDelta = dT / 1000000.0f;
if (dT < std::chrono::microseconds(1000)) {
auto remaining = std::chrono::microseconds(1000) - dT;
std::this_thread::sleep_for(remaining);
dT += remaining;
}
// Decrease framerate when focus is lost.
if (!engine->window->getFlag(RWindowFlags::IN_FOCUS)) {
auto remaining = std::chrono::microseconds((int) 41666.67f) - dT;
std::this_thread::sleep_for(remaining);
dT += remaining;
}
engine->frameDelta = dT.count() / 1000000.0f;
}
}
float Engine::framerate() const {
return 1.f / frameDelta;
@@ -174,7 +170,6 @@ void Engine::loadConfig() {
if (!file.is_open()) {
std::cerr << "Couldn't load engine config." << std::endl;
quit();
return;
}
std::string line;
while (std::getline(file, line)) {

View File

@@ -3,6 +3,7 @@
#include <types/entity/camera.h>
#include <uuid_v4.h>
#include <engine/engine.h>
#include <engine/utils/instanceOf.h>
//inline LinearAlgebra::Vector3 Entity::GetPos() const
//{ return LinearAlgebra::Vector3(coordinates[1]); }
@@ -56,27 +57,23 @@ GLfloat Entity::getScale() const {
}
void Entity::loadTexture() {
Texture texture(name, texturePath.c_str(), true);
Texture texture(this, texturePath.c_str(), true);
}
void Entity::loadGeometry() {
VertexArray geometry;
geometry.name = name;
geometry.load(modelPath);
engine->world->geometryList.push_back(geometry);
VertexArray vArray(this, modelPath, true);
}
VertexArray* Entity::getGeometry() {
if (engine->world->geometryList.empty())
loadGeometry();
for (auto& vArray : engine->world->geometryList)
if (vArray.name == name) {
if (std::find(vArray.usedBy.begin(), vArray.usedBy.end(), this) == vArray.usedBy.end())
vArray.usedBy.push_back(this);
for (auto& entity : vArray.usedBy)
if (instanceOf(this, entity)) {
if (std::find(vArray.usedBy.begin(), vArray.usedBy.end(), this) == vArray.usedBy.end()) //If this is the first time the entity is using it.
vArray.usedBy.push_back(this);
return &vArray;
}
loadGeometry();
return getGeometry();
}
@@ -183,12 +180,15 @@ bool Entity::isCollidingWith(Entity *entity) {
}
Texture* Entity::getTexture() {
if (engine->world->textureList.empty())
loadTexture();
for (auto& texture : engine->world->textureList)
if (texture->name == name) {
if (std::find(texture->usedBy.begin(), texture->usedBy.end(), this) == texture->usedBy.end())
texture->usedBy.push_back(this);
return texture;
}
for (auto& entity : texture->usedBy)
if (instanceOf(this, entity)) {
if (std::find(texture->usedBy.begin(), texture->usedBy.end(), this) == texture->usedBy.end()) //If this is the first time the entity is using it.
texture->usedBy.push_back(this);
return texture;
}
loadTexture();
return getTexture();
}
@@ -209,7 +209,7 @@ void Entity::erase() {
vA->usedBy.erase(vA->usedBy.begin() + 1);
if (vA->usedBy.empty())
vA->erase(vA);
vA->erase();
for (int i = 0; i < engine->world->GetChildren().size(); i++)
if (engine->world->GetChildren()[i] == this)

View File

@@ -2,7 +2,7 @@
#include <SOIL/SOIL.h>
#include <engine/engine.h>
void Texture::load(const std::string& file, bool storeOnTextureList) {
void Texture::load(Entity* entity, const std::string& file, bool storeOnTextureList) {
id = SOIL_load_OGL_texture(file.c_str(), SOIL_LOAD_RGBA, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
//If we can't load the missing texture
@@ -11,8 +11,9 @@ void Texture::load(const std::string& file, bool storeOnTextureList) {
if (id == 0)
engine->setError(ENGINE_ERROR_CODE::TEXTURE_NOT_FOUND, false),
load("assets/textures/missing.png", false);
load(entity, "assets/textures/missing.png", false);
usedBy.push_back(entity);
if (storeOnTextureList)
engine->world->textureList.push_back(new Texture(*this));
}
@@ -21,9 +22,8 @@ void Texture::erase() {
Texture::erase(this);
}
Texture::Texture(std::string& name, const char *filePath, bool storeOnTextureList) {
this->name = name;
load(filePath, storeOnTextureList);
Texture::Texture(Entity* entity, const char *filePath, bool storeOnTextureList) {
load(entity, filePath, storeOnTextureList);
}
void Texture::erase(Texture* texture) const {
@@ -44,17 +44,16 @@ void Texture::erase(Texture* texture) const {
}
MultiTexture::MultiTexture(const std::string& name, const char* pathContainingTextures, bool storeOnTextureList) {
MultiTexture::MultiTexture(Entity* entity, const char* pathContainingTextures, bool storeOnTextureList) {
for (const auto& entry : std::filesystem::directory_iterator(pathContainingTextures))
if (entry.is_regular_file() && entry.path().extension() == ".png") {
Texture texture;
texture.load(entry.path().c_str(), false);
texture.name = name;
texture.load(entity, entry.path().c_str(), false);
//The base texture *must* go in the first slot.
if (entry.path().filename() == "default.png")
this->id = texture.id,
this->name = texture.name;
this->usedBy = texture.usedBy;
else multi.push_back(texture);
}

View File

@@ -90,82 +90,21 @@ void VertexArray::drawWireframe() {
glEnd();
}
std::tuple<float, float> ComputeVertexRoll(Vertex vertex, float roll) {
float newX = vertex.x * std::cos(roll) - vertex.y * std::sin(roll);
float newY = vertex.x * std::sin(roll) + vertex.y * std::cos(roll);
return {newX, newY};
}
std::tuple<float, float> ComputeVertexYaw(Vertex vertex, float yaw) {
float newX = vertex.x * std::cos(yaw) + vertex.z * std::sin(yaw);
float newZ = -vertex.x * std::sin(yaw) + vertex.z * std::cos(yaw);
return {newX, newZ};
}
std::tuple<float, float> ComputeVertexPitch(Vertex vertex, float pitch) {
float newY = vertex.y * std::cos(pitch) - vertex.z * std::sin(pitch);
float newZ = vertex.y * std::sin(pitch) + vertex.z * std::cos(pitch);
return {newY, newZ};
}
void VertexArray::rotate(const Vector3& angle) {
float angleX = Math::Radians(angle.x);
float angleY = Math::Radians(angle.y);
float angleZ = Math::Radians(angle.z);
// Roll
for (Vertex &vertex: vertices) {
/*float newX = vertex.x * std::cos(angleZ) - vertex.y * std::sin(angleZ);
float newY = vertex.x * std::sin(angleZ) + vertex.y * std::cos(angleZ);*/
auto result = ComputeVertexRoll(vertex, angleZ);
vertex.x = std::get<0>(result);
vertex.y = std::get<1>(result);
}
// Yaw
for (Vertex &vertex: vertices) {
/*float newX = vertex.x * std::cos(angleY) + vertex.z * std::sin(angleY);
float newZ = -vertex.x * std::sin(angleY) + vertex.z * std::cos(angleY);*/
auto result = ComputeVertexYaw(vertex, angleY);
vertex.x = std::get<0>(result);
vertex.z = std::get<1>(result);
}
// Pitch
for (Vertex &vertex: vertices) {
/*float newY = vertex.y * std::cos(angleX) - vertex.z * std::sin(angleX);
float newZ = vertex.y * std::sin(angleX) + vertex.z * std::cos(angleX);*/
auto result = ComputeVertexPitch(vertex, angleX);
vertex.y = std::get<0>(result);
vertex.z = std::get<1>(result);
}
}
void VertexArray::erase(const VertexArray& vArray) {
if (engine->useVBO)
glDeleteBuffers(1, &vArray.vbo),
glDeleteBuffers(1, &vArray.tbo),
glDeleteBuffers(1, &vArray.ebo);
for (int i = 0; i < engine->world->geometryList.size(); i++)
if (engine->world->geometryList[i].name == vArray.name)
engine->world->geometryList.erase(engine->world->geometryList.begin() + i);
}
void VertexArray::erase(VertexArray* erasee) const {
void VertexArray::erase() const {
if (!usedBy.empty())
engine->setError(ENGINE_ERROR_CODE::VERTEX_ARRAY_ERASED_WHILE_IN_USE, false);
for (int i = 0; i < engine->world->geometryList.size(); i++)
if (&engine->world->geometryList[i] == erasee)
if (&engine->world->geometryList[i] == this)
engine->world->geometryList.erase(engine->world->geometryList.begin() + i);
delete erasee;
delete this;
}
VertexArray::VertexArray(const std::string &filename) {
VertexArray::VertexArray(Entity* entity, const std::string& filename, bool storeOnGeometryList) {
load(filename);
usedBy.push_back(entity);
if (storeOnGeometryList)
engine->world->geometryList.push_back(*this);
}
OBB VertexArray::getCachedOBB(const Matrix3x3& rotationMatrix) const {