From eb0e7f7a51701d2ddfcf3486ddf5f3430ba273b6 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Wed, 4 May 2022 15:27:42 +0200 Subject: [PATCH] Abstracted away the creation of buffers Added a transform Updated the TODO.md Updated default shaders to include the apropriate three 4x4 matrices to render in 3D --- MyGraphicsEngine/Buffer.cpp | 47 +++++++++++ .../include/MyGraphicsEngine/Buffer.h | 19 +++++ .../include/MyGraphicsEngine/Transform.h | 8 ++ MyGraphicsEngine/shaders/fragment.shader | 4 +- MyGraphicsEngine/shaders/vertex.shader | 10 ++- SandboxApplication/Sandbox.cpp | 77 ++++++++++--------- TODO.md | 5 +- imgui.ini | 11 +-- 8 files changed, 131 insertions(+), 50 deletions(-) create mode 100644 MyGraphicsEngine/Buffer.cpp create mode 100644 MyGraphicsEngine/include/MyGraphicsEngine/Buffer.h create mode 100644 MyGraphicsEngine/include/MyGraphicsEngine/Transform.h diff --git a/MyGraphicsEngine/Buffer.cpp b/MyGraphicsEngine/Buffer.cpp new file mode 100644 index 0000000..9fdba04 --- /dev/null +++ b/MyGraphicsEngine/Buffer.cpp @@ -0,0 +1,47 @@ +#include + + +int Buffer::getBufferID() { + return id; +} + +void Buffer::createBuffer() { + glGenBuffers(1, (GLuint*) &id); +} + +void Buffer::setBufferData(void* data, size_t dataSize, bool elementBuffer = false ) { + + if (elementBuffer) { + glBufferData(GL_ELEMENT_ARRAY_BUFFER, dataSize, data, GL_STATIC_DRAW); + + } + else { + glBufferData(GL_ARRAY_BUFFER, dataSize, data, GL_STATIC_DRAW); + } + +} + +void Buffer::Bind(bool elementBuffer = false ) { + if (elementBuffer) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); + + } + else { + glBindBuffer(GL_ARRAY_BUFFER, id); + + } +} + +void Buffer::Unbind(bool elementBuffer = false) { + if (elementBuffer) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + } + else { + glBindBuffer(GL_ARRAY_BUFFER, 0); + + } +} + +void Buffer::Delete() { + glDeleteBuffers(1, (GLuint*) &id); +} \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/Buffer.h b/MyGraphicsEngine/include/MyGraphicsEngine/Buffer.h new file mode 100644 index 0000000..a96d976 --- /dev/null +++ b/MyGraphicsEngine/include/MyGraphicsEngine/Buffer.h @@ -0,0 +1,19 @@ +#pragma once +#include +class Buffer { +private: + unsigned int id; +public: + + int getBufferID(); + + void createBuffer(); + + void setBufferData(void* data, size_t dataSize, bool elementBuffer ); + + void Bind(bool elementBuffer); + void Unbind(bool elementBuffer); + + void Delete(); + +}; \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/Transform.h b/MyGraphicsEngine/include/MyGraphicsEngine/Transform.h new file mode 100644 index 0000000..82a01be --- /dev/null +++ b/MyGraphicsEngine/include/MyGraphicsEngine/Transform.h @@ -0,0 +1,8 @@ +#pragma once +#include + +struct Transform { + glm::vec3 Position; + glm::vec3 Rotation; + glm::vec3 Scale; +}; \ No newline at end of file diff --git a/MyGraphicsEngine/shaders/fragment.shader b/MyGraphicsEngine/shaders/fragment.shader index 7f9f820..3148a0f 100644 --- a/MyGraphicsEngine/shaders/fragment.shader +++ b/MyGraphicsEngine/shaders/fragment.shader @@ -1,7 +1,7 @@ -#version 330 core +#version 440 core out vec4 FragColor; -void main{ +void main(){ FragColor = vec4(0.0f, 1.0f, 0.0f , 1.0f); } \ No newline at end of file diff --git a/MyGraphicsEngine/shaders/vertex.shader b/MyGraphicsEngine/shaders/vertex.shader index 4a38668..9c3ab45 100644 --- a/MyGraphicsEngine/shaders/vertex.shader +++ b/MyGraphicsEngine/shaders/vertex.shader @@ -1,6 +1,10 @@ -#version 330 core -layout in vec3 aPos; +#version 440 core +in layout(location=0) vec3 aPos; +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; + void main() { - gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); + gl_Position = P * V * M * vec4(aPos.x, aPos.y, aPos.z, 1.0); } \ No newline at end of file diff --git a/SandboxApplication/Sandbox.cpp b/SandboxApplication/Sandbox.cpp index b11c27b..ff9842d 100644 --- a/SandboxApplication/Sandbox.cpp +++ b/SandboxApplication/Sandbox.cpp @@ -2,6 +2,9 @@ #include #include #include +#include +#include + #include #include "imgui.h" @@ -29,6 +32,10 @@ int main(int argc, char* argv[]) { //ModelImporter::Test(); + Transform t; + t.Scale.x = 1.0f; + t.Scale.y = 1.0f; + t.Rotation.y = 90.0f; Mesh mesh; @@ -44,6 +51,8 @@ int main(int argc, char* argv[]) { 0,1,3, 1,2,3 }; + + Camera cam(glm::vec3(2.0f, 0.0f, 0.0f), glm::vec3(0.0f,0.0f,0.0f), 90.0f); @@ -73,43 +82,30 @@ int main(int argc, char* argv[]) { spdlog::info("Vertices: {}, {} bytes", mesh.vertices.size(), sizeof(glm::vec3)); spdlog::info("Elements: {}, {} bytes", mesh.elements.size(), sizeof(GLushort)); - unsigned int VBO, VAO, EBO; + + Buffer VertexBuffer; + Buffer ElementBuffer; + + unsigned int VAO; glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - glGenBuffers(1, &EBO); - glBindVertexArray(VAO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, mesh.vertices.size() * sizeof(glm::vec3), &mesh.vertices[0], GL_STATIC_DRAW); + VertexBuffer.createBuffer(); + VertexBuffer.Bind(false); + VertexBuffer.setBufferData(&mesh.vertices[0], mesh.vertices.size() * sizeof(glm::vec3),false); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.elements.size() * sizeof(GLushort), &mesh.elements[0], GL_STATIC_DRAW); + ElementBuffer.createBuffer(); + ElementBuffer.Bind(true); + ElementBuffer.setBufferData(&mesh.elements[0], mesh.elements.size() * sizeof(GLushort), true); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, 0); + VertexBuffer.Unbind(false); glBindVertexArray(0); - - glm::vec3 rotation = glm::vec3(0.0f , 90.0f, 0.0f); - - - glm::mat4 tran = glm::translate(glm::mat4(),glm::vec3(0.0f, 0.0f, 0.0f)); - glm::mat4 scale = glm::scale(glm::mat4(), glm::vec3(1.0f, 1.0f, 1.0f)); - glm::mat4 rot = - glm::rotate(glm::mat4(), glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) * - glm::rotate(glm::mat4(), glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) * - glm::rotate(glm::mat4(), glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - - - glm::mat4 model = tran * rot * scale; - - - glm::mat4 projection = glm::perspective(cam.Zoom, (800.0f / 600.0f), 0.001f, 100.0f); - /* * gau_Manager* mgr; ga_Mixer* mixer; @@ -131,7 +127,18 @@ int main(int argc, char* argv[]) { while (!GameWindow.WindowShouldClose()) { - + glm::mat4 tran = glm::translate(glm::mat4(), t.Position); + glm::mat4 scale = glm::scale(glm::mat4(), t.Scale); + glm::mat4 rot = + glm::rotate(glm::mat4(), glm::radians(t.Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)) * + glm::rotate(glm::mat4(), glm::radians(t.Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)) * + glm::rotate(glm::mat4(), glm::radians(t.Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); + + + glm::mat4 model = tran * rot * scale; + + glm::mat4 projection = glm::perspective(cam.Zoom, (800.0f / 600.0f), 0.001f, 100.0f); + GameWindow.Poll(); @@ -142,7 +149,7 @@ int main(int argc, char* argv[]) { shader.setUniformMat4("M", model); shader.setUniformMat4("V", cam.GetViewMatrix()); glBindVertexArray(VAO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + ElementBuffer.Bind(true); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, NULL); glBindVertexArray(0); @@ -151,13 +158,13 @@ int main(int argc, char* argv[]) { ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - ImGui::Begin("Test"); + ImGui::Begin("Transform Test Tool"); + + ImGui::SliderFloat("Scale Y:", (float*)&t.Scale.y, 1, 4); + ImGui::SliderFloat("Scale X:", (float*)&t.Scale.x, 1, 4); + + ImGui::SliderFloat("Position X:", (float*)&t.Position.z, -5, 5); - ImGui::Text("Hello world!"); - bool isChecked = false; - ImGui::Checkbox("Yellow!", &isChecked); - float v = 5; - ImGui::SliderFloat("Something to slide on..", &v, 1, 10 ); ImGui::End(); @@ -176,7 +183,7 @@ int main(int argc, char* argv[]) { ImGui::DestroyContext(); glDeleteVertexArrays(1, &VAO); - glDeleteBuffers(1, &EBO); + ElementBuffer.Delete(); } diff --git a/TODO.md b/TODO.md index ae717bd..b699487 100644 --- a/TODO.md +++ b/TODO.md @@ -6,10 +6,11 @@ Setup build system \ Link with GLFW \ Basic Window \ - Basic Triangle rendering \ + Basic Triangle rendering \ + Basic IMGui \ Basic Textures \ Link GLEW \ - Work on basic logging \ + Work on basic logging \ Input handling \ More shader work \ Load FBX model files \ diff --git a/imgui.ini b/imgui.ini index b19d6c0..f4374b5 100644 --- a/imgui.ini +++ b/imgui.ini @@ -3,13 +3,8 @@ Pos=60,60 Size=400,400 Collapsed=0 -[Window][Test] -Pos=268,47 -Size=201,94 -Collapsed=0 - -[Window][Dear ImGui Demo] -Pos=650,20 -Size=550,680 +[Window][Transform Test Tool] +Pos=443,154 +Size=396,490 Collapsed=0