Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright 2011-2018 Daniel S. Buckstein
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- /*
- animal3D SDK: Minimal 3D Animation Framework
- By Daniel S. Buckstein
- drawPhong_fs4x.glsl
- Receive variables for calculating Phong shading, output result.
- */
- //This file was modified by Summer Softleigh with permission from the author.
- #version 410
- // 1) declare varyings to receive data from vertex shader
- // 2) declare uniform variables for textures; see demo code for hints
- // 3) grab samples from textures
- // 4) calculate diffuse and specular coefficients
- // 5) calculate Phong shading model
- // 6) assign result to output color
- in vec3 vPosition;
- in vec4 vNormal;
- in vec2 vTexcoord;
- uniform vec4 uLightPos_obj;
- uniform vec4 uEyePos_obj;
- uniform sampler2D uTex_dm;
- uniform sampler2D uTex_sm;
- uniform mat4 uAtlas;
- out vec4 rtFragColor;
- vec3 calculateReflectDir(vec3 N, vec3 L)
- {
- return 2.0f * dot(N, L) * N - L;
- }
- float calculateDiffuse(vec3 N, vec3 L)
- {
- 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.
- }
- float calculateSpecular(vec3 V, vec3 R)
- {
- return dot(V, R) * 0.5f + 0.5f; //Same thing as diffuse, just with names changed for clarity.
- }
- void main()
- {
- vec3 normal = normalize(vNormal.xyz);
- vec3 fragToLightDir = normalize(uLightPos_obj.xyz - vPosition);
- vec3 viewToLightDir = normalize(uEyePos_obj.xyz - vPosition);
- vec3 reflectDir = calculateReflectDir(normal, fragToLightDir);
- float diffuse = calculateDiffuse(normal, fragToLightDir);
- float specular = calculateSpecular(viewToLightDir, reflectDir);
- vec4 diffuseTextureColor = texture(uTex_dm, (uAtlas * vec4(vTexcoord, 0.0f, 1.0f)).xy);
- vec3 diffuseColor = diffuseTextureColor.rgb * diffuse;
- vec4 specularTextureColor = texture(uTex_sm, (uAtlas * vec4(vTexcoord, 0.0f, 1.0f)).xy);
- 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.
- rtFragColor = vec4(diffuseColor.rgb, diffuseTextureColor.a * specularTextureColor.a) + vec4(specularColor.rgb, 0.0f); //We want to multiply alpha, not add it.
- }
Add Comment
Please, Sign In to add comment