Compare commits

...

2 Commits

Author SHA1 Message Date
alistair 00084967ba shader templating 12 months ago
alistair 3a735b401d substitute shaders 12 months ago
  1. 37
      assets/shaders/deferred_lighting.frag
  2. 31
      assets/shaders/forward-frag.gl
  3. 48
      assets/shaders/g_frag.gl
  4. 29
      assets/shaders/screen.frag
  5. 33
      assets/shaders/uniformtest.vert
  6. 5
      assets/shaders/write/material_properties.gl
  7. 9
      assets/shaders/write/ubo_info.gl
  8. 3
      source/camera.hpp
  9. 130
      source/main.cpp
  10. 3
      source/mesh.cpp
  11. 42
      source/shaders.cpp
  12. 5
      source/shaders.h
  13. 12
      source/uniform-buffer.hpp
  14. 13
      source/window.cpp
  15. 10
      source/window.hpp

37
assets/shaders/deferred_lighting.frag

@ -13,35 +13,8 @@ struct light_info { @@ -13,35 +13,8 @@ struct light_info {
vec4 position;
};
layout (std140) uniform ubo_info
{
// alignment offset
vec4 view_pos; // 16 0
mat4 model; // 16 16
mat4 view; // 16 80
mat4 projection; // 16 144
vec4 offset; // 16 208
vec4 colour; // 16 224
int debug_shader; // 4 240
int instancing_enabled; // 4 244
};
layout (std140) uniform material_properties
{
// alignment offset
vec4 col_diffuse; // 16 0
vec4 col_spec; // 16 16
vec4 col_ambient; // 16 32
vec4 col_emissive; // 16 48
vec4 col_transparent; // 16 64
vec4 col_reflective; // 16 80
float reflectivity; // 4 96
bool twosided; // 4 100
float shininess; // 4 104
int max_lights; // 4 16112
int current_lights; // 4 16116
bool enable_fog;
light_info lights[500]; // 16 112
};
##ubo_info##
##material_properties##
float attenuation_val(float distance) {
float attenuation_constant = 1.0;
@ -71,7 +44,7 @@ vec3 apply_kernel(float kernel[9], sampler2D tex) { @@ -71,7 +44,7 @@ vec3 apply_kernel(float kernel[9], sampler2D tex) {
vec3 sampleTex[9];
for(int i = 0; i < 9; i++)
{
sampleTex[i] = vec3(texture(tex, TexCoords.st + offsets[i]).rgb);
sampleTex[i] = vec3(texture(tex, TexCoords.st + offsets[i]).a);
}
vec3 col = vec3(0.0);
for(int i = 0; i < 9; i++)
@ -169,14 +142,14 @@ if (enable_fog) { @@ -169,14 +142,14 @@ if (enable_fog) {
lighting = clamp(lighting, vec3(0), vec3(1));
}
if (length(edge) > 0.1) {
if (length(edge) > 0.2) {
edge = vec3(1);
} else {
edge = vec3(0);
}
FragColor = vec4(lighting, 1.0);
FragColor = vec4(lighting * (1-edge), 1.0);
}

31
assets/shaders/forward-frag.gl

@ -11,35 +11,8 @@ struct light_info { @@ -11,35 +11,8 @@ struct light_info {
vec4 position;
};
layout (std140) uniform ubo_info
{
// alignment offset
vec4 view_pos; // 16 0
mat4 model; // 16 16
mat4 view; // 16 80
mat4 projection; // 16 144
vec4 offset; // 16 208
vec4 colour; // 16 224
int debug_shader; // 4 240
int instancing_enabled; // 4 244
};
layout (std140) uniform material_properties
{
// alignment offset
vec4 col_diffuse; // 16 0
vec4 col_spec; // 16 16
vec4 col_ambient; // 16 32
vec4 col_emissive; // 16 48
vec4 col_transparent; // 16 64
vec4 col_reflective; // 16 80
float reflectivity; // 4 96
bool twosided; // 4 100
float shininess; // 4 104
int max_lights; // 4 16112
int current_lights; // 4 16116
bool enable_fog;
light_info lights[500]; // 16 112
};
##ubo_info##
##material_properties##
struct Material {
sampler2D texture_diffuse1;

48
assets/shaders/g_frag.gl

@ -13,51 +13,9 @@ struct light_info { @@ -13,51 +13,9 @@ struct light_info {
vec4 colour;
vec4 position;
};
layout (std140) uniform ubo_info
{
// alignment offset
vec4 view_pos; // 16 0
mat4 model; // 16 16
mat4 view; // 16 80
mat4 projection; // 16 144
vec4 offset; // 16 208
vec4 colour; // 16 224
int debug_shader; // 4 240
int instancing_enabled; // 4 244
};
layout (std140) uniform material_properties
{
// alignment offset
vec4 col_diffuse; // 16 0
vec4 col_spec; // 16 16
vec4 col_ambient; // 16 32
vec4 col_emissive; // 16 48
vec4 col_transparent; // 16 64
vec4 col_reflective; // 16 80
float reflectivity; // 4 96
bool twosided; // 4 100
float shininess; // 4 104
int max_lights; // 4 16112
int current_lights; // 4 16116
bool enable_fog;
light_info lights[500]; // 16 112
};
struct Material {
sampler2D texture_diffuse1;
sampler2D texture_diffuse2;
sampler2D texture_diffuse3;
sampler2D texture_specular1;
sampler2D texture_specular2;
sampler2D texture_specular3;
sampler2D texture_normal1;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float shininess;
};
##ubo_info##
##material_properties##
struct Material {
sampler2D texture_diffuse1;
@ -111,7 +69,7 @@ main() { @@ -111,7 +69,7 @@ main() {
gAlbedoSpec.rgb = toLinear(tex.rgb); // store specular intensity in gAlbedoSpec's alpha component
// linear depth
float near = 0.1;
float far = 1000;
float far = 500;
float ndc = gl_FragCoord.z * 2.0 - 1.0;
float linearDepth = (2.0 * near * far) / (far + near - ndc * (far - near));
gAlbedoSpec.a = linearDepth / far; // texture(material.texture_specular1, theTexCoord).r;

29
assets/shaders/screen.frag

@ -3,31 +3,22 @@ out vec4 FragColor; @@ -3,31 +3,22 @@ out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D screenTexture;
uniform float gamma = 2.2;
uniform float exposure = 1.0;
uniform bool use_hdr = false;
layout (std140) uniform material_properties
{
// alignment offset
vec4 col_diffuse; // 16 0
vec4 col_spec; // 16 16
vec4 col_ambient; // 16 32
vec4 col_emissive; // 16 48
vec4 col_transparent; // 16 64
vec4 col_reflective; // 16 80
float reflectivity; // 4 96
int twosided; // 4 100
float shininess; // 4 104
struct light_info {
vec4 colour;
vec4 position;
};
##ubo_info##
##material_properties##
uniform sampler2D screenTexture;
void main()
{
vec3 hdrcol = texture(screenTexture, TexCoords).rgb;
if (use_hdr) {
vec3 mapped = vec3(1.0) - exp((-hdrcol) * exposure);

33
assets/shaders/uniformtest.vert

@ -18,35 +18,10 @@ struct light_info { @@ -18,35 +18,10 @@ struct light_info {
vec4 position;
};
layout (std140) uniform ubo_info
{
// alignment offset
vec4 view_pos; // 16 0
mat4 model; // 16 16
mat4 view; // 16 80
mat4 projection; // 16 144
vec4 offset; // 16 208
vec4 colour; // 16 224
int debug_shader; // 4 240
int instancing_enabled; // 4 244
};
layout (std140) uniform material_properties
{
// alignment offset
vec4 col_diffuse; // 16 0
vec4 col_spec; // 16 16
vec4 col_ambient; // 16 32
vec4 col_emissive; // 16 48
vec4 col_transparent; // 16 64
vec4 col_reflective; // 16 80
float reflectivity; // 4 96
bool twosided; // 4 100
float shininess; // 4 104
int max_lights; // 4 16112
int current_lights; // 4 16116
bool enable_fog;
light_info lights[500]; // 16 112
};
##ubo_info##
##material_properties##
void main()
{
mat4 final_model = model;

5
assets/shaders/write/material_properties.gl

@ -8,5 +8,8 @@ layout (std140) uniform ubo_info @@ -8,5 +8,8 @@ layout (std140) uniform ubo_info
vec4 offset; // 16 208
vec4 colour; // 16 224
int debug_shader; // 4 240
int instancing_enabled; // 4 244
bool instancing_enabled; // 4 244
float gamma; // 4 248
float exposure; // 4 252
bool use_hdr; // 4 256
};

9
assets/shaders/write/ubo_info.gl

@ -8,7 +8,8 @@ layout (std140) uniform ubo_info @@ -8,7 +8,8 @@ layout (std140) uniform ubo_info
vec4 offset; // 16 208
vec4 colour; // 16 224
int debug_shader; // 4 240
int instancing_enabled; // 4 244
}; 4 244
};244
};
bool instancing_enabled; // 4 244
float gamma; // 4 248
float exposure; // 4 252
bool use_hdr; // 4 256
};

3
source/camera.hpp

@ -33,9 +33,10 @@ struct camera_third_person { @@ -33,9 +33,10 @@ struct camera_third_person {
glm::vec3 up = glm::vec3(0.0, 1.0, 0.0);
glm::vec3 direction = glm::vec3(0);
glm::vec3 right = glm::vec3(0);
// TODO: store in uniforms
float fov = 40.0;
float near_plane = 0.1;
float far_plane = 100000;
float far_plane = 10000;
glm::vec3 euler_angles;
void update_projection(float aspect_ratio) {

130
source/main.cpp

@ -251,6 +251,11 @@ struct g_buffer { @@ -251,6 +251,11 @@ struct g_buffer {
glCheckError();
}
void bind() {
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
glViewport(0,0,logical_width, logical_height);
}
};
@ -327,13 +332,16 @@ int main_loop(game_state& g) @@ -327,13 +332,16 @@ int main_loop(game_state& g)
auto forward_lighting = std::make_shared<shader_handle>(g.asset_dir / "shaders" / "uniformtest.vert",
g.asset_dir / "shaders" / "forward-frag.gl");
g.asset_dir / "shaders" / "forward-frag.gl",
g.shader_templates);
auto deferred_lighting = std::make_shared<shader_handle>(g.asset_dir / "shaders" / "screen.vert",
g.asset_dir / "shaders" / "deferred_lighting.frag");
g.asset_dir / "shaders" / "deferred_lighting.frag",
g.shader_templates);
auto g_shader =
std::make_shared<shader_handle>(g.asset_dir / "shaders" / "uniformtest.vert",
g.asset_dir / "shaders" / "g_frag.gl");
g.asset_dir / "shaders" / "g_frag.gl",
g.shader_templates);
auto forward_pass_shader = g_shader;
// set scaling
@ -342,30 +350,24 @@ int main_loop(game_state& g) @@ -342,30 +350,24 @@ int main_loop(game_state& g)
G.create_buffers();
glCheckError();
ubo_info s = {};
//ubo_proxy model_ubo (s);
material_properties m {};
ubo_proxy mat_ubo_s (m);
auto mat_ubo = std::make_shared<ubo_proxy<material_properties>>(std::move(m));
auto model_ubo = std::make_shared<ubo_proxy<ubo_info>>(s);
glCheckError();
auto output = fmt::output_file(std::string(g.asset_dir / "shaders" / "write" / fmt::format("{}.gl", model_ubo->block_name)));
output.print("{}", model_ubo->fmt_uniform_block());
fmt::output_file(std::string(g.asset_dir / "shaders" / "write" / fmt::format("{}.gl", mat_ubo->block_name))).print("{}", model_ubo->fmt_uniform_block());
auto output = fmt::output_file(std::string(g.asset_dir / "shaders" / "write" / fmt::format("{}.gl", g.model_ubo->block_name)));
output.print("{}", g.model_ubo->fmt_uniform_block());
fmt::output_file(std::string(g.asset_dir / "shaders" / "write" / fmt::format("{}.gl", g.material_ubo->block_name))).print("{}", g.model_ubo->fmt_uniform_block());
model_ubo->gen_ubo(2);
model_ubo->bind_to_shader(g_shader->program);
model_ubo->bind_to_shader(deferred_lighting->program);
model_ubo->bind_to_shader(forward_lighting->program);
model_ubo->update_all_fields_force();
g.model_ubo->gen_ubo(2);
g.model_ubo->bind_to_shader(g_shader->program);
g.model_ubo->bind_to_shader(deferred_lighting->program);
g.model_ubo->bind_to_shader(forward_lighting->program);
g.model_ubo->bind_to_shader(g.screen_shader->program);
g.model_ubo->update_all_fields_force();
mat_ubo->gen_ubo(3);
mat_ubo->bind_to_shader(g_shader->program);
mat_ubo->bind_to_shader(deferred_lighting->program);
mat_ubo->bind_to_shader(forward_lighting->program);
g.material_ubo->gen_ubo(3);
g.material_ubo->bind_to_shader(g_shader->program);
g.material_ubo->bind_to_shader(g.screen_shader->program);
g.material_ubo->bind_to_shader(deferred_lighting->program);
g.material_ubo->bind_to_shader(forward_lighting->program);
glCheckError();
camera_third_person cam {float(g.windowx)/g.windowy};
@ -450,12 +452,12 @@ int main_loop(game_state& g) @@ -450,12 +452,12 @@ int main_loop(game_state& g)
float z = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
// position component of transformation matrix
light_info l {glm::vec4(5 * x,5 * y, 5 * z,1), glm::vec4(pos[3][0], pos[3][1] + 2, pos[3][2], 0)};
mat_ubo->lights()[i] = l;
g.material_ubo->lights()[i] = l;
}
num_lights = lily_positions.size() + 1;
light_info camera_light {glm::vec4(2,2,2,0), glm::vec4(0,0,0,1)};
mat_ubo->lights()[0] = camera_light;
g.material_ubo->lights()[0] = camera_light;
for (int i = num_lights; i < max_lights; i++) {
float x = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
@ -466,11 +468,11 @@ int main_loop(game_state& g) @@ -466,11 +468,11 @@ int main_loop(game_state& g)
float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
// position component of transformation matrix
light_info l {glm::vec4(5 * x,5 * y, 5 * z,1), glm::vec4(p * extent, q * extent, r * extent, 0)};
mat_ubo->lights()[i] = l;
g.material_ubo->lights()[i] = l;
}
mat_ubo->update_all_fields_force();
mat_ubo->current_lights(3);
g.material_ubo->update_all_fields_force();
g.material_ubo->current_lights(3);
glDisable(GL_CULL_FACE);
double last_frame = SDL_GetTicks();
@ -484,9 +486,8 @@ int main_loop(game_state& g) @@ -484,9 +486,8 @@ int main_loop(game_state& g)
if (deferred) {
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, G.fbo);
glViewport(0,0,G.logical_width, G.logical_height);
G.bind_textures();
G.bind();
forward_pass_shader = g_shader;
} else {
forward_pass_shader = forward_lighting;
@ -502,7 +503,7 @@ int main_loop(game_state& g) @@ -502,7 +503,7 @@ int main_loop(game_state& g)
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
model_ubo->colour(glm::vec4(255,0,0,1));
g.model_ubo->colour(glm::vec4(255,0,0,1));
if (show_demo_window) {
ImGui::ShowMetricsWindow(&show_demo_window);
@ -514,8 +515,8 @@ int main_loop(game_state& g) @@ -514,8 +515,8 @@ int main_loop(game_state& g)
}
const char *debug_shaders[] = {"output", "normal", "position", "albedo", "specular"};
model_ubo->debug_shader(debug_shader);
model_ubo->update_all_fields();
g.model_ubo->debug_shader(debug_shader);
g.model_ubo->update_all_fields();
auto view = g.registry.view<static_model_transform>();
@ -531,9 +532,9 @@ int main_loop(game_state& g) @@ -531,9 +532,9 @@ int main_loop(game_state& g)
auto transform = g.registry.get<static_model_transform>(entity);
cam.update_view_transform_look_at(transform.position, parent_transform.position);
model_ubo->view(cam.view_transform);
model_ubo->view_pos(glm::vec4(transform.position, 0));
model_ubo->projection(cam.projection);
g.model_ubo->view(cam.view_transform);
g.model_ubo->view_pos(glm::vec4(transform.position, 0));
g.model_ubo->projection(cam.projection);
//tri_shader->set("view", cam.view_transform);
@ -541,53 +542,53 @@ int main_loop(game_state& g) @@ -541,53 +542,53 @@ int main_loop(game_state& g)
if (move_lights) {
for (int i = 10; i < max_lights/2; i++) {
light_info light = mat_ubo->lights()[i];
light_info light = g.material_ubo->lights()[i];
light.light_pos = glm::rotate(light.light_pos, (float)(0.0001 * frame_time), glm::vec3(0,1,0));
// position component of transformation matrix
mat_ubo->lights()[i] = light;
g.material_ubo->lights()[i] = light;
}
}
camera_light.light_pos = glm::vec4(parent_transform.position, 0);
mat_ubo->lights()[0] = camera_light;
mat_ubo->update_all_fields();
g.material_ubo->lights()[0] = camera_light;
g.material_ubo->update_all_fields();
//xenia.draw(mat_ubo, model_ubo, forward_pass_shader, glm::rotate(t1, (float)M_PI, glm::vec3(1,0,0)));
//xenia.draw(g.material_ubo, g.model_ubo, forward_pass_shader, glm::rotate(t1, (float)M_PI, glm::vec3(1,0,0)));
auto xenia_transform = glm::mat4(1);
xenia_transform = glm::translate(xenia_transform, parent_transform.position);
xenia_transform = glm::translate(xenia_transform, glm::vec3(0, -8, 0));
xenia_transform = glm::rotate(xenia_transform, (float)M_PI, glm::vec3(1,0,0));
xenia_transform = glm::rotate(xenia_transform, (float)M_PI/2 - (glm::radians(cam.euler_angles[0] * walking_state.z) - glm::radians((cam.euler_angles[0] + 90) * walking_state.x)), glm::vec3(0,1,0));
xenia_transform = glm::scale(xenia_transform, glm::vec3(0.5));
xenia.draw(mat_ubo, model_ubo, forward_pass_shader, xenia_transform);
xenia.draw(g.material_ubo, g.model_ubo, forward_pass_shader, xenia_transform);
for(auto entity: view) {
static_model_transform &model = view.get<static_model_transform>(entity);
//tri_shader->set("model", model.get_transform());
//model_ubo->model(model.get_transform());
//model_ubo->update_all_fields();
//g.model_ubo->model(model.get_transform());
//g.model_ubo->update_all_fields();
}
glClearColor(0.0F, 0.0F, 0, 1.0F);
mat_ubo->update_all_fields();
model_ubo->update_all_fields();
g.material_ubo->update_all_fields();
g.model_ubo->update_all_fields();
srand(920731031);
auto t1 = glm::mat4(1);
ground.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
model.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
ferns.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
grass.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
dead_tree.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
leaf_tree.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
ntrees.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
vegetation.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
dead2.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
lily.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
dry_grass.draw(mat_ubo, model_ubo, forward_pass_shader, t1);
ground.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
model.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
ferns.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
grass.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
dead_tree.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
leaf_tree.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
ntrees.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
vegetation.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
dead2.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
lily.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
dry_grass.draw(g.material_ubo, g.model_ubo, forward_pass_shader, t1);
break;
}
@ -618,19 +619,18 @@ int main_loop(game_state& g) @@ -618,19 +619,18 @@ int main_loop(game_state& g)
ImGui::Checkbox("Enable fog", (bool *)&fog_enabled);
ImGui::Checkbox("Move lights", (bool *)&move_lights);
ImGui::Checkbox("Deferred Shading", (bool *)&deferred);
ImGui::Checkbox("HDR", (bool *)&tone_mapping.use_hdr);
ImGui::SliderFloat("Exposure", &tone_mapping.exposure, 0, 3.0);
ImGui::SliderFloat("Gamma", &tone_mapping.gamma, 0, 4.0);
ImGui::Checkbox("HDR", (bool *)&g.model_ubo->use_hdr());
ImGui::SliderFloat("Exposure", &g.model_ubo->exposure(), 0, 3.0);
ImGui::SliderFloat("Gamma", &g.model_ubo->gamma(), 0, 4.0);
ImGui::Combo("Debug shader", &debug_shader, debug_shaders, IM_ARRAYSIZE(debug_shaders));
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(g.window);
mat_ubo->current_lights(num_lights);
mat_ubo->enable_fog(fog_enabled);
g.screen_shader->set("gamma", tone_mapping.gamma);
g.screen_shader->set("exposure", tone_mapping.exposure);
g.screen_shader->set("use_hdr", tone_mapping.use_hdr);
g.material_ubo->current_lights(num_lights);
g.material_ubo->enable_fog(fog_enabled);
while (SDL_PollEvent(&e) != 0) {
ImGui_ImplSDL2_ProcessEvent(&e);

3
source/mesh.cpp

@ -292,9 +292,7 @@ void print_mat(glm::mat4 x) { @@ -292,9 +292,7 @@ void print_mat(glm::mat4 x) {
void Model::process_node(aiNode *node, const aiScene *scene,
glm::mat4 transform_accum) {
auto next_transform = transform_accum * mat4from_aimat(node->mTransformation);
print_mat(transform_accum);
spdlog::info("Got child node {}", node->mName.C_Str());
print_mat(next_transform);
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(process_mesh(mesh, scene, next_transform));
@ -367,7 +365,6 @@ Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene, @@ -367,7 +365,6 @@ Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene,
glCheckError();
print_mat(transform_accum);
return Mesh(vertices, indices, textures, transform_accum);
}

42
source/shaders.cpp

@ -1,10 +1,26 @@ @@ -1,10 +1,26 @@
#include <fstream>
#include <spdlog/spdlog.h>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "shaders.h"
std::string format_with_line_numbers(const std::string &text) {
std::stringstream input{text};
std::stringstream out{};
int position;
int line_no = 1;
std::string line;
while (std::getline(input, line, '\n')) {
out << fmt::format("{} | {}\n", line_no++, line);
}
return out.str();
}
bool shader_handle::handle_error(const std::string &filename) {
GLint fShaderCompiled = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &fShaderCompiled);
@ -22,11 +38,18 @@ bool shader_handle::handle_error(const std::string &filename) { @@ -22,11 +38,18 @@ bool shader_handle::handle_error(const std::string &filename) {
glGetShaderInfoLog(shader, max_length, &max_length, &err_log[0]);
std::cerr << (char *)(&err_log[0]) << " (" << filename << ")" << std::endl;
spdlog::info("Shader errored:\n{}", format_with_line_numbers(source));
return true;
}
void shader_handle::add_shader(GLuint type, const std::string &filename) {
add_shader(type, filename, {});
}
void shader_handle::add_shader(
GLuint type, const std::string &filename,
std::unordered_map<std::string, std::string> templates) {
shader = glCreateShader(type);
// read source
@ -40,6 +63,15 @@ void shader_handle::add_shader(GLuint type, const std::string &filename) { @@ -40,6 +63,15 @@ void shader_handle::add_shader(GLuint type, const std::string &filename) {
throw std::runtime_error("Could not open filepath " + filename);
}
// apply templates
for (const auto &[key, content] : templates) {
auto position = std::string::npos;
while ((position = source.rfind(key, position)) != std::string::npos) {
source.replace(position, key.size(), content);
}
}
spdlog::info("New shader: {}\n{}", filename, source);
const char *cstr = source.c_str();
glShaderSource(shader, 1, &cstr, NULL);
@ -62,6 +94,16 @@ shader_handle::shader_handle(std::string filename) @@ -62,6 +94,16 @@ shader_handle::shader_handle(std::string filename)
add_shader(GL_VERTEX_SHADER, std::move(filename));
}
shader_handle::shader_handle(
std::string filename, std::string filename2,
std::unordered_map<std::string, std::string> templates)
: program(glCreateProgram()) {
add_shader(GL_VERTEX_SHADER, std::move(filename), templates);
add_shader(GL_FRAGMENT_SHADER, std::move(filename2), templates);
link();
}
shader_handle::shader_handle(std::string filename, std::string filename2)
: program(glCreateProgram()) {
add_shader(GL_VERTEX_SHADER, std::move(filename));

5
source/shaders.h

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <glm/matrix.hpp>
@ -25,6 +26,8 @@ public: @@ -25,6 +26,8 @@ public:
GLuint shader = 0;
GLuint type = 0;
shader_handle(std::string filename, std::string filename2,
std::unordered_map<std::string, std::string> templates);
shader_handle(std::string filename);
shader_handle(std::string filename, std::string filename2);
~shader_handle();
@ -38,6 +41,8 @@ public: @@ -38,6 +41,8 @@ public:
void use();
void link();
void add_shader(GLuint type, const std::string& filename);
void add_shader(GLuint type, const std::string& filename,
std::unordered_map<std::string, std::string> templates);
void set(const std::string& name, float value);
void set(const std::string& name, bool value);

12
source/uniform-buffer.hpp

@ -20,7 +20,7 @@ struct light_info { @@ -20,7 +20,7 @@ struct light_info {
glm::vec4 light_pos;
};
REFL_AUTO(type(struct light_info))
REFL_AUTO(type(light_info))
// TODO: how to get angle of fragment to camera;
// use for horizon, fog, sky colouring in fragment shader
@ -34,12 +34,16 @@ struct ubo_info { @@ -34,12 +34,16 @@ struct ubo_info {
glm::mat4 projection;
glm::vec4 offset;
glm::vec4 colour;
int instancing_enabled;
bool instancing_enabled;
float gamma = 2.2;
float exposure = 1.0;
bool use_hdr = false;
};
REFL_AUTO(type(ubo_info), field(view_pos), field(model), field(view),
field(projection), field(offset), field(colour), field(debug_shader),
field(instancing_enabled));
field(instancing_enabled), field(gamma), field(exposure),
field(use_hdr));
struct material_properties {
// not implemented: for material properties provided by assimp
@ -54,7 +58,7 @@ struct material_properties { @@ -54,7 +58,7 @@ struct material_properties {
float shininess;
int max_lights = 500;
int current_lights = 0;
int enable_fog = 0;
bool enable_fog = 0;
struct light_info lights[500];
// BELOW NOT PART OF SHADER
std::vector<GLuint> diffuse_texture{};

13
source/window.cpp

@ -127,7 +127,8 @@ void game_state::init_renderbuffer() @@ -127,7 +127,8 @@ void game_state::init_renderbuffer()
windowx, windowy, platonic_shapes.quad_vao);
screen_shader =
std::make_shared<shader_handle>(asset_dir / "shaders" / "screen.vert",
asset_dir / "shaders" / "screen.frag");
asset_dir / "shaders" / "screen.frag",
shader_templates);
screen_shader->use();
screen_shader->set("screenTexture", 4);
@ -136,15 +137,18 @@ void game_state::init_renderbuffer() @@ -136,15 +137,18 @@ void game_state::init_renderbuffer()
void framebuffer_render_context::bind() const
{
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glViewport(0,0, width, height);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, colour);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
glViewport(0,0,width, height);
glCheckError();
}
void framebuffer_render_context::unbind() const
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0, width, height);
glCheckError();
}
@ -169,6 +173,7 @@ void framebuffer_render_context::clear() @@ -169,6 +173,7 @@ void framebuffer_render_context::clear()
void framebuffer_render_context::present(std::shared_ptr<shader_handle>& shader)
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,width, height);
glDisable(GL_DEPTH_TEST);
shader->use();

10
source/window.hpp

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
#include "glheader.hpp"
#include "shaders.h"
#include "uniform-buffer.hpp"
struct framebuffer_render_context {
int width;
@ -76,6 +77,15 @@ struct game_state { @@ -76,6 +77,15 @@ struct game_state {
const std::filesystem::path asset_dir;
std::string window_name;
entt::registry registry;
std::shared_ptr<ubo_proxy<material_properties>> material_ubo =
std::make_shared<ubo_proxy<material_properties>>();
std::shared_ptr<ubo_proxy<ubo_info>> model_ubo =
std::make_shared<ubo_proxy<ubo_info>>();
std::unordered_map<std::string, std::string> shader_templates{
{"##" + material_ubo->block_name + "##",
material_ubo->fmt_uniform_block()},
{"##" + model_ubo->block_name + "##", model_ubo->fmt_uniform_block()}};
struct {
GLuint quad_vao, quad_vbo;

Loading…
Cancel
Save