Separate code in square.h and triangle.h to .cpp files

This commit is contained in:
SeanOMik 2021-11-27 14:06:51 -05:00
parent 97e1ce44cd
commit d3555e112d
5 changed files with 117 additions and 72 deletions

View File

@ -53,8 +53,8 @@ int main(int argc, char *argv[]) {
{glm::vec3(0.f, 0.5f, 0.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f)}, // top
};
std::shared_ptr<simpleengine::shapes_2d::Triangle> tri(new simpleengine::shapes_2d::Triangle(base_shader_program, vertices));
//tri->set_texture(wall_texture);
auto tri = std::make_shared<simpleengine::shapes_2d::Triangle>(base_shader_program, vertices);
tri->set_texture(wall_texture);
game.add_event(tri);
/* std::vector<simpleengine::Vertex> vertices = {
@ -69,7 +69,8 @@ int main(int argc, char *argv[]) {
1, 2, 3
};
std::shared_ptr<simpleengine::Event> square(new simpleengine::shapes_2d::Square(base_shader_program, crate_texture, vertices, indicies));
auto square = std::make_shared<simpleengine::shapes_2d::Square>(base_shader_program, vertices, indicies);
//square->set_texture(crate_texture);
game.add_event(square); */
return game.run();

View File

@ -19,17 +19,20 @@
#include "../../gfx/vbo.h"
#include "../../gfx/vao.h"
#include "../../gfx/texture.h"
#include "../../gfx/shader.h"
#include <stdint.h>
#include <vector>
#include "../../optional.h"
namespace simpleengine::shapes_2d {
class Square : public simpleengine::Renderable {
private:
using super = simpleengine::Renderable;
private:
std::shared_ptr<GLuint> shader_program;
Texture& texture;
gfx::Shader shader;
nonstd::optional<Texture> texture;
public:
std::vector<Vertex> vertices;
std::vector<GLuint> indicies;
@ -37,42 +40,17 @@ namespace simpleengine::shapes_2d {
gfx::VBO vbo;
gfx::VAO vao;
Square(std::shared_ptr<GLuint> shader_program, Texture& texture, std::vector<Vertex> vertices, std::vector<GLuint> 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)),
texture(texture) {
Square(gfx::Shader shader, std::vector<Vertex> vertices, std::vector<GLuint> indicies);
vao.bind();
vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size());
ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint));
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);
}
Square(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices, std::vector<GLuint> indicies);
virtual ~Square() = default;
void set_texture(Texture texture);
virtual void update(const float& delta_time) override {
virtual void update(const float& delta_time) override;
}
virtual void render(std::shared_ptr<GLFWwindow> target) override {
texture.bind();
glUseProgram(*shader_program);
vao.bind();
glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0);
}
virtual void render(std::shared_ptr<GLFWwindow> target) override;
};
}

View File

@ -35,48 +35,17 @@ namespace simpleengine::shapes_2d {
gfx::VBO vbo;
gfx::VAO vao;
Triangle(gfx::Shader shader, std::vector<Vertex> vertices) : super(nullptr),
shader(shader), vertices(vertices), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), texture(nonstd::nullopt) {
vao.bind();
vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size());
Triangle(gfx::Shader shader, std::vector<Vertex> vertices);
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));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
Triangle(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices) :
Triangle(gfx::Shader(shader_program), vertices) {
}
Triangle(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices);
virtual ~Triangle() = default;
void set_texture(Texture texture) {
this->texture = texture;
}
void set_texture(Texture texture);
virtual void update(const float& delta_time) override {
virtual void update(const float& delta_time) override;
}
virtual void render(std::shared_ptr<GLFWwindow> target) override {
shader.use();
// If theres a texture set, tell the fragment shader that and bind to the texture for drawing.
if (texture.has_value()) {
shader.setUniformInt("texture_is_set", true, false);
texture.value().bind();
} else {
shader.setUniformInt("texture_is_set", false, false);
}
vao.bind();
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
}
virtual void render(std::shared_ptr<GLFWwindow> target) override;
};
}

53
src/shapes/2d/square.cpp Normal file
View File

@ -0,0 +1,53 @@
#include "shapes/2d/square.h"
namespace simpleengine::shapes_2d {
Square::Square(gfx::Shader shader, std::vector<Vertex> vertices, std::vector<GLuint> indicies) :
super(nullptr), shader(shader), vertices(vertices), indicies(indicies),
ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)),
texture(nonstd::nullopt) {
vao.bind();
vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size());
ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint));
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);
}
Square::Square(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices, std::vector<GLuint> indicies) :
Square(gfx::Shader(shader_program), vertices, indicies) {
}
void Square::set_texture(Texture texture) {
this->texture = texture;
}
void Square::update(const float& delta_time) {
}
void Square::render(std::shared_ptr<GLFWwindow> target) {
shader.use();
// If theres a texture set, tell the fragment shader that and bind to the texture for drawing.
if (texture.has_value()) {
shader.setUniformInt("texture_is_set", true, false);
texture.value().bind();
} else {
shader.setUniformInt("texture_is_set", false, false);
}
vao.bind();
glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0);
}
}

View File

@ -0,0 +1,44 @@
#include "shapes/2d/triangle.h"
namespace simpleengine::shapes_2d {
Triangle::Triangle(gfx::Shader shader, std::vector<Vertex> vertices) : super(nullptr),
shader(shader), vertices(vertices), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), texture(nonstd::nullopt) {
vao.bind();
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));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
Triangle::Triangle(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices) :
Triangle(gfx::Shader(shader_program), vertices) {
}
void Triangle::set_texture(Texture texture) {
this->texture = texture;
}
void Triangle::update(const float& delta_time) {
}
void Triangle::render(std::shared_ptr<GLFWwindow> target) {
shader.use();
// If theres a texture set, tell the fragment shader that and bind to the texture for drawing.
if (texture.has_value()) {
shader.setUniformInt("texture_is_set", true, false);
texture.value().bind();
} else {
shader.setUniformInt("texture_is_set", false, false);
}
vao.bind();
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
}
}