add light shader

This commit is contained in:
Jay Robson 2024-02-24 22:52:29 +11:00
parent 8c06cbe16d
commit 787339ebee
23 changed files with 91 additions and 409 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,3 +0,0 @@
linear_extrude(0.0001)
square([1, 1], center = true);

View File

@ -1,10 +0,0 @@
$fn = 256;
linear_extrude(0.0001)
translate([0.5, 0.5])
difference()
{
circle(d = 0.81);
circle(d = 0.8);
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

13
assets/shader/light.fsh Normal file
View File

@ -0,0 +1,13 @@
#version 460 core
in vec4 frag_pos;
out vec4 frag_colour;
void main()
{
float distance = length(frag_pos);
frag_colour = vec4(vec3(distance / 50.0f), 1);
gl_FragDepth = distance / 50.0f;
}

28
assets/shader/light.gsh Normal file
View File

@ -0,0 +1,28 @@
#version 460 core
layout (triangles) in;
layout (triangle_strip, max_vertices=18) out;
uniform mat4 shadow_mats[6];
out vec4 frag_pos;
void main()
{
for(int i = 0; i < 6; i++)
{
gl_Layer = i;
for(int j = 0; j < 3; j++)
{
frag_pos = gl_in[j].gl_Position;
gl_Position = shadow_mats[i] * frag_pos;
EmitVertex();
}
EndPrimitive();
}
}

13
assets/shader/light.vsh Normal file
View File

@ -0,0 +1,13 @@
#version 460 core
layout (location = 2) in vec4 aPos;
uniform mat4 model;
uniform mat4 camera;
void main()
{
gl_Position = camera * model * aPos;
}

Binary file not shown.

Binary file not shown.

BIN
assets/test.glb (Stored with Git LFS)

Binary file not shown.

View File

@ -1,382 +0,0 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.0.44",
"version":"2.0"
},
"scene":0,
"scenes":[
{
"extras":{
"SimpleBake_Props":{},
"bake_to_vertex_color_props":{}
},
"name":"Scene",
"nodes":[
0,
1,
2,
3
]
}
],
"nodes":[
{
"mesh":0,
"name":"Cylinder",
"translation":[
0,
0,
0.5
]
},
{
"mesh":1,
"name":"Cylinder.001",
"translation":[
1,
0,
0.5
]
},
{
"mesh":2,
"name":"Cylinder.002",
"translation":[
3,
0,
0.5
]
},
{
"mesh":3,
"name":"Cylinder.003",
"translation":[
2,
0,
0.5
]
}
],
"materials":[
{
"doubleSided":true,
"name":"Material.006",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.800000011920929,
0.800000011920929,
0.800000011920929,
1
],
"metallicFactor":0,
"roughnessFactor":0.75
}
},
{
"doubleSided":true,
"name":"Material.005",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.800000011920929,
0.800000011920929,
0.800000011920929,
1
],
"metallicFactor":0,
"roughnessFactor":0.5
}
},
{
"doubleSided":true,
"name":"Material.003",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.800000011920929,
0.800000011920929,
0.800000011920929,
1
],
"metallicFactor":0,
"roughnessFactor":0.125
}
},
{
"doubleSided":true,
"name":"Material.004",
"pbrMetallicRoughness":{
"baseColorFactor":[
0.800000011920929,
0.800000011920929,
0.800000011920929,
1
],
"metallicFactor":0,
"roughnessFactor":0.25
}
}
],
"meshes":[
{
"name":"Cylinder",
"primitives":[
{
"attributes":{
"POSITION":0,
"NORMAL":1,
"TEXCOORD_0":2
},
"indices":3,
"material":0
}
]
},
{
"name":"Cylinder.001",
"primitives":[
{
"attributes":{
"POSITION":4,
"NORMAL":5,
"TEXCOORD_0":6
},
"indices":3,
"material":1
}
]
},
{
"name":"Cylinder.002",
"primitives":[
{
"attributes":{
"POSITION":7,
"NORMAL":8,
"TEXCOORD_0":9
},
"indices":3,
"material":2
}
]
},
{
"name":"Cylinder.003",
"primitives":[
{
"attributes":{
"POSITION":10,
"NORMAL":11,
"TEXCOORD_0":12
},
"indices":3,
"material":3
}
]
}
],
"accessors":[
{
"bufferView":0,
"componentType":5126,
"count":192,
"max":[
0.20000000298023224,
0.20000000298023224,
0.5
],
"min":[
-0.20000000298023224,
-0.20000000298023224,
-0.5
],
"type":"VEC3"
},
{
"bufferView":1,
"componentType":5126,
"count":192,
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":192,
"type":"VEC2"
},
{
"bufferView":3,
"componentType":5123,
"count":372,
"type":"SCALAR"
},
{
"bufferView":4,
"componentType":5126,
"count":192,
"max":[
0.20000000298023224,
0.20000000298023224,
0.5
],
"min":[
-0.20000000298023224,
-0.20000000298023224,
-0.5
],
"type":"VEC3"
},
{
"bufferView":5,
"componentType":5126,
"count":192,
"type":"VEC3"
},
{
"bufferView":6,
"componentType":5126,
"count":192,
"type":"VEC2"
},
{
"bufferView":7,
"componentType":5126,
"count":192,
"max":[
0.20000000298023224,
0.20000000298023224,
0.5
],
"min":[
-0.20000000298023224,
-0.20000000298023224,
-0.5
],
"type":"VEC3"
},
{
"bufferView":8,
"componentType":5126,
"count":192,
"type":"VEC3"
},
{
"bufferView":9,
"componentType":5126,
"count":192,
"type":"VEC2"
},
{
"bufferView":10,
"componentType":5126,
"count":192,
"max":[
0.20000000298023224,
0.20000000298023224,
0.5
],
"min":[
-0.20000000298023224,
-0.20000000298023224,
-0.5
],
"type":"VEC3"
},
{
"bufferView":11,
"componentType":5126,
"count":192,
"type":"VEC3"
},
{
"bufferView":12,
"componentType":5126,
"count":192,
"type":"VEC2"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":2304,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":2304,
"byteOffset":2304,
"target":34962
},
{
"buffer":0,
"byteLength":1536,
"byteOffset":4608,
"target":34962
},
{
"buffer":0,
"byteLength":744,
"byteOffset":6144,
"target":34963
},
{
"buffer":0,
"byteLength":2304,
"byteOffset":6888,
"target":34962
},
{
"buffer":0,
"byteLength":2304,
"byteOffset":9192,
"target":34962
},
{
"buffer":0,
"byteLength":1536,
"byteOffset":11496,
"target":34962
},
{
"buffer":0,
"byteLength":2304,
"byteOffset":13032,
"target":34962
},
{
"buffer":0,
"byteLength":2304,
"byteOffset":15336,
"target":34962
},
{
"buffer":0,
"byteLength":1536,
"byteOffset":17640,
"target":34962
},
{
"buffer":0,
"byteLength":2304,
"byteOffset":19176,
"target":34962
},
{
"buffer":0,
"byteLength":2304,
"byteOffset":21480,
"target":34962
},
{
"buffer":0,
"byteLength":1536,
"byteOffset":23784,
"target":34962
}
],
"buffers":[
{
"byteLength":25320,
"uri":"test.bin"
}
]
}

