Cleanup
This commit is contained in:
BIN
assets/textures/multi/1.png
Normal file
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 |
@@ -18,23 +18,25 @@ private:
|
||||
///Pitch Yaw Roll, The orientation of the entity in the world,
|
||||
Vector3 angle = {0,0,0};
|
||||
///Loads the texture for the entity.
|
||||
virtual void loadTexture();
|
||||
///Loads the geometry for it.
|
||||
void loadGeometry();
|
||||
|
||||
///The scale it should be rendered at.
|
||||
Vector3 scale = {1.0f, 1.0f, 1.0f};
|
||||
protected:
|
||||
std::string modelPath;
|
||||
std::string texturePath;
|
||||
///If the entity has a parent entity, It's here.
|
||||
Entity* parent;
|
||||
///Entity list of child entities.
|
||||
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:
|
||||
std::string name; //Entity name. TODO remove this.
|
||||
bool alive;
|
||||
|
||||
|
||||
virtual std::vector<std::string> GetEntityUUIDList() const { return std::vector<std::string> { "" }; }
|
||||
|
||||
template <class T>
|
||||
|
@@ -33,22 +33,24 @@ public:
|
||||
void erase() override;
|
||||
///Every texture other than the base texture.
|
||||
std::vector<Texture> multi;
|
||||
///Loads a multi-texture from a given directory. One must be called "default.png"
|
||||
MultiTexture(Entity* entity, const char* pathContainingTextures, bool storeOnTextureList);
|
||||
///Loads a list of Textures into a MultiTexture.
|
||||
MultiTexture(Entity* entity, const std::vector<std::string>& textures, bool storeOnTextureList);
|
||||
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.
|
||||
class MotionTexture : public Texture {
|
||||
private:
|
||||
std::chrono::high_resolution_clock::time_point lastUpdate;
|
||||
int lastDisplayedIndex;
|
||||
u16 msDelay;
|
||||
public:
|
||||
int lastDisplayedIndex = 0;
|
||||
u16 msDelay = 0;
|
||||
bool doAnim = true;
|
||||
public:
|
||||
std::vector<Texture> motion;
|
||||
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;
|
||||
};
|
@@ -19,6 +19,5 @@ public:
|
||||
//We will probably end up having a different camera point class controlled by the "world".
|
||||
//void update(float elapsed) override;
|
||||
void pre_render() override;
|
||||
void loadTexture() override;
|
||||
Cube();
|
||||
};
|
@@ -10,7 +10,7 @@ void Ball::update(float elapsed) {
|
||||
|
||||
Ball::Ball() {
|
||||
name = "ball";
|
||||
modelPath = "assets/models/sphere_lo.obj";
|
||||
texturePath = "assets/textures/missing.png";
|
||||
loadGeometry("assets/models/sphere_lo.obj");
|
||||
loadTexture("assets/textures/missing.png");
|
||||
setScale(1.0f);
|
||||
}
|
@@ -8,20 +8,20 @@ void Cube::pre_render() {
|
||||
|
||||
Cube::Cube() {
|
||||
name = "cube";
|
||||
modelPath = "assets/models/cube.obj";
|
||||
texturePath = "assets/textures/multi/";
|
||||
setScale(0.5f);
|
||||
}
|
||||
|
||||
void Cube::loadTexture() {
|
||||
//MultiTexture texture(this, texturePath.c_str(), true);
|
||||
|
||||
|
||||
std::vector<std::string> textures = {
|
||||
loadGeometry("assets/models/cube.obj");
|
||||
std::vector<std::string> motionTextures = {
|
||||
"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/6.png"
|
||||
};
|
||||
"assets/textures/motion/3.png", "assets/textures/motion/4.png", "assets/textures/motion/5.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);
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@
|
||||
|
||||
DemoSkybox::DemoSkybox() {
|
||||
name = "skybox";
|
||||
modelPath = engine->workingDir + "/assets/models/sphere_vlo.obj";
|
||||
texturePath = engine->workingDir + "/assets/textures/skybox.png";
|
||||
loadGeometry("assets/models/sphere_vlo.obj");
|
||||
loadTexture("assets/textures/skybox.png");
|
||||
setScale(engine->farPlane);
|
||||
}
|
@@ -57,17 +57,23 @@ Vector3 Entity::getScale() const {
|
||||
return scale;
|
||||
}
|
||||
|
||||
void Entity::loadTexture() {
|
||||
Texture texture(this, texturePath.c_str(), true);
|
||||
void Entity::loadTexture(const std::string& file) {
|
||||
Texture texture(this, file.c_str(), true);
|
||||
}
|
||||
|
||||
void Entity::loadGeometry() {
|
||||
VertexArray vArray(this, modelPath, true);
|
||||
void Entity::loadMultiTexture(const std::vector<std::string>& files) {
|
||||
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() {
|
||||
if (engine->world->geometryList.empty())
|
||||
loadGeometry();
|
||||
for (auto& vArray : engine->world->geometryList)
|
||||
for (auto& entity : vArray->usedBy)
|
||||
if (instanceOf(this, entity)) {
|
||||
@@ -75,8 +81,18 @@ VertexArray* Entity::getGeometry() {
|
||||
vArray->usedBy.push_back(this);
|
||||
return vArray;
|
||||
}
|
||||
loadGeometry();
|
||||
return getGeometry();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -100,7 +116,6 @@ void Entity::render() {
|
||||
MultiTexture texture;
|
||||
texture.id = baseTexture->id;
|
||||
|
||||
|
||||
if (auto* t = dynamic_cast<MultiTexture*>(getTexture())) {
|
||||
texture.multi.emplace_back(*t);
|
||||
for (const auto &item: t->multi)
|
||||
@@ -164,7 +179,7 @@ void Entity::render() {
|
||||
getGeometry()->draw();
|
||||
|
||||
//Reset the multi-texture units.
|
||||
for (int j = 0; j <= i; j++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
glActiveTexture(GL_TEXTURE1 + j);
|
||||
glClientActiveTexture(GL_TEXTURE1 + j);
|
||||
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
@@ -195,20 +210,6 @@ void Entity::render() {
|
||||
|
||||
//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() {
|
||||
Texture* t = getTexture();
|
||||
VertexArray* vA = getGeometry();
|
||||
|
@@ -21,7 +21,5 @@ void Skybox::render() {
|
||||
|
||||
Skybox::Skybox() {
|
||||
name = "skybox";
|
||||
modelPath = engine->workingDir + "/assets/models/sphere_vlo.obj";
|
||||
texturePath = engine->workingDir + "/assets/textures/missing.png";
|
||||
setScale(engine->farPlane);
|
||||
}
|
@@ -57,35 +57,31 @@ void Texture::erase(Texture* texture) const {
|
||||
engine->world->textureList.erase(engine->world->textureList.begin() + i);
|
||||
}
|
||||
|
||||
|
||||
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(entity, (const char*) 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->usedBy = texture.usedBy;
|
||||
else multi.push_back(texture);
|
||||
}
|
||||
MultiTexture::MultiTexture(Entity* entity, const std::vector<std::string>& textures, bool storeOnTextureList) {
|
||||
Texture base;
|
||||
for (const auto& t : textures) {
|
||||
if (id != 0)
|
||||
multi.emplace_back(entity, t.c_str(), false);
|
||||
else
|
||||
base = Texture(entity, t.c_str(), false),
|
||||
id = base.id,
|
||||
usedBy = base.usedBy;
|
||||
}
|
||||
|
||||
if (storeOnTextureList)
|
||||
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)
|
||||
engine->setError(ENGINE_ERROR_CODE::MULTI_TEXTURE_SIZE_EXCEEDS, true);
|
||||
engine->setError(ENGINE_ERROR_CODE::MULTI_TEXTURE_SIZE_EXCEEDS, false);
|
||||
}
|
||||
|
||||
void MultiTexture::erase() {
|
||||
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;
|
||||
this->doAnim = doAnim;
|
||||
Texture base(entity, textures[0].c_str(), false);
|
||||
std::vector<std::string> extra(textures.begin()+1, textures.end());
|
||||
|
||||
@@ -127,3 +123,17 @@ Texture* MotionTexture::current() {
|
||||
|
||||
return &motion[lastDisplayedIndex - 1];
|
||||
}
|
||||
|
||||
void MotionTexture::advance() {
|
||||
lastDisplayedIndex++;
|
||||
if (lastDisplayedIndex > motion.size())
|
||||
lastDisplayedIndex = 0;
|
||||
}
|
||||
|
||||
void MotionTexture::toggleAnim() {
|
||||
doAnim = !doAnim;
|
||||
}
|
||||
|
||||
Texture* MotionTexture::base() {
|
||||
return this;
|
||||
}
|
||||
|
Reference in New Issue
Block a user