Browse Source

substitute shaders

master
alistair 12 months ago
parent
commit
3a735b401d
  1. 31
      assets/shaders/deferred_lighting.frag
  2. 31
      assets/shaders/forward-frag.gl
  3. 46
      assets/shaders/g_frag.gl
  4. 18
      assets/shaders/screen.frag
  5. 33
      assets/shaders/uniformtest.vert
  6. 2
      assets/shaders/write/material_properties.gl
  7. 4
      assets/shaders/write/ubo_info.gl
  8. 3
      source/camera.hpp
  9. 33
      source/main.cpp
  10. 3
      source/mesh.cpp
  11. 27
      source/shaders.cpp
  12. 5
      source/shaders.h
  13. 6
      source/uniform-buffer.hpp

31
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;

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;

46
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;

18
assets/shaders/screen.frag

@ -9,25 +9,11 @@ uniform float gamma = 2.2; @@ -9,25 +9,11 @@ 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
};
##material_properties##
##ubo_info##
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;

2
assets/shaders/write/material_properties.gl

@ -8,5 +8,5 @@ layout (std140) uniform ubo_info @@ -8,5 +8,5 @@ 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
};

4
assets/shaders/write/ubo_info.gl

@ -8,7 +8,7 @@ layout (std140) uniform ubo_info @@ -8,7 +8,7 @@ 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
bool instancing_enabled; // 4 244
};4 244
};244
};

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 = 1000;
glm::vec3 euler_angles;
void update_projection(float aspect_ratio) {

33
source/main.cpp

@ -325,15 +325,34 @@ int main_loop(game_state& g) @@ -325,15 +325,34 @@ int main_loop(game_state& g)
glm::vec3 walking_state = glm::vec3(0);
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);
std::unordered_map<std::string, std::string> templates {
{"##" + mat_ubo->block_name + "##", mat_ubo->fmt_uniform_block()},
{"##" + model_ubo->block_name + "##", model_ubo->fmt_uniform_block()}
};
for (auto item:templates) {
spdlog::info("Shader: {}", item.first);
}
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",
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",
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",
templates);
auto forward_pass_shader = g_shader;
// set scaling
@ -342,14 +361,6 @@ int main_loop(game_state& g) @@ -342,14 +361,6 @@ 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)));

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

27
source/shaders.cpp

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
#include <fstream>
#include <spdlog/spdlog.h>
#include <unordered_map>
#include <utility>
#include <vector>
@ -27,6 +28,12 @@ bool shader_handle::handle_error(const std::string &filename) { @@ -27,6 +28,12 @@ bool shader_handle::handle_error(const std::string &filename) {
}
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 +47,16 @@ void shader_handle::add_shader(GLuint type, const std::string &filename) { @@ -40,6 +47,16 @@ 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 &item : templates) {
auto position = std::string::npos;
while ((position = source.rfind(item.first, position)) !=
std::string::npos) {
source.replace(position, item.first.size(), item.second);
}
}
spdlog::info("New shader: {}\n{}", filename, source);
const char *cstr = source.c_str();
glShaderSource(shader, 1, &cstr, NULL);
@ -62,6 +79,16 @@ shader_handle::shader_handle(std::string filename) @@ -62,6 +79,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);

6
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,7 +34,7 @@ struct ubo_info { @@ -34,7 +34,7 @@ struct ubo_info {
glm::mat4 projection;
glm::vec4 offset;
glm::vec4 colour;
int instancing_enabled;
bool instancing_enabled;
};
REFL_AUTO(type(ubo_info), field(view_pos), field(model), field(view),
@ -54,7 +54,7 @@ struct material_properties { @@ -54,7 +54,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{};

Loading…
Cancel
Save