Delete "Vertex" replace it with "LitVertex"

This commit is contained in:
SeanOMik 2022-08-18 16:03:51 -04:00
parent 0226819a46
commit 0a3d80d17c
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
10 changed files with 99 additions and 45 deletions

View File

@ -67,11 +67,11 @@ int main(int argc, char *argv[]) {
terrain->translate(0.f, -5.f, -15.f); terrain->translate(0.f, -5.f, -15.f);
game.add_event(terrain); */ game.add_event(terrain); */
se::gfx::Texture stall_texture("examples/dev_testing/resources/stallTextureb.png"); /* se::gfx::Texture stall_texture("examples/dev_testing/resources/stallTextureb.png");
auto stall = std::make_shared<se::objects_3d::Mesh>(game.get_window(), core_shader, stall_texture, "examples/dev_testing/resources/stall.obj"); auto stall = std::make_shared<se::objects_3d::Mesh>(game.get_window(), core_shader, stall_texture, "examples/dev_testing/resources/stall.obj");
stall->translate(10.f, -5.f, 0.f); stall->translate(10.f, -5.f, 0.f);
stall->rotate_y(90.f); stall->rotate_y(90.f);
game.add_event(stall); game.add_event(stall); */
/* std::vector<se::Vertex> square_vertices = { /* std::vector<se::Vertex> square_vertices = {
{ se::Vectorf(0.5f, 0.5f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, // top right { se::Vectorf(0.5f, 0.5f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, // top right
@ -107,7 +107,7 @@ int main(int argc, char *argv[]) {
tri->scale(.75f); tri->scale(.75f);
game.add_event(tri); */ game.add_event(tri); */
/* std::vector<se::Vertex> cube_vertices = { std::vector<se::LitVertex> cube_vertices = {
{ se::Vectorf(-1.f, -1.f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) }, { se::Vectorf(-1.f, -1.f, -1.f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(0.f, 0.f) },
{ se::Vectorf(1.f, -1.f, -1.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, { se::Vectorf(1.f, -1.f, -1.f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) },
{ se::Vectorf(1.f, 1.f, -1.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(2.f, 0.f) }, { se::Vectorf(1.f, 1.f, -1.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(2.f, 0.f) },
@ -137,8 +137,9 @@ int main(int argc, char *argv[]) {
}; };
auto cube = std::make_shared<se::gfx::Model>(game.get_window(), core_shader, cube_vertices, cube_indicies); auto cube = std::make_shared<se::gfx::Model>(game.get_window(), core_shader, cube_vertices, cube_indicies);
cube->translate(3.f, 0.f, 0.f); cube->calculate_normals();
game.add_event(cube); */ cube->translate(3.5f, 0.f, 0.f);
game.add_event(cube);
auto camera = std::make_shared<se::Camera>(game.get_window(), core_shader, 70, glm::vec3(0, 0, 0)); auto camera = std::make_shared<se::Camera>(game.get_window(), core_shader, 70, glm::vec3(0, 0, 0));
game.add_event(camera); game.add_event(camera);

View File

@ -12,7 +12,7 @@
namespace simpleengine::gfx { namespace simpleengine::gfx {
class Model : public simpleengine::Renderable, public simpleengine::Transformable { class Model : public simpleengine::Renderable, public simpleengine::Transformable {
public: public:
std::vector<Vertex> vertices; std::vector<LitVertex> vertices;
std::vector<GLuint> indicies; std::vector<GLuint> indicies;
gfx::VBO ebo; gfx::VBO ebo;
gfx::VBO vbo; gfx::VBO vbo;
@ -20,14 +20,25 @@ namespace simpleengine::gfx {
gfx::Shader shader; gfx::Shader shader;
Model(GLFWwindow* window, gfx::Shader shader, std::vector<Vertex> vertices, std::vector<GLuint> indicies = std::vector<GLuint>()); Model(GLFWwindow* window, gfx::Shader shader, std::vector<LitVertex> vertices, std::vector<GLuint> indicies = std::vector<GLuint>());
Model(GLFWwindow* window, GLuint shader_program, std::vector<Vertex> vertices, Model(GLFWwindow* window, GLuint shader_program, std::vector<LitVertex> vertices,
std::vector<GLuint> indicies = std::vector<GLuint>()); std::vector<GLuint> indicies = std::vector<GLuint>());
protected: protected:
virtual void setup_vertices(); virtual void setup_vertices();
public: public:
virtual void update(const float& delta_time) override; virtual void update(const float& delta_time) override;
virtual void render(GLFWwindow* target) override; virtual void render(GLFWwindow* target) override;
private:
glm::vec3 compute_face_normal(const glm::vec3& p1, const glm::vec3& p2, const glm::vec3& p3);
public:
/**
* @brief Calculate the normals of the model.
*
* @note This **will** overwrite the existing normals.
*
*/
void calculate_normals();
}; };
} }

View File

@ -21,14 +21,14 @@ namespace simpleengine::gfx {
public: public:
std::vector<gfx::Texture> textures; std::vector<gfx::Texture> textures;
TexturedModel(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, std::vector<Vertex> vertices, TexturedModel(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, std::vector<LitVertex> vertices,
std::vector<GLuint> indicies = std::vector<GLuint>()); std::vector<GLuint> indicies = std::vector<GLuint>());
TexturedModel(GLFWwindow* window, GLuint shader_program, gfx::Texture texture, std::vector<Vertex> vertices, TexturedModel(GLFWwindow* window, GLuint shader_program, gfx::Texture texture, std::vector<LitVertex> vertices,
std::vector<GLuint> indicies = std::vector<GLuint>()); std::vector<GLuint> indicies = std::vector<GLuint>());
TexturedModel(GLFWwindow* window, gfx::Shader shader, std::vector<gfx::Texture> textures, std::vector<Vertex> vertices, TexturedModel(GLFWwindow* window, gfx::Shader shader, std::vector<gfx::Texture> textures, std::vector<LitVertex> vertices,
std::vector<GLuint> indicies = std::vector<GLuint>()); std::vector<GLuint> indicies = std::vector<GLuint>());
TexturedModel(GLFWwindow* window, GLuint shader_program, std::vector<gfx::Texture> textures, std::vector<Vertex> vertices, TexturedModel(GLFWwindow* window, GLuint shader_program, std::vector<gfx::Texture> textures, std::vector<LitVertex> vertices,
std::vector<GLuint> indicies = std::vector<GLuint>()); std::vector<GLuint> indicies = std::vector<GLuint>());
virtual void update(const float& delta_time) override; virtual void update(const float& delta_time) override;

View File

@ -65,5 +65,20 @@ namespace simpleengine::gfx {
// don't unbind VAOs (nor VBOs) when it's not directly necessary. // don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0); glBindVertexArray(0);
} }
void disable_attrib(const VBO& vbo, GLuint index) const {
bind();
vbo.bind();
glDisableVertexAttribArray(index);
//glDisableVertexArrayAttrib(index);
}
void set_attrib_value(const VBO& vbo, GLuint index, float f) const {
bind();
vbo.bind();
glVertexAttrib1f(index, f);
}
}; };
} }

View File

@ -26,7 +26,7 @@ namespace simpleengine::objects_3d {
gfx::Texture texture; gfx::Texture texture;
Terrain(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, int grid_x, int grid_y, float size = 800.f, int vertex_count = 128) : Terrain(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, int grid_x, int grid_y, float size = 800.f, int vertex_count = 128) :
simpleengine::gfx::TexturedModel(window, shader, { texture }, std::vector<Vertex>()), x(grid_x * size), y(grid_y * size), simpleengine::gfx::TexturedModel(window, shader, { texture }, std::vector<LitVertex>()), x(grid_x * size), y(grid_y * size),
texture(texture), size(size), vertex_count(vertex_count) { texture(texture), size(size), vertex_count(vertex_count) {
generate_terrain(); generate_terrain();

View File

@ -14,20 +14,7 @@
#include "vector.h" #include "vector.h"
namespace simpleengine { namespace simpleengine {
class Vertex {
public:
simpleengine::Vectorf position;
glm::vec3 color;
glm::vec2 tex_coord;
float texture_id = 0.f;
Vertex() = default;
Vertex(simpleengine::Vectorf position, glm::vec3 color, glm::vec2 tex_coord, int texture_id = 0) :
position(position), color(color), tex_coord(tex_coord), texture_id((float) texture_id) {
}
};
/** /**
* @brief A `Vertex` that can be lit up. * @brief A `Vertex` that can be lit up.
@ -39,16 +26,21 @@ namespace simpleengine {
glm::vec3 color; glm::vec3 color;
glm::vec2 tex_coord; glm::vec2 tex_coord;
glm::vec3 normal; glm::vec3 normal;
float texture_id = 0.f; float texture_id = -1.f;
LitVertex() = default; LitVertex() = default;
LitVertex(simpleengine::Vectorf position, glm::vec3 color, glm::vec2 tex_coord, glm::vec3 normal, int texture_id = 0) : LitVertex(simpleengine::Vectorf position, glm::vec3 color, glm::vec2 tex_coord, int texture_id = -1.f) :
position(position), color(color), tex_coord(tex_coord), normal(glm::vec3(0.f)), texture_id((float) texture_id) {
}
LitVertex(simpleengine::Vectorf position, glm::vec3 color, glm::vec2 tex_coord, glm::vec3 normal, int texture_id = -1.f) :
position(position), color(color), tex_coord(tex_coord), normal(normal), texture_id((float) texture_id) { position(position), color(color), tex_coord(tex_coord), normal(normal), texture_id((float) texture_id) {
} }
LitVertex(simpleengine::Vectorf position, glm::vec2 tex_coord, glm::vec3 normal, int texture_id = 0) : LitVertex(simpleengine::Vectorf position, glm::vec2 tex_coord, glm::vec3 normal, int texture_id = -1.f) :
position(position), color(glm::vec3(1.f)), tex_coord(tex_coord), normal(normal), texture_id((float) texture_id) { position(position), color(glm::vec3(1.f)), tex_coord(tex_coord), normal(normal), texture_id((float) texture_id) {
} }

View File

@ -36,10 +36,10 @@ void main() {
float reflectivity = u_texture_reflectivity[id]; float reflectivity = u_texture_reflectivity[id];
vec3 final_specular = calculate_specular(unit_normal, shine_damper, reflectivity); vec3 final_specular = calculate_specular(unit_normal, shine_damper, reflectivity);
fs_color = vec4(diffuse, 1.f) * texture(u_textures[1], vs_texcoord) + vec4(final_specular, 1.f); fs_color = vec4(diffuse, 1.f) * texture(u_textures[id], vs_texcoord) + vec4(final_specular, 1.f);
//fs_color = texture(u_textures[1], vs_texcoord); //fs_color = texture(u_textures[1], vs_texcoord);
} else { } else {
fs_color = vec4(diffuse, 1.f) * vec4(vs_color, 1.f); // We don't add any reflectivity to solid colored vectors. fs_color = vec4(vs_color, 1.f); // We don't add any reflectivity to solid colored vectors.
} }
} }

View File

@ -1,31 +1,36 @@
#include "gfx/model.h" #include "gfx/model.h"
namespace simpleengine::gfx { namespace simpleengine::gfx {
Model::Model(GLFWwindow* window, gfx::Shader shader, std::vector<Vertex> vertices, std::vector<GLuint> indicies) : Model::Model(GLFWwindow* window, gfx::Shader shader, std::vector<LitVertex> vertices, std::vector<GLuint> indicies) :
simpleengine::Renderable(window), shader(shader), vertices(vertices), indicies(indicies), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)), simpleengine::Renderable(window), shader(shader), vertices(vertices), indicies(indicies), vbo(gfx::VBO(GL_ARRAY_BUFFER, false)),
ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)) { ebo(gfx::VBO(GL_ELEMENT_ARRAY_BUFFER, false)) {
setup_vertices(); setup_vertices();
} }
Model::Model(GLFWwindow* window, GLuint shader_program, std::vector<Vertex> vertices, std::vector<GLuint> indicies) : Model::Model(GLFWwindow* window, GLuint shader_program, std::vector<LitVertex> vertices, std::vector<GLuint> indicies) :
Model(window, gfx::Shader(shader_program), vertices, indicies) { Model(window, gfx::Shader(shader_program), vertices, indicies) {
} }
void Model::setup_vertices() { void Model::setup_vertices() {
vao.bind(); vao.bind();
vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); vbo.buffer(vertices.data(), 0, sizeof(LitVertex) * vertices.size());
if (!indicies.empty()) { if (!indicies.empty()) {
ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint)); ebo.buffer(indicies.data(), 0, indicies.size() * sizeof(GLuint));
} }
// Enable VAO attributes // Enable VAO attributes
vao.enable_attrib(vbo, 0, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, position)); vao.enable_attrib(vbo, 0, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, position));
vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, color)); vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, color));
vao.enable_attrib(vbo, 2, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, normal));
// Attribute 2 is used for normals // Attribute 2 is used for normals
vao.enable_attrib(vbo, 3, 2, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, tex_coord)); vao.enable_attrib(vbo, 3, 2, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, tex_coord));
vao.enable_attrib(vbo, 4, 1, GL_FLOAT, sizeof(Vertex), offsetof(Vertex, texture_id)); vao.enable_attrib(vbo, 4, 1, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, texture_id));
/* vao.disable_attrib(vbo, 2);
vao.disable_attrib(vbo, 4);
vao.set_attrib_value(vbo, 4, -1.f); */
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
@ -49,4 +54,34 @@ namespace simpleengine::gfx {
glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, indicies.size(), GL_UNSIGNED_INT, 0);
} }
} }
glm::vec3 Model::compute_face_normal(const glm::vec3& p1, const glm::vec3& p2, const glm::vec3& p3) {
// Uses p2 as a new origin for p1,p3
auto a = p3 - p2;
auto b = p1 - p2;
// Compute the cross product a X b to get the face normal
return glm::normalize(glm::cross(a, b));
}
void Model::calculate_normals() {
std::vector<glm::vec3> normals = std::vector<glm::vec3>(vertices.size());
for (int i = 0; i < indicies.size(); i+=3) {
const glm::vec3& a = vertices[indicies[i]].position;
const glm::vec3& b = vertices[indicies[i + 1]].position;
const glm::vec3& c = vertices[indicies[i + 2]].position;
glm::vec3 normal = compute_face_normal(a, b, c);
normals[indicies[i]] += normal;
normals[indicies[i + 1]] += normal;
normals[indicies[i + 2]] += normal;
}
for (int i = 0; i < normals.size(); i++) {
normals[i] = glm::normalize(normals[i]);
vertices[i].normal = normals[i];
}
}
} }

View File

@ -2,24 +2,24 @@
#include "gfx/ssbo.h" #include "gfx/ssbo.h"
namespace simpleengine::gfx { namespace simpleengine::gfx {
TexturedModel::TexturedModel(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, std::vector<Vertex> vertices, TexturedModel::TexturedModel(GLFWwindow* window, gfx::Shader shader, gfx::Texture texture, std::vector<LitVertex> vertices,
std::vector<GLuint> indicies) : simpleengine::gfx::Model(window, shader, vertices, indicies), std::vector<GLuint> indicies) : simpleengine::gfx::Model(window, shader, vertices, indicies),
textures(std::vector<gfx::Texture>{texture}) { textures(std::vector<gfx::Texture>{texture}) {
} }
TexturedModel::TexturedModel(GLFWwindow* window, GLuint shader_program, gfx::Texture texture, TexturedModel::TexturedModel(GLFWwindow* window, GLuint shader_program, gfx::Texture texture,
std::vector<Vertex> vertices, std::vector<GLuint> indicies) : TexturedModel(window, gfx::Shader(shader_program), std::vector<LitVertex> vertices, std::vector<GLuint> indicies) : TexturedModel(window, gfx::Shader(shader_program),
std::vector<gfx::Texture>{texture}, vertices, indicies) { std::vector<gfx::Texture>{texture}, vertices, indicies) {
} }
TexturedModel::TexturedModel(GLFWwindow* window, gfx::Shader shader, std::vector<gfx::Texture> textures, std::vector<Vertex> vertices, TexturedModel::TexturedModel(GLFWwindow* window, gfx::Shader shader, std::vector<gfx::Texture> textures, std::vector<LitVertex> vertices,
std::vector<GLuint> indicies) : simpleengine::gfx::Model(window, shader, vertices, indicies), std::vector<GLuint> indicies) : simpleengine::gfx::Model(window, shader, vertices, indicies),
textures(textures) { textures(textures) {
} }
TexturedModel::TexturedModel(GLFWwindow* window, GLuint shader_program, std::vector<gfx::Texture> textures, TexturedModel::TexturedModel(GLFWwindow* window, GLuint shader_program, std::vector<gfx::Texture> textures,
std::vector<Vertex> vertices, std::vector<GLuint> indicies) : TexturedModel(window, gfx::Shader(shader_program), std::vector<LitVertex> vertices, std::vector<GLuint> indicies) : TexturedModel(window, gfx::Shader(shader_program),
textures, vertices, indicies) { textures, vertices, indicies) {
} }

View File

@ -41,7 +41,7 @@ namespace simpleengine::objects_3d {
} }
Mesh::Mesh(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::ifstream file_stream) : Mesh::Mesh(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::ifstream file_stream) :
simpleengine::gfx::TexturedModel(window, shader, std::vector<gfx::Texture>{texture}, std::vector<Vertex>()) { simpleengine::gfx::TexturedModel(window, shader, std::vector<gfx::Texture>{texture}, std::vector<LitVertex>()) {
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;