From d4537b88a5bfedf4a28570abfec096eadea6badc Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Mon, 12 Aug 2024 21:40:37 -0400 Subject: [PATCH] Shaders up and running --- shaders/fragment.glsl | 58 +++++++++++++++++++++++++++++++++++++++++++ shaders/vertex.glsl | 30 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 shaders/fragment.glsl diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl new file mode 100644 index 0000000..247042a --- /dev/null +++ b/shaders/fragment.glsl @@ -0,0 +1,58 @@ +#version 430 +layout(binding = 0) uniform sampler2D diffuse_tex; +layout(location = 1) uniform float time; + +layout(std140, binding = 0) uniform SceneUniforms +{ + mat4 PV; //camera projection * view matrix + vec4 eye_w; //world-space eye position +}; + +layout(std140, binding = 1) uniform LightUniforms +{ + vec4 La; //ambient light color + vec4 Ld; //diffuse light color + vec4 Ls; //specular light color + vec4 light_w; //world-space light position +}; + +layout(std140, binding = 2) uniform MaterialUniforms +{ + vec4 ka; //ambient material color + vec4 kd; //diffuse material color + vec4 ks; //specular material color + float shininess; //specular exponent +}; + +in VertexData +{ + vec2 tex_coord; + vec3 pw; //world-space vertex position + vec3 nw; //world-space normal vector +} inData; //block is named 'inData' + +out vec4 fragcolor; //the output color for this fragment + +void main(void) +{ + //Compute per-fragment Phong lighting + vec4 ktex = texture(diffuse_tex, inData.tex_coord); + + vec4 ambient_term = ka*ktex*La; + + const float eps = 1e-8; //small value to avoid division by 0 + float d = distance(light_w.xyz, inData.pw.xyz); + float atten = 1.0/(d*d+eps); //d-squared attenuation + + vec3 nw = normalize(inData.nw); //world-space unit normal vector + vec3 lw = normalize(light_w.xyz - inData.pw.xyz); //world-space unit light vector + vec4 diffuse_term = atten*kd*ktex*Ld*max(0.0, dot(nw, lw)); + + vec3 vw = normalize(eye_w.xyz - inData.pw.xyz); //world-space unit view vector + vec3 rw = reflect(-lw, nw); //world-space unit reflection vector + + vec4 specular_term = atten*ks*Ls*pow(max(0.0, dot(rw, vw)), shininess); + + fragcolor = ambient_term + diffuse_term + specular_term; +} + diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index e69de29..c847c18 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -0,0 +1,30 @@ +#version 430 +layout(location = 0) uniform mat4 M; +layout(location = 1) uniform float time; + +layout(std140, binding = 0) uniform SceneUniforms +{ + mat4 PV; //camera projection * view matrix + vec4 eye_w; //world-space eye position +}; + +layout(location = 0) in vec3 pos_attrib; //this variable holds the position of mesh vertices +layout(location = 1) in vec2 tex_coord_attrib; +layout(location = 2) in vec3 normal_attrib; + +out VertexData +{ + vec2 tex_coord; + vec3 pw; //world-space vertex position + vec3 nw; //world-space normal vector +} outData; + +void main(void) +{ + gl_Position = PV*M*vec4(pos_attrib, 1.0); //transform vertices and send result into pipeline + + //Use dot notation to access members of the interface block + outData.tex_coord = tex_coord_attrib; //send tex_coord to fragment shader + outData.pw = vec3(M*vec4(pos_attrib, 1.0)); //world-space vertex position + outData.nw = vec3(M*vec4(normal_attrib, 0.0)); //world-space normal vector +} \ No newline at end of file