Finally fixed the shader.

This commit is contained in:
2014-05-26 16:43:59 -04:30
parent 1bbe0db68d
commit c2afd241fc
3 changed files with 28 additions and 23 deletions

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef GL_ES
precision mediump float;
#endif
@@ -33,27 +32,29 @@ varying vec3 v_normal;
// Fragment shaded diffuse color.
varying vec4 v_diffuse;
// Vector from the fragment to the light source.
varying vec3 v_lightVector;
// Vector from the fragment to the camera.
varying vec3 v_eyeVector;
// The light vector reflected around the fragment normal.
varying vec3 v_reflectedVector;
// The clamped dot product between the normal and the light vector.
varying float v_nDotL;
void main(){
// Normalize the input varyings.
vec3 normal = normalize(v_normal);
vec3 lightVector = normalize(v_lightVector);
vec3 eyeVector = normalize(v_eyeVector);
vec3 normal = normalize(v_normal);
vec3 eyeVector = normalize(v_eyeVector);
vec3 reflectedVector = normalize(v_reflectedVector);
// Specular Term:
vec4 specular = u_specular * pow(max(dot(reflectedVector, eyeVector), 0.0), u_shiny);
// Specular Term.
vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
if(v_nDotL > 0.0){
specular = u_specular * pow(max(dot(reflectedVector, eyeVector), 0.0), u_shiny);
}
// Aggregate light color.
vec4 finalColor = clamp(vec4(u_ambient.rgb + v_diffuse.rgb + specular.rgb, 1.0), 0.0, 1.0);
vec4 finalColor = clamp(vec4(/*u_ambient.rgb*/ + v_diffuse.rgb + specular.rgb, 1.0), 0.0, 1.0);
// Final color.
gl_FragColor = finalColor;

View File

@@ -13,13 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Model-view matrix.
uniform mat4 u_projTrans;
// The world space geometric transformation to apply to this vertex.
uniform mat4 u_geomTrans;
// The inverse transpose of the geometric transformation matrix.
uniform mat4 u_normalMatrix;
// Light source position
uniform vec3 u_lightPos;
@@ -41,30 +43,32 @@ attribute vec4 a_normal;
// Fragment normal.
varying vec3 v_normal;
// Diffuse shaded color to pass to the fragment shader.
// Diffuse shaded color.
varying vec4 v_diffuse;
// The vector from the vertex to the light source.
varying vec3 v_lightVector;
// The vector from the vertex to the camera.
varying vec3 v_eyeVector;
// The light vector reflected around the vertex normal.
varying vec3 v_reflectedVector;
// The clamped dot product between the normal and the light vector.
varying float v_nDotL;
void main(){
// Apply the geometric transformation to the original position of the vertex.
vec4 transformedPosition = u_geomTrans * a_position;
vec3 lightVector = normalize(u_lightPos.xyz);
vec3 invLightVector = normalize(-u_lightPos.xyz);
// Set the varyings.
v_normal = normalize(a_normal.xyz);
v_lightVector = normalize(transformedPosition.xyz - u_lightPos.xyz);
v_eyeVector = normalize(u_cameraPos.xyz - transformedPosition.xyz);
v_reflectedVector = normalize(reflect(-v_lightVector, a_normal.xyz));
v_normal = normalize(vec4(u_normalMatrix * a_normal).xyz);
v_eyeVector = normalize(u_cameraPos.xyz - transformedPosition.xyz);
v_reflectedVector = normalize(reflect(-lightVector, v_normal));
// Diffuse Term.
v_diffuse = u_lightDiffuse * u_materialDiffuse * max(dot(a_normal.xyz, v_lightVector), 0.0);
float invNDotL = max(dot(v_normal.xyz, invLightVector), 0.0);
v_nDotL = max(dot(v_normal.xyz, lightVector), 0.0);
v_diffuse = (u_lightDiffuse * u_materialDiffuse * v_nDotL) + (vec4(0.1, 0.1, 0.2, 1.0) * u_materialDiffuse * invNDotL);
gl_Position = u_projTrans * transformedPosition;
}