#version 450 core layout(binding = 0) uniform sampler2D heightTexture; in TE_OUT { vec3 position; vec2 texCoord; vec3 normal; } fs_in; layout(std140, binding = 0) uniform SceneUniforms { mat4 PV; //camera projection * view matrix vec4 eye_w; //world-space eye position }; out vec4 FragColor; float getHeight(vec2 uv) { return texture(heightTexture, uv).r * 0.05f; } vec3 calculateNormals() { // Calculate height at base_uv float height = getHeight(fs_in.texCoord); // Calculate normals using height differences around the fragment float eps = 64.0f / 3601.0f; float hRight = getHeight(fs_in.texCoord + vec2(eps, 0.0)); float hLeft = getHeight(fs_in.texCoord - vec2(eps, 0.0)); float hUp = getHeight(fs_in.texCoord + vec2(0.0, eps)); float hDown = getHeight(fs_in.texCoord - vec2(0.0, eps)); float heightTL = getHeight(fs_in.texCoord + vec2(-eps, eps)); float heightT = getHeight(fs_in.texCoord + vec2( 0.0, eps)); float heightTR = getHeight(fs_in.texCoord + vec2( eps, eps)); float heightL = getHeight(fs_in.texCoord + vec2(-eps, 0.0)); float heightR = getHeight(fs_in.texCoord + vec2( eps, 0.0)); float heightBL = getHeight(fs_in.texCoord + vec2(-eps, -eps)); float heightB = getHeight(fs_in.texCoord + vec2( 0.0, -eps)); float heightBR = getHeight(fs_in.texCoord + vec2( eps, -eps)); float sobelX = (heightTL + 2.0 * heightL + heightBL) - (heightTR + 2.0 * heightR + heightBR); float sobelZ = (heightTL + 2.0 * heightT + heightTR) - (heightBL + 2.0 * heightB + heightBR); vec3 tangentX = normalize(vec3(eps, 0.0, sobelX)); vec3 tangentZ = normalize(vec3(0.0, sobelZ, eps)); // Final normal for the fragment return normalize(cross(tangentZ, tangentX)); } void main() { //FragColor = vec4(fragNormal, 1.0f); vec3 normal = calculateNormals(); float shininess = 1.0f; vec3 lightColor = vec3(0.5f); vec3 objectColor = vec3(texture(heightTexture, fs_in.texCoord).r); // Ambient vec3 ambient = vec3(0.15f) * vec3(0.15f); // Diffuse vec3 norm = normalize(normal); vec3 lightDir = normalize(vec3(1.0f, 1.0f, 0.0f) - fs_in.position); float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * objectColor; // Specular (Phong) vec3 viewDir = normalize(eye_w.xyz - fs_in.position); vec3 reflectDir = reflect(-lightDir, norm); float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess); vec3 specular = spec * lightColor; // Combine results vec3 result = ambient + diffuse; FragColor = vec4(result, 1.0); //FragColor = vec4((fs_in.position * 0.5 + 0.5), 1.0); } // layout(binding = 0) uniform sampler2D heightTexture; // 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 vec3 frag_position; // in VertexData { // vec2 tex_coord; // } inData; // out vec4 frag_color; //the output color for this fragment // void main(void) // { // //Compute per-fragment Phong lighting // // vec4 ktex = vec4(vec3(texture(heightTexture, inData.tex_coord).r, 1.0); // // 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); // // frag_color = ambient_term + diffuse_term + specular_term; // float height = texture(heightTexture, inData.tex_coord).r; // //frag_color = vec4(inData.tex_coord, 0.0f, 1.0f); // // if (height > 0.8) frag_color = vec4(vec3(1.0f-height), 1.0f); // // else frag_color = vec4(vec3(height), 1.0f); // frag_color = vec4(height, height, height, 1.0); // }