Add texture support to the renderer
- Refactor to use the new model::Model struct - Implement blinn-phong shading - Add texture support for shaders
This commit is contained in:
parent
67f5913907
commit
0af4622525
5 changed files with 109 additions and 37 deletions
|
|
@ -1,11 +0,0 @@
|
|||
#version 330 core
|
||||
in vec3 v_normal;
|
||||
out vec4 color;
|
||||
uniform vec3 light_dir;
|
||||
|
||||
void main() {
|
||||
float brightness = dot(normalize(v_normal), normalize(light_dir));
|
||||
vec3 dark_color = vec3(0.6, 0.0, 0.0);
|
||||
vec3 regular_color = vec3(1.0, 0.0, 0.0);
|
||||
color = vec4(mix(dark_color, regular_color, brightness), 1.0);
|
||||
}
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
#version 330 core
|
||||
in vec3 position;
|
||||
in vec3 normal;
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
out vec3 v_normal;
|
||||
void main() {
|
||||
mat4 modelview = view * model;
|
||||
v_normal = transpose(inverse(mat3(modelview))) * normal;
|
||||
gl_Position = projection * modelview * vec4(position, 1.0);
|
||||
}
|
||||
34
resources/shaders/gl_textured.frag
Normal file
34
resources/shaders/gl_textured.frag
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#version 330 core
|
||||
|
||||
in vec3 v_normal;
|
||||
in vec2 v_tex;
|
||||
in vec3 v_position;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
uniform vec3 u_light;
|
||||
uniform sampler2D tex;
|
||||
uniform vec3 color; // base colour factor (acts as solid colour when no texture)
|
||||
|
||||
void main() {
|
||||
// Combine base texture (or constant white) with colour factor supplied by CPU.
|
||||
vec3 base_col = texture(tex, v_tex).rgb * color;
|
||||
|
||||
vec3 ambient_color = base_col * 0.2;
|
||||
vec3 diffuse_color = base_col * 0.6;
|
||||
vec3 specular_color = vec3(1.0);
|
||||
|
||||
// u_light is the direction **from the light towards the fragment**.
|
||||
float diffuse = max(dot(normalize(v_normal), normalize(u_light)), 0.0);
|
||||
|
||||
vec3 camera_dir = normalize(-v_position);
|
||||
vec3 half_dir = normalize(normalize(u_light) + camera_dir);
|
||||
float specular = pow(max(dot(half_dir, normalize(v_normal)), 0.0), 16.0);
|
||||
|
||||
vec3 result = ambient_color + diffuse * diffuse_color + specular * specular_color;
|
||||
|
||||
// Convert from linear to sRGB for display (approximate γ-correction)
|
||||
result = pow(result, vec3(1.0 / 2.2));
|
||||
|
||||
frag_color = vec4(result, 1.0);
|
||||
}
|
||||
23
resources/shaders/gl_textured.vert
Normal file
23
resources/shaders/gl_textured.vert
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#version 330 core
|
||||
|
||||
in vec3 position;
|
||||
in vec3 normal;
|
||||
in vec2 tex_coords;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
uniform vec2 uv_offset;
|
||||
uniform vec2 uv_scale;
|
||||
|
||||
out vec3 v_normal;
|
||||
out vec2 v_tex;
|
||||
out vec3 v_position;
|
||||
|
||||
void main() {
|
||||
mat4 modelview = view * model;
|
||||
v_normal = transpose(inverse(mat3(modelview))) * normal;
|
||||
v_tex = tex_coords * uv_scale + uv_offset;
|
||||
v_position = (modelview * vec4(position, 1.0)).xyz;
|
||||
gl_Position = projection * modelview * vec4(position, 1.0);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue