Merge branch 'testing'

This commit is contained in:
2024-07-17 13:52:36 -04:00
15 changed files with 196 additions and 140 deletions

View File

@@ -49,7 +49,7 @@ CPMAddPackage(
CPMAddPackage( CPMAddPackage(
NAME JGL NAME JGL
URL https://git.redacted.cc/josh/JGL/archive/Prerelease-16.zip URL https://git.redacted.cc/josh/JGL/archive/Prerelease-20.zip
) )
CPMAddPackage( CPMAddPackage(

View File

@@ -49,7 +49,7 @@ private:
Vector3 globalLightColor = {0, 0, 0}; Vector3 globalLightColor = {0, 0, 0};
Vector4 globalFogColor = {0, 0, 0, 0}; Vector4 globalFogColor = {0, 0, 0, 0};
Vector2 globalFogRange = {0, 0}; Vector2 globalFogRange = {0, 0};
GLenum globalFogMode = NULL; ///There's Linear, GL_EXP, GL_EXP2. GLenum globalFogMode = 0; ///There's Linear, GL_EXP, GL_EXP2.
GLfloat globalFogDensity = 0.0f; GLfloat globalFogDensity = 0.0f;
public: public:

View File

@@ -18,23 +18,25 @@ private:
///Pitch Yaw Roll, The orientation of the entity in the world, ///Pitch Yaw Roll, The orientation of the entity in the world,
Vector3 angle = {0,0,0}; Vector3 angle = {0,0,0};
///Loads the texture for the entity. ///Loads the texture for the entity.
virtual void loadTexture();
///Loads the geometry for it.
void loadGeometry();
///The scale it should be rendered at. ///The scale it should be rendered at.
GLfloat scale = 1.0f; Vector3 scale = {1.0f, 1.0f, 1.0f};
protected: protected:
std::string modelPath;
std::string texturePath;
///If the entity has a parent entity, It's here. ///If the entity has a parent entity, It's here.
Entity* parent; Entity* parent;
///Entity list of child entities. ///Entity list of child entities.
std::vector<Entity*> children; std::vector<Entity*> children;
///Loads the geometry for it.
void loadGeometry(const std::string& file);
void loadTexture(const std::string& file);
void loadMultiTexture(const std::vector<std::string>& files);
void loadMotionTexture(const std::vector<std::string>& files, u16 frameDelta, bool doAnim = true);
public: public:
std::string name; //Entity name. TODO remove this. std::string name; //Entity name. TODO remove this.
bool alive; bool alive;
virtual std::vector<std::string> GetEntityUUIDList() const { return std::vector<std::string> { "" }; } virtual std::vector<std::string> GetEntityUUIDList() const { return std::vector<std::string> { "" }; }
template <class T> template <class T>
@@ -75,7 +77,7 @@ public:
[[nodiscard]] Entity* GetFamilyTreeRoot() const; [[nodiscard]] Entity* GetFamilyTreeRoot() const;
Entity *Add(Entity *newChild); Entity *Add(Entity *newChild);
void setScale(const float& multiplier); void setScale(const float& multiplier);
[[nodiscard]] GLfloat getScale() const; [[nodiscard]] Vector3 getScale() const;
// TODO: Constrain to DerivedEntityType // TODO: Constrain to DerivedEntityType
template <typename T> template <typename T>

View File

@@ -8,11 +8,11 @@ public:
std::string name; std::string name;
///The shader program ///The shader program
GLuint id = NULL; GLuint id = 0;
///The individual shaders ///The individual shaders
GLuint vertex = NULL; GLuint vertex = 0;
GLuint fragment = NULL; GLuint fragment = 0;
Shader(const char* name, const std::string& vertexPath, const std::string& fragmentPath); Shader(const char* name, const std::string& vertexPath, const std::string& fragmentPath);
Shader(const char* name, const std::string& filePath, const GLenum& type); Shader(const char* name, const std::string& filePath, const GLenum& type);

View File

@@ -33,22 +33,24 @@ public:
void erase() override; void erase() override;
///Every texture other than the base texture. ///Every texture other than the base texture.
std::vector<Texture> multi; std::vector<Texture> multi;
///Loads a multi-texture from a given directory. One must be called "default.png" ///Loads a list of Textures into a MultiTexture.
MultiTexture(Entity* entity, const char* pathContainingTextures, bool storeOnTextureList); MultiTexture(Entity* entity, const std::vector<std::string>& textures, bool storeOnTextureList);
MultiTexture() = default; MultiTexture() = default;
//TODO load multi-texture from array of std::string of file names.
}; };
///A list of textures where the one actually displayed depends on how much time has elapsed. ///A list of textures where the one actually displayed depends on how much time has elapsed.
class MotionTexture : public Texture { class MotionTexture : public Texture {
private: private:
std::chrono::high_resolution_clock::time_point lastUpdate; std::chrono::high_resolution_clock::time_point lastUpdate;
int lastDisplayedIndex; int lastDisplayedIndex = 0;
u16 msDelay; u16 msDelay = 0;
public:
bool doAnim = true; bool doAnim = true;
public:
std::vector<Texture> motion; std::vector<Texture> motion;
Texture* current(); Texture* current();
MotionTexture(Entity* entity, const std::vector<std::string>& textures,u16 msBetweenFrames, bool storeOnTextureList); Texture* base();
void advance();
void toggleAnim();
MotionTexture(Entity* entity, const std::vector<std::string>& textures,u16 msBetweenFrames, bool storeOnTextureList, bool doAnim = true);
MotionTexture() = default; MotionTexture() = default;
}; };

View File

@@ -17,8 +17,7 @@ public:
//The "camera point" is the position the camera will want to be while following the player. //The "camera point" is the position the camera will want to be while following the player.
//We will probably end up having a different camera point class controlled by the "world". //We will probably end up having a different camera point class controlled by the "world".
void update(float elapsed) override; //void update(float elapsed) override;
void pre_render() override; void pre_render() override;
void loadTexture() override;
Cube(); Cube();
}; };

