This commit is contained in:
2024-07-17 13:52:24 -04:00
parent 7f65fee891
commit f5ab983176
11 changed files with 87 additions and 75 deletions

BIN
assets/textures/multi/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 6.2 KiB

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.
Vector3 scale = {1.0f, 1.0f, 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>

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

@@ -19,6 +19,5 @@ public:
//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 @@ 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

@@ -8,20 +8,20 @@ void Cube::pre_render() {
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", "assets/textures/motion/default.png", "assets/textures/motion/1.png", "assets/textures/motion/2.png",
"assets/textures/motion/3.png", "assets/textures/motion/4.png", "assets/textures/motion/5.png", "assets/textures/motion/3.png", "assets/textures/motion/4.png", "assets/textures/motion/5.png",
"assets/textures/motion/6.png" "assets/textures/motion/6.png"
}; };
MotionTexture texture(this, textures, 250, true); std::vector<std::string> multiTextures = {
"assets/textures/multi/default.png", "assets/textures/multi/1.png", "assets/textures/multi/2.png", "assets/textures/multi/3.png",
"assets/textures/multi/4.png", "assets/textures/multi/5.png", "assets/textures/multi/6.png"
};
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

@@ -57,17 +57,23 @@ 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); Texture texture(this, file.c_str(), true);
} }
void Entity::loadGeometry() { void Entity::loadMultiTexture(const std::vector<std::string>& files) {
VertexArray vArray(this, modelPath, true); MultiTexture texture(this, files, true);
}
void Entity::loadMotionTexture(const std::vector<std::string>& files, u16 frameDelta, bool doAnim) {
MotionTexture(this, files, frameDelta, true, doAnim);
}
void Entity::loadGeometry(const std::string& file) {
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,8 +81,18 @@ 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; inline Texture* baseTexture = new Texture;
@@ -100,7 +116,6 @@ void Entity::render() {
MultiTexture texture; MultiTexture texture;
texture.id = baseTexture->id; texture.id = baseTexture->id;
if (auto* t = dynamic_cast<MultiTexture*>(getTexture())) { if (auto* t = dynamic_cast<MultiTexture*>(getTexture())) {
texture.multi.emplace_back(*t); texture.multi.emplace_back(*t);
for (const auto &item: t->multi) for (const auto &item: t->multi)
@@ -164,7 +179,7 @@ void Entity::render() {
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);
@@ -195,20 +210,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();

View File

@@ -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

@@ -57,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.
//In Fixed-Function OpenGL you only have 8 TMUs available.
//TODO: multi-pass multi-texturing (will be slower).
if (multi.size() > 6) if (multi.size() > 6)
engine->setError(ENGINE_ERROR_CODE::MULTI_TEXTURE_SIZE_EXCEEDS, true); engine->setError(ENGINE_ERROR_CODE::MULTI_TEXTURE_SIZE_EXCEEDS, false);
} }
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());
@@ -126,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;
}