From 902f3484cfc09e7197ebec0de435411a3aa905a5 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Thu, 27 Oct 2022 21:52:14 +0200 Subject: [PATCH] Broken stuff --- headers/renderer.hpp | 12 +++++++--- src/main.cpp | 18 +++++++++------ src/renderer.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 12 deletions(-) diff --git a/headers/renderer.hpp b/headers/renderer.hpp index 755aa47..b5ff243 100644 --- a/headers/renderer.hpp +++ b/headers/renderer.hpp @@ -21,11 +21,16 @@ namespace Renderer { class RenderObject { public: - RenderObject(); + RenderObject(std::vector verts, std::vector indices); void render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform); + void transform(glm::mat4 T); private: - std::vector verts; - std::vector indices; + Shaders::Shader shader; + unsigned int EBO; + unsigned int VBO; + unsigned int VAO; + std::vector vertsVec; + std::vector indicesVec; }; class Renderer3D { @@ -35,6 +40,7 @@ namespace Renderer { void setCamera(glm::vec3 pos); void setFOV(float deg); + void spawnObject(RenderObject ro); void render(); private: std::vector renderObjects = std::vector(); diff --git a/src/main.cpp b/src/main.cpp index dfc6bf1..96debd9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,6 +49,8 @@ int main() { return 1; } + Renderer::Renderer3D renderer(win); + glfwMakeContextCurrent(win); if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { @@ -56,18 +58,18 @@ int main() { return 1; } - float verts[] = { + std::vector verts({ 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f - }; + }); // Vert struc: x y z r g b tx ty - unsigned int indices[] = { + std::vector indices({ 0, 1, 3, 1, 2, 3 - }; + }); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glfwSetFramebufferSizeCallback(win, framebuffer_size_callback); @@ -75,8 +77,9 @@ int main() { float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); - Renderer::Obj2D ro(indices, sizeof(indices), verts, sizeof(verts)); - ro.setTexture(RUSTY_METAL_TEXTURE); + Renderer::RenderObject ro(verts, indices); + renderer.spawnObject(ro); + // ro.setTexture(RUSTY_METAL_TEXTURE); // Window width & height while (!glfwWindowShouldClose(win)) { @@ -97,7 +100,8 @@ int main() { T = glm::rotate(T, rotang, glm::vec3(1.0, 0.0, 1.0)); T = glm::scale(T, glm::vec3(0.5, 0.5, 0.5)); ro.transform(T); - ro.render(win); + + renderer.render(); // glfw glfwSwapBuffers(win); diff --git a/src/renderer.cpp b/src/renderer.cpp index 2595955..707f818 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -19,6 +19,10 @@ namespace Renderer { renderObjects = ROs; } + void Renderer3D::spawnObject(RenderObject ro) { + renderObjects.push_back(ro); + } + void Renderer3D::setFOV(float fov) { int width, height; glfwGetWindowSize(window, &width, &height); @@ -35,11 +39,55 @@ namespace Renderer { } // RenderObject - RenderObject::RenderObject() { + RenderObject::RenderObject(std::vector verts, std::vector indices) + : shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) { + vertsVec = verts; + indicesVec = indices; + + float* vertsArray = &vertsVec[0]; + unsigned int* indicesArray = &indicesVec[0]; + + // Vertex Array Object + glGenVertexArrays(1, &VAO); // gen the VAO + glBindVertexArray(VAO); // bind it + + // Copy the verts into the buffer + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, vertsVec.size(), vertsArray, GL_DYNAMIC_DRAW); // for moving stuff + // Copy the indices for the verts into another buffer + glGenBuffers(1, &EBO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indicesVec.size(), indicesArray, GL_STATIC_DRAW); + + // Shader stuff + // Pos + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)0); + glEnableVertexAttribArray(0); + + // Color + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(3*sizeof(float))); + glEnableVertexAttribArray(1); + + // Texture + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, VERTEX_ATTRIB_PTR_SIZE, (void*)(6*sizeof(float))); + glEnableVertexAttribArray(2); } - void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) {} + void RenderObject::render(GLFWwindow* win, glm::mat4 cameraTransform, glm::mat4 projectionTransform) { + shader.setMat4("view", cameraTransform); + shader.setMat4("projection", projectionTransform); + + shader.use(); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glDrawElements(GL_TRIANGLES, indicesVec.size(), GL_UNSIGNED_INT, 0); + } + + void RenderObject::transform(glm::mat4 T) { + shader.setMat4("model", T); + } void Obj2D::transform(glm::mat4 T) { shader.setMat4("model", T);