Merge Square and Triangle in one class: Model or TexturedModel

This commit is contained in:
SeanOMik 2021-12-07 14:51:12 -05:00
parent a64a748019
commit 4a09752f20
12 changed files with 205 additions and 249 deletions

View File

@ -1,6 +1,6 @@
#include "simpleengine/camera.h" #include "simpleengine/camera.h"
#include "simpleengine/gfx/model.h"
#include "simpleengine/gfx/texture.h" #include "simpleengine/gfx/texture.h"
#include "simpleengine/objects/2d/shapes/square.h"
#include "simpleengine/vector.h" #include "simpleengine/vector.h"
#include <glm/ext/matrix_clip_space.hpp> #include <glm/ext/matrix_clip_space.hpp>
#include <simpleengine/gfx/shader.h> #include <simpleengine/gfx/shader.h>
@ -8,7 +8,6 @@
#include <simpleengine/event/event.h> #include <simpleengine/event/event.h>
#include <simpleengine/shader_program.h> #include <simpleengine/shader_program.h>
#include <simpleengine/game.h> #include <simpleengine/game.h>
#include <simpleengine/objects/2d/shapes/triangle.h>
#include <simpleengine/vertex.h> #include <simpleengine/vertex.h>
#include <simpleengine/objects/3d/obj_model.h> #include <simpleengine/objects/3d/obj_model.h>
@ -48,18 +47,38 @@ int main(int argc, char *argv[]) {
simpleengine::gfx::Texture stall_texture("resources/stallTexture.png"); simpleengine::gfx::Texture stall_texture("resources/stallTexture.png");
auto stall = std::make_shared<simpleengine::objects_3d::ObjModel>(simpleengine::gfx::Shader(base_shader_program), "resources/stall.obj"); auto stall = std::make_shared<simpleengine::objects_3d::ObjModel>(game.get_window(), simpleengine::gfx::Shader(base_shader_program),
stall_texture, "resources/stall.obj");
stall->set_texture(stall_texture); stall->set_texture(stall_texture);
stall->translate(0.f, -4.f, -18.f);
game.add_event(stall); game.add_event(stall);
/* std::vector<simpleengine::Vertex> vertices = { /* std::vector<simpleengine::Vertex> square_vertices = {
{ simpleengine::Vectorf(-0.5f, -0.5f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, // bottom left { simpleengine::Vectorf(0.5f, 0.5f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, // top right
{ simpleengine::Vectorf(0.5f, -0.5f, -1.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, // bottom right { simpleengine::Vectorf(0.5f, -0.5f, -1.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, // bottom right
{ simpleengine::Vectorf(0.f, 0.5f, -1.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f) }, // top { simpleengine::Vectorf(-0.5f, -0.5f, -1.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f) }, // bottom left
{ simpleengine::Vectorf(-0.5f, 0.5f, -1.f), glm::vec3(.5f, 0.5f, 0.f), glm::vec2(0.5f, 1.0f) }, // top left
}; };
auto tri = std::make_shared<simpleengine::objects_2d::shapes::Triangle>(base_shader_program, vertices); std::vector<GLuint> indicies = {
//tri->set_texture(wall_texture); 0, 1, 3,
1, 2, 3
};
auto square = std::make_shared<simpleengine::gfx::Model>(game.get_window(), base_shader_program, square_vertices, indicies);
square->translate(1.25f, 0.f, -1.f);
square->scale(.75f);
game.add_event(square);
std::vector<simpleengine::Vertex> tri_vertices = {
{ simpleengine::Vectorf(-0.5f, -0.5f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, // top right
{ simpleengine::Vectorf(0.5f, -0.5f, -1.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, // bottom right
{ simpleengine::Vectorf(0.f, 0.5f, -1.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f) }, // bottom left
};
auto tri = std::make_shared<simpleengine::gfx::Model>(game.get_window(), base_shader_program, tri_vertices);
tri->translate(-1.25f, 0.f, -1.f);
tri->scale(.75f);
game.add_event(tri); */ game.add_event(tri); */
/* std::vector<simpleengine::Vertex> vertices = { /* std::vector<simpleengine::Vertex> vertices = {
@ -109,8 +128,7 @@ int main(int argc, char *argv[]) {
23,21,22 23,21,22
}; };
auto square = std::make_shared<simpleengine::shapes_2d::Square>(base_shader_program, vertices, indicies); auto square = std::make_shared<simpleengine::gfx::Model>(game.get_window(), base_shader_program, vertices, indicies);
//square->set_texture(crate_texture);
game.add_event(square); */ game.add_event(square); */
auto camera = std::make_shared<simpleengine::Camera>(game.get_window(), base_shader_program); auto camera = std::make_shared<simpleengine::Camera>(game.get_window(), base_shader_program);

View File

@ -0,0 +1,33 @@
#pragma once
#include "shader.h"
#include "vao.h"
#include "vbo.h"
#include "../vertex.h"
#include "../renderable.h"
#include "../transformable.h"
#include <vector>
namespace simpleengine::gfx {
class Model : public simpleengine::Renderable, public simpleengine::Transformable {
public:
std::vector<Vertex> vertices;
std::vector<GLuint> indicies;
gfx::VBO ebo;
gfx::VBO vbo;
gfx::VAO vao;
gfx::Shader shader;
Model(GLFWwindow* window, gfx::Shader shader, std::vector<Vertex> vertices, std::vector<GLuint> indicies = std::vector<GLuint>());
Model(GLFWwindow* window, std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices,
std::vector<GLuint> indicies = std::vector<GLuint>());
protected:
void setup_vertexes();
public:
virtual void update(const float& delta_time) override;
virtual void render(GLFWwindow* target) override;
};
}

View File

@ -0,0 +1,30 @@
#pragma once
/* #include "shader.h"
#include "vao.h"
#include "vbo.h"
#include "../vertex.h"
#include "../renderable.h"
#include "../transformable.h" */
#include "shader.h"
#include "model.h"
#include "texture.h"
#include <GLFW/glfw3.h>
#include <vector>
namespace simpleengine::gfx {
class TexturedModel : public simpleengine::gfx::Model {
public:
gfx::Texture texture;
TexturedModel(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, std::vector<Vertex> vertices, std::vector<GLuint> indicies);
TexturedModel(GLFWwindow* window, std::shared_ptr<GLuint> shader_program, gfx::Texture texture, std::vector<Vertex> vertices,
std::vector<GLuint> indicies = std::vector<GLuint>());
virtual void update(const float& delta_time) override;
virtual void render(GLFWwindow* target) override;
};
}

View File

@ -15,6 +15,8 @@ namespace simpleengine::gfx {
GLint type; GLint type;
bool dynamic; bool dynamic;
VBO() = default;
VBO(GLint type, bool dynamic) : type(type), dynamic(dynamic) { VBO(GLint type, bool dynamic) : type(type), dynamic(dynamic) {
glGenBuffers(1, &handle); glGenBuffers(1, &handle);
} }

View File

@ -1,52 +0,0 @@
#pragma once
#ifdef __linux__
#include <GL/glew.h>
#include <GL/gl.h>
#elif
#include <gl/glew.h>
#include <gl/gl.h>
#endif
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include "../../../renderable.h"
#include "../../../vertex.h"
#include "../../../transformable.h"
#include "../../../optional.h"
#include "../../../gfx/vbo.h"
#include "../../../gfx/vao.h"
#include "../../../gfx/texture.h"
#include "../../../gfx/shader.h"
#include <stdint.h>
#include <vector>
namespace simpleengine::objects_2d::shapes {
class Square : public simpleengine::Renderable, public simpleengine::Transformable {
private:
gfx::Shader shader;
nonstd::optional<gfx::Texture> texture;
public:
std::vector<Vertex> vertices;
std::vector<GLuint> indicies;
gfx::VBO ebo;
gfx::VBO vbo;
gfx::VAO vao;
Square(gfx::Shader shader, std::vector<Vertex> vertices, std::vector<GLuint> indicies);
Square(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices, std::vector<GLuint> indicies);
virtual ~Square() = default;
void set_texture(gfx::Texture texture);
virtual void update(const float& delta_time) override;
virtual void render(GLFWwindow* target) override;
};
}

View File

@ -1,47 +0,0 @@
#pragma once
#ifdef __linux__
#include <GL/glew.h>
#include <GL/gl.h>
#elif
#include <gl/glew.h>
#include <gl/gl.h>
#endif
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include "../../../renderable.h"
#include "../../../vertex.h"
#include "../../../gfx/vbo.h"
#include "../../../gfx/vao.h"
#include "../../../gfx/shader.h"
#include "../../../gfx/texture.h"
#include "../../../optional.h"
#include "../../../transformable.h"
#include <vector>
namespace simpleengine::objects_2d::shapes {
class Triangle : public simpleengine::Renderable, public simpleengine::Transformable {
private:
gfx::Shader shader; // This only stores the shader program
nonstd::optional<gfx::Texture> texture;
public:
std::vector<Vertex> vertices;
gfx::VBO vbo;
gfx::VAO vao;
Triangle(gfx::Shader shader, std::vector<Vertex> vertices);
Triangle(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices);
virtual ~Triangle() = default;
void set_texture(gfx::Texture texture);
virtual void update(const float& delta_time) override;
virtual void render(GLFWwindow* target) override;
};
}

View File

@ -14,9 +14,10 @@
#include "../../gfx/vbo.h" #include "../../gfx/vbo.h"
#include "../../gfx/shader.h" #include "../../gfx/shader.h"
#include "../../gfx/texture.h" #include "../../gfx/texture.h"
#include "../../gfx/textured_model.h"
namespace simpleengine::objects_3d { namespace simpleengine::objects_3d {
class ObjModel : public simpleengine::Renderable, public simpleengine::Transformable { class ObjModel : public simpleengine::gfx::TexturedModel {
private: private:
std::vector<std::string> split_string(std::string str, const char delim) { std::vector<std::string> split_string(std::string str, const char delim) {
std::istringstream ss(str); std::istringstream ss(str);
@ -50,31 +51,31 @@ namespace simpleengine::objects_3d {
out_normals.at(currentVertexIndex) = current_norm; out_normals.at(currentVertexIndex) = current_norm;
} }
nonstd::optional<gfx::Texture> texture; //nonstd::optional<gfx::Texture> texture;
public: public:
std::vector<simpleengine::Vertex> model_vertices; /* std::vector<simpleengine::Vertex> model_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;
gfx::Shader shader; gfx::Shader shader; */
ObjModel(gfx::Shader shader, std::string filename) : ObjModel(shader, std::ifstream(filename, std::ios::in | std::ios::binary)) { ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::string filename) :
ObjModel(window, shader, texture, std::ifstream(filename, std::ios::in | std::ios::binary)) {
} }
ObjModel(gfx::Shader shader, std::ifstream file_stream) : ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::ifstream file_stream) :
shader(shader), ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), simpleengine::gfx::TexturedModel(window, shader, texture, std::vector<Vertex>()) {
texture(nonstd::nullopt) {
if (!file_stream.is_open()) { if (!file_stream.is_open()) {
std::cerr << "File stream that was given to ObjModel::ObjModel is not open!" << std::endl; std::cerr << "File stream that was given to ObjModel::ObjModel is not open!" << std::endl;
throw std::runtime_error("Failed to open ObjModel model file"); throw std::runtime_error("Failed to open ObjModel model file");
} }
std::vector<glm::vec3> vertices; std::vector<glm::vec3> obj_vertices;
std::vector<glm::vec2> read_textures; std::vector<glm::vec2> obj_textures;
std::vector<glm::vec3> read_normals; std::vector<glm::vec3> obj_normals;
std::vector<glm::vec2> textures; std::vector<glm::vec2> textures;
std::vector<glm::vec3> normals; std::vector<glm::vec3> normals;
@ -85,13 +86,13 @@ namespace simpleengine::objects_3d {
if (line_tokens.front() == "v") { if (line_tokens.front() == "v") {
//glm::vec3 vertex(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); //glm::vec3 vertex(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3]));
vertices.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); obj_vertices.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3]));
} else if (line_tokens.front() == "vt") { } else if (line_tokens.front() == "vt") {
read_textures.emplace_back(stof(line_tokens[1]), stof(line_tokens[2])); obj_textures.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]));
} else if (line_tokens.front() == "vn") { } else if (line_tokens.front() == "vn") {
read_normals.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3])); obj_normals.emplace_back(stof(line_tokens[1]), stof(line_tokens[2]), stof(line_tokens[3]));
} else if (line_tokens.front() == "f") { } else if (line_tokens.front() == "f") {
auto size = vertices.size(); auto size = obj_vertices.size();
textures.resize(size); textures.resize(size);
normals.resize(size); normals.resize(size);
@ -110,19 +111,19 @@ namespace simpleengine::objects_3d {
std::vector<std::string> vertex2 = split_string(line_tokens[2], '/'); std::vector<std::string> vertex2 = split_string(line_tokens[2], '/');
std::vector<std::string> vertex3 = split_string(line_tokens[3], '/'); std::vector<std::string> vertex3 = split_string(line_tokens[3], '/');
process_vertex(vertex1, indicies, read_textures, read_normals, textures, normals); process_vertex(vertex1, indicies, obj_textures, obj_normals, textures, normals);
process_vertex(vertex2, indicies, read_textures, read_normals, textures, normals); process_vertex(vertex2, indicies, obj_textures, obj_normals, textures, normals);
process_vertex(vertex3, indicies, read_textures, read_normals, textures, normals); process_vertex(vertex3, indicies, obj_textures, obj_normals, textures, normals);
} while (std::getline(file_stream, line)); } while (std::getline(file_stream, line));
file_stream.close(); file_stream.close();
for (int i = 0; i < vertices.size(); i++) { for (int i = 0; i < obj_vertices.size(); i++) {
model_vertices.emplace_back(simpleengine::Vectorf(vertices.at(i)), glm::vec3(1.f), textures.at(i)); vertices.emplace_back(simpleengine::Vectorf(obj_vertices.at(i)), glm::vec3(1.f), textures.at(i));
} }
vao.bind(); vao.bind();
vbo.buffer(model_vertices.data(), 0, sizeof(Vertex) * model_vertices.size()); 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));
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position));
@ -136,8 +137,6 @@ namespace simpleengine::objects_3d {
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
this->translate(glm::vec3(0.f, -1.f, -15.f));
} }
void set_texture(gfx::Texture texture) { void set_texture(gfx::Texture texture) {
@ -148,7 +147,7 @@ namespace simpleengine::objects_3d {
this->rotate_y(0.5f); this->rotate_y(0.5f);
} }
virtual void render(GLFWwindow* target) override { /* virtual void render(GLFWwindow* target) override {
shader.use(); shader.use();
shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false); shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false);
@ -163,6 +162,6 @@ namespace simpleengine::objects_3d {
vao.bind(); vao.bind();
glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0);
} } */
}; };
} }

