Separate code in square.h and triangle.h to .cpp files
This commit is contained in:
parent
97e1ce44cd
commit
d3555e112d
|
@ -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
|
{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));
|
auto tri = std::make_shared<simpleengine::shapes_2d::Triangle>(base_shader_program, vertices);
|
||||||
//tri->set_texture(wall_texture);
|
tri->set_texture(wall_texture);
|
||||||
game.add_event(tri);
|
game.add_event(tri);
|
||||||
|
|
||||||
/* std::vector<simpleengine::Vertex> vertices = {
|
/* std::vector<simpleengine::Vertex> vertices = {
|
||||||
|
@ -69,7 +69,8 @@ int main(int argc, char *argv[]) {
|
||||||
1, 2, 3
|
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); */
|
game.add_event(square); */
|
||||||
|
|
||||||
return game.run();
|
return game.run();
|
||||||
|
|
|
@ -19,17 +19,20 @@
|
||||||
#include "../../gfx/vbo.h"
|
#include "../../gfx/vbo.h"
|
||||||
#include "../../gfx/vao.h"
|
#include "../../gfx/vao.h"
|
||||||
#include "../../gfx/texture.h"
|
#include "../../gfx/texture.h"
|
||||||
|
#include "../../gfx/shader.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "../../optional.h"
|
||||||
|
|
||||||
namespace simpleengine::shapes_2d {
|
namespace simpleengine::shapes_2d {
|
||||||
class Square : public simpleengine::Renderable {
|
class Square : public simpleengine::Renderable {
|
||||||
private:
|
private:
|
||||||
using super = simpleengine::Renderable;
|
using super = simpleengine::Renderable;
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<GLuint> shader_program;
|
gfx::Shader shader;
|
||||||
Texture& texture;
|
nonstd::optional<Texture> texture;
|
||||||
public:
|
public:
|
||||||
std::vector<Vertex> vertices;
|
std::vector<Vertex> vertices;
|
||||||
std::vector<GLuint> indicies;
|
std::vector<GLuint> indicies;
|
||||||
|
@ -37,42 +40,17 @@ namespace simpleengine::shapes_2d {
|
||||||
gfx::VBO vbo;
|
gfx::VBO vbo;
|
||||||
gfx::VAO vao;
|
gfx::VAO vao;
|
||||||
|
|
||||||
Square(std::shared_ptr<GLuint> shader_program, Texture& texture, std::vector<Vertex> vertices, std::vector<GLuint> indicies) :
|
Square(gfx::Shader shader, 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) {
|
|
||||||
|
|
||||||
vao.bind();
|
Square(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices, std::vector<GLuint> indicies);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~Square() = default;
|
virtual ~Square() = default;
|
||||||
|
|
||||||
virtual void update(const float& delta_time) override {
|
void set_texture(Texture texture);
|
||||||
|
|
||||||
}
|
virtual void update(const float& delta_time) override;
|
||||||
|
|
||||||
virtual void render(std::shared_ptr<GLFWwindow> target) 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);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,48 +35,17 @@ namespace simpleengine::shapes_2d {
|
||||||
gfx::VBO vbo;
|
gfx::VBO vbo;
|
||||||
gfx::VAO vao;
|
gfx::VAO vao;
|
||||||
|
|
||||||
Triangle(gfx::Shader shader, std::vector<Vertex> vertices) : super(nullptr),
|
Triangle(gfx::Shader shader, std::vector<Vertex> vertices);
|
||||||
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));
|
Triangle(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices);
|
||||||
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) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~Triangle() = default;
|
virtual ~Triangle() = default;
|
||||||
|
|
||||||
void set_texture(Texture texture) {
|
void set_texture(Texture texture);
|
||||||
this->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;
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue