From db94d85168a692109753bc48117aa02cd59e33a0 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sun, 12 Dec 2021 22:21:10 -0500 Subject: [PATCH] Add engine resources to store core shaders, create core shader class --- CMakeLists.txt | 11 ++ examples/dev_testing/src/main.cpp | 108 +++++++++--------- .../simpleengine/gfx/shaders/core_3d_shader.h | 32 ++++++ resources/shaders/fragment_core.glsl | 46 ++++++++ resources/shaders/vertex_core.glsl | 31 +++++ src/objects/3d/obj_model.cpp | 2 +- 6 files changed, 172 insertions(+), 58 deletions(-) create mode 100644 include/simpleengine/gfx/shaders/core_3d_shader.h create mode 100644 resources/shaders/fragment_core.glsl create mode 100644 resources/shaders/vertex_core.glsl diff --git a/CMakeLists.txt b/CMakeLists.txt index 72db49d..c2933aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,11 +19,22 @@ add_library(simpleengine STATIC ${source_list}) # Link headers target_include_directories(simpleengine PUBLIC include PRIVATE include/simpleengine) +# Embed shaders +file(GLOB_RECURSE simpleeng_resources_list resources/**) +cmrc_add_resource_library( + simpleengine_resources# ALIAS simpleengine::res::shaders + #NAMESPACE simpleengine__res + #WHENCE resources/shaders + #PREFIX resources/shaders + ${simpleeng_resources_list} +) + # Link dependencies target_link_libraries(simpleengine PUBLIC GLEW::GLEW) target_link_libraries(simpleengine PUBLIC glfw) target_link_libraries(simpleengine PUBLIC glm::glm) target_link_libraries(simpleengine PUBLIC soil2) +target_link_libraries(simpleengine PRIVATE simpleengine_resources) include_directories(${GLM_INCLUDE_DIRS}) diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index 13be6c7..c89a833 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -23,6 +24,8 @@ CMRC_DECLARE(resource_shaders); #include +namespace se = simpleengine; + std::string read_resource_shader(const std::string& path) { auto fs = cmrc::resource_shaders::get_filesystem(); cmrc::file vertex_file = fs.open(path); @@ -31,43 +34,34 @@ std::string read_resource_shader(const std::string& path) { } int main(int argc, char *argv[]) { - simpleengine::Game game(640, 480, "SimpleEngine 3D OpenGL - Developer Testing", GLFW_OPENGL_CORE_PROFILE, 4, 4, false); + se::Game game(640, 480, "SimpleEngine 3D OpenGL - Developer Testing", GLFW_OPENGL_CORE_PROFILE, 4, 4, false); - // Load shaders - std::string vertex_core = read_resource_shader("shaders/vertex_core.glsl"); - std::string fragment_core = read_resource_shader("shaders/fragment_core.glsl"); + /* se::gfx::Texture wall_texture("resources/wall.jpg"); + se::gfx::Texture crate_texture("resources/container.jpg", true, true); */ - // Compile shader program - simpleengine::ShaderProgram shader_prog; - shader_prog.add_shader_from_source(simpleengine::gfx::ShaderType::ST_Vertex, vertex_core); - shader_prog.add_shader_from_source(simpleengine::gfx::ShaderType::ST_Fragment, fragment_core); - shader_prog.link(); - simpleengine::gfx::Shader core_shader = shader_prog.shaders.front(); + // Load core shaders from simpleengine resources + se::gfx::shaders::Core3dShader core_shader; - /* simpleengine::gfx::Texture wall_texture("resources/wall.jpg"); - simpleengine::gfx::Texture crate_texture("resources/container.jpg", true, true); */ - - - auto light = std::make_shared(core_shader, glm::vec3(0.f, 0.f, -20.f), glm::vec3(1.f, 1.f, 1.f)); + 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.png"); + se::gfx::Texture white_texture("examples/dev_testing/resources/white_texture.png"); /* white_texture.shine_damper = 10; white_texture.reflectivity = 1; */ - auto dragon = std::make_shared(game.get_window(), core_shader, white_texture, "resources/dragon.obj"); + auto dragon = std::make_shared(game.get_window(), core_shader, white_texture, "examples/dev_testing/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"); + /* se::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, -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 - { 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, 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 + /* std::vector 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(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, // bottom right + { se::Vectorf(-0.5f, -0.5f, -1.f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.5f, 1.0f) }, // bottom left + { se::Vectorf(-0.5f, 0.5f, -1.f), glm::vec3(.5f, 0.5f, 0.f), glm::vec2(0.5f, 1.0f) }, // top left }; std::vector indicies = { @@ -75,52 +69,52 @@ int main(int argc, char *argv[]) { 1, 2, 3 }; - auto square = std::make_shared(game.get_window(), core_shader, square_vertices, indicies); + auto square = std::make_shared(game.get_window(), core_shader, square_vertices, indicies); square->translate(1.25f, 0.f, -1.f); square->scale(.75f); game.add_event(square); - std::vector 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 + std::vector tri_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(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, // bottom right + { se::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(game.get_window(), core_shader, tri_vertices); + auto tri = std::make_shared(game.get_window(), core_shader, tri_vertices); tri->translate(-1.25f, 0.f, -1.f); tri->scale(.75f); game.add_event(tri); */ - /* std::vector cube_vertices = { - { simpleengine::Vectorf(-0.5f,0.5f,-0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, - { simpleengine::Vectorf(-0.5f,-0.5f,-0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, - { simpleengine::Vectorf(0.5f,-0.5f,-0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, - { simpleengine::Vectorf(0.5f,0.5f,-0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, + /* std::vector cube_vertices = { + { se::Vectorf(-0.5f,0.5f,-0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, + { se::Vectorf(-0.5f,-0.5f,-0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, + { se::Vectorf(0.5f,-0.5f,-0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, + { se::Vectorf(0.5f,0.5f,-0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, - { simpleengine::Vectorf(-0.5f,0.5f,0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, - { simpleengine::Vectorf(-0.5f,-0.5f,0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, - { simpleengine::Vectorf(0.5f,-0.5f,0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, - { simpleengine::Vectorf(0.5f,0.5f,0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, + { se::Vectorf(-0.5f,0.5f,0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, + { se::Vectorf(-0.5f,-0.5f,0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, + { se::Vectorf(0.5f,-0.5f,0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, + { se::Vectorf(0.5f,0.5f,0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, - { simpleengine::Vectorf(0.5f,0.5f,-0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, - { simpleengine::Vectorf(0.5f,-0.5f,-0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, - { simpleengine::Vectorf(0.5f,-0.5f,0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, - { simpleengine::Vectorf(0.5f,0.5f,0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, + { se::Vectorf(0.5f,0.5f,-0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, + { se::Vectorf(0.5f,-0.5f,-0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, + { se::Vectorf(0.5f,-0.5f,0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, + { se::Vectorf(0.5f,0.5f,0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, - { simpleengine::Vectorf(-0.5f,0.5f,-0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, - { simpleengine::Vectorf(-0.5f,-0.5f,-0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, - { simpleengine::Vectorf(-0.5f,-0.5f,0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, - { simpleengine::Vectorf(-0.5f,0.5f,0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, + { se::Vectorf(-0.5f,0.5f,-0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, + { se::Vectorf(-0.5f,-0.5f,-0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, + { se::Vectorf(-0.5f,-0.5f,0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, + { se::Vectorf(-0.5f,0.5f,0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, - { simpleengine::Vectorf(-0.5f,0.5f,0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, - { simpleengine::Vectorf(-0.5f,0.5f,-0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, - { simpleengine::Vectorf(0.5f,0.5f,-0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, - { simpleengine::Vectorf(0.5f,0.5f,0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, + { se::Vectorf(-0.5f,0.5f,0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, + { se::Vectorf(-0.5f,0.5f,-0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, + { se::Vectorf(0.5f,0.5f,-0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, + { se::Vectorf(0.5f,0.5f,0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, - { simpleengine::Vectorf(-0.5f,-0.5f,0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, - { simpleengine::Vectorf(-0.5f,-0.5f,-0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, - { simpleengine::Vectorf(0.5f,-0.5f,-0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, - { simpleengine::Vectorf(0.5f,-0.5f,0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, + { se::Vectorf(-0.5f,-0.5f,0.5f), glm::vec3(1.f, 0.f, 0.f), glm::vec2(1.f, 1.f) }, + { se::Vectorf(-0.5f,-0.5f,-0.5f), glm::vec3(0.f, 1.f, 0.f), glm::vec2(1.f, 0.f) }, + { se::Vectorf(0.5f,-0.5f,-0.5f), glm::vec3(0.f, 0.f, 1.f), glm::vec2(0.f, 0.f) }, + { se::Vectorf(0.5f,-0.5f,0.5f), glm::vec3(1.f, 1.f, 0.f), glm::vec2(0.f, 1.f) }, }; std::vector cube_indicies = { @@ -138,10 +132,10 @@ int main(int argc, char *argv[]) { 23,21,22 }; - auto cube = std::make_shared(game.get_window(), core_shader, cube_vertices, cube_indicies); + 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, 70, glm::vec3(0, 0, -10)); + 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/shaders/core_3d_shader.h b/include/simpleengine/gfx/shaders/core_3d_shader.h new file mode 100644 index 0000000..f68aa80 --- /dev/null +++ b/include/simpleengine/gfx/shaders/core_3d_shader.h @@ -0,0 +1,32 @@ +#pragma once + +#include "../shader.h" + +#include +CMRC_DECLARE(simpleengine_resources); + +namespace simpleengine::gfx::shaders { + class Core3dShader : public simpleengine::gfx::Shader { + private: + gfx::Shader fragment_shader; + public: + Core3dShader() { + this->program = glCreateProgram(); + + auto vertex_fs = cmrc::simpleengine_resources::get_filesystem(); + cmrc::file vertex_file = vertex_fs.open("resources/shaders/vertex_core.glsl"); + std::string vertex_source(vertex_file.begin()); + Shader vertex = gfx::Shader::from_source(program, ShaderType::ST_Vertex, vertex_source); + this->shader = vertex.shader; + + auto fragment_fs = cmrc::simpleengine_resources::get_filesystem(); + cmrc::file fragment_file = fragment_fs.open("resources/shaders/fragment_core.glsl"); + std::string fragment_source(fragment_file.begin()); + fragment_shader = gfx::Shader::from_source(program, ShaderType::ST_Fragment, fragment_source); + + link(); + delete_shader(); + fragment_shader.delete_shader(); + } + }; +} \ No newline at end of file diff --git a/resources/shaders/fragment_core.glsl b/resources/shaders/fragment_core.glsl new file mode 100644 index 0000000..d5fe2e6 --- /dev/null +++ b/resources/shaders/fragment_core.glsl @@ -0,0 +1,46 @@ +#version 440 + +in vec3 vs_position; +in mat4 vs_transform; +in vec2 vs_texcoord; +in vec3 vs_normal; +in vec3 vs_to_light; +in vec3 vs_to_camera; + +uniform bool texture_is_set; +uniform sampler2D vs_texture; +uniform vec3 light_color; +uniform float shine_damper; +uniform float reflectivity; + +out vec4 fs_color; + +void main() { + // Lighting + 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; + + // Specular lighting + // only do all this math is reflectivity is > 0 + vec3 final_specular = vec3(0.f); + if (reflectivity > 0) { + vec3 unit_vector_to_camera = normalize(vs_to_camera); + vec3 light_direction = -unit_vector_to_camera; + vec3 reflected_light_dir = reflect(light_direction, unit_normal); + float specular_factor = dot(reflected_light_dir, unit_vector_to_camera); + specular_factor = max(specular_factor, 0.f); + float damped_specular = pow(specular_factor, shine_damper); + final_specular = damped_specular * reflectivity * light_color; + } + + if (texture_is_set) { + //fs_color = vec4(0.5 * unit_normal + vec3(0.5), 1.f); // Visualize normals + fs_color = vec4(diffuse, 1.f) * texture(vs_texture, vs_texcoord) + vec4(final_specular, 1.f); + } else { + fs_color = vec4(diffuse, 1.f) + vec4(final_specular, 1.f); + } +} \ No newline at end of file diff --git a/resources/shaders/vertex_core.glsl b/resources/shaders/vertex_core.glsl new file mode 100644 index 0000000..42b9be3 --- /dev/null +++ b/resources/shaders/vertex_core.glsl @@ -0,0 +1,31 @@ +#version 440 + +layout (location = 0) in vec3 vertex_position; +layout (location = 1) in vec2 vertex_texcoord; +layout (location = 2) in vec3 vertex_normal; + +out vec3 vs_position; +out vec2 vs_texcoord; +out mat4 vs_transform; +out vec3 vs_normal; +out vec3 vs_to_light; +out vec3 vs_to_camera; + +uniform mat4 transform_matrix; +uniform mat4 view_matrix; +uniform mat4 projection_matrix; +uniform vec3 light_position; + +void main() { + vec4 world_pos = (transform_matrix * vec4(vertex_position, 1.f)); + + vs_position = world_pos.xyz; + vs_transform = transform_matrix; + vs_texcoord = vertex_texcoord; + + 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_to_camera = (inverse(view_matrix) * vec4(0.f, 0.f, 0.f, 1.f)).xyz - world_pos.xyz; +} \ No newline at end of file diff --git a/src/objects/3d/obj_model.cpp b/src/objects/3d/obj_model.cpp index 70f5ad0..046139e 100644 --- a/src/objects/3d/obj_model.cpp +++ b/src/objects/3d/obj_model.cpp @@ -115,6 +115,6 @@ namespace simpleengine::objects_3d { } void ObjModel::update(const float& delta_time) { - this->rotate_y(0.5f); // Slowly rotate (for debugging) + this->rotate_y(0.05f); // Slowly rotate (for debugging) } } \ No newline at end of file