View File

@ -52,10 +52,6 @@ namespace simpleengine {
transform_matrix = glm::translate(transform_matrix, glm::vec3(x, y, z)); transform_matrix = glm::translate(transform_matrix, glm::vec3(x, y, z));
} }
virtual void translate(const float& x, const float& y, const float& z) {
transform_matrix = glm::translate(transform_matrix, glm::vec3(x, y, z));
}
virtual void translate(const glm::vec3& vec) { virtual void translate(const glm::vec3& vec) {
transform_matrix = glm::translate(transform_matrix, vec); transform_matrix = glm::translate(transform_matrix, vec);
} }

54
src/gfx/model.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "gfx/model.h"
namespace simpleengine::gfx {
Model::Model(GLFWwindow* window, gfx::Shader shader, std::vector<Vertex> vertices, std::vector<GLuint> indicies) :
simpleengine::Renderable(window), shader(shader), vertices(vertices), indicies(indicies), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)),
ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)) {
setup_vertexes();
}
Model::Model(GLFWwindow* window, std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices, std::vector<GLuint> indicies) :
Model(window, gfx::Shader(shader_program), vertices, indicies) {
}
void Model::setup_vertexes() {
vao.bind();
vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size());
if (!indicies.empty()) {
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);
}
void Model::update(const float& delta_time) {
//this->rotate_y(1.f);
}
void Model::render(GLFWwindow* target) {
shader.use();
shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false);
// When binding to the texture, also tell the shader if the texture is set or not.
shader.set_uniform_int("texture_is_set", (GLint) false, false);
vao.bind();
if (indicies.empty()) {
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
} else {
glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0);
}
}
}

View File

@ -0,0 +1,34 @@
#include "gfx/textured_model.h"
namespace simpleengine::gfx {
TexturedModel::TexturedModel(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, std::vector<Vertex> vertices,
std::vector<GLuint> indicies) : simpleengine::gfx::Model(window, shader, vertices, indicies), texture(texture) {
}
TexturedModel::TexturedModel(GLFWwindow* window, std::shared_ptr<GLuint> shader_program, gfx::Texture texture,
std::vector<Vertex> vertices, std::vector<GLuint> indicies) : TexturedModel(window, gfx::Shader(shader_program),
texture, vertices, indicies) {
}
void TexturedModel::update(const float& delta_time) {
}
void TexturedModel::render(GLFWwindow* target) {
shader.use();
shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false);
// When binding to the texture, tell the shader if the texture is set or not.
shader.set_uniform_int("texture_is_set", (GLint) true, false);
texture.bind();
vao.bind();
if (indicies.empty()) {
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
} else {
glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0);
}
}
}

View File

@ -1,55 +0,0 @@
#include "objects/2d/shapes/square.h"
namespace simpleengine::objects_2d::shapes {
Square::Square(gfx::Shader shader, std::vector<Vertex> vertices, std::vector<GLuint> indicies) :
simpleengine::Renderable(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(gfx::Texture texture) {
this->texture = texture;
}
void Square::update(const float& delta_time) {
this->rotate_y(1.f);
}
void Square::render(GLFWwindow* target) {
shader.use();
shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false);
// When binding to the texture, also tell the shader if the texture is set or not.
if (texture.has_value()) {
shader.set_uniform_int("texture_is_set", true, false);
texture.value().bind();
} else {
shader.set_uniform_int("texture_is_set", false, false);
}
vao.bind();
glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0);
}
}

