From 79f5fba2ec81d0825984ebc35d71fb5a3742aedc Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Thu, 25 Nov 2021 22:28:47 -0500 Subject: [PATCH] Add color to `Vertex`es --- examples/dev_testing/src/main.cpp | 31 ++++++----------- include/simpleengine/gfx/vao.h | 5 +-- include/simpleengine/gfx/vbo.h | 4 +-- include/simpleengine/shapes/2d/square.h | 19 +++++++--- include/simpleengine/shapes/2d/triangle.h | 42 ++++++++++++++++++++--- 5 files changed, 68 insertions(+), 33 deletions(-) diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index 3cb299f..6a41d0c 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -41,34 +41,25 @@ int main(int argc, char *argv[]) { shader_prog.link(); std::shared_ptr base_shader_program = shader_prog.program; - /* std::vector vertices = { - glm::vec3(-0.5f, -0.5f, 0.f), - glm::vec3(0.5f, -0.5f, 0.f), - glm::vec3(0.f, 0.5f, 0.f), - }; */ - std::vector vertices = { - glm::vec3(0.5f, 0.5f, 0.0f), - glm::vec3(0.5f, -0.5f, 0.0f), - glm::vec3(-0.5f, 0.5f, 0.0f), - - glm::vec3(0.5f, -0.5f, 0.0f), - glm::vec3(-0.5f, -0.5f, 0.0f), - glm::vec3(-0.5f, 0.5f, 0.0f), + std::vector vertices = { + {glm::vec3(-0.5f, -0.5f, 0.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 1.f)}, + {glm::vec3(0.5f, -0.5f, 0.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(0.f, 0.f)}, + {glm::vec3(0.f, 0.5f, 0.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(1.f, 0.f)}, }; std::shared_ptr tri(new simpleengine::shapes_2d::Triangle(base_shader_program, vertices)); game.add_event(tri); - /* std::vector vertices = { - glm::vec3(0.5f, 0.5f, 0.f), // top right - glm::vec3(0.5f, -0.5f, 0.f), // bottom right - glm::vec3(-0.5f, -0.5f, 0.f), // bottom left - glm::vec3(-0.5f, 0.5f, 0.f), // top left + /* std::vector vertices = { + { glm::vec3(0.5f, 0.5f, 0.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 1.f) }, + { glm::vec3(0.5f, -0.5f, 0.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(0.f, 0.f) }, + { glm::vec3(-0.5f, -0.5f, 0.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(1.f, 0.f) }, + { glm::vec3(-0.5f, 0.5f, 0.f), glm::vec3(0.75f, 0.75f, 0.75f), glm::vec2(1.f, 1.f) }, }; std::vector indicies = { - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle + 0, 1, 3, + 1, 2, 3 }; std::shared_ptr square(new simpleengine::shapes_2d::Square(base_shader_program, vertices, indicies)); diff --git a/include/simpleengine/gfx/vao.h b/include/simpleengine/gfx/vao.h index 7fdb154..787e578 100644 --- a/include/simpleengine/gfx/vao.h +++ b/include/simpleengine/gfx/vao.h @@ -17,7 +17,7 @@ namespace simpleengine::gfx { GLuint handle; VAO() { - glGenVertexArrays(1, &handle); + glCreateVertexArrays(1, &handle); } ~VAO() { @@ -43,6 +43,7 @@ namespace simpleengine::gfx { glBindVertexArray(0); } + // TODO: Fix this. void enable_attrib(VBO vbo, GLuint index, GLint size, GLenum type, GLsizei stride, size_t offset) { bind(); vbo.bind(); @@ -69,7 +70,7 @@ namespace simpleengine::gfx { // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's // bound vertex buffer object so afterwards we can safely unbind. - glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ARRAY_BUFFER, 0); // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this // rarely happens. Modifying other VAOs requires a call to glBindVertexArray anyways so we generally diff --git a/include/simpleengine/gfx/vbo.h b/include/simpleengine/gfx/vbo.h index 807b5a0..9596752 100644 --- a/include/simpleengine/gfx/vbo.h +++ b/include/simpleengine/gfx/vbo.h @@ -32,9 +32,9 @@ namespace simpleengine::gfx { glBindBuffer(type, handle); } - void buffer(void *data, size_t offset, size_t count) { + void buffer(void *data, size_t offset, size_t size) { bind(); - glBufferData(type, count - offset, data, dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW); + glBufferData(type, size - offset, data, dynamic ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW); } }; } diff --git a/include/simpleengine/shapes/2d/square.h b/include/simpleengine/shapes/2d/square.h index 9e60fd1..0322201 100644 --- a/include/simpleengine/shapes/2d/square.h +++ b/include/simpleengine/shapes/2d/square.h @@ -30,23 +30,32 @@ namespace simpleengine::shapes_2d { private: std::shared_ptr shader_program; public: - std::vector vertices; + std::vector vertices; std::vector indicies; gfx::VBO ebo; gfx::VBO vbo; gfx::VAO vao; - Square(std::shared_ptr shader_program, std::vector vertices, std::vector indicies) : + Square(std::shared_ptr shader_program, std::vector vertices, std::vector indicies) : super(nullptr), shader_program(shader_program), vertices(vertices), indicies(indicies), ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), vao(gfx::VAO()) { vao.bind(); - vbo.buffer(vertices.data(), 0, vertices.size() * sizeof(float) * 3); + vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint)); - // idfk why its 3 - vao.enable_attrib(ebo, 0, 3, GL_FLOAT, 3 * sizeof(float), 0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color)); + glEnableVertexAttribArray(1); + + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, tex_coord)); + glEnableVertexAttribArray(2); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); } virtual ~Square() = default; diff --git a/include/simpleengine/shapes/2d/triangle.h b/include/simpleengine/shapes/2d/triangle.h index 0a1a605..2b5004b 100644 --- a/include/simpleengine/shapes/2d/triangle.h +++ b/include/simpleengine/shapes/2d/triangle.h @@ -29,17 +29,51 @@ namespace simpleengine::shapes_2d { private: std::shared_ptr shader_program; public: - std::vector vertices; + std::vector vertices; gfx::VBO vbo; gfx::VAO vao; - Triangle(std::shared_ptr shader_program, std::vector vertices) : super(nullptr), + Triangle(std::shared_ptr shader_program, std::vector vertices) : super(nullptr), shader_program(shader_program), vertices(vertices), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), vao(gfx::VAO()) { vao.bind(); - vbo.buffer(vertices.data(), 0, vertices.size() * sizeof(float) * 3); // 3 floats are in each "row" of the vector. - vao.enable_attrib(vbo, 0, 3, GL_FLOAT, 3 * sizeof(float), 0); + vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); + + /* vao.enable_attrib(vbo, 0, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, position)); + vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, color)); + vao.enable_attrib(vbo, 2, 2, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, tex_coord)); */ + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color)); + glEnableVertexAttribArray(1); + + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, tex_coord)); + glEnableVertexAttribArray(2); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + /* glCreateVertexArrays(1, &vao); + glBindVertexArray(vao); + + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), vertices.data(), GL_STATIC_DRAW); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color)); + glEnableVertexAttribArray(1); + + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, tex_coord)); + glEnableVertexAttribArray(2); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); */ } virtual ~Triangle() = default;