From ddc587f4c0da10294efa31cbb3500e5c7e70aa57 Mon Sep 17 00:00:00 2001 From: AmyFoxie Date: Tue, 7 Nov 2023 23:59:16 +0100 Subject: [PATCH] vertex colors on opengl --- engine/gl/GlBuffer.cpp | 4 +- engine/gl/GlVertexArrayObject.cpp | 3 +- engine/gl/GlVertexArrayObject.h | 6 ++- engine/gl/test/main.cpp | 76 +++++++++++++++++++++++-------- 4 files changed, 65 insertions(+), 24 deletions(-) diff --git a/engine/gl/GlBuffer.cpp b/engine/gl/GlBuffer.cpp index b614f42..814a32d 100644 --- a/engine/gl/GlBuffer.cpp +++ b/engine/gl/GlBuffer.cpp @@ -30,9 +30,9 @@ namespace Vixen::Gl { buffer, static_cast(size), nullptr, - this->flags + flags ); - spdlog::trace("Created new GL commandBuffer {} ({}B) and flags {}", buffer, size, this->flags); + spdlog::trace("Created new GL commandBuffer {} ({}B) and flags {}", buffer, size, flags); } GlBuffer::~GlBuffer() { diff --git a/engine/gl/GlVertexArrayObject.cpp b/engine/gl/GlVertexArrayObject.cpp index 484c0bb..915a095 100644 --- a/engine/gl/GlVertexArrayObject.cpp +++ b/engine/gl/GlVertexArrayObject.cpp @@ -12,8 +12,7 @@ namespace Vixen::Gl { glVertexArrayAttribFormat(vao, location.index, location.size, location.type, location.normalized, location.offset); glEnableVertexArrayAttrib(vao, location.index); - // TODO: The binding index doesn't necessarily need to be the same as the attribute index - glVertexArrayAttribBinding(vao, location.index, location.index); + glVertexArrayAttribBinding(vao, location.index, binding.index); } if (binding.buffer->getBufferUsage() & Buffer::Usage::INDEX) diff --git a/engine/gl/GlVertexArrayObject.h b/engine/gl/GlVertexArrayObject.h index 475bed2..b168550 100644 --- a/engine/gl/GlVertexArrayObject.h +++ b/engine/gl/GlVertexArrayObject.h @@ -23,8 +23,10 @@ namespace Vixen::Gl { GLsizei stride; }; - VertexBinding(const std::shared_ptr &buffer, const std::vector &locations) - : buffer(buffer), locations(locations) {} + VertexBinding(uint32_t index, const std::shared_ptr &buffer, const std::vector &locations) + : index(index), buffer(buffer), locations(locations) {} + + uint32_t index; std::shared_ptr buffer; diff --git a/engine/gl/test/main.cpp b/engine/gl/test/main.cpp index 9399353..b5c6c94 100644 --- a/engine/gl/test/main.cpp +++ b/engine/gl/test/main.cpp @@ -12,24 +12,18 @@ #endif +struct Vertex { + glm::vec3 position; + glm::vec3 color; +}; + int main() { #ifdef _WIN32 system(("chcp " + std::to_string(CP_UTF8)).c_str()); #endif spdlog::set_level(spdlog::level::trace); - std::vector vertices = { - {0.5f, 0.5f, 0.0f}, // top right - {0.5f, -0.5f, 0.0f}, // bottom right - {-0.5f, -0.5f, 0.0f}, // bottom left - {-0.5f, 0.5f, 0.0f} // top left - }; - std::vector indices = { // note that we start from 0! - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle - }; - - auto window = Vixen::Gl::GlWindow("Vixen OpenGL Test", 720, 480, true); + auto window = Vixen::Gl::GlWindow("Vixen OpenGL Test", 1920, 1080, true); window.center(); window.setVisible(true); @@ -44,26 +38,64 @@ int main() { Vixen::Gl::GlShaderProgram program(vertexModule, fragmentModule); + std::vector vertices{ + {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}}, + {{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}}, + {{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}}, + {{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}} + }; + + std::vector indices{ + 0, 1, 2, + 2, 3, 0 + }; + auto vbo = std::make_shared( Vixen::Buffer::Usage::VERTEX | Vixen::Buffer::Usage::INDEX, - vertices.size() * sizeof(glm::vec3) + - indices.size() * sizeof(std::uint32_t) + vertices.size() * sizeof(Vertex) + + indices.size() * sizeof(uint32_t) + ); + vbo->write( + reinterpret_cast(vertices.data()), + sizeof(Vertex) * vertices.size(), + 0 + ); + vbo->write( + reinterpret_cast(indices.data()), + sizeof(uint32_t) * indices.size(), + sizeof(Vertex) * vertices.size() ); - vbo->write(reinterpret_cast(vertices.data()), vertices.size() * sizeof(glm::vec3), 0); - vbo->write(reinterpret_cast(indices.data()), vertices.size() * sizeof(glm::vec3), 0); auto vao = Vixen::Gl::GlVertexArrayObject( { Vixen::Gl::VertexBinding( + 0, vbo, { - Vixen::Gl::VertexBinding::Location(0, 3, GL_FLOAT, GL_FALSE, 0, sizeof(glm::vec3)) + Vixen::Gl::VertexBinding::Location( + 0, + 3, + GL_FLOAT, + GL_FALSE, + offsetof(Vertex, position), + sizeof(Vertex) + ), + Vixen::Gl::VertexBinding::Location( + 1, + 3, + GL_FLOAT, + GL_FALSE, + offsetof(Vertex, color), + sizeof(Vertex) + ) } ) }, - vertices.size() * sizeof(glm::vec3) + vertices.size() * sizeof(Vertex) ); + double old = glfwGetTime(); + uint32_t fps; while (!window.shouldClose()) { window.clear(); @@ -73,6 +105,14 @@ int main() { window.update(); window.swap(); + + fps++; + double now = glfwGetTime(); + if (now - old >= 1) { + spdlog::info("FPS: {}", fps); + old = now; + fps = 0; + } } return EXIT_SUCCESS; }