View File

@ -1,56 +0,0 @@
#include "objects/2d/shapes/triangle.h"
namespace simpleengine::objects_2d::shapes {
Triangle::Triangle(gfx::Shader shader, std::vector<Vertex> vertices) : simpleengine::Renderable(nullptr),
simpleengine::Transformable(glm::mat4(1.f)), 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);
// rotate_z(90.f);
// scale(.5f);
}
Triangle::Triangle(std::shared_ptr<GLuint> shader_program, std::vector<Vertex> vertices) :
Triangle(gfx::Shader(shader_program), vertices) {
}
void Triangle::set_texture(gfx::Texture texture) {
this->texture = texture;
// Tell the shader that there is a texture set.
shader.set_uniform_int("texture_is_set", true);
}
void Triangle::update(const float& delta_time) {
/* for (Vertex& vertex : vertices) {
vertex.position.translate_x(0.01f);
vertex.position.translate_y(0.01f);
} */
}
void Triangle::render(GLFWwindow* target) {
shader.use();
shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false);
// When binding to the texture, also tell the shader if the texture is set or not.
if (texture.has_value()) {
shader.set_uniform_int("texture_is_set", true, false);
texture.value().bind();
} else {
shader.set_uniform_int("texture_is_set", false, false);
}
vao.bind();
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
}
}