13.07.2015 Views

Mali GPU User Interface Engine Application Development Guide

Mali GPU User Interface Engine Application Development Guide

Mali GPU User Interface Engine Application Development Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Tutorial on Constructing Custom ShadersLighting calculation in the vertex shaderThe vertex shader code in Example 6-27 calculates the position of a fragment based on theinterpolated POSITION and lighting vectors from the transformations:Example 6-27 Specular lighting calculation in vertex shaderuniform mat4 WORLD_VIEW_PROJECTION;uniform mat3 WORLD;// the light at each location depends on the relative angles between light position,point location, camera angle, and lighting colorsuniform vec3 LIGHT_POSITION;uniform vec3 CAMERA_POSITION;uniform vec3 DIFFUSE_LIGHT;uniform vec3 SPECULAR_LIGHT;// interpolated values for this point on primitive shapeattribute vec4 POSITION;attribute vec3 NORMAL;// light values to pass to fragment shadervarying vec3 vDiffuse;varying vec3 vSpecular;void main(void){// calculate the position based on the transformationgl_Position = WORLD_VIEW_PROJECTION * POSITION;// vector variables used to calculate lightingvec3 normal = normalize(WORLD * NORMAL);vec3 pos = WORLD * POSITION.xyz;// determine the angle from the light to the shape positionvec3 lightVector = normalize(LIGHT_POSITION - pos);// calculate a multiplier for the diffuse lightfloat nDotL = max(dot(normal, lightVector), 0.0);vDiffuse = DIFFUSE_LIGHT * nDotL;}// No point in calculating specular reflections from the backside of vertices.float specPow = 0.0;if (nDotL > 0.0){normalize the camera position and calculate the light reflectionvec3 cameraVector = normalize(CAMERA_POSITION - pos);vec3 reflectVector = reflect(-cameraVector, normal);// determine the amount of reflected lightspecPow = pow(max(dot(reflectVector, lightVector), 0.0), 4.0);}// assign the specular light based on the specular uniform and// the computed multipliervSpecular = SPECULAR_LIGHT * specPow;As shown in Example 6-28, the fragment shader simply combines the light sources:Example 6-28 Fragment shader adds specular light to background lightuniform vec3 AMBIENT_LIGHT;uniform vec4 COLOR;varying vec3 vDiffuse; // calculated diffuse componentvarying vec3 vSpecular; // calculated specular component based on light positionARM DUI 0527A-02a Copyright © 2010 ARM. All rights reserved. 6-21ID070710Non-Confidential - Draft - Beta

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!