diff --git a/examples/dev_testing/resources/shaders/fragment_core.glsl b/examples/dev_testing/resources/shaders/fragment_core.glsl index b1ff237..68976ea 100644 --- a/examples/dev_testing/resources/shaders/fragment_core.glsl +++ b/examples/dev_testing/resources/shaders/fragment_core.glsl @@ -2,18 +2,27 @@ in vec3 vs_position; in mat4 vs_transform; -in vec3 vs_color; in vec2 vs_texcoord; +in vec3 vs_normal; +in vec3 vs_to_light; uniform bool texture_is_set; uniform sampler2D vs_texture; +uniform vec3 light_color; out vec4 fs_color; void main() { + vec3 unit_normal = normalize(vs_normal); + vec3 unit_light_vector = normalize(vs_to_light); + + float dot_prod = dot(unit_normal, unit_light_vector); + float brightness = max(dot_prod, 0.f); + vec3 diffuse = brightness * light_color; + if (texture_is_set) { - fs_color = texture(vs_texture, vs_texcoord) * vec4(vs_color, 1.0); + fs_color = vec4(diffuse, 1.f) * texture(vs_texture, vs_texcoord); } else { - fs_color = vec4(vs_color, 1.0); + fs_color = vec4(diffuse, 1.f); } } \ No newline at end of file diff --git a/examples/dev_testing/resources/shaders/vertex_core.glsl b/examples/dev_testing/resources/shaders/vertex_core.glsl index 222c2cc..b99b0f3 100644 --- a/examples/dev_testing/resources/shaders/vertex_core.glsl +++ b/examples/dev_testing/resources/shaders/vertex_core.glsl @@ -1,26 +1,32 @@ #version 440 layout (location = 0) in vec3 vertex_position; -layout (location = 1) in vec3 vertex_color; -layout (location = 2) in vec2 vertex_texcoord; +layout (location = 1) in vec2 vertex_texcoord; +layout (location = 2) in vec3 vertex_normal; out vec3 vs_position; -out vec3 vs_color; out vec2 vs_texcoord; out mat4 vs_transform; +out vec3 vs_normal; +out vec3 vs_to_light; uniform mat4 transform_matrix; uniform mat4 view_matrix; uniform mat4 projection_matrix; +uniform vec3 light_position; void main() { - vs_position = vertex_position; + vec4 world_pos = (transform_matrix * vec4(vertex_position, 1.f)); + + vs_position = world_pos.xyz; vs_transform = transform_matrix; - vs_color = vertex_color; vs_texcoord = vertex_texcoord; - gl_Position = projection_matrix * view_matrix * transform_matrix * vec4(vertex_position, 1.0f); + gl_Position = projection_matrix * view_matrix * world_pos; + vs_normal = (transform_matrix * vec4(vertex_normal, 0.f)).xyz; + vs_to_light = light_position - world_pos.xyz; + /* vs_position = vec4(transform_matrix * vec4(vertex_position, 1.f)).xyz; vs_transform = transform_matrix; vs_color = vertex_color; diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index 5be195e..c6efe2c 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -1,8 +1,10 @@ #include "simpleengine/camera.h" +#include "simpleengine/gfx/light.h" #include "simpleengine/gfx/model.h" #include "simpleengine/gfx/texture.h" #include "simpleengine/vector.h" #include +#include #include #include #include @@ -45,11 +47,19 @@ int main(int argc, char *argv[]) { /* simpleengine::gfx::Texture wall_texture("resources/wall.jpg"); simpleengine::gfx::Texture crate_texture("resources/container.jpg", true, true); */ - simpleengine::gfx::Texture stall_texture("resources/stallTexture.png"); + auto light = std::make_shared(core_shader, glm::vec3(0.f, 0.f, -20.f), glm::vec3(1.f, 1.f, 1.f)); + game.add_event(light); + + simpleengine::gfx::Texture white_texture("resources/white_texture.jpg"); + auto dragon = std::make_shared(game.get_window(), core_shader, white_texture, "resources/dragon.obj"); + dragon->translate(0.f, -5.f, -25.f); + game.add_event(dragon); + + /* simpleengine::gfx::Texture stall_texture("resources/stallTexture.png"); auto stall = std::make_shared(game.get_window(), core_shader, stall_texture, "resources/stall.obj"); - stall->translate(0.f, -4.f, -18.f); - game.add_event(stall); + stall->translate(0.f, -4.f, -25.f); + game.add_event(stall); */ /* std::vector square_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 @@ -129,7 +139,7 @@ int main(int argc, char *argv[]) { auto cube = std::make_shared(game.get_window(), core_shader, cube_vertices, cube_indicies); game.add_event(cube); */ - auto camera = std::make_shared(game.get_window(), core_shader); + auto camera = std::make_shared(game.get_window(), core_shader, 70, glm::vec3(0, 0, -10)); game.add_event(camera); return game.run(); diff --git a/include/simpleengine/gfx/light.h b/include/simpleengine/gfx/light.h new file mode 100644 index 0000000..6e6c3a4 --- /dev/null +++ b/include/simpleengine/gfx/light.h @@ -0,0 +1,30 @@ +#pragma once + +#include "shader.h" +#include "../renderable.h" + +#include + +namespace simpleengine::gfx { + class Light : public simpleengine::Renderable { + public: + gfx::Shader shader; + glm::vec3 position; + glm::vec3 color; + + Light(gfx::Shader shader, glm::vec3 position, glm::vec3 color) : shader(shader), position(position), color(color) { + + } + + virtual void update(const float& delta_time) override { + shader.use(); + shader.set_uniform_float_vec3("light_position", position, false); + shader.set_uniform_float_vec3("light_color", color, false); + shader.unuse(); + } + + virtual void render(GLFWwindow* target) override { + + } + }; +} \ No newline at end of file diff --git a/include/simpleengine/objects/3d/obj_model.h b/include/simpleengine/objects/3d/obj_model.h index 84f4164..1e935dd 100644 --- a/include/simpleengine/objects/3d/obj_model.h +++ b/include/simpleengine/objects/3d/obj_model.h @@ -22,7 +22,15 @@ namespace simpleengine::objects_3d { std::vector split_string(std::string str, const char delim); static void process_vertex(const std::vector& vertex_data, std::vector& indicies, const std::vector& in_textures, const std::vector& in_normals, std::vector& out_textures, std::vector& out_normals); + private: + /** + * @brief This is replaced with `lit_vertices`!!!! + * + */ + using simpleengine::gfx::Model::vertices; public: + std::vector lit_vertices; + ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::string filename); ObjModel(GLFWwindow *window, gfx::Shader shader, gfx::Texture texture, std::ifstream file_stream); diff --git a/include/simpleengine/transformable.h b/include/simpleengine/transformable.h index ec467d7..3623acc 100644 --- a/include/simpleengine/transformable.h +++ b/include/simpleengine/transformable.h @@ -93,7 +93,7 @@ namespace simpleengine { } virtual void scale(float scalar) { - transform_matrix = scalar * transform_matrix; + transform_matrix = glm::scale(transform_matrix, glm::vec3(scalar, scalar, scalar)); } }; } \ No newline at end of file diff --git a/include/simpleengine/vector.h b/include/simpleengine/vector.h index 73b984d..53bc814 100644 --- a/include/simpleengine/vector.h +++ b/include/simpleengine/vector.h @@ -11,6 +11,8 @@ namespace simpleengine { private: glm::vec<3, VectorType, glm::defaultp> inner_vec; public: + Vector() = default; + Vector(VectorType x, VectorType y, VectorType z) : inner_vec(x, y, z) { } diff --git a/include/simpleengine/vertex.h b/include/simpleengine/vertex.h index 665d1b7..98aec1b 100644 --- a/include/simpleengine/vertex.h +++ b/include/simpleengine/vertex.h @@ -1,5 +1,6 @@ #pragma once +#include #ifdef __linux__ #include #include @@ -15,13 +16,33 @@ namespace simpleengine { class Vertex { public: - //glm::vec3 position; simpleengine::Vectorf position; glm::vec3 color; glm::vec2 tex_coord; + Vertex() = default; + Vertex(simpleengine::Vectorf position, glm::vec3 color, glm::vec2 tex_coord) : position(position), color(color), tex_coord(tex_coord) { } }; + + /** + * @brief A `Vertex` that can be lit up. + * + */ + class LitVertex { + public: + simpleengine::Vectorf position; + glm::vec3 color; + glm::vec2 tex_coord; + glm::vec3 normal; + + LitVertex() = default; + + LitVertex(simpleengine::Vectorf position, glm::vec3 color, glm::vec2 tex_coord, glm::vec3 normal) : + position(position), color(color), tex_coord(tex_coord), normal(normal) { + + } + }; } \ No newline at end of file diff --git a/src/objects/3d/obj_model.cpp b/src/objects/3d/obj_model.cpp index 770a414..ed7b717 100644 --- a/src/objects/3d/obj_model.cpp +++ b/src/objects/3d/obj_model.cpp @@ -89,21 +89,17 @@ namespace simpleengine::objects_3d { file_stream.close(); for (int i = 0; i < obj_vertices.size(); i++) { - vertices.emplace_back(simpleengine::Vectorf(obj_vertices.at(i)), glm::vec3(1.f), textures.at(i)); + lit_vertices.emplace_back(simpleengine::Vectorf(obj_vertices.at(i)), glm::vec3(1.f), textures.at(i), normals.at(i)); } vao.bind(); - vbo.buffer(vertices.data(), 0, sizeof(Vertex) * vertices.size()); + vbo.buffer(lit_vertices.data(), 0, sizeof(LitVertex) * lit_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); + vao.enable_attrib(vbo, 0, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, position)); + //vao.enable_attrib(vbo, 1, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, color)); + vao.enable_attrib(vbo, 1, 2, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, tex_coord)); + vao.enable_attrib(vbo, 2, 3, GL_FLOAT, sizeof(LitVertex), offsetof(LitVertex, normal)); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0);