Refactor
This commit is contained in:
@@ -27,6 +27,7 @@ enum class ENGINE_ERROR_CODE: uint8_t {
|
||||
INVALID_SHADER_TYPE = 5,
|
||||
SHADER_NOT_FOUND = 6,
|
||||
MODEL_NOT_FOUND = 7,
|
||||
MULTI_TEXTURE_SIZE_EXCEEDS = 8
|
||||
};
|
||||
|
||||
struct EngineError {
|
||||
@@ -48,12 +49,11 @@ public:
|
||||
//float tickDelta = NULL;
|
||||
bool fullscreen;
|
||||
bool debug = true;
|
||||
bool useVBO = true;
|
||||
bool useVBO = false;
|
||||
uint64_t tickCount = 0;
|
||||
uint64_t frameCount = 0;
|
||||
float frameDelta = 0;
|
||||
GLenum glError = GL_NO_ERROR;
|
||||
GLint maxTextureUnits;
|
||||
float nearPlane = 0.01f;
|
||||
float farPlane = 100.0f;
|
||||
float fov = 75;
|
||||
|
@@ -14,8 +14,8 @@ public:
|
||||
GLuint vertex = NULL;
|
||||
GLuint fragment = NULL;
|
||||
|
||||
Shader(const char* name, const std::string& vertexPath, const std::string& fragmentPath);
|
||||
Shader(const char* name, const std::string& filePath, const GLenum& type);
|
||||
void load(std::string filePath, GLenum type);
|
||||
void link();
|
||||
Shader(std::string& name, std::string filePath, GLenum type);
|
||||
Shader() = default;
|
||||
};
|
||||
|
@@ -37,8 +37,8 @@ public:
|
||||
static void erase(const VertexArray& vArray);
|
||||
virtual void draw();
|
||||
void drawWireframe();
|
||||
VertexArray() = default;
|
||||
VertexArray(const std::string& filename);
|
||||
VertexArray() = default;
|
||||
|
||||
GLuint vbo = 0; //Vertices
|
||||
GLuint ebo = 0; //Indices
|
||||
|
@@ -3,8 +3,8 @@
|
||||
#include <sstream>
|
||||
#include <engine/world.h>
|
||||
#include <thread>
|
||||
#include "types/entity/camera.h"
|
||||
#include "types/entity/skybox.h"
|
||||
#include <types/entity/camera.h>
|
||||
#include <types/entity/skybox.h>
|
||||
|
||||
#include <GL/glu.h>
|
||||
|
||||
@@ -72,7 +72,6 @@ void Engine::initGL()
|
||||
engine->world->setAmbientLightColor(1.0f, 1.0f, 1.0f, 0.0f);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
|
||||
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
|
||||
auto* camera = new Camera();
|
||||
camera->SetPos({0.0f, -2.0f, -5.0f});
|
||||
camera->angle.y = 0.0f;
|
||||
@@ -84,12 +83,7 @@ void Engine::initGL()
|
||||
skybox->angle = {0,0,0};
|
||||
skybox->SetParent(engine->world);
|
||||
|
||||
Shader test;
|
||||
test.name = "test";
|
||||
test.load(engine->workingDir + "/assets/shaders/defaultVertex.glsl", GL_VERTEX_SHADER);
|
||||
test.load(engine->workingDir + "/assets/shaders/defaultFragment.glsl", GL_FRAGMENT_SHADER);
|
||||
test.link();
|
||||
engine->world->shaderList.push_back(test);
|
||||
Shader test("test", engine->workingDir + "/assets/shaders/defaultVertex.glsl", engine->workingDir + "/assets/shaders/defaultFragment.glsl");
|
||||
}
|
||||
|
||||
void Engine::preRender()
|
||||
|
@@ -127,10 +127,16 @@ void Entity::render() {
|
||||
if (engine->world->getGlobalFogMode() == NULL || getGeometry()->name == "skybox")
|
||||
glDisable(GL_FOG);
|
||||
|
||||
if (!multiTexture) {
|
||||
glPushMatrix();
|
||||
glPushMatrix();
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
glRotatef(angle.x, 1.0f, 0.0f, 0.0f);
|
||||
glRotatef(angle.y, 0.0f, 1.0f, 0.0f);
|
||||
glRotatef(angle.z, 0.0f, 0.0f, 1.0f);
|
||||
if (getScale() != 1.0f)
|
||||
glScalef(getScale(), getScale(), getScale());
|
||||
|
||||
//Set the single texture as the active texture.
|
||||
if (!multiTexture) {
|
||||
//Set up the single texture.
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glClientActiveTexture(GL_TEXTURE0);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
@@ -138,18 +144,9 @@ void Entity::render() {
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glBindTexture(GL_TEXTURE_2D, getTexture()->id);
|
||||
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
|
||||
glRotatef(angle.x, 1.0f, 0.0f, 0.0f);
|
||||
glRotatef(angle.y, 0.0f, 1.0f, 0.0f);
|
||||
glRotatef(angle.z, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
if (getScale() != 1.0f)
|
||||
glScalef(getScale(), getScale(), getScale());
|
||||
|
||||
getGeometry()->draw();
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glClientActiveTexture(GL_TEXTURE0);
|
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
@@ -158,20 +155,10 @@ void Entity::render() {
|
||||
|
||||
if (multiTexture) {
|
||||
auto* multi = (MultiTexture*) getTexture();
|
||||
glPushMatrix();
|
||||
|
||||
glTranslatef(position.x, position.y, position.z);
|
||||
glRotatef(angle.x, 1.0f, 0.0f, 0.0f);
|
||||
glRotatef(angle.y, 0.0f, 1.0f, 0.0f);
|
||||
glRotatef(angle.z, 0.0f, 0.0f, 1.0f);
|
||||
if (getScale() != 1.0f)
|
||||
glScalef(getScale(), getScale(), getScale());
|
||||
|
||||
//Set the single texture as the active texture.
|
||||
//Set up the base texture.
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glClientActiveTexture(GL_TEXTURE0);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glBindTexture(GL_TEXTURE_2D, multi->id);
|
||||
@@ -203,52 +190,31 @@ void Entity::render() {
|
||||
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++;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
//Draw code
|
||||
if (!engine->useVBO)
|
||||
glDrawElements(GL_TRIANGLES, getGeometry()->indices.size(), GL_UNSIGNED_INT, &getGeometry()->indices[0]);
|
||||
|
||||
if (engine->useVBO) {
|
||||
//Vertices
|
||||
glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->vbo);
|
||||
glVertexPointer(3, GL_FLOAT, 0, nullptr);
|
||||
|
||||
//Indices
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, getGeometry()->ebo);
|
||||
|
||||
//Draw
|
||||
glDrawElements(GL_TRIANGLES, getGeometry()->indices.size(), GL_UNSIGNED_INT, nullptr);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
getGeometry()->draw();
|
||||
glDisable(GL_BLEND);
|
||||
|
||||
for (int j = 0; j <= i; j++) {
|
||||
glActiveTexture(GL_TEXTURE1 + j);
|
||||
glClientActiveTexture(GL_TEXTURE1 + j);
|
||||
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);
|
||||
@@ -261,10 +227,6 @@ void Entity::render() {
|
||||
getCollider().draw();
|
||||
glColor4f(1, 1, 1,1);
|
||||
}
|
||||
|
||||
if (engine->world->getGlobalFogMode() != NULL)
|
||||
glEnable(GL_FOG);
|
||||
|
||||
glPopMatrix();
|
||||
}
|
||||
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
//TODO Shader::Erase
|
||||
void compilationError(GLuint shader) {
|
||||
GLint success;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
@@ -50,14 +51,14 @@ void Shader::load(std::string filePath, GLenum type) {
|
||||
|
||||
if (type == GL_VERTEX_SHADER) {
|
||||
vertex = glCreateShader(type);
|
||||
glShaderSource(vertex, 1, &source, NULL);
|
||||
glShaderSource(vertex, 1, &source, nullptr);
|
||||
glCompileShader(vertex);
|
||||
compilationError(vertex);
|
||||
}
|
||||
|
||||
if (type == GL_FRAGMENT_SHADER) {
|
||||
fragment = glCreateShader(type);
|
||||
glShaderSource(fragment, 1, &source, NULL);
|
||||
glShaderSource(fragment, 1, &source, nullptr);
|
||||
glCompileShader(fragment);
|
||||
compilationError(fragment);
|
||||
}
|
||||
@@ -76,9 +77,18 @@ void Shader::link() {
|
||||
|
||||
glLinkProgram(id);
|
||||
linkingError(id);
|
||||
engine->world->shaderList.push_back(*this);
|
||||
}
|
||||
|
||||
Shader::Shader(std::string& name, std::string filePath, GLenum type) {
|
||||
Shader::Shader(const char* name, const std::string& vertexPath, const std::string& fragmentPath) {
|
||||
this->name = name;
|
||||
load(filePath, type);
|
||||
load(vertexPath, GL_VERTEX_SHADER);
|
||||
load(fragmentPath, GL_FRAGMENT_SHADER);
|
||||
link();
|
||||
}
|
||||
|
||||
Shader::Shader(const char *name, const std::string &filePath, const GLenum &type) {
|
||||
this->name = name;
|
||||
load(filePath, type);
|
||||
link();
|
||||
}
|
||||
|
@@ -32,7 +32,6 @@ Texture::Texture(std::string& name, const char *filePath, bool storeOnTextureLis
|
||||
|
||||
|
||||
MultiTexture::MultiTexture(const std::string& name, const char* pathContainingTextures, bool storeOnTextureList) {
|
||||
this->name = name;
|
||||
for (const auto& entry : std::filesystem::directory_iterator(pathContainingTextures))
|
||||
if (entry.is_regular_file() && entry.path().extension() == ".png") {
|
||||
Texture texture;
|
||||
@@ -48,9 +47,9 @@ MultiTexture::MultiTexture(const std::string& name, const char* pathContainingTe
|
||||
if (storeOnTextureList)
|
||||
engine->world->textureList.push_back(new MultiTexture(*this));
|
||||
|
||||
//You cannot have more than 7 additional textures.
|
||||
//You cannot have more than 7 additional textures, 8 total.
|
||||
if (multi.size() > 7)
|
||||
engine->setError(ENGINE_ERROR_CODE::TEXTURE_NOT_FOUND, true);
|
||||
engine->setError(ENGINE_ERROR_CODE::MULTI_TEXTURE_SIZE_EXCEEDS, true);
|
||||
}
|
||||
|
||||
void MultiTexture::erase(GLuint id) {
|
||||
|
Reference in New Issue
Block a user