xKamuna

GLSL Example 1

May 6th, 2019
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Copyright 2011-2018 Daniel S. Buckstein
  3.     Licensed under the Apache License, Version 2.0 (the "License");
  4.     you may not use this file except in compliance with the License.
  5.     You may obtain a copy of the License at
  6.         http://www.apache.org/licenses/LICENSE-2.0
  7.     Unless required by applicable law or agreed to in writing, software
  8.     distributed under the License is distributed on an "AS IS" BASIS,
  9.     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10.     See the License for the specific language governing permissions and
  11.     limitations under the License.
  12. */
  13. /*
  14.     animal3D SDK: Minimal 3D Animation Framework
  15.     By Daniel S. Buckstein
  16.    
  17.     drawPhong_fs4x.glsl
  18.     Receive variables for calculating Phong shading, output result.
  19. */
  20. //This file was modified by Summer Softleigh with permission from the author.
  21. #version 410
  22. //  1) declare varyings to receive data from vertex shader
  23. //  2) declare uniform variables for textures; see demo code for hints
  24. //  3) grab samples from textures
  25. //  4) calculate diffuse and specular coefficients
  26. //  5) calculate Phong shading model
  27. //  6) assign result to output color
  28. in vec3 vPosition;
  29. in vec4 vNormal;
  30. in vec2 vTexcoord;
  31. uniform vec4 uLightPos_obj;
  32. uniform vec4 uEyePos_obj;
  33. uniform sampler2D uTex_dm;
  34. uniform sampler2D uTex_sm;
  35. uniform mat4 uAtlas;
  36. out vec4 rtFragColor;
  37. vec3 calculateReflectDir(vec3 N, vec3 L)
  38. {
  39.     return 2.0f * dot(N, L) * N - L;
  40. }
  41. float calculateDiffuse(vec3 N, vec3 L)
  42. {
  43.     return dot(N, L) * 0.5f + 0.5f; //Change the range instead of clamping to 0.0f.  This makes it brighter in the back than Dan's program.  max(0.0f, dot(N, L)) gives the same as Dan's.
  44. }
  45. float calculateSpecular(vec3 V, vec3 R)
  46. {
  47.     return dot(V, R) * 0.5f + 0.5f; //Same thing as diffuse, just with names changed for clarity.
  48. }  
  49. void main()
  50. {
  51.     vec3 normal = normalize(vNormal.xyz);
  52.     vec3 fragToLightDir = normalize(uLightPos_obj.xyz - vPosition);
  53.     vec3 viewToLightDir = normalize(uEyePos_obj.xyz - vPosition);
  54.     vec3 reflectDir = calculateReflectDir(normal, fragToLightDir);
  55.     float diffuse = calculateDiffuse(normal, fragToLightDir);
  56.     float specular = calculateSpecular(viewToLightDir, reflectDir);
  57.    
  58.     vec4 diffuseTextureColor = texture(uTex_dm, (uAtlas * vec4(vTexcoord, 0.0f, 1.0f)).xy);
  59.     vec3 diffuseColor = diffuseTextureColor.rgb * diffuse;
  60.     vec4 specularTextureColor = texture(uTex_sm, (uAtlas * vec4(vTexcoord, 0.0f, 1.0f)).xy);
  61.     vec3 specularColor = specularTextureColor.rgb * vec3(pow(specular, 8.0f)); //8.0f for moderate shininess.  Could use a uniform for this, but for the sake of simplifying the homework, we'll use a constant literal here.
  62.     rtFragColor = vec4(diffuseColor.rgb, diffuseTextureColor.a * specularTextureColor.a) + vec4(specularColor.rgb, 0.0f); //We want to multiply alpha, not add it.
  63. }
Add Comment
Please, Sign In to add comment