feat: added phong and basic shaders, updated mesh format

This commit is contained in:
Daniel Ledda
2023-01-10 03:54:24 +01:00
parent 93dadfbf4b
commit 658b5d693a
10 changed files with 384 additions and 116 deletions

View File

@@ -1,10 +1,10 @@
#version 330 core
out vec4 FragColor;
out vec4 frag_color;
uniform sampler2D ourTexture;
uniform sampler2D texture;
in vec2 TexCoord;
in vec2 uv;
void main() {
FragColor = texture(ourTexture, TexCoord);
frag_color = texture(texture, uv);
};

View File

@@ -1,14 +1,15 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
layout (location = 0) in vec3 xyz;
layout (location = 1) in vec2 uv;
layout (location = 1) in vec2 normal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
out vec2 TexCoord;
out vec2 tex_coord;
void main() {
gl_Position = projection * view * model * vec4(aPos, 1.0);
TexCoord = aTexCoord;
gl_Position = projection * view * model * vec4(xyz, 1.0);
tex_coord = uv;
}

View File

@@ -0,0 +1,35 @@
#version 330 core
out vec4 frag_color;
uniform vec3 light_pos;
uniform vec3 solid_color;
in vec3 normal_cameraspace;
in vec3 light_direction_cameraspace;
in vec3 eye_direction_cameraspace;
in vec3 position_worldspace;
void main() {
vec4 material_diffuse_color = vec4(solid_color, 1);
vec4 material_ambient_color = vec4(0.1, 0.1, 0.1, 1.0) * material_diffuse_color;
vec4 material_specular_color = vec4(1.0, 1.0, 1.0, 1.0);
vec3 normal = normalize(normal_cameraspace);
vec3 light_dir = normalize(light_direction_cameraspace);
float cos_theta = clamp(dot(normal, light_dir), 0, 1);
vec3 eye = normalize(eye_direction_cameraspace);
vec3 reflected = reflect(-light_dir, normal);
float cos_alpha = clamp(dot(eye, reflected), 0, 1);
vec4 light_color = vec4(1, 1, 1, 1);
float light_power = 60;
float distance = length(light_pos - position_worldspace);
float dist_sq = pow(distance, 2);
vec4 diffuse = material_diffuse_color * light_color * light_power * cos_theta / dist_sq;
vec4 specular = material_specular_color * light_color * light_power * pow(cos_alpha, 10) / dist_sq;
frag_color = material_ambient_color + diffuse + specular;
};

View File

@@ -0,0 +1,26 @@
#version 330 core
layout (location = 0) in vec3 a_xyz;
layout (location = 1) in vec2 a_uv;
layout (location = 2) in vec3 a_normal;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
uniform vec3 light_pos;
out vec3 normal_cameraspace;
out vec3 light_direction_cameraspace;
out vec3 eye_direction_cameraspace;
out vec3 position_worldspace;
void main() {
vec3 vertex_cameraspace = (view * model * vec4(a_xyz, 1)).xyz;
vec3 light_pos_cameraspace = (view * vec4(light_pos, 1)).xyz;
normal_cameraspace = (transpose(inverse(view * model)) * vec4(a_normal, 0)).xyz;
light_direction_cameraspace = light_pos_cameraspace + eye_direction_cameraspace;
eye_direction_cameraspace = vec3(0, 0, 0) - vertex_cameraspace;
position_worldspace = (model * vec4(a_xyz, 1)).xyz;
gl_Position = projection * view * model * vec4(a_xyz, 1);
}