Refactor & Cleanup

This commit is contained in:
2024-05-20 21:34:55 -04:00
parent 7ac97ae44d
commit 9eaddb812d
5 changed files with 51 additions and 78 deletions

View File

@@ -13,7 +13,7 @@ public:
std::string name;
std::vector<Entity*> usedBy;
GLuint id = 0;
void load(const char* file, bool storeOnTextureList);
void load(const std::string& file, bool storeOnTextureList);
virtual void erase();
void erase(Texture* texture) const;
Texture(std::string& name, const char* filePath, bool storeOnTextureList);

View File

@@ -5,7 +5,7 @@
#include <freeCam.h>
#include <demoSky.h>
int main(int argc, char** argv)
int main()
{
engine->window = new ReWindow::RWindow("Re3D Test Application", 1152, 864, RenderingAPI::OPENGL);
engine->world = new(World);
@@ -42,9 +42,8 @@ int main(int argc, char** argv)
#ifdef WINDOWS_SMH
extern "C" {
int wmain(int argc, wchar_t* argv[])
{
return main(0, nullptr);
int wmain(int argc, wchar_t* argv[]) {
return main();
}
}
#endif

View File

@@ -90,28 +90,12 @@ VertexArray* Entity::getGeometry() {
//Default rendering routine.
void Entity::render() {
bool cameraInside = false;
bool multiTexture = false;
//Camera inside the entity check.
Camera* camera = nullptr;
for (auto& e : engine->world->GetChildren())
if (auto* c = dynamic_cast<Camera*>(e))
camera = c;
if (camera) {
cameraInside = getCollider().contains(camera->position);
}
if (cameraInside)
glCullFace(GL_FRONT);
//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;
if (engine->world->getGlobalFogMode() == NULL || getGeometry()->name == "skybox")
glDisable(GL_FOG);
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glRotatef(angle.x, 1.0f, 0.0f, 0.0f);
@@ -120,7 +104,7 @@ void Entity::render() {
if (getScale() != 1.0f)
glScalef(getScale(), getScale(), getScale());
//Set up primary texture.
//Set up texture.
glActiveTexture(GL_TEXTURE0);
glClientActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
@@ -128,25 +112,23 @@ void Entity::render() {
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, getTexture()->id);
if (!engine->useVBO) {
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x);
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);
if (engine->useVBO)
glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo),
glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
}
if (!multiTexture) {
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
if (!multiTexture)
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND),
getGeometry()->draw();
}
if (multiTexture) {
auto* multi = (MultiTexture*) getTexture();
int i = 0;
GLfloat blendingFactor[3] = {engine->world->getAmbientLightColor().x * 0.2f, engine->world->getAmbientLightColor().y * 0.2f, engine->world->getAmbientLightColor().z * 0.2f};
for(auto& texture : multi->multi) {
glActiveTexture(GL_TEXTURE1 + i);
glClientActiveTexture(GL_TEXTURE1 + i);
@@ -154,30 +136,25 @@ void Entity::render() {
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if (!engine->useVBO) {
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x);
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);
if (engine->useVBO)
glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo),
glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
}
//Texture unit mode.
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
//Makes the lighting work the way you'd think it would.
GLfloat blendingFactor[3] = {engine->world->getAmbientLightColor().x * 0.2f, engine->world->getAmbientLightColor().y * 0.2f, engine->world->getAmbientLightColor().z * 0.2f};
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, blendingFactor);
glBindTexture(GL_TEXTURE_2D, texture.id);
i++;
}
getGeometry()->draw();
glEnable(GL_BLEND);
getGeometry()->draw();
glDisable(GL_BLEND);
//Reset the multi-texture units.
for (int j = 0; j <= i; j++) {
glActiveTexture(GL_TEXTURE1 + j);
glClientActiveTexture(GL_TEXTURE1 + j);
@@ -186,9 +163,9 @@ void Entity::render() {
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
if (cameraInside)
glCullFace(GL_BACK);
}
//Reset texture unit 0.
glActiveTexture(GL_TEXTURE0);
glClientActiveTexture(GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
@@ -218,8 +195,8 @@ bool Entity::isCollidingWith(Entity *entity) {
Texture* Entity::getTexture() {
for (auto& texture : engine->world->textureList)
if (texture->name == name) {
if (std::find(texture->usedBy.begin(), texture->usedBy.end(), this) == texture->usedBy.end())
texture->usedBy.push_back(this);
if (std::find(texture->usedBy.begin(), texture->usedBy.end(), this) == texture->usedBy.end())
texture->usedBy.push_back(this);
return texture;
}
loadTexture();

View File

@@ -2,16 +2,17 @@
#include <SOIL/SOIL.h>
#include <engine/engine.h>
void Texture::load(const char* file, bool storeOnTextureList) {
id = SOIL_load_OGL_texture(file, SOIL_LOAD_RGBA, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
void Texture::load(const std::string& file, bool storeOnTextureList) {
id = SOIL_load_OGL_texture(file.c_str(), SOIL_LOAD_RGBA, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
//If we can't load the missing texture
if (file == "assets/textures/missing.png" && id == 0) {
if (file == "assets/textures/missing.png" && id == 0)
engine->setError(ENGINE_ERROR_CODE::TEXTURE_NOT_FOUND, true);
}
if (id == 0) {
engine->setError(ENGINE_ERROR_CODE::TEXTURE_NOT_FOUND, false);
if (id == 0)
engine->setError(ENGINE_ERROR_CODE::TEXTURE_NOT_FOUND, false),
load("assets/textures/missing.png", false);
}
if (storeOnTextureList)
engine->world->textureList.push_back(new Texture(*this));
}
@@ -51,10 +52,10 @@ MultiTexture::MultiTexture(const std::string& name, const char* pathContainingTe
texture.name = name;
//The base texture *must* go in the first slot.
if (entry.path().filename() == "default.png") {
this->id = texture.id;
if (entry.path().filename() == "default.png")
this->id = texture.id,
this->name = texture.name;
} else {multi.push_back(texture);}
else multi.push_back(texture);
}
if (storeOnTextureList)

View File

@@ -4,10 +4,11 @@
#include <cmath>
#include <Collage/types/model.h>
void VertexArray::load (const std::string& filename) {
Model m(filename);
vertices = m.getVertices();
indices = m.getIndices();
texCoords = m.getTextureInformation()[0].textureCoordinates;
auto m = new Model(filename);
vertices = m->getVertices();
indices = m->getIndices();
texCoords = m->getTextureInformation()[0].textureCoordinates;
delete m;
if (engine->useVBO) {
if (vbo == 0) {
@@ -43,11 +44,10 @@ void VertexArray::draw() {
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if (!engine->useVBO) {
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].x);
glTexCoordPointer(2, GL_FLOAT, sizeof(TextureCoordinate), &texCoords[0].x);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, &indices[0]);
}
if (!engine->useVBO)
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &vertices[0].x), //Vertices
glTexCoordPointer(2, GL_FLOAT, sizeof(TextureCoordinate), &texCoords[0].x), //Texture Coordinates
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, &indices[0]); //Indices
if (engine->useVBO) {
//Vertices
@@ -62,11 +62,10 @@ void VertexArray::draw() {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, nullptr);
//Reset bound buffer.
//Reset selected buffer.
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);
@@ -80,10 +79,9 @@ void VertexArray::drawWireframe() {
int index1 = static_cast<int>(indices[i]);
int index2 = static_cast<int>(indices[i + 1]);
if (index1 >= 0 && index1 < vertices.size() && index2 >= 0 && index2 < vertices.size()) {
glVertex3f(vertices[index1].x, vertices[index1].y, vertices[index1].z);
if (index1 >= 0 && index1 < vertices.size() && index2 >= 0 && index2 < vertices.size())
glVertex3f(vertices[index1].x, vertices[index1].y, vertices[index1].z),
glVertex3f(vertices[index2].x, vertices[index2].y, vertices[index2].z);
}
}
}
glEnd();
@@ -119,8 +117,8 @@ void VertexArray::rotate(const Vector3& angle) {
/*float newX = vertex.x * std::cos(angleZ) - vertex.y * std::sin(angleZ);
float newY = vertex.x * std::sin(angleZ) + vertex.y * std::cos(angleZ);*/
auto result = ComputeVertexRoll(vertex, angleZ);
vertex.x = std::get<0>(result);
vertex.y = std::get<1>(result);
vertex.x = std::get<0>(result);
vertex.y = std::get<1>(result);
}
// Yaw
@@ -151,16 +149,14 @@ VertexArray VertexArray::getBoundingVolume(Vector3 angle) {
}
void VertexArray::erase(const VertexArray& vArray) {
if (engine->useVBO) {
glDeleteBuffers(1, &vArray.vbo);
glDeleteBuffers(1, &vArray.tbo);
if (engine->useVBO)
glDeleteBuffers(1, &vArray.vbo),
glDeleteBuffers(1, &vArray.tbo),
glDeleteBuffers(1, &vArray.ebo);
}
for (int i = 0; i < engine->world->geometryList.size(); i++) {
for (int i = 0; i < engine->world->geometryList.size(); i++)
if (engine->world->geometryList[i].name == vArray.name)
engine->world->geometryList.erase(engine->world->geometryList.begin() + i);
}
}
void VertexArray::erase(VertexArray* erasee) const {