chaos_framework/graphics/
SHADERS.rs1
2pub static DEFAULT_VS: &str = r#"
3#version 330 core
4layout (location = 0) in vec3 aPos;
5layout (location = 1) in vec4 aColor;
6layout (location = 2) in vec2 aTexCoord;
7layout (location = 3) in vec3 aNormal;
8
9uniform mat4 model;
10uniform mat4 view;
11uniform mat4 proj;
12uniform vec3 color;
13
14out vec4 fColor;
15out vec3 Normal;
16out vec3 FragPos;
17out vec2 TexCoord; // Pass texture coordinates to the fragment shader
18
19void main() {
20 gl_Position = proj * view * model * vec4(aPos, 1.0);
21 fColor = vec4(color, 1.0);
22 TexCoord = aTexCoord; // Pass texture coordinates
23 FragPos = vec3(model * vec4(aPos, 1.0));
24 Normal = mat3(transpose(inverse(model))) * aNormal;
25}
26"#;
27
28pub static DEFAULT_FS: &str = r#"
29#version 330 core
30out vec4 FragColor;
31
32in vec4 fColor;
33in vec2 TexCoord;
34in vec3 Normal;
35in vec3 FragPos;
36
37uniform vec3 lightColor[256];
38uniform vec3 lightPos[256];
39uniform vec3 viewPos;
40
41uniform int has_texture;
42uniform int num_lights;
43
44uniform sampler2D texture1;
45
46void main()
47{
48 vec4 texColor = fColor;
49
50 if (has_texture == 1) {
51 texColor = texture(texture1, TexCoord) * fColor;
52 }
53
54 vec3 ambientStrength = vec3(0.1);
55 vec3 specularStrength = vec3(0.5);
56
57 vec3 result = vec3(0.0);
58
59 for (int i = 0; i < num_lights; ++i) {
60 vec3 lightDir = normalize(lightPos[i] - FragPos);
61 float distance = length(lightPos[i] - FragPos);
62
63 float constant_attenuation = 1.0;
64 float linear_attenuation = 0.045;
65 float quadratic_attenuation = 0.016;
66
67 float attenuation = 1.0 / (constant_attenuation + linear_attenuation * distance + quadratic_attenuation * distance * distance);
68
69 vec3 norm = normalize(Normal);
70 float diff = max(dot(norm, lightDir), 0.0);
71 vec3 diffuse = diff * lightColor[i] * attenuation;
72
73 vec3 viewDir = normalize(viewPos - FragPos);
74 vec3 reflectDir = reflect(-lightDir, norm);
75
76 float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
77 vec3 specular = specularStrength * spec * lightColor[i] * attenuation;
78
79 result += ((ambientStrength / num_lights) + diffuse + specular) * texColor.rgb;
80 }
81
82 FragColor = vec4(result, texColor.a);
83}
84"#;
85
86
87pub static RUSSIMP_VS: &str = r#"
88#version 430 core
89
90layout(location = 0) in vec3 pos;
91layout(location = 1) in vec3 norm;
92layout(location = 2) in vec2 tex;
93layout(location = 3) in ivec4 boneIds;
94layout(location = 4) in vec4 weights;
95
96uniform mat4 proj;
97uniform mat4 view;
98uniform mat4 model;
99uniform vec3 color;
100
101const int MAX_BONES = 100;
102uniform mat4 finalBonesMatrices[MAX_BONES];
103
104out vec2 TexCoord;
105out vec3 Normal;
106out vec3 FragPos;
107out vec4 fColor;
108
109void main()
110{
111 mat4 boneTransform = mat4(0.0);
112 for (int i = 0; i < 4; i++) {
113 if (boneIds[i] != -1) {
114 boneTransform += finalBonesMatrices[boneIds[i]] * weights[i];
115 }
116 }
117
118 vec4 f_pos = boneTransform * vec4(pos, 1.0);
119 gl_Position = proj * view * model * f_pos;
120
121 TexCoord = tex;
122 fColor = vec4(color, 1.0);
123 FragPos = vec3(model * f_pos);
124
125 // Correcting the normal transformation
126 Normal = mat3(transpose(inverse(model))) * norm;
127}
128
129"#;
130
131use crate::Shader;
132
133use std::sync::LazyLock;
134
135pub static DEFAULT_SHADER: LazyLock<Shader> = LazyLock::new(|| {
136 Shader::new_pipeline(DEFAULT_VS, DEFAULT_FS)
137});
138
139
140pub static RUSSIMP_SHADER: LazyLock<Shader> = LazyLock::new(|| {
141 Shader::new_pipeline(RUSSIMP_VS, DEFAULT_FS)
142});
143