From f7e17fe25f9c6698ea0636e5dab6625e00d7c99c Mon Sep 17 00:00:00 2001 From: Maurice Mohlek Date: Fri, 11 Aug 2017 20:34:47 +0200 Subject: [PATCH] vao, vbo and stuff --- CMakeLists.txt | 2 ++ src/engine/Buffer.cpp | 17 +++++++++-- src/engine/Buffer.h | 5 ++++ src/engine/Camera.cpp | 25 ++++++++++++++++ src/engine/Camera.h | 16 ++++++++++ src/engine/VertexArrayObject.cpp | 20 +++++++++++-- src/engine/VertexArrayObject.h | 2 ++ src/engine/model/3ds/Model3DS.cpp | 10 ++++--- src/engine/model/3ds/Model3DS.h | 14 ++++++--- src/engine/shader/ShaderProgram.cpp | 3 ++ src/engine/shader/ShaderProgram.h | 7 ++++- src/main.cpp | 45 +++++++++++++++++------------ src/shader/MainShader.cpp | 17 +++++++++++ src/shader/MainShader.h | 15 ++++++++++ src/shader/main.vert | 2 +- 15 files changed, 168 insertions(+), 32 deletions(-) create mode 100644 src/engine/Camera.cpp create mode 100644 src/engine/Camera.h create mode 100644 src/shader/MainShader.cpp create mode 100644 src/shader/MainShader.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e9b72f..5b04296 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,10 +9,12 @@ set(SOURCE src/engine/Buffer.cpp src/engine/VertexArrayObject.cpp src/engine/Object.cpp + src/engine/Camera.cpp src/engine/shader/ShaderLoader.cpp src/engine/shader/Shader.cpp src/engine/shader/ShaderProgram.cpp src/engine/model/3ds/Model3DS.cpp + src/shader/MainShader.cpp src/main.cpp ) diff --git a/src/engine/Buffer.cpp b/src/engine/Buffer.cpp index 43c378c..22c48e2 100644 --- a/src/engine/Buffer.cpp +++ b/src/engine/Buffer.cpp @@ -2,10 +2,12 @@ #include +#define MEMCPY + using namespace Engine; Buffer::Buffer(GLenum bufferType) : bufferType(bufferType) { - glGenBuffers(1, &this->bufferId); + glCreateBuffers(1, &this->bufferId); } Buffer::~Buffer() { @@ -33,9 +35,20 @@ void* Buffer::map(GLenum access = GL_READ_WRITE) { void Buffer::pushData(void* data, int size) { this->size = size; + + #ifdef MEMCPY void* addr = map(GL_WRITE_ONLY); - std::memcpy(addr, data, this->size); + unmap(); + #else + if (glNamedBufferSubData) { + glNamedBufferSubData(this->bufferId, 0, size, data); + } + + bind(); + glBufferSubData(this->bufferType, 0, size, data); + #endif + } bool Buffer::unmap() { diff --git a/src/engine/Buffer.h b/src/engine/Buffer.h index 2e8b58b..f2d65a4 100644 --- a/src/engine/Buffer.h +++ b/src/engine/Buffer.h @@ -1,3 +1,6 @@ +#ifndef BUFFER_H +#define BUFFER_H + #include namespace Engine { @@ -24,3 +27,5 @@ namespace Engine { bool unmap(); }; } + +#endif diff --git a/src/engine/Camera.cpp b/src/engine/Camera.cpp new file mode 100644 index 0000000..eeb0dbf --- /dev/null +++ b/src/engine/Camera.cpp @@ -0,0 +1,25 @@ +#include "Camera.h" + +using namespace Engine; + +Camera::Camera() { + glGenBuffers(1, &this->cameraBuffer); +} + +Camera::~Camera() { + glBindBuffer(GL_UNIFORM_BUFFER, this->cameraBuffer); + glDeleteBuffers(1, &this->cameraBuffer); +} + +void Camera::setCamera(glm::vec3 cameraPos, glm::vec3 lookAt) { + if (!this->cameraBuffer) { + return; + } + + float realMatrix[] = {cameraPos.x, cameraPos.y, cameraPos.z, 0.0f, + lookAt.x, lookAt.y, lookAt.z, 0.0f}; + + glBindBuffer(GL_UNIFORM_BUFFER, this->cameraBuffer); + glBufferData(GL_UNIFORM_BUFFER, sizeof(float) * 8, realMatrix, GL_DYNAMIC_DRAW); + glBindBuffer(GL_UNIFORM_BUFFER, 0); +} diff --git a/src/engine/Camera.h b/src/engine/Camera.h new file mode 100644 index 0000000..0348d18 --- /dev/null +++ b/src/engine/Camera.h @@ -0,0 +1,16 @@ +#include + +#include + +namespace Engine { + + class Camera { + private: + GLuint cameraBuffer; + public: + Camera(); + ~Camera(); + + void setCamera(glm::vec3 cameraPos, glm::vec3 lookAt); + }; +} diff --git a/src/engine/VertexArrayObject.cpp b/src/engine/VertexArrayObject.cpp index 07991c9..59e1aba 100644 --- a/src/engine/VertexArrayObject.cpp +++ b/src/engine/VertexArrayObject.cpp @@ -3,6 +3,10 @@ using namespace Engine; VertexArrayObject::VertexArrayObject() { + if (glCreateVertexArrays) { + glCreateVertexArrays(1, &this->vaoId); + return; + } glGenVertexArrays(1, &this->vaoId); } @@ -13,7 +17,19 @@ VertexArrayObject::~VertexArrayObject() { void VertexArrayObject::addBuffer(std::shared_ptr& buffer) { const int index = this->buffers.size(); - glEnableVertexAttribArray(index); - glVertexAttribPointer(index, buffer->size, buffer->dataType, GL_FALSE, buffer->stride, 0); + + if (glEnableVertexArrayAttrib) { + glEnableVertexArrayAttrib(this->vaoId, index); + } else { + bind(); + glEnableVertexAttribArray(index); + } + + buffer->bind(); + glVertexAttribPointer(index, 3, buffer->dataType, GL_FALSE, buffer->stride, 0); this->buffers.push_back(buffer); } + +void VertexArrayObject::bind() { + glBindVertexArray(this->vaoId); +} diff --git a/src/engine/VertexArrayObject.h b/src/engine/VertexArrayObject.h index ef3c01b..6cdd8fa 100644 --- a/src/engine/VertexArrayObject.h +++ b/src/engine/VertexArrayObject.h @@ -16,5 +16,7 @@ namespace Engine { virtual ~VertexArrayObject(); void addBuffer(std::shared_ptr& buffer); + + void bind(); }; } diff --git a/src/engine/model/3ds/Model3DS.cpp b/src/engine/model/3ds/Model3DS.cpp index c56273f..4c56dbb 100644 --- a/src/engine/model/3ds/Model3DS.cpp +++ b/src/engine/model/3ds/Model3DS.cpp @@ -9,6 +9,8 @@ using namespace Engine; Model3DS::Model3DS(std::string&& filename) { + this->vertexVBO = std::make_shared(); + this->normalsVBO = std::make_shared(); this->model = lib3ds_file_load(filename.c_str()); if (!model) { @@ -56,11 +58,11 @@ void Model3DS::createVBO() { unsigned int size = sizeof(Lib3dsVector) * 3 * totalFaces; - this->vertexVBO.stride = sizeof(Lib3dsVector) * 3; - this->vertexVBO.pushData(vertices, size); + this->vertexVBO->stride = sizeof(Lib3dsVector) * 3; + this->vertexVBO->pushData(vertices, size); - this->normalsVBO.stride = sizeof(Lib3dsVector) * 3; - this->normalsVBO.pushData(normals, size); + this->normalsVBO->stride = sizeof(Lib3dsVector) * 3; + this->normalsVBO->pushData(normals, size); delete vertices; delete normals; diff --git a/src/engine/model/3ds/Model3DS.h b/src/engine/model/3ds/Model3DS.h index 8c99b74..9c28e43 100644 --- a/src/engine/model/3ds/Model3DS.h +++ b/src/engine/model/3ds/Model3DS.h @@ -1,9 +1,13 @@ +#ifndef MODEL3DS_H +#define MODEL3DS_H + #include #include -#include "../../Buffer.h" - #include +#include + +#include "../../Buffer.h" namespace Engine { class Model3DS { @@ -12,8 +16,8 @@ namespace Engine { unsigned int getTotalFaces(); public: - Engine::Buffer vertexVBO; - Engine::Buffer normalsVBO; + std::shared_ptr vertexVBO; + std::shared_ptr normalsVBO; Model3DS(std::string&& filename); ~Model3DS(); @@ -21,3 +25,5 @@ namespace Engine { void createVBO(); }; } + +#endif diff --git a/src/engine/shader/ShaderProgram.cpp b/src/engine/shader/ShaderProgram.cpp index 57b0fdc..91e5c7c 100644 --- a/src/engine/shader/ShaderProgram.cpp +++ b/src/engine/shader/ShaderProgram.cpp @@ -28,6 +28,9 @@ void printProgramInfoLog(GLuint obj) { } } +GLint ShaderProgram::getUniformLocation(std::string&& name) { + return glGetUniformLocation(this->programId, name.c_str()); +} void ShaderProgram::link() { this->programId = glCreateProgram(); diff --git a/src/engine/shader/ShaderProgram.h b/src/engine/shader/ShaderProgram.h index 21eef7a..e0656a6 100644 --- a/src/engine/shader/ShaderProgram.h +++ b/src/engine/shader/ShaderProgram.h @@ -1,6 +1,8 @@ #include +#include #include +#include #include #include "Shader.h" @@ -13,10 +15,13 @@ namespace Engine { public: std::vector> shaders; - + std::map uniforms; + ShaderProgram(); virtual ~ShaderProgram(); + GLint getUniformLocation(std::string&& name); + void link(); void use(); void end(); diff --git a/src/main.cpp b/src/main.cpp index 19055a9..4567638 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,11 +1,16 @@ #include #include +#include +#include + #include "engine/Window.h" -#include "engine/shader/ShaderProgram.h" -#include "engine/shader/ShaderLoader.h" +#include "engine/Camera.h" #include "engine/model/3ds/Model3DS.h" +#include "engine/VertexArrayObject.h" + +#include "shader/MainShader.h" using namespace Engine; @@ -14,27 +19,31 @@ int main(int argc, char** argv) { Window* win = new Window(); win->create(); - - ShaderProgram p1; - - ShaderLoader loadMainVertex(GL_VERTEX_SHADER, "shader/main.vert"); - Shader mainVertex(loadMainVertex); - p1.shaders.push_back(std::make_shared(mainVertex)); - - ShaderLoader loadMainFrag(GL_FRAGMENT_SHADER, "shader/main.frag"); - Shader mainFrag(loadMainFrag); - p1.shaders.push_back(std::make_shared(mainFrag)); - p1.link(); + Game::Shader::MainShader mainShader; + + mainShader.link(); Model3DS dragon("resource/dragon.3ds"); - - while (win->loop()) { - glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT); + + float points[] = { + 0.5f, 0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + -0.5f, -0.5f, 0.0f + }; - p1.use(); + VertexArrayObject vao; + std::shared_ptr vbo = std::make_shared(); + vbo->pushData(points, 9 * sizeof(float)); + vbo->stride = 3 * sizeof(float); + + vao.addBuffer(vbo); + + while (win->loop()) { + mainShader.use(); - p1.end(); + vao.bind(); + glDrawArrays(GL_TRIANGLES, 0, 3); } win->close(); diff --git a/src/shader/MainShader.cpp b/src/shader/MainShader.cpp new file mode 100644 index 0000000..83bdbce --- /dev/null +++ b/src/shader/MainShader.cpp @@ -0,0 +1,17 @@ +#include "MainShader.h" + +#include +#include "../engine/shader/Shader.h" +#include "../engine/shader/ShaderLoader.h" + +using namespace Game::Shader; + +MainShader::MainShader() { + Engine::ShaderLoader loadMainFragment(GL_FRAGMENT_SHADER, "shader/main.frag"); + Engine::Shader mainFragment(loadMainFragment); + this->shaders.push_back(std::make_shared(mainFragment)); + + Engine::ShaderLoader loadMainVertex(GL_VERTEX_SHADER, "shader/main.vert"); + Engine::Shader mainVertex(loadMainVertex); + this->shaders.push_back(std::make_shared(mainVertex)); +} diff --git a/src/shader/MainShader.h b/src/shader/MainShader.h new file mode 100644 index 0000000..3e4b84f --- /dev/null +++ b/src/shader/MainShader.h @@ -0,0 +1,15 @@ +#include "../engine/shader/ShaderProgram.h" + +#include + +namespace Game { + namespace Shader { + class MainShader : public Engine::ShaderProgram { + + public: + GLint positionLocation = getUniformLocation("position"); + + MainShader(); + }; + } +} diff --git a/src/shader/main.vert b/src/shader/main.vert index ec618e9..a611a69 100644 --- a/src/shader/main.vert +++ b/src/shader/main.vert @@ -1,6 +1,6 @@ #version 450 core -layout (location = 0) in vec3 aPos; +in vec3 aPos; void main() { gl_Position = vec4(aPos, 1.0);