View File

@@ -10,7 +10,7 @@ int main()
engine->window = new ReWindow::RWindow("Re3D Test Application", 1152, 864, 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();
JGL::InitTextEngine(); JGL::Update(engine->window->getSize());
Fonts::initFonts(); Fonts::initFonts();
engine->window->setVsyncEnabled(false); engine->window->setVsyncEnabled(false);
engine->window->setResizable(false); engine->window->setResizable(false);

View File

@@ -10,7 +10,7 @@ void Ball::update(float elapsed) {
Ball::Ball() { Ball::Ball() {
name = "ball"; name = "ball";
modelPath = "assets/models/sphere_lo.obj"; loadGeometry("assets/models/sphere_lo.obj");
texturePath = "assets/textures/missing.png"; loadTexture("assets/textures/missing.png");
setScale(1.0f); setScale(1.0f);
} }

View File

@@ -1,31 +1,27 @@
#include <cube.h> #include <cube.h>
#include <Redacted3D/engine/engine.h> #include <Redacted3D/engine/engine.h>
#include <ball.h>
void Cube::pre_render() { void Cube::pre_render() {
getGeometry(); getGeometry();
setAngle(getAngle().Add(24 * engine->frameDelta)); setAngle(getAngle().Add(24 * engine->frameDelta));
} }
void Cube::update(float elapsed) {
}
Cube::Cube() { Cube::Cube() {
name = "cube"; name = "cube";
modelPath = "assets/models/cube.obj";
texturePath = "assets/textures/multi/";
setScale(0.5f); setScale(0.5f);
}
void Cube::loadTexture() { loadGeometry("assets/models/cube.obj");
//MultiTexture texture(this, texturePath.c_str(), true); std::vector<std::string> motionTextures = {
std::vector<std::string> textures; "assets/textures/motion/default.png", "assets/textures/motion/1.png", "assets/textures/motion/2.png",
textures.emplace_back("assets/textures/motion/default.png"); "assets/textures/motion/3.png", "assets/textures/motion/4.png", "assets/textures/motion/5.png",
textures.emplace_back("assets/textures/motion/1.png"); "assets/textures/motion/6.png"
textures.emplace_back("assets/textures/motion/2.png"); };
textures.emplace_back("assets/textures/motion/3.png");
textures.emplace_back("assets/textures/motion/4.png"); std::vector<std::string> multiTextures = {
textures.emplace_back("assets/textures/motion/5.png"); "assets/textures/multi/default.png", "assets/textures/multi/1.png", "assets/textures/multi/2.png", "assets/textures/multi/3.png",
textures.emplace_back("assets/textures/motion/6.png"); "assets/textures/multi/4.png", "assets/textures/multi/5.png", "assets/textures/multi/6.png"
MotionTexture texture(this, textures, 250, true); };
loadMultiTexture(multiTextures);
//loadMotionTexture(motionTextures, 250, true);
} }

View File

@@ -3,7 +3,7 @@
DemoSkybox::DemoSkybox() { DemoSkybox::DemoSkybox() {
name = "skybox"; name = "skybox";
modelPath = engine->workingDir + "/assets/models/sphere_vlo.obj"; loadGeometry("assets/models/sphere_vlo.obj");
texturePath = engine->workingDir + "/assets/textures/skybox.png"; loadTexture("assets/textures/skybox.png");
setScale(engine->farPlane); setScale(engine->farPlane);
} }

View File

@@ -37,14 +37,23 @@ void FreeCam::pre_render() {
if (engine->window->isKeyDown(Keys::E)) if (engine->window->isKeyDown(Keys::E))
setAngle(getAngle().x, getAngle().y, getAngle().z + 75.0f * engine->frameDelta); setAngle(getAngle().x, getAngle().y, getAngle().z + 75.0f * engine->frameDelta);
} }
Vector3 textAngle = {0,0,0};
void FreeCam::jglRenderPass() { void FreeCam::jglRenderPass() {
JGL::J3D::DrawString3D(JGL::Colors::White, "Entity Count: " + std::to_string(engine->world->getEntityCount()), {2.5,0,0}, -0.005125, 48, Fonts::ModeSeven); textAngle.x = textAngle.x - (96.f * engine->frameDelta);
JGL::J3D::DrawString3D(JGL::Colors::White, "Cam Pos: X: " + std::to_string(position.x) + " Y: " + std::to_string(position.y) + " Z: " + std::to_string(position.z), {4.75, 0.25,0}, -0.005125, 32, Fonts::ModeSeven); using namespace JGL;
J3D::Begin();
J3D::DrawString(JGL::Colors::Red, "Text", {0, -2, 0}, textAngle, 4.f, 32, Fonts::Jupiteroid);
J3D::End();
JGL::J3D::DrawString3D(JGL::Colors::White, "FPS: " + std::to_string(engine->framerate()), {0,0,0}, -0.005125, 48, Fonts::ModeSeven); J2D::Begin();
JGL::J3D::DrawString3D(JGL::Colors::White, "Framecount: " + std::to_string(engine->frameCount), {0,0.25,0}, -0.005125, 48, Fonts::ModeSeven); J2D::FillRect({255,0,0,128}, {0, 72}, {100, 100});
JGL::J3D::DrawString3D(JGL::Colors::White, "Frame dT: " + std::to_string(engine->frameDelta), {0,0.5,0}, -0.005125, 48, Fonts::ModeSeven); J2D::FillCircle(JGL::Colors::White, {16, 128}, 12, 16);
J2D::DrawString(JGL::Colors::White, "Framerate: " + std::to_string((int) engine->framerate()), 0, -16, 1, 16, Fonts::Jupiteroid);
J2D::DrawString(JGL::Colors::White, "Framecount: " + std::to_string(engine->frameCount), 0, -33, 1,16, Fonts::Jupiteroid);
J2D::DrawString(JGL::Colors::White, "Position: " + std::to_string(position.x) + " " + std::to_string(position.y) + " " + std::to_string(position.z), 0, -50, 1,16, Fonts::Jupiteroid);
J2D::DrawString(JGL::Colors::White, "ViewAngle: " + std::to_string(getAngle().x) + " " + std::to_string(getAngle().y) + " " + std::to_string(getAngle().z), 0, -67, 1,16, Fonts::Jupiteroid);
J2D::End();
} }

View File

@@ -1,7 +1,9 @@
#include <sstream> #include <sstream>
#include <thread> #include <thread>
#include <JGL/JGL.h>
#include <Redacted3D/engine/engine.h> #include <Redacted3D/engine/engine.h>
#include <Redacted3D/types/entity/camera.h> #include <Redacted3D/types/entity/camera.h>
#include <jlog/jlog.hpp>
using namespace J3ML; using namespace J3ML;
@@ -80,6 +82,7 @@ void Engine::initGL() const {
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE);
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_LIGHT0);
} }
void Engine::init() { void Engine::init() {
@@ -109,11 +112,15 @@ void Engine::render() {
if (auto* c = dynamic_cast<Camera*>(e); c == nullptr) //If it's not a camera. if (auto* c = dynamic_cast<Camera*>(e); c == nullptr) //If it's not a camera.
e->render(); e->render();
if (glGetError() != GL_NO_ERROR) int glError = glGetError();
if (glError != GL_NO_ERROR) {
FATAL("GL Error: " + std::to_string(glError))
exit(0); exit(0);
}
} }
void Engine::jglRenderPass() { void Engine::jglRenderPass() {
JGL::Update(engine->window->getSize());
glDisable(GL_LIGHTING); glDisable(GL_LIGHTING);
///If elements are attached to a camera that's not the active one then they shouldn't be drawn. ///If elements are attached to a camera that's not the active one then they shouldn't be drawn.
if(world->getActiveCamera() != nullptr) if(world->getActiveCamera() != nullptr)

View File

@@ -42,7 +42,7 @@ Entity::Entity() {
ticksAlive = 0; ticksAlive = 0;
children = std::vector<Entity*> (); children = std::vector<Entity*> ();
parent = nullptr; parent = nullptr;
scale = 1.0f; scale = {1.0f, 1.0f, 1.0f};
//UUIDv4::UUIDGenerator<std::mt19937_64> uuidGenerator; //UUIDv4::UUIDGenerator<std::mt19937_64> uuidGenerator;
//UUIDv4::UUID id = uuidGenerator.getUUID(); //UUIDv4::UUID id = uuidGenerator.getUUID();
@@ -50,24 +50,43 @@ Entity::Entity() {
} }
void Entity::setScale(const float &multiplier) { void Entity::setScale(const float &multiplier) {
scale = multiplier; scale.Set(multiplier, multiplier, multiplier);
} }
GLfloat Entity::getScale() const { Vector3 Entity::getScale() const {
return scale; return scale;
} }
void Entity::loadTexture() { void Entity::loadTexture(const std::string& file) {
Texture texture(this, texturePath.c_str(), true); for (const auto* e : engine->world->GetChildren())
if (instanceOf(this, e))
return;
Texture texture(this, file.c_str(), true);
} }
void Entity::loadGeometry() { void Entity::loadMultiTexture(const std::vector<std::string>& files) {
VertexArray vArray(this, modelPath, true); for (const auto* e : engine->world->GetChildren())
if (instanceOf(this, e))
return;
MultiTexture texture(this, files, true);
}
void Entity::loadMotionTexture(const std::vector<std::string>& files, u16 frameDelta, bool doAnim) {
for (const auto* e : engine->world->GetChildren())
if (instanceOf(this, e))
return;
MotionTexture(this, files, frameDelta, true, doAnim);
}
void Entity::loadGeometry(const std::string& file) {
for (const auto& vA : engine->world->geometryList)
for (const auto& reference : vA->usedBy)
if (instanceOf(this, reference))
return;
VertexArray vArray(this, file, true);
} }
VertexArray* Entity::getGeometry() { VertexArray* Entity::getGeometry() {
if (engine->world->geometryList.empty())
loadGeometry();
for (auto& vArray : engine->world->geometryList) for (auto& vArray : engine->world->geometryList)
for (auto& entity : vArray->usedBy) for (auto& entity : vArray->usedBy)
if (instanceOf(this, entity)) { if (instanceOf(this, entity)) {
@@ -75,28 +94,60 @@ VertexArray* Entity::getGeometry() {
vArray->usedBy.push_back(this); vArray->usedBy.push_back(this);
return vArray; return vArray;
} }
loadGeometry(); return nullptr;
return getGeometry();
} }
Texture* Entity::getTexture() {
for (auto& texture : engine->world->textureList)
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;
}
return nullptr;
}
inline Texture* baseTexture = new Texture;
void Entity::render() { void Entity::render() {
bool multiTexture = false; if (!baseTexture) {
bool motionTexture = false; Texture t;
glGenTextures(1, &t.id);
glBindTexture(GL_TEXTURE_2D, t.id);
if (auto* t = dynamic_cast<MultiTexture*>(getTexture())) unsigned char whitePixel[4] = { 255, 255, 255, 255};
multiTexture = true; glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, whitePixel);
if (!multiTexture) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
if (auto* t = dynamic_cast<MotionTexture*>(getTexture())) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
motionTexture = true; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glActiveTexture(0);
baseTexture = new Texture(t);
}
MultiTexture texture;
texture.id = baseTexture->id;
if (auto* t = dynamic_cast<MultiTexture*>(getTexture())) {
texture.multi.emplace_back(*t);
for (const auto &item: t->multi)
texture.multi.push_back(item);
}
else if (auto* j = dynamic_cast<MotionTexture*>(getTexture()))
texture.multi.push_back(*j->current());
else if (auto* k = dynamic_cast<Texture*>(getTexture()))
texture.multi.push_back(*k);
glPushMatrix(); glPushMatrix();
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);
if (getScale() != 1.0f) if (getScale().x != 1.0f || getScale().y != 1.0f || getScale().z != 1.0f)
glScalef(getScale(), getScale(), getScale()); glScalef(getScale().x, getScale().y, getScale().z);
//Set up texture. //Set up texture.
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@@ -104,7 +155,9 @@ void Entity::render() {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, motionTexture ? ((MotionTexture*)getTexture())->current()->id : getTexture()->id); glBindTexture(GL_TEXTURE_2D, texture.id);
//Texture unit mode.
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
if (!engine->useVBO) if (!engine->useVBO)
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x), glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x),
@@ -114,41 +167,32 @@ void Entity::render() {
glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo), glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo),
glTexCoordPointer(2, GL_FLOAT, 0, nullptr); glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
if (!multiTexture)
getGeometry()->draw();
if (multiTexture) { int i = 0;
auto* multi = (MultiTexture*) getTexture(); for(auto& t : texture.multi) {
glActiveTexture(GL_TEXTURE1 + i);
glClientActiveTexture(GL_TEXTURE1 + i);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
int i = 0; if (!engine->useVBO)
GLfloat blendingFactor[3] = {engine->world->getAmbientLightColor().x * 0.2f, engine->world->getAmbientLightColor().y * 0.2f, engine->world->getAmbientLightColor().z * 0.2f}; glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x),
for(auto& texture : multi->multi) { glTexCoordPointer(2, GL_FLOAT, sizeof(TextureCoordinate), &getGeometry()->texCoords[0].x);
glActiveTexture(GL_TEXTURE1 + i);
glClientActiveTexture(GL_TEXTURE1 + i);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if (!engine->useVBO) if (engine->useVBO)
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x), glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo),
glTexCoordPointer(2, GL_FLOAT, sizeof(TextureCoordinate), &getGeometry()->texCoords[0].x); glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
if (engine->useVBO)
glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo),
glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
//Texture unit mode. //Texture unit mode.
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
glBindTexture(GL_TEXTURE_2D, t.id);
//Makes the lighting work the way you'd think it would.
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, blendingFactor);
glBindTexture(GL_TEXTURE_2D, texture.id);
i++; i++;
} }
getGeometry()->draw(); getGeometry()->draw();
//Reset the multi-texture units. //Reset the multi-texture units.
for (int j = 0; j <= i; j++) { for (int j = 0; j < i; j++) {
glActiveTexture(GL_TEXTURE1 + j); glActiveTexture(GL_TEXTURE1 + j);
glClientActiveTexture(GL_TEXTURE1 + j); glClientActiveTexture(GL_TEXTURE1 + j);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
@@ -156,7 +200,6 @@ void Entity::render() {
glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY);
} }
}
//Reset texture unit 0. //Reset texture unit 0.
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@@ -180,20 +223,6 @@ void Entity::render() {
//bool Entity::isCollidingWith(Entity *entity) {} //bool Entity::isCollidingWith(Entity *entity) {}
Texture* Entity::getTexture() {
if (engine->world->textureList.empty())
loadTexture();
for (auto& texture : engine->world->textureList)
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();
}
void Entity::erase() { void Entity::erase() {
Texture* t = getTexture(); Texture* t = getTexture();
VertexArray* vA = getGeometry(); VertexArray* vA = getGeometry();
@@ -220,9 +249,10 @@ void Entity::erase() {
AABB Entity::getAABB() { AABB Entity::getAABB() {
AABB aabb = Collision::genMinimallyEnclosingAABB(getGeometry()); AABB aabb = Collision::genMinimallyEnclosingAABB(getGeometry());
if (getScale() != 1) //TODO this won't look right.
aabb.minPoint *= scale, if (getScale().x != 1)
aabb.maxPoint *= scale; aabb.minPoint *= scale.x,
aabb.maxPoint *= scale.x;
aabb.minPoint += position; aabb.minPoint += position;
aabb.maxPoint += position; aabb.maxPoint += position;
@@ -231,7 +261,8 @@ AABB Entity::getAABB() {
Sphere Entity::getSphere() { Sphere Entity::getSphere() {
Sphere sphere = Collision::genMinimallyEnclosingSphere(getGeometry()); Sphere sphere = Collision::genMinimallyEnclosingSphere(getGeometry());
sphere.Radius *= scale; //TODO this won't look right.
sphere.Radius *= scale.x;
sphere.Position = position; sphere.Position = position;
return sphere; return sphere;
} }

View File

@@ -12,7 +12,7 @@ void Skybox::render() {
glTranslatef(position.x ,position.y, position.z); glTranslatef(position.x ,position.y, position.z);
glBindTexture(GL_TEXTURE_2D, getTexture()->id); glBindTexture(GL_TEXTURE_2D, getTexture()->id);
glCullFace(GL_FRONT); glCullFace(GL_FRONT);
glScalef(getScale(), getScale(), getScale()); glScalef(getScale().x, getScale().y, getScale().z);
getGeometry()->draw(); getGeometry()->draw();
glCullFace(GL_BACK); glCullFace(GL_BACK);
glBindTexture(GL_TEXTURE_2D,0); glBindTexture(GL_TEXTURE_2D,0);
@@ -21,7 +21,5 @@ void Skybox::render() {
Skybox::Skybox() { Skybox::Skybox() {
name = "skybox"; name = "skybox";
modelPath = engine->workingDir + "/assets/models/sphere_vlo.obj";
texturePath = engine->workingDir + "/assets/textures/missing.png";
setScale(engine->farPlane); setScale(engine->farPlane);
} }

View File

@@ -1,9 +1,10 @@
#include <Redacted3D/types/texture.h>
#include <ReTexture/rTexture.h> #include <ReTexture/rTexture.h>
#include <Redacted3D/types/texture.h>
#include <Redacted3D/engine/engine.h> #include <Redacted3D/engine/engine.h>
void Texture::load(Entity* entity, const std::string& file, bool storeOnTextureList) { void Texture::load(Entity* entity, const std::string& file, bool storeOnTextureList) {
auto* texture = new RTexture(file, {RTextureFlag::INVERT_Y}); auto* texture = new RTexture(file, {RTextureFlag::INVERT_Y});
glGenTextures(1, &id); glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
if (texture->format == RTextureFormat::RGBA) if (texture->format == RTextureFormat::RGBA)
@@ -11,6 +12,7 @@ void Texture::load(Entity* entity, const std::string& file, bool storeOnTextureL
if (texture->format == RTextureFormat::RGB) if (texture->format == RTextureFormat::RGB)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture->width, texture->height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture->pixelData.data()); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture->width, texture->height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture->pixelData.data());
delete texture; delete texture;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -55,35 +57,31 @@ void Texture::erase(Texture* texture) const {
engine->world->textureList.erase(engine->world->textureList.begin() + i); engine->world->textureList.erase(engine->world->textureList.begin() + i);
} }
MultiTexture::MultiTexture(Entity* entity, const std::vector<std::string>& textures, bool storeOnTextureList) {
MultiTexture::MultiTexture(Entity* entity, const char* pathContainingTextures, bool storeOnTextureList) { Texture base;
for (const auto& entry : std::filesystem::directory_iterator(pathContainingTextures)) for (const auto& t : textures) {
if (entry.is_regular_file() && entry.path().extension() == ".png") { if (id != 0)
Texture texture(entity, (const char*) entry.path().c_str(), false); multi.emplace_back(entity, t.c_str(), false);
else
//The base texture *must* go in the first slot. base = Texture(entity, t.c_str(), false),
if (entry.path().filename() == "default.png") id = base.id,
this->id = texture.id, usedBy = base.usedBy;
this->usedBy = texture.usedBy; }
else multi.push_back(texture);
}
if (storeOnTextureList) if (storeOnTextureList)
engine->world->textureList.push_back(new MultiTexture(*this)); engine->world->textureList.push_back(new MultiTexture(*this));
//You cannot have more than 8 total textures rendered in one pass. if (multi.size() > 6)
//In Fixed-Function OpenGL you only have 8 TMUs available. engine->setError(ENGINE_ERROR_CODE::MULTI_TEXTURE_SIZE_EXCEEDS, false);
//TODO: multi-pass multi-texturing (will be slower).
if (multi.size() > 7)
engine->setError(ENGINE_ERROR_CODE::MULTI_TEXTURE_SIZE_EXCEEDS, true);
} }
void MultiTexture::erase() { void MultiTexture::erase() {
Texture::erase(this); Texture::erase(this);
} }
MotionTexture::MotionTexture(Entity* entity, const std::vector<std::string>& textures, u16 msBetweenFrames, bool storeOnTextureList) { MotionTexture::MotionTexture(Entity* entity, const std::vector<std::string>& textures, u16 msBetweenFrames, bool storeOnTextureList, bool doAnim) {
msDelay = msBetweenFrames; msDelay = msBetweenFrames;
this->doAnim = doAnim;
Texture base(entity, textures[0].c_str(), false); Texture base(entity, textures[0].c_str(), false);
std::vector<std::string> extra(textures.begin()+1, textures.end()); std::vector<std::string> extra(textures.begin()+1, textures.end());
@@ -124,4 +122,18 @@ Texture* MotionTexture::current() {
return this; return this;
return &motion[lastDisplayedIndex - 1]; return &motion[lastDisplayedIndex - 1];
} }
void MotionTexture::advance() {
lastDisplayedIndex++;
if (lastDisplayedIndex > motion.size())
lastDisplayedIndex = 0;
}
void MotionTexture::toggleAnim() {
doAnim = !doAnim;
}
Texture* MotionTexture::base() {
return this;
}