Refactor
This commit is contained in:
@@ -49,7 +49,7 @@ public:
|
||||
//float tickDelta = NULL;
|
||||
bool fullscreen;
|
||||
bool debug = true;
|
||||
bool useVBO = false;
|
||||
bool useVBO = true;
|
||||
uint64_t tickCount = 0;
|
||||
uint64_t frameCount = 0;
|
||||
float frameDelta = 0;
|
||||
|
@@ -7,18 +7,19 @@
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
void load(const char* file, bool storeOnTextureList);
|
||||
std::string name;
|
||||
std::vector<std::string> usedBy;
|
||||
virtual void erase(GLuint id);
|
||||
GLuint id = 0;
|
||||
void load(const char* file, bool storeOnTextureList);
|
||||
virtual void erase();
|
||||
static void erase(Texture* texture);
|
||||
Texture(std::string& name, const char* filePath, bool storeOnTextureList);
|
||||
Texture() = default;
|
||||
};
|
||||
|
||||
class MultiTexture : public Texture {
|
||||
public:
|
||||
void erase(GLuint id) override;
|
||||
void erase() override;
|
||||
std::vector<Texture> multi;
|
||||
MultiTexture(const std::string& name, const char* pathContainingTextures, bool storeOnTextureList);
|
||||
};
|
@@ -121,6 +121,7 @@ void Entity::render() {
|
||||
}
|
||||
}
|
||||
|
||||
//Done to avoid using the dynamic cast more than once because it's comparatively slow.
|
||||
if (auto* t = dynamic_cast<MultiTexture*>(this->getTexture()))
|
||||
multiTexture = true;
|
||||
|
||||
@@ -135,43 +136,29 @@ void Entity::render() {
|
||||
if (getScale() != 1.0f)
|
||||
glScalef(getScale(), getScale(), getScale());
|
||||
|
||||
if (!multiTexture) {
|
||||
//Set up the single texture.
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glClientActiveTexture(GL_TEXTURE0);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glBindTexture(GL_TEXTURE_2D, getTexture()->id);
|
||||
//Set up primary texture.
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glClientActiveTexture(GL_TEXTURE0);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glBindTexture(GL_TEXTURE_2D, getTexture()->id);
|
||||
|
||||
getGeometry()->draw();
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glClientActiveTexture(GL_TEXTURE0);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
if (!engine->useVBO) {
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x);
|
||||
glTexCoordPointer(2, GL_FLOAT, sizeof(TextureCoordinate), &getGeometry()->texCoords[0].x);
|
||||
}
|
||||
|
||||
if (engine->useVBO) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo);
|
||||
glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
|
||||
}
|
||||
|
||||
if (!multiTexture)
|
||||
getGeometry()->draw();
|
||||
|
||||
if (multiTexture) {
|
||||
auto* multi = (MultiTexture*) getTexture();
|
||||
//Set up the base texture.
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glClientActiveTexture(GL_TEXTURE0);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glBindTexture(GL_TEXTURE_2D, multi->id);
|
||||
|
||||
if (!engine->useVBO) {
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x);
|
||||
glTexCoordPointer(2, GL_FLOAT, sizeof(TextureCoordinate), &getGeometry()->texCoords[0].x);
|
||||
}
|
||||
|
||||
if (engine->useVBO) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo);
|
||||
glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for(auto& texture : multi->multi) {
|
||||
@@ -213,21 +200,19 @@ void Entity::render() {
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
}
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glClientActiveTexture(GL_TEXTURE0);
|
||||
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
}
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glClientActiveTexture(GL_TEXTURE0);
|
||||
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glDisableClientState(GL_VERTEX_ARRAY);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glPopMatrix();
|
||||
|
||||
if (engine->debug) {
|
||||
glDisable(GL_LIGHTING);
|
||||
glPushMatrix();
|
||||
glColor4f(1, 0, 0, 1);
|
||||
glTranslatef(0, 0, 0);
|
||||
getCollider().draw();
|
||||
glColor4f(1, 1, 1, 1);
|
||||
glPopMatrix();
|
||||
|
@@ -16,13 +16,8 @@ void Texture::load(const char* file, bool storeOnTextureList) {
|
||||
engine->world->textureList.push_back(new Texture(*this));
|
||||
}
|
||||
|
||||
void Texture::erase(GLuint id) {
|
||||
glDeleteTextures(1, &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);
|
||||
}
|
||||
void Texture::erase() {
|
||||
Texture::erase(this);
|
||||
}
|
||||
|
||||
Texture::Texture(std::string& name, const char *filePath, bool storeOnTextureList) {
|
||||
@@ -30,6 +25,18 @@ Texture::Texture(std::string& name, const char *filePath, bool storeOnTextureLis
|
||||
load(filePath, storeOnTextureList);
|
||||
}
|
||||
|
||||
void Texture::erase(Texture* texture) {
|
||||
glDeleteTextures(1, &texture->id);
|
||||
|
||||
if (auto* t = dynamic_cast<MultiTexture*>(texture))
|
||||
for (auto &m : t->multi)
|
||||
glDeleteTextures(1, &m.id);
|
||||
|
||||
for (int i = 0; i < engine->world->textureList.size(); i++)
|
||||
if (engine->world->textureList[i] == texture)
|
||||
engine->world->textureList.erase(engine->world->textureList.begin() + i);
|
||||
}
|
||||
|
||||
|
||||
MultiTexture::MultiTexture(const std::string& name, const char* pathContainingTextures, bool storeOnTextureList) {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(pathContainingTextures))
|
||||
@@ -44,26 +51,17 @@ MultiTexture::MultiTexture(const std::string& name, const char* pathContainingTe
|
||||
this->name = texture.name;
|
||||
} else {multi.push_back(texture);}
|
||||
}
|
||||
|
||||
if (storeOnTextureList)
|
||||
engine->world->textureList.push_back(new MultiTexture(*this));
|
||||
|
||||
//You cannot have more than 7 additional textures, 8 total.
|
||||
//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() > 7)
|
||||
engine->setError(ENGINE_ERROR_CODE::MULTI_TEXTURE_SIZE_EXCEEDS, true);
|
||||
}
|
||||
|
||||
void MultiTexture::erase(GLuint id) {
|
||||
//Delete multi-textures.
|
||||
for (auto & i : multi) {
|
||||
glDeleteTextures(1, &i.id);
|
||||
}
|
||||
|
||||
//Delete primary texture.
|
||||
glDeleteTextures(1, &id);
|
||||
|
||||
//Delete texture entry from world list.
|
||||
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);
|
||||
}
|
||||
}
|
||||
void MultiTexture::erase() {
|
||||
Texture::erase(this);
|
||||
}
|
Reference in New Issue
Block a user