forked from Redacted/Re3D
Multi-Textured entities.
This commit is contained in:
@@ -49,7 +49,7 @@ CPMAddPackage(
|
||||
|
||||
CPMAddPackage(
|
||||
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(
|
||||
|
@@ -1,4 +1,4 @@
|
||||
Fullscreen: 0
|
||||
Resolution: 1152 864
|
||||
Debug: 1
|
||||
Debug: 0
|
||||
CameraFOV: 72.5
|
||||
|
@@ -53,6 +53,7 @@ public:
|
||||
uint64_t frameCount = 0;
|
||||
float frameDelta = 0;
|
||||
GLenum glError = GL_NO_ERROR;
|
||||
GLint maxTextureUnits;
|
||||
float nearPlane = 0.01f;
|
||||
float farPlane = 100.0f;
|
||||
float fov = 75;
|
||||
|
@@ -2,12 +2,11 @@
|
||||
#include <cstdint>
|
||||
#include <types/vector.h>
|
||||
#include <types/vertex.h>
|
||||
#include <types/texture.h>
|
||||
#include <J3ML/LinearAlgebra/Matrix4x4.h>
|
||||
#include <J3ML/LinearAlgebra/Vector3.h>
|
||||
#include <archive.h>
|
||||
#include <engine/collision.h>
|
||||
//#include <JGL/JGL.h>
|
||||
//#include <JGL/Colors.h>
|
||||
|
||||
using J3ML::LinearAlgebra::Matrix4x4;
|
||||
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,
|
||||
bool collidable = true; // Entities are solid by default.
|
||||
GLuint getTextureID();
|
||||
|
||||
virtual Texture* getTexture();
|
||||
virtual VertexArray* getGeometry();
|
||||
virtual Collider getCollider();
|
||||
|
||||
std::vector<Entity*> GetChildren();
|
||||
virtual void SetParent(Entity* parent);
|
||||
bool IsDescendantOf(Entity* ancestor);
|
||||
|
@@ -6,12 +6,19 @@
|
||||
#include <string>
|
||||
|
||||
class Texture {
|
||||
private:
|
||||
void load(const char* file);
|
||||
public:
|
||||
void load(const char* file, bool storeOnTextureList);
|
||||
std::string name;
|
||||
std::vector<std::string> usedBy;
|
||||
static void erase(GLuint id);
|
||||
GLuint id;
|
||||
Texture(std::string name, const char* filePath);
|
||||
virtual void erase(GLuint id);
|
||||
GLuint id = 0;
|
||||
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);
|
||||
};
|
@@ -6,9 +6,10 @@
|
||||
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->init();
|
||||
engine->window->setVsyncEnabled(false);
|
||||
auto* cube1 = new(Cube);
|
||||
cube1->name = "cube1";
|
||||
cube1->SetPos({-5, -2, 0});
|
||||
|
@@ -66,7 +66,7 @@ void Engine::initGL()
|
||||
{
|
||||
//engine->window->setRenderer(RenderingAPI::OPENGL);
|
||||
engine->window->Open();
|
||||
engine->window->setResizable(true);
|
||||
engine->window->setResizable(false);
|
||||
engine->initGL();
|
||||
engine->loadConfig();
|
||||
|
||||
@@ -74,7 +74,7 @@ void Engine::initGL()
|
||||
|
||||
//glDisable(GL_CULL_FACE);
|
||||
//glEnable(GL_BLEND);
|
||||
|
||||
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
|
||||
auto* camera = new Camera();
|
||||
camera->SetPos({0.0f, -2.0f, -5.0f});
|
||||
camera->angle.y = 0.0f;
|
||||
|
@@ -76,8 +76,7 @@ Collider Entity::getCollider() {
|
||||
}
|
||||
|
||||
void Entity::loadTexture() {
|
||||
Texture texture(name, texturePath.c_str());
|
||||
engine->world->textureList.push_back(texture);
|
||||
Texture texture(name, texturePath.c_str(), true);
|
||||
}
|
||||
|
||||
void Entity::loadGeometry() {
|
||||
@@ -87,16 +86,6 @@ void Entity::loadGeometry() {
|
||||
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() {
|
||||
if (engine->world->geometryList.empty())
|
||||
loadGeometry();
|
||||
@@ -112,6 +101,7 @@ VertexArray* Entity::getGeometry() {
|
||||
void Entity::render() {
|
||||
|
||||
bool cameraInside = false;
|
||||
bool isMultiTexture = false;
|
||||
|
||||
//Run the example vertex shader.
|
||||
for (auto& shader : engine->world->shaderList)
|
||||
@@ -144,11 +134,20 @@ void Entity::render() {
|
||||
glDisable(GL_FOG);
|
||||
|
||||
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);
|
||||
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, getTextureID());
|
||||
if (getScale() != 1.0f)
|
||||
glScalef(getScale(),getScale(),getScale());
|
||||
getGeometry()->draw();
|
||||
@@ -168,9 +167,6 @@ void Entity::render() {
|
||||
|
||||
if (engine->world->getGlobalFogMode() != NULL)
|
||||
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) {
|
||||
@@ -189,3 +185,11 @@ bool Entity::isCollidingWith(Entity *entity) {
|
||||
if (thisCollider.type == ColliderType::AxisAlignedBoundingBox && otherCollider.type == ColliderType::CollisionMap)
|
||||
return Collision::collisionMapVSAxisAlignedBoundingBoxCollides(thisCollider, otherCollider);
|
||||
}
|
||||
|
||||
Texture* Entity::getTexture() {
|
||||
for (auto& texture : engine->world->textureList)
|
||||
if (texture.name == name)
|
||||
return &texture;
|
||||
loadTexture();
|
||||
return getTexture();
|
||||
}
|
||||
|
@@ -13,7 +13,7 @@ void Skybox::render() {
|
||||
glColor3f(0.75,0.75,0.75);
|
||||
glPushMatrix();
|
||||
glTranslatef(position.x ,position.y, position.z);
|
||||
glBindTexture(GL_TEXTURE_2D, getTextureID());
|
||||
glBindTexture(GL_TEXTURE_2D, getTexture()->id);
|
||||
glCullFace(GL_FRONT);
|
||||
glScalef(getScale(), getScale(), getScale());
|
||||
getGeometry()->draw();
|
||||
|
@@ -1,9 +1,8 @@
|
||||
#include <types/texture.h>
|
||||
#include <SOIL/SOIL.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);
|
||||
|
||||
//If we can't load the missing texture
|
||||
@@ -12,8 +11,10 @@ void Texture::load(const char* file) {
|
||||
}
|
||||
if (id == 0) {
|
||||
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) {
|
||||
@@ -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;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user