1
0
forked from Redacted/Re3D

Multi-Textured entities.

This commit is contained in:
2024-03-31 23:13:29 -04:00
parent a5f3510a45
commit 6d1fe1b431
10 changed files with 88 additions and 35 deletions

View File

@@ -49,7 +49,7 @@ CPMAddPackage(
CPMAddPackage( CPMAddPackage(
NAME ReWindow NAME ReWindow
URL https://git.redacted.cc/Redacted/ReWindow/archive/v0.2.21.zip URL https://git.redacted.cc/Redacted/ReWindow/archive/vA0.2.22.zip
) )
CPMAddPackage( CPMAddPackage(

View File

@@ -1,4 +1,4 @@
Fullscreen: 0 Fullscreen: 0
Resolution: 1152 864 Resolution: 1152 864
Debug: 1 Debug: 0
CameraFOV: 72.5 CameraFOV: 72.5

View File

@@ -53,6 +53,7 @@ public:
uint64_t frameCount = 0; uint64_t frameCount = 0;
float frameDelta = 0; float frameDelta = 0;
GLenum glError = GL_NO_ERROR; GLenum glError = GL_NO_ERROR;
GLint maxTextureUnits;
float nearPlane = 0.01f; float nearPlane = 0.01f;
float farPlane = 100.0f; float farPlane = 100.0f;
float fov = 75; float fov = 75;

View File

@@ -2,12 +2,11 @@
#include <cstdint> #include <cstdint>
#include <types/vector.h> #include <types/vector.h>
#include <types/vertex.h> #include <types/vertex.h>
#include <types/texture.h>
#include <J3ML/LinearAlgebra/Matrix4x4.h> #include <J3ML/LinearAlgebra/Matrix4x4.h>
#include <J3ML/LinearAlgebra/Vector3.h> #include <J3ML/LinearAlgebra/Vector3.h>
#include <archive.h> #include <archive.h>
#include <engine/collision.h> #include <engine/collision.h>
//#include <JGL/JGL.h>
//#include <JGL/Colors.h>
using J3ML::LinearAlgebra::Matrix4x4; using J3ML::LinearAlgebra::Matrix4x4;
using J3ML::LinearAlgebra::Vector3; using J3ML::LinearAlgebra::Vector3;
@@ -45,9 +44,11 @@ public:
Vector3 angle = {0,0,0}; //Pitch Yaw Roll, The orientation of the entity in the world, Vector3 angle = {0,0,0}; //Pitch Yaw Roll, The orientation of the entity in the world,
bool collidable = true; // Entities are solid by default. bool collidable = true; // Entities are solid by default.
GLuint getTextureID();
virtual Texture* getTexture();
virtual VertexArray* getGeometry(); virtual VertexArray* getGeometry();
virtual Collider getCollider(); virtual Collider getCollider();
std::vector<Entity*> GetChildren(); std::vector<Entity*> GetChildren();
virtual void SetParent(Entity* parent); virtual void SetParent(Entity* parent);
bool IsDescendantOf(Entity* ancestor); bool IsDescendantOf(Entity* ancestor);

View File

@@ -6,12 +6,19 @@
#include <string> #include <string>
class Texture { class Texture {
private:
void load(const char* file);
public: public:
void load(const char* file, bool storeOnTextureList);
std::string name; std::string name;
std::vector<std::string> usedBy; std::vector<std::string> usedBy;
static void erase(GLuint id); virtual void erase(GLuint id);
GLuint id; GLuint id = 0;
Texture(std::string name, const char* filePath); Texture(std::string& name, const char* filePath, bool storeOnTextureList);
Texture() = default;
};
class MultiTexture : public Texture {
public:
void erase(GLuint id) override;
std::vector<Texture> multi;
MultiTexture(const std::string& name, const char* pathContainingTextures, bool storeOnTextureList);
}; };

View File

@@ -6,9 +6,10 @@
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
engine->window = new ReWindow::RWindow("Re3D Test Application", 1024, 768, RenderingAPI::OPENGL); engine->window = new ReWindow::RWindow("Re3D Test Application", 1152, 864, RenderingAPI::OPENGL);
engine->world = new World(); engine->world = new World();
engine->init(); engine->init();
engine->window->setVsyncEnabled(false);
auto* cube1 = new(Cube); auto* cube1 = new(Cube);
cube1->name = "cube1"; cube1->name = "cube1";
cube1->SetPos({-5, -2, 0}); cube1->SetPos({-5, -2, 0});

View File

@@ -66,7 +66,7 @@ void Engine::initGL()
{ {
//engine->window->setRenderer(RenderingAPI::OPENGL); //engine->window->setRenderer(RenderingAPI::OPENGL);
engine->window->Open(); engine->window->Open();
engine->window->setResizable(true); engine->window->setResizable(false);
engine->initGL(); engine->initGL();
engine->loadConfig(); engine->loadConfig();
@@ -74,7 +74,7 @@ void Engine::initGL()
//glDisable(GL_CULL_FACE); //glDisable(GL_CULL_FACE);
//glEnable(GL_BLEND); //glEnable(GL_BLEND);
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
auto* camera = new Camera(); auto* camera = new Camera();
camera->SetPos({0.0f, -2.0f, -5.0f}); camera->SetPos({0.0f, -2.0f, -5.0f});
camera->angle.y = 0.0f; camera->angle.y = 0.0f;

View File

@@ -76,8 +76,7 @@ Collider Entity::getCollider() {
} }
void Entity::loadTexture() { void Entity::loadTexture() {
Texture texture(name, texturePath.c_str()); Texture texture(name, texturePath.c_str(), true);
engine->world->textureList.push_back(texture);
} }
void Entity::loadGeometry() { void Entity::loadGeometry() {
@@ -87,16 +86,6 @@ void Entity::loadGeometry() {
engine->world->geometryList.push_back(geometry); engine->world->geometryList.push_back(geometry);
} }
GLuint Entity::getTextureID() {
if (engine->world->textureList.empty())
loadTexture();
for (auto& texture : engine->world->textureList)
if (texture.name == name)
return texture.id;
Entity::loadTexture();
return getTextureID();
}
VertexArray* Entity::getGeometry() { VertexArray* Entity::getGeometry() {
if (engine->world->geometryList.empty()) if (engine->world->geometryList.empty())
loadGeometry(); loadGeometry();
@@ -112,6 +101,7 @@ VertexArray* Entity::getGeometry() {
void Entity::render() { void Entity::render() {
bool cameraInside = false; bool cameraInside = false;
bool isMultiTexture = false;
//Run the example vertex shader. //Run the example vertex shader.
for (auto& shader : engine->world->shaderList) for (auto& shader : engine->world->shaderList)
@@ -144,11 +134,20 @@ void Entity::render() {
glDisable(GL_FOG); glDisable(GL_FOG);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, getTexture()->id);
//If texture is multi-texture.
if (auto* t = dynamic_cast<MultiTexture*>(this->getTexture())) {
for (int i = 0; i < t->multi.size()-1; i++) {
glActiveTexture(GL_TEXTURE + i);
glBindTexture(GL_TEXTURE_2D, t->multi[i-1].id);
}
}
glTranslatef(position.x ,position.y,position.z); glTranslatef(position.x ,position.y,position.z);
glRotatef(angle.x,1.0f, 0.0f, 0.0f); glRotatef(angle.x,1.0f, 0.0f, 0.0f);
glRotatef(angle.y,0.0f, 1.0f, 0.0f); glRotatef(angle.y,0.0f, 1.0f, 0.0f);
glRotatef(angle.z,0.0f, 0.0f, 1.0f); glRotatef(angle.z,0.0f, 0.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, getTextureID());
if (getScale() != 1.0f) if (getScale() != 1.0f)
glScalef(getScale(),getScale(),getScale()); glScalef(getScale(),getScale(),getScale());
getGeometry()->draw(); getGeometry()->draw();
@@ -168,9 +167,6 @@ void Entity::render() {
if (engine->world->getGlobalFogMode() != NULL) if (engine->world->getGlobalFogMode() != NULL)
glEnable(GL_FOG); glEnable(GL_FOG);
//JGL::J2D::FillRect2D(JGL::Colors::Yellow, {0, 0}, {1, 1});
//JGL::J2D::DrawString2D(JGL::Colors::Blue, "ENTITY", 0, 0, 1/50.f);
} }
bool Entity::isCollidingWith(Entity *entity) { bool Entity::isCollidingWith(Entity *entity) {
@@ -189,3 +185,11 @@ bool Entity::isCollidingWith(Entity *entity) {
if (thisCollider.type == ColliderType::AxisAlignedBoundingBox && otherCollider.type == ColliderType::CollisionMap) if (thisCollider.type == ColliderType::AxisAlignedBoundingBox && otherCollider.type == ColliderType::CollisionMap)
return Collision::collisionMapVSAxisAlignedBoundingBoxCollides(thisCollider, otherCollider); return Collision::collisionMapVSAxisAlignedBoundingBoxCollides(thisCollider, otherCollider);
} }
Texture* Entity::getTexture() {
for (auto& texture : engine->world->textureList)
if (texture.name == name)
return &texture;
loadTexture();
return getTexture();
}

View File

@@ -13,7 +13,7 @@ void Skybox::render() {
glColor3f(0.75,0.75,0.75); glColor3f(0.75,0.75,0.75);
glPushMatrix(); glPushMatrix();
glTranslatef(position.x ,position.y, position.z); glTranslatef(position.x ,position.y, position.z);
glBindTexture(GL_TEXTURE_2D, getTextureID()); glBindTexture(GL_TEXTURE_2D, getTexture()->id);
glCullFace(GL_FRONT); glCullFace(GL_FRONT);
glScalef(getScale(), getScale(), getScale()); glScalef(getScale(), getScale(), getScale());
getGeometry()->draw(); getGeometry()->draw();

View File

@@ -1,9 +1,8 @@
#include <types/texture.h> #include <types/texture.h>
#include <SOIL/SOIL.h> #include <SOIL/SOIL.h>
#include <engine/engine.h> #include <engine/engine.h>
#include <iostream>
void Texture::load(const char* file) { void Texture::load(const char* file, bool storeOnTextureList) {
id = SOIL_load_OGL_texture(file, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y); id = SOIL_load_OGL_texture(file, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y);
//If we can't load the missing texture //If we can't load the missing texture
@@ -12,8 +11,10 @@ void Texture::load(const char* file) {
} }
if (id == 0) { if (id == 0) {
engine->setError(ENGINE_ERROR_CODE::TEXTURE_NOT_FOUND, false); engine->setError(ENGINE_ERROR_CODE::TEXTURE_NOT_FOUND, false);
load("assets/textures/missing.png"); load("assets/textures/missing.png", false);
} }
if (storeOnTextureList)
engine->world->textureList.push_back(*this);
} }
void Texture::erase(GLuint id) { void Texture::erase(GLuint id) {
@@ -25,7 +26,45 @@ void Texture::erase(GLuint id) {
} }
} }
Texture::Texture(std::string name, const char *filePath) { Texture::Texture(std::string& name, const char *filePath, bool storeOnTextureList) {
this->name = name; this->name = name;
load(filePath); load(filePath, storeOnTextureList);
}
MultiTexture::MultiTexture(const std::string& name, const char* pathContainingTextures, bool storeOnTextureList) {
this->name = name;
bool first = true;
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 = entry.path().filename();
//So we don't waste the slot.
if (first) {
this->id = texture.id;
this->name = texture.name;
this->usedBy = texture.usedBy;
first = false;
} else {multi.push_back(texture);}
}
if (storeOnTextureList)
engine->world->textureList.push_back(*this);
//You cannot have more than 32 textures in one multi-texture.
if (multi.size()+1 > engine->maxTextureUnits)
engine->setError(ENGINE_ERROR_CODE::TEXTURE_NOT_FOUND, true);
}
void MultiTexture::erase(GLuint id) {
glDeleteTextures(1, &id);
for (auto & i : multi) {
glDeleteTextures(1, &i.id);
}
for (int i = 0; i < engine->world->textureList.size(); i++) {
if (engine->world->textureList[i].id == id)
engine->world->textureList.erase(engine->world->textureList.begin() + i);
}
} }