Add engine resources to store core shaders, create core shader class

This commit is contained in:
SeanOMik 2021-12-12 22:21:10 -05:00
parent a507e2f169
commit db94d85168
6 changed files with 172 additions and 58 deletions

View File

@ -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})

View File

@ -12,6 +12,7 @@
#include <simpleengine/game.h>
#include <simpleengine/vertex.h>
#include <simpleengine/objects/3d/obj_model.h>
#include <simpleengine/gfx/shaders/core_3d_shader.h>
#include <chrono>
#include <iostream>
@ -23,6 +24,8 @@ CMRC_DECLARE(resource_shaders);
#include <SOIL2/SOIL2.h>
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<simpleengine::gfx::Light>(core_shader, glm::vec3(0.f, 0.f, -20.f), glm::vec3(1.f, 1.f, 1.f));
auto light = std::make_shared<se::gfx::Light>(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<simpleengine::objects_3d::ObjModel>(game.get_window(), core_shader, white_texture, "resources/dragon.obj");
auto dragon = std::make_shared<se::objects_3d::ObjModel>(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<simpleengine::objects_3d::ObjModel>(game.get_window(), core_shader, stall_texture, "resources/stall.obj");
/* se::gfx::Texture stall_texture("resources/stallTexture.png");
auto stall = std::make_shared<se::objects_3d::ObjModel>(game.get_window(), core_shader, stall_texture, "resources/stall.obj");
stall->translate(0.f, -4.f, -25.f);
game.add_event(stall); */
/* 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) }, // 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<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(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<GLuint> indicies = {
@ -75,52 +69,52 @@ int main(int argc, char *argv[]) {
1, 2, 3
};
auto square = std::make_shared<simpleengine::gfx::Model>(game.get_window(), core_shader, square_vertices, indicies);
auto square = std::make_shared<se::gfx::Model>(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<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
std::vector<se::Vertex> 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<simpleengine::gfx::Model>(game.get_window(), core_shader, tri_vertices);
auto tri = std::make_shared<se::gfx::Model>(game.get_window(), core_shader, tri_vertices);
tri->translate(-1.25f, 0.f, -1.f);
tri->scale(.75f);
game.add_event(tri); */
/* std::vector<simpleengine::Vertex> 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<se::Vertex> 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<GLuint> cube_indicies = {
@ -138,10 +132,10 @@ int main(int argc, char *argv[]) {
23,21,22
};
auto cube = std::make_shared<simpleengine::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);
game.add_event(cube); */
auto camera = std::make_shared<simpleengine::Camera>(game.get_window(), core_shader, 70, glm::vec3(0, 0, -10));
auto camera = std::make_shared<se::Camera>(game.get_window(), core_shader, 70, glm::vec3(0, 0, -10));
game.add_event(camera);
return game.run();

View File

@ -0,0 +1,32 @@
#pragma once
#include "../shader.h"
#include <cmrc/cmrc.hpp>
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();
}
};
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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)
}
}