From a44392f1cc7612a001f4f40079c44d5ae529aa86 Mon Sep 17 00:00:00 2001 From: SArpnt <36542932+SArpnt@users.noreply.github.com> Date: Wed, 17 Feb 2021 08:26:55 -0500 Subject: [PATCH 1/4] fixed todo typos --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9b15d264..7ddbe8e4 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,8 @@ Pull requests are welcome. For major changes, please open an issue first to disc - [x] 3D Cube - [x] 3D Transforms - [x] WASD - - [x] basic input handling - - [x] Mouse controlled camers + - [x] Basic input handling + - [x] Mouse controlled camera - [x] Window Class - [x] Resize context on window resize - [x] Time Class @@ -92,4 +92,4 @@ Pull requests are welcome. For major changes, please open an issue first to disc - [ ] Scene Graph - [ ] XML based scene graph import - [ ] Test ON Windows - - [ ] Port to Emscripten + - [ ] Port to Emscripten \ No newline at end of file From 4b1818ca09ecf07cf9674009f193716922abda53 Mon Sep 17 00:00:00 2001 From: SArpnt <36542932+SArpnt@users.noreply.github.com> Date: Wed, 17 Feb 2021 10:05:05 -0500 Subject: [PATCH 2/4] formatting again --- engine/ce_assets.h | 7 +-- engine/core/time.cpp | 5 ++- engine/core/tpnt_log.h | 4 +- engine/main.cpp | 20 ++++----- engine/managers/asset_manager.cpp | 5 +-- engine/managers/asset_manager.h | 5 ++- engine/math/transform.cpp | 8 ++-- engine/math/transform.h | 16 +++---- engine/rendering/camera.cpp | 27 +++++------ engine/rendering/camera.h | 14 +++--- engine/rendering/material.cpp | 3 +- engine/rendering/rendering_engine.cpp | 64 ++++++++++----------------- engine/rendering/rendering_engine.h | 13 +++--- engine/rendering/shader.cpp | 3 +- engine/rendering/shader.h | 1 - engine/rendering/texture.cpp | 2 +- engine/rendering/texture.h | 1 - 17 files changed, 80 insertions(+), 118 deletions(-) diff --git a/engine/ce_assets.h b/engine/ce_assets.h index 78753747..3b33619d 100644 --- a/engine/ce_assets.h +++ b/engine/ce_assets.h @@ -1,5 +1,4 @@ -#ifndef _CE_ASSETS_H_ -#define _CE_ASSETS_H_ +#pragma once #include #include @@ -31,6 +30,4 @@ namespace ce { specularTex = "missing.png", shader = ""; }; -} - -#endif // !_CE_ASSETS_H_ +} \ No newline at end of file diff --git a/engine/core/time.cpp b/engine/core/time.cpp index a2a8e747..3627a0a0 100644 --- a/engine/core/time.cpp +++ b/engine/core/time.cpp @@ -5,7 +5,8 @@ float ce::Time::getTime() { return SDL_GetTicks() / 1000.0f; } -ce::Time::Time() : m_last(getTime()), m_now(0), m_dt(0), m_fps(0) {} +ce::Time::Time() + : m_last(getTime()), m_now(0), m_dt(0), m_fps(0) {} ce::Time::~Time() {} @@ -14,4 +15,4 @@ void ce::Time::update() { m_dt = m_now - m_last; m_fps = 1 / m_dt; m_last = m_now; -} +} \ No newline at end of file diff --git a/engine/core/tpnt_log.h b/engine/core/tpnt_log.h index b455a6f8..bcefc5b5 100644 --- a/engine/core/tpnt_log.h +++ b/engine/core/tpnt_log.h @@ -24,7 +24,7 @@ #define TPNT_LOGGER_FORMAT "[%s][%s:%d|%s]: %s\n" #define TPNT_LOGGER_FILE strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__ #define TPNT_LOGGER_CONTEXT TPNT_LOGGER_FILE, __LINE__, __FUNCTION__ -#define TPNT_LOG(L, X) printf(TPNT_LOGGER_FORMAT, TPNT_LOGGER_STR(L), TPNT_LOGGER_CONTEXT,TPNT_LOGGER_STR(X)) +#define TPNT_LOG(L, X) printf(TPNT_LOGGER_FORMAT, TPNT_LOGGER_STR(L), TPNT_LOGGER_CONTEXT, TPNT_LOGGER_STR(X)) #define TPNT_LOG_RESET "\033[0m" #define TPNT_LOG_SUCCESS "\033[1;32m" #define TPNT_LOG_WARN "\033[1;33m" @@ -33,4 +33,4 @@ #define LOG_INFO(X) TPNT_LOG("INFO", X) #define LOG_WARN(X) TPNT_LOG(TPNT_LOG_WARN + "WARN" + TPNT_LOG_RESET, X) #define LOG_ERROR(X) TPNT_LOG(TPNT_LOG_ERROR + "ERROR" + TPNT_LOG_RESET, X) -#define LOG_SUCCESS(X) TPNT_LOG(TPNT_LOG_SUCCESS + "SUCCESS" + TPNT_LOG_RESET, X) +#define LOG_SUCCESS(X) TPNT_LOG(TPNT_LOG_SUCCESS + "SUCCESS" + TPNT_LOG_RESET, X) \ No newline at end of file diff --git a/engine/main.cpp b/engine/main.cpp index 4a43b146..6969f6b7 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -91,7 +91,7 @@ int main(int argc, char* argv[]) { ce::RenderingEngine* renderingEngine = new ce::RenderingEngine(); renderingEngine->setFOV(75.0f); renderingEngine->setSize(window->getWindowSize()); - renderingEngine->setClipRange(0.1f,100.0f); + renderingEngine->setClipRange(0.1f, 100.0f); ce::Transform* transform = new ce::Transform(); ce::Mesh* mesh = new ce::Mesh(vertices, vertexCount, indices, indexCount); @@ -99,10 +99,10 @@ int main(int argc, char* argv[]) { ce::Material* material = new ce::Material(shader); ce::Material* planeMaterial = new ce::Material(shader); material->setTexture(new ce::Texture("uv-map.png")); - - ce::Mesh* plane = new ce::Mesh(planeVertices,planeIndexCount,planeIndices,planeVertexCount); + + ce::Mesh* plane = new ce::Mesh(planeVertices, planeIndexCount, planeIndices, planeVertexCount); ce::Transform* planeTransform = new ce::Transform(); - + float mouseSensitivity = 0.1f; ce::Camera* camera = new ce::Camera(); // Seperate so i can put in a player class later @@ -159,10 +159,8 @@ int main(int argc, char* argv[]) { if ((event.key.keysym.sym == SDLK_a && cameraVelocity.x < 0) || (event.key.keysym.sym == SDLK_d && cameraVelocity.x > 0)) cameraVelocity.x = 0; - if ((event.key.keysym.sym == SDLK_SPACE && - cameraVelocity.y > 0) || - (event.key.keysym.sym == SDLK_LSHIFT && - cameraVelocity.y < 0)) + if ((event.key.keysym.sym == SDLK_SPACE && cameraVelocity.y > 0) || + (event.key.keysym.sym == SDLK_LSHIFT && cameraVelocity.y < 0)) cameraVelocity.y = 0; break; } @@ -180,7 +178,7 @@ int main(int argc, char* argv[]) { transform->roll(25.0f * time->getDeltaTime()); transform->yaw(50.0f * time->getDeltaTime()); transform->pitch(100.0f * time->getDeltaTime()); - + // Camera glm::vec3 cameraFront = camera->getTransform()->getForward(), @@ -190,10 +188,10 @@ int main(int argc, char* argv[]) { (cameraFront * cameraVelocity.z) + (cameraRight * cameraVelocity.x) + (cameraUp * cameraVelocity.y)); - + /* Render */ renderingEngine->registerCommand({transform, material, mesh, mesh->GetIndexCount()}); - renderingEngine->registerCommand({planeTransform,planeMaterial,plane,plane->GetIndexCount()}); + renderingEngine->registerCommand({planeTransform, planeMaterial, plane, plane->GetIndexCount()}); renderingEngine->render(); window->swapBuffers(); diff --git a/engine/managers/asset_manager.cpp b/engine/managers/asset_manager.cpp index 52db06ab..69f8bdeb 100644 --- a/engine/managers/asset_manager.cpp +++ b/engine/managers/asset_manager.cpp @@ -25,8 +25,7 @@ std::string ce::AssetManager::load_text_file(std::string path) { text = filestream.str(); LOG_SUCCESS("LOADED_FILE: " + path); } catch (std::fstream::failure e) { - LOG_ERROR( - "FILE_NOT_SUCCESSFULLY_READ: (" + path + ") " + std::string(e.what())); + LOG_ERROR("FILE_NOT_SUCCESSFULLY_READ: (" + path + ") " + std::string(e.what())); } return text; } @@ -65,4 +64,4 @@ ce::TextureFile ce::AssetManager::getTextureFile(std::string filename) { void ce::AssetManager::freeTextureFile(ce::TextureFile textureFile) { stbi_image_free(textureFile.data); -} +} \ No newline at end of file diff --git a/engine/managers/asset_manager.h b/engine/managers/asset_manager.h index 5dd47861..0638cfb2 100644 --- a/engine/managers/asset_manager.h +++ b/engine/managers/asset_manager.h @@ -7,8 +7,9 @@ namespace ce { class AssetManager { private: - inline static const std::string SHADER_FOLDER = "shaders", - TEXTURE_FOLDER = "textures"; + inline static const std::string + SHADER_FOLDER = "shaders", + TEXTURE_FOLDER = "textures"; static std::string load_text_file(std::string file); diff --git a/engine/math/transform.cpp b/engine/math/transform.cpp index ca6ee491..51fa07ef 100644 --- a/engine/math/transform.cpp +++ b/engine/math/transform.cpp @@ -31,15 +31,13 @@ void ce::Transform::sendToShader(ce::Shader* shader) { shader->setMat4("transform.model", getMatrix()); } -glm::vec3 ce::Transform::getForward() -{ +glm::vec3 ce::Transform::getForward() { float yaw = glm::radians(getYaw()), pitch = glm::radians(getPitch()); glm::vec3 dir( - cos(yaw)*cos(pitch), + cos(yaw) * cos(pitch), sin(pitch), - sin(yaw)*cos(pitch) - ); + sin(yaw) * cos(pitch)); return glm::normalize(dir); } diff --git a/engine/math/transform.h b/engine/math/transform.h index 5ff49605..6237039a 100644 --- a/engine/math/transform.h +++ b/engine/math/transform.h @@ -15,19 +15,15 @@ namespace ce { glm::vec3 m_pos, m_rot, m_scale; public: - static glm::vec3 GetGlobalUp(){return glm::vec3(0.0f,1.0f,0.0f);} - + static glm::vec3 GetGlobalUp() { return glm::vec3(0.0f, 1.0f, 0.0f); } + Transform(); ~Transform(); glm::vec3 getPosition() { return m_pos; } void setPosition(glm::vec3 pos) { m_pos = pos; } - void setPosition(float x, float y, float z) { - setPosition(glm::vec3(x, y, z)); - } + void setPosition(float x, float y, float z) { setPosition(glm::vec3(x, y, z)); } void translate(glm::vec3 delta) { m_pos += delta; } - void translate(float x, float y, float z) { - translate(glm::vec3(x, y, z)); - } + void translate(float x, float y, float z) { translate(glm::vec3(x, y, z)); } void setPitch(float pitch) { m_rot.x = pitch; } void pitch(float delta) { m_rot.x += delta; } @@ -43,9 +39,7 @@ namespace ce { glm::vec3 getRotation() { return m_rot; } void setRotation(glm::vec3 rot) { m_rot = rot; } - void setRotation(float x, float y, float z) { - setRotation(glm::vec3(x, y, z)); - } + void setRotation(float x, float y, float z) { setRotation(glm::vec3(x, y, z)); } void rotate(glm::vec3 delta) { m_rot += delta; } void rotate(float x, float y, float z) { rotate(glm::vec3(x, y, z)); } diff --git a/engine/rendering/camera.cpp b/engine/rendering/camera.cpp index 20c77678..88b89a27 100644 --- a/engine/rendering/camera.cpp +++ b/engine/rendering/camera.cpp @@ -1,44 +1,37 @@ #include "camera.h" ce::Camera::Camera() -:m_transform(NULL) -{ + : m_transform(NULL) { m_transform = new Transform(); } -ce::Camera::~Camera() -{ +ce::Camera::~Camera() { delete m_transform; } -glm::mat4 ce::Camera::getView() -{ +glm::mat4 ce::Camera::getView() { glm::vec3 pos = m_transform->getPosition(), forward = m_transform->getForward(); - return glm::lookAt(pos,pos+forward,ce::Transform::GetGlobalUp()); + return glm::lookAt(pos, pos + forward, ce::Transform::GetGlobalUp()); } -glm::vec3 ce::Camera::getRight() -{ +glm::vec3 ce::Camera::getRight() { glm::vec3 forward = m_transform->getForward(), up = ce::Transform::GetGlobalUp(); - return glm::normalize(glm::cross(forward,up)); + return glm::normalize(glm::cross(forward, up)); } -void ce::Camera::sendToShader(ce::Shader* shader) -{ - shader->setMat4("transform.view",getView()); +void ce::Camera::sendToShader(ce::Shader* shader) { + shader->setMat4("transform.view", getView()); } -void ce::Camera::boundPitch() -{ +void ce::Camera::boundPitch() { float cameraPitch = m_transform->getPitch(); if (cameraPitch > 89.9f) // TODO: fix broken 90 degree pitch cameraPitch = 89.9f; if (cameraPitch < -89.9f) cameraPitch = -89.9f; m_transform->setPitch(cameraPitch); -} - +} \ No newline at end of file diff --git a/engine/rendering/camera.h b/engine/rendering/camera.h index cb7b3fa3..23c6437c 100644 --- a/engine/rendering/camera.h +++ b/engine/rendering/camera.h @@ -5,18 +5,20 @@ namespace ce { class Camera { - private: + private: ce::Transform* m_transform; - float speed, - fov; - public: + float + speed, + fov; + + public: Camera(); ~Camera(); - + glm::mat4 getView(); glm::vec3 getRight(); void sendToShader(ce::Shader* shader); - Transform* getTransform(){return m_transform;} + Transform* getTransform() { return m_transform; } void boundPitch(); }; } diff --git a/engine/rendering/material.cpp b/engine/rendering/material.cpp index 7c69a3c2..33aed684 100644 --- a/engine/rendering/material.cpp +++ b/engine/rendering/material.cpp @@ -6,8 +6,7 @@ ce::Material::Material(Shader* shader) update(); } -ce::Material::~Material() -{ +ce::Material::~Material() { delete m_shader; delete m_texture; } diff --git a/engine/rendering/rendering_engine.cpp b/engine/rendering/rendering_engine.cpp index fa8c2866..9b78d78c 100644 --- a/engine/rendering/rendering_engine.cpp +++ b/engine/rendering/rendering_engine.cpp @@ -1,44 +1,39 @@ #include "rendering_engine.h" #include -void ce::RenderingEngine::clear() -{ +void ce::RenderingEngine::clear() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } -void ce::RenderingEngine::render(unsigned count) -{ - glDrawElements(GL_TRIANGLES,count,GL_UNSIGNED_INT,0); +void ce::RenderingEngine::render(unsigned count) { + glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, 0); } -void ce::RenderingEngine::bind(RenderCommand command) -{ +void ce::RenderingEngine::bind(RenderCommand command) { // Update Shader Values command.material->update(); - + command.mesh->sendToShader(command.material->getShader()); command.transform->sendToShader(command.material->getShader()); - command.material->getShader()->setMat4("transform.proj",getProjection()); + command.material->getShader()->setMat4("transform.proj", getProjection()); m_camera->sendToShader(command.material->getShader()); - + // Bind Things command.material->bind(); command.mesh->bind(); } -void ce::RenderingEngine::unbind(RenderCommand command) -{ +void ce::RenderingEngine::unbind(RenderCommand command) { command.mesh->unbind(); command.material->unbind(); } ce::RenderingEngine::RenderingEngine() -:m_aspectRatio(0), -m_fov(0), -m_near(0), -m_far(0), -m_camera(NULL) -{ + : m_aspectRatio(0), + m_fov(0), + m_near(0), + m_far(0), + m_camera(NULL) { /* * GLEW */ @@ -57,43 +52,32 @@ m_camera(NULL) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - + setClearColor(glm::vec4()); } -ce::RenderingEngine::~RenderingEngine() -{ - -} +ce::RenderingEngine::~RenderingEngine() {} -void ce::RenderingEngine::setClearColor(glm::vec4 color) -{ - glClearColor(color.r,color.g,color.b,color.a); +void ce::RenderingEngine::setClearColor(glm::vec4 color) { + glClearColor(color.r, color.g, color.b, color.a); } -void ce::RenderingEngine::setSize(glm::vec2 size) -{ +void ce::RenderingEngine::setSize(glm::vec2 size) { glViewport(0, 0, size.x, size.y); - m_aspectRatio = size.x/size.y; + m_aspectRatio = size.x / size.y; } -glm::mat4 ce::RenderingEngine::getProjection() -{ - return glm::perspective(m_fov,m_aspectRatio, m_near, m_far); +glm::mat4 ce::RenderingEngine::getProjection() { + return glm::perspective(m_fov, m_aspectRatio, m_near, m_far); } - - -void ce::RenderingEngine::render() -{ - +void ce::RenderingEngine::render() { clear(); - for(int i=0;i #include #include #include -#include "camera.h" namespace ce { struct RenderCommand { @@ -19,7 +18,7 @@ namespace ce { class RenderingEngine { private: float m_aspectRatio, m_fov, m_near, m_far; - + Camera* m_camera; std::vector m_commands = {}; @@ -40,7 +39,7 @@ namespace ce { } void setSize(glm::vec2 size); void setClearColor(glm::vec4 color); - void setCamera(Camera* camera){m_camera = camera;} + void setCamera(Camera* camera) { m_camera = camera; } glm::mat4 getProjection(); @@ -49,6 +48,4 @@ namespace ce { } void render(); }; -} - -#endif // _RENDER_ENGINE_H_ +} \ No newline at end of file diff --git a/engine/rendering/shader.cpp b/engine/rendering/shader.cpp index 025c49b3..cae405a8 100644 --- a/engine/rendering/shader.cpp +++ b/engine/rendering/shader.cpp @@ -85,7 +85,8 @@ int ce::Shader::registerUniform(std::string name) { return location; } -ce::Shader::Shader(const char* name) : program(glCreateProgram()) { +ce::Shader::Shader(const char* name) + : program(glCreateProgram()) { ShaderFile shaderFile = ce::AssetManager::getShaderFile(name); int vertexShader = 0; diff --git a/engine/rendering/shader.h b/engine/rendering/shader.h index 9d4080ba..6ef522e1 100644 --- a/engine/rendering/shader.h +++ b/engine/rendering/shader.h @@ -11,7 +11,6 @@ using namespace glm; namespace ce { - class Shader { private: inline static int MIN_LOC = 0; diff --git a/engine/rendering/texture.cpp b/engine/rendering/texture.cpp index 6f0d13e2..e34903e0 100644 --- a/engine/rendering/texture.cpp +++ b/engine/rendering/texture.cpp @@ -47,4 +47,4 @@ void ce::Texture::unbind() { glDisable(type); glActiveTexture(0); glBindTexture(this->type, 0); -} +} \ No newline at end of file diff --git a/engine/rendering/texture.h b/engine/rendering/texture.h index 60e965d0..6daa61a1 100644 --- a/engine/rendering/texture.h +++ b/engine/rendering/texture.h @@ -5,7 +5,6 @@ #include namespace ce { - class Texture { private: GLuint texture; From f329025c5e672917c86dcbd9356b2a293eb389cc Mon Sep 17 00:00:00 2001 From: SArpnt <36542932+SArpnt@users.noreply.github.com> Date: Wed, 17 Feb 2021 12:37:33 -0500 Subject: [PATCH 3/4] partially dealt with buffer issues (incomplete) --- engine/main.cpp | 8 ++-- engine/rendering/mesh.cpp | 53 +++++++++++++++------------ engine/rendering/mesh.h | 6 +-- engine/rendering/rendering_engine.cpp | 10 +++-- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/engine/main.cpp b/engine/main.cpp index 6969f6b7..f4a91181 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -97,11 +97,11 @@ int main(int argc, char* argv[]) { ce::Mesh* mesh = new ce::Mesh(vertices, vertexCount, indices, indexCount); ce::Shader* shader = new ce::Shader("basic"); ce::Material* material = new ce::Material(shader); - ce::Material* planeMaterial = new ce::Material(shader); material->setTexture(new ce::Texture("uv-map.png")); - ce::Mesh* plane = new ce::Mesh(planeVertices, planeIndexCount, planeIndices, planeVertexCount); - ce::Transform* planeTransform = new ce::Transform(); + //ce::Material* planeMaterial = new ce::Material(shader); + //ce::Mesh* plane = new ce::Mesh(planeVertices, planeIndexCount, planeIndices, planeVertexCount); + //ce::Transform* planeTransform = new ce::Transform(); float mouseSensitivity = 0.1f; ce::Camera* camera = new ce::Camera(); @@ -191,7 +191,7 @@ int main(int argc, char* argv[]) { /* Render */ renderingEngine->registerCommand({transform, material, mesh, mesh->GetIndexCount()}); - renderingEngine->registerCommand({planeTransform, planeMaterial, plane, plane->GetIndexCount()}); + //renderingEngine->registerCommand({planeTransform, planeMaterial, plane, plane->GetIndexCount()}); renderingEngine->render(); window->swapBuffers(); diff --git a/engine/rendering/mesh.cpp b/engine/rendering/mesh.cpp index 0270ab77..5ba39793 100644 --- a/engine/rendering/mesh.cpp +++ b/engine/rendering/mesh.cpp @@ -1,4 +1,5 @@ #include "mesh.h" +#include "iostream" #include "shader.h" #include "vertex.h" @@ -7,46 +8,50 @@ ce::Mesh::Mesh(Vertex* vertexArray, const unsigned vertexCount, GLuint* indexArray, const unsigned indexCount) : vertexCount(vertexCount), indexCount(indexCount), VAO(0), VBO(0), EBO(0) { glGenVertexArrays(1, &VAO); - bind(); - this->initVAO(vertexArray, indexArray); - unbind(); + std::cout << "created VAO" << VAO << std::endl; + glBindVertexArray(VAO); + initVAO(vertexArray, indexArray); + glBindVertexArray(0); } ce::Mesh::~Mesh() { glDeleteVertexArrays(1, &VAO); - glDeleteBuffers(1, &this->VBO); - glDeleteBuffers(1, &this->EBO); + glDeleteBuffers(1, &VBO); + glDeleteBuffers(1, &EBO); } - +/* void ce::Mesh::bind() { glBindVertexArray(VAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); } void ce::Mesh::unbind() { glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } - +*/ void ce::Mesh::sendToShader(ce::Shader* shader) { - bind(); - shader->vertexAttribPointer("aPos", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), - (GLvoid*)offsetof(Vertex, position)); - shader->vertexAttribPointer("aColor", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), - (GLvoid*)offsetof(Vertex, color)); - shader->vertexAttribPointer("aTexCoord", 2, GL_FLOAT, GL_FALSE, - sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoord)); - unbind(); + glBindVertexArray(VAO); + shader->vertexAttribPointer("aPos", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); + shader->vertexAttribPointer("aColor", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color)); + shader->vertexAttribPointer("aTexCoord", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoord)); + glBindVertexArray(0); } void ce::Mesh::initVAO(Vertex* vertexArray, GLuint* indexArray) { - glGenBuffers(1, &this->VBO); - glBindBuffer(GL_ARRAY_BUFFER, this->VBO); - glBufferData(GL_ARRAY_BUFFER, this->vertexCount * sizeof(Vertex), - vertexArray, GL_STATIC_DRAW); + glGenBuffers(1, &VBO); + std::cout << "created VBO" << VBO << std::endl; + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(Vertex), vertexArray, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); if (indexCount > 0) { - glGenBuffers(1, &this->EBO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indexCount * sizeof(GLuint), - indexArray, GL_STATIC_DRAW); + glGenBuffers(1, &EBO); + std::cout << "created EBO" << EBO << std::endl; + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(GLuint), indexArray, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -} +} \ No newline at end of file diff --git a/engine/rendering/mesh.h b/engine/rendering/mesh.h index 9c928741..1f9c1f64 100644 --- a/engine/rendering/mesh.h +++ b/engine/rendering/mesh.h @@ -6,20 +6,20 @@ namespace ce { class Mesh { - private: unsigned vertexCount, indexCount; - GLuint VAO, VBO, EBO; + void initVAO(Vertex* vertexArray, GLuint* indexArray); public: + GLuint VAO, VBO, EBO; Mesh(Vertex* vertexArray, const unsigned vertexCount, GLuint* indexArray = NULL, const unsigned indexCount = 0); ~Mesh(); void sendToShader(class Shader* shader); unsigned GetIndexCount() { return indexCount; }; - void bind(), unbind(); + //void bind(), unbind(); }; } \ No newline at end of file diff --git a/engine/rendering/rendering_engine.cpp b/engine/rendering/rendering_engine.cpp index 9b78d78c..75ba2783 100644 --- a/engine/rendering/rendering_engine.cpp +++ b/engine/rendering/rendering_engine.cpp @@ -19,12 +19,16 @@ void ce::RenderingEngine::bind(RenderCommand command) { m_camera->sendToShader(command.material->getShader()); // Bind Things + glBindVertexArray(command.mesh->VAO); + glBindBuffer(GL_ARRAY_BUFFER, command.mesh->VBO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, command.mesh->EBO); command.material->bind(); - command.mesh->bind(); } void ce::RenderingEngine::unbind(RenderCommand command) { - command.mesh->unbind(); + glBindVertexArray(0); // unbind mesh + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + //glBindBuffer(GL_ARRAY_BUFFER, 0); // TODO: this causes issues still command.material->unbind(); } @@ -77,7 +81,7 @@ void ce::RenderingEngine::render() { RenderCommand command = m_commands[i]; bind(command); render(command.points); - unbind(command); + unbind(command); // TODO: should this go outside the for loop? } m_commands.clear(); } \ No newline at end of file From 569c3cf679420af32babc9d733f4d03bb18f0b8e Mon Sep 17 00:00:00 2001 From: SArpnt <36542932+SArpnt@users.noreply.github.com> Date: Wed, 17 Feb 2021 12:59:44 -0500 Subject: [PATCH 4/4] fully working cube (plane not working) --- engine/main.cpp | 8 ++++---- engine/rendering/mesh.cpp | 22 +++++++++++----------- engine/rendering/mesh.h | 7 +++---- engine/rendering/rendering_engine.cpp | 11 ++++------- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/engine/main.cpp b/engine/main.cpp index f4a91181..212c312e 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -99,9 +99,9 @@ int main(int argc, char* argv[]) { ce::Material* material = new ce::Material(shader); material->setTexture(new ce::Texture("uv-map.png")); - //ce::Material* planeMaterial = new ce::Material(shader); - //ce::Mesh* plane = new ce::Mesh(planeVertices, planeIndexCount, planeIndices, planeVertexCount); - //ce::Transform* planeTransform = new ce::Transform(); + ce::Material* planeMaterial = new ce::Material(shader); + ce::Mesh* plane = new ce::Mesh(planeVertices, planeIndexCount, planeIndices, planeVertexCount); + ce::Transform* planeTransform = new ce::Transform(); float mouseSensitivity = 0.1f; ce::Camera* camera = new ce::Camera(); @@ -191,7 +191,7 @@ int main(int argc, char* argv[]) { /* Render */ renderingEngine->registerCommand({transform, material, mesh, mesh->GetIndexCount()}); - //renderingEngine->registerCommand({planeTransform, planeMaterial, plane, plane->GetIndexCount()}); + renderingEngine->registerCommand({planeTransform, planeMaterial, plane, plane->GetIndexCount()}); renderingEngine->render(); window->swapBuffers(); diff --git a/engine/rendering/mesh.cpp b/engine/rendering/mesh.cpp index 5ba39793..ece7d05f 100644 --- a/engine/rendering/mesh.cpp +++ b/engine/rendering/mesh.cpp @@ -1,5 +1,4 @@ #include "mesh.h" -#include "iostream" #include "shader.h" #include "vertex.h" @@ -8,48 +7,49 @@ ce::Mesh::Mesh(Vertex* vertexArray, const unsigned vertexCount, GLuint* indexArray, const unsigned indexCount) : vertexCount(vertexCount), indexCount(indexCount), VAO(0), VBO(0), EBO(0) { glGenVertexArrays(1, &VAO); - std::cout << "created VAO" << VAO << std::endl; glBindVertexArray(VAO); initVAO(vertexArray, indexArray); glBindVertexArray(0); } - ce::Mesh::~Mesh() { glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &EBO); } -/* + void ce::Mesh::bind() { glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); } - void ce::Mesh::unbind() { glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -*/ -void ce::Mesh::sendToShader(ce::Shader* shader) { - glBindVertexArray(VAO); + +void ce::Mesh::sendToShader(ce::Shader* shader, bool bind) { + if (bind) { + glBindVertexArray(VAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + } shader->vertexAttribPointer("aPos", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); shader->vertexAttribPointer("aColor", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color)); shader->vertexAttribPointer("aTexCoord", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoord)); - glBindVertexArray(0); + if (bind) { + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } } void ce::Mesh::initVAO(Vertex* vertexArray, GLuint* indexArray) { glGenBuffers(1, &VBO); - std::cout << "created VBO" << VBO << std::endl; glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(Vertex), vertexArray, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); if (indexCount > 0) { glGenBuffers(1, &EBO); - std::cout << "created EBO" << EBO << std::endl; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(GLuint), indexArray, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); diff --git a/engine/rendering/mesh.h b/engine/rendering/mesh.h index 1f9c1f64..cbb42367 100644 --- a/engine/rendering/mesh.h +++ b/engine/rendering/mesh.h @@ -8,18 +8,17 @@ namespace ce { class Mesh { private: unsigned vertexCount, indexCount; - + GLuint VAO, VBO, EBO; void initVAO(Vertex* vertexArray, GLuint* indexArray); public: - GLuint VAO, VBO, EBO; Mesh(Vertex* vertexArray, const unsigned vertexCount, GLuint* indexArray = NULL, const unsigned indexCount = 0); ~Mesh(); - void sendToShader(class Shader* shader); + void sendToShader(class Shader* shader, bool bind = true); unsigned GetIndexCount() { return indexCount; }; - //void bind(), unbind(); + void bind(), unbind(); }; } \ No newline at end of file diff --git a/engine/rendering/rendering_engine.cpp b/engine/rendering/rendering_engine.cpp index 75ba2783..2a2a76cc 100644 --- a/engine/rendering/rendering_engine.cpp +++ b/engine/rendering/rendering_engine.cpp @@ -10,25 +10,22 @@ void ce::RenderingEngine::render(unsigned count) { } void ce::RenderingEngine::bind(RenderCommand command) { - // Update Shader Values + // Update Shader Values TODO: shouldn't this be somewhere else instead of the bind command? command.material->update(); + // TODO: get rid of unneccecary binding command.mesh->sendToShader(command.material->getShader()); command.transform->sendToShader(command.material->getShader()); command.material->getShader()->setMat4("transform.proj", getProjection()); m_camera->sendToShader(command.material->getShader()); // Bind Things - glBindVertexArray(command.mesh->VAO); - glBindBuffer(GL_ARRAY_BUFFER, command.mesh->VBO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, command.mesh->EBO); + command.mesh->bind(); command.material->bind(); } void ce::RenderingEngine::unbind(RenderCommand command) { - glBindVertexArray(0); // unbind mesh - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - //glBindBuffer(GL_ARRAY_BUFFER, 0); // TODO: this causes issues still + command.mesh->unbind(); command.material->unbind(); }