Add color to `Vertex`es

This commit is contained in:
SeanOMik 2021-11-25 22:28:47 -05:00
parent 625ba885c5
commit 79f5fba2ec
5 changed files with 68 additions and 33 deletions

View File

@ -41,34 +41,25 @@ int main(int argc, char *argv[]) {
shader_prog.link(); shader_prog.link();
std::shared_ptr<GLuint> base_shader_program = shader_prog.program; std::shared_ptr<GLuint> base_shader_program = shader_prog.program;
/* std::vector<glm::vec3> vertices = { std::vector<simpleengine::Vertex> vertices = {
glm::vec3(-0.5f, -0.5f, 0.f), {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.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.5f, 0.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(1.f, 0.f)},
}; */
std::vector<glm::vec3> 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::shared_ptr<simpleengine::Event> tri(new simpleengine::shapes_2d::Triangle(base_shader_program, vertices)); std::shared_ptr<simpleengine::Event> tri(new simpleengine::shapes_2d::Triangle(base_shader_program, vertices));
game.add_event(tri); game.add_event(tri);
/* std::vector<glm::vec3> vertices = { /* std::vector<simpleengine::Vertex> vertices = {
glm::vec3(0.5f, 0.5f, 0.f), // top right { 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), // bottom right { 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), // bottom left { 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), // top left { glm::vec3(-0.5f, 0.5f, 0.f), glm::vec3(0.75f, 0.75f, 0.75f), glm::vec2(1.f, 1.f) },
}; };
std::vector<GLuint> indicies = { std::vector<GLuint> indicies = {
0, 1, 3, // first triangle 0, 1, 3,
1, 2, 3 // second triangle 1, 2, 3
}; };
std::shared_ptr<simpleengine::Event> square(new simpleengine::shapes_2d::Square(base_shader_program, vertices, indicies)); std::shared_ptr<simpleengine::Event> square(new simpleengine::shapes_2d::Square(base_shader_program, vertices, indicies));

View File

@ -17,7 +17,7 @@ namespace simpleengine::gfx {
GLuint handle; GLuint handle;
VAO() { VAO() {
glGenVertexArrays(1, &handle); glCreateVertexArrays(1, &handle);
} }
~VAO() { ~VAO() {
@ -43,6 +43,7 @@ namespace simpleengine::gfx {
glBindVertexArray(0); glBindVertexArray(0);
} }
// TODO: Fix this.
void enable_attrib(VBO vbo, GLuint index, GLint size, GLenum type, GLsizei stride, size_t offset) { void enable_attrib(VBO vbo, GLuint index, GLint size, GLenum type, GLsizei stride, size_t offset) {
bind(); bind();
vbo.bind(); vbo.bind();

View File

@ -32,9 +32,9 @@ namespace simpleengine::gfx {
glBindBuffer(type, handle); glBindBuffer(type, handle);
} }
void buffer(void *data, size_t offset, size_t count) { void buffer(void *data, size_t offset, size_t size) {
bind(); 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);
} }
}; };
} }

View File

@ -30,23 +30,32 @@ namespace simpleengine::shapes_2d {
private: private:
std::shared_ptr<GLuint> shader_program; std::shared_ptr<GLuint> shader_program;
public: public:
std::vector<glm::vec3> vertices; std::vector<Vertex> vertices;
std::vector<GLuint> indicies; std::vector<GLuint> indicies;
gfx::VBO ebo; gfx::VBO ebo;
gfx::VBO vbo; gfx::VBO vbo;
gfx::VAO vao; gfx::VAO vao;
Square(std::shared_ptr<GLuint> shader_program, std::vector<glm::vec3> vertices, std::vector<GLuint> indicies) : Square(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices, std::vector<GLuint> indicies) :
super(nullptr), shader_program(shader_program), vertices(vertices), indicies(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)), ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)),
vao(gfx::VAO()) { vao(gfx::VAO()) {
vao.bind(); 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)); ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint));
// idfk why its 3 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position));
vao.enable_attrib(ebo, 0, 3, GL_FLOAT, 3 * sizeof(float), 0); 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; virtual ~Square() = default;

View File

@ -29,17 +29,51 @@ namespace simpleengine::shapes_2d {
private: private:
std::shared_ptr<GLuint> shader_program; std::shared_ptr<GLuint> shader_program;
public: public:
std::vector<glm::vec3> vertices; std::vector<Vertex> vertices;
gfx::VBO vbo; gfx::VBO vbo;
gfx::VAO vao; gfx::VAO vao;
Triangle(std::shared_ptr<GLuint> shader_program, std::vector<glm::vec3> vertices) : super(nullptr), Triangle(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices) : super(nullptr),
shader_program(shader_program), vertices(vertices), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), shader_program(shader_program), vertices(vertices), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)),
vao(gfx::VAO()) { vao(gfx::VAO()) {
vao.bind(); vao.bind();
vbo.buffer(vertices.data(), 0, vertices.size() * sizeof(float) * 3); // 3 floats are in each "row" of the vector. vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size());
vao.enable_attrib(vbo, 0, 3, GL_FLOAT, 3 * sizeof(float), 0);
/* 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; virtual ~Triangle() = default;