View File

@ -13,6 +13,7 @@ using namespace Sim::Graphics;
Shader Shader::MAIN;
Shader Shader::BLUR;
Shader Shader::LIGHT;
static int load_shader(const char* src, int type)
{
@ -39,6 +40,12 @@ static std::string read_shader(const char* path)
return ss.str();
}
static std::string read_shader(const char* base, const char* file)
{
std::string path = std::string(base) + "/" + std::string(file);
return read_shader(path.c_str());
}
Shader::Shader()
{
@ -60,18 +67,29 @@ Shader::~Shader()
void Shader::load(const char* path, const char* file_vsh, const char* file_fsh)
{
std::string path_vsh = std::string(path) + "/" + std::string(file_vsh);
std::string path_fsh = std::string(path) + "/" + std::string(file_fsh);
std::string shader_vsh = read_shader(path_vsh.c_str());
std::string shader_fsh = read_shader(path_fsh.c_str());
load(path, file_vsh, nullptr, file_fsh);
}
void Shader::load(const char* path, const char* file_vsh, const char* file_gsh, const char* file_fsh)
{
std::string shader_vsh = file_vsh ? read_shader(path, file_vsh) : "";
std::string shader_gsh = file_gsh ? read_shader(path, file_gsh) : "";
std::string shader_fsh = file_fsh ? read_shader(path, file_fsh) : "";
int success;
int vsh_id = load_shader(shader_vsh.c_str(), GL_VERTEX_SHADER);
int fsh_id = load_shader(shader_fsh.c_str(), GL_FRAGMENT_SHADER);
int vsh_id = file_vsh ? load_shader(shader_vsh.c_str(), GL_VERTEX_SHADER) : 0;
int gsh_id = file_gsh ? load_shader(shader_gsh.c_str(), GL_GEOMETRY_SHADER) : 0;
int fsh_id = file_fsh ? load_shader(shader_fsh.c_str(), GL_FRAGMENT_SHADER) : 0;
prog_id = glCreateProgram();
if(file_vsh)
glAttachShader(prog_id, vsh_id);
if(file_gsh)
glAttachShader(prog_id, gsh_id);
if(file_fsh)
glAttachShader(prog_id, fsh_id);
glLinkProgram(prog_id);
glGetProgramiv(prog_id, GL_LINK_STATUS, &success);
@ -85,7 +103,12 @@ void Shader::load(const char* path, const char* file_vsh, const char* file_fsh)
}
glUseProgram(prog_id);
if(file_vsh)
glDeleteShader(vsh_id);
if(file_gsh)
glDeleteShader(gsh_id);
if(file_fsh)
glDeleteShader(fsh_id);
}

View File

@ -16,12 +16,14 @@ public:
static Shader MAIN;
static Shader BLUR;
static Shader LIGHT;
Shader();
Shader(const Shader& o) = delete;
Shader(Shader&& o);
~Shader();
void load(const char* path, const char* file_vsh, const char* file_gsh, const char* file_fsh);
void load(const char* path, const char* file_vsh, const char* file_fsh);
void use();

View File

@ -119,6 +119,7 @@ void Window::create()
Font::init();
UI::init();
Shader::LIGHT.load("../assets/shader", "light.vsh", "light.gsh", "light.fsh");
Shader::MAIN.load("../assets/shader", "main.vsh", "main.fsh");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
@ -255,7 +256,7 @@ void Window::render()
projection_matrix = mat_projection;
glClearColor(0, 0, 0, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glFrontFace(GL_CW);
render_scene();