Created
February 24, 2021 06:05
-
-
Save shakesoda/dcd1b7d61eb376b7540347d25058187d to your computer and use it in GitHub Desktop.
simple per vertex shading example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 | |
in vec2 uv; | |
in vec3 diffuse; | |
in vec3 specular; | |
in vec2 color_mixes; // x=diffuse mix, y=specular mix | |
out vec4 outColor; | |
uniform sampler2D texture0; | |
void main() { | |
vec4 color = texture(texture0, uv); | |
vec3 tex_diffuse = diffuse * color_mixes.x; | |
vec3 tex_specular = specular * color_mixes.y; | |
outColor = vec4(tex_diffuse + tex_specular, color.a); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version 330 | |
uniform mat4 matVP; // view/projection matrix | |
uniform mat4 matGeo; // object matrix | |
uniform mat4 matInvView; // inverse view matrix | |
layout (location = 0) in vec3 a_pos; | |
layout (location = 1) in vec3 a_normal; | |
layout (location = 2) in vec2 a_uv; | |
layout (location = 3) in vec4 a_color; | |
out vec2 uv; | |
out vec3 diffuse; | |
out vec3 specular; | |
out vec2 color_mixes; | |
struct Material { | |
vec4 color; | |
float ambient; | |
float shininess; | |
float diffuse_mix; | |
float specular_mix; | |
}; | |
struct Light { | |
vec3 dir; | |
vec3 color; | |
}; | |
void main() { | |
uv = a_uv; | |
Material mat; | |
mat.color = vec4(1.0, 0.5, 1.0, 1.0); | |
mat.ambient = 0.5; | |
mat.shininess = 5.0; | |
mat.diffuse_mix = 1.0; | |
mat.specular_mix = 1.0; | |
Light light1; | |
light1.dir = vec3(0.0, -0.707, -0.707); | |
light1.color = vec3(1.0, 1.0, 1.0); | |
Light light2; | |
light2.dir = vec3(0.0, 0.707, 0.707); | |
light2.color = vec3(0.25, 0.1, 0.1); | |
color_mixes = vec2(mat.diffuse_mix, mat.specular_mix); | |
color_mixes *= exp2(-1.0); // just darken | |
diffuse = vec3(mat.ambient); | |
diffuse += vec3(max(0.0, dot(light1.dir, a_normal))) * light1.color; | |
diffuse += vec3(max(0.0, dot(light2.dir, a_normal))) * light2.color; | |
diffuse *= mat.color.rgb * a_color.rgb; | |
vec3 view_dir = normalize(mat3(matInvView) * vec3(0.0, 0.0, -1.0)); | |
specular = vec3(0.0); | |
specular += vec3(pow(max(0.0, dot(reflect(-light1.dir, a_normal), view_dir)), mat.shininess)) * light1.color; | |
specular += vec3(pow(max(0.0, dot(reflect(-light2.dir, a_normal), view_dir)), mat.shininess)) * light2.color; | |
gl_Position = matVP * matGeo * vec4(a_pos, 1.0); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment