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 Shaders6.3.3 Locations and view transformations as vectors and matricesUse the setUniform() and setUniformMatrix() functions to pass a vector or matrix to the shaderprogram.The code in Example 6-8 passes vectors representing the light and camera position:Example 6-8 Passing a vector uniform to the shaderfloat angleRad = 23.0f/(180*3.15149265);vec3 camPos=vec3(75.0f * cos(angleRad), 75.0f * sin(angleRad), 0.0f);vec3 lightPosition(75.0f, 75.0f, 0.0f);context->setUniform("LIGHT_POSITION", lightPosition);context->setUniform("CAMERA_POSITION", camPos);glClearColor(0.0f, 0.0f, 0.0f, 1.0f);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);glEnable(GL_CULL_FACE);glCullFace(GL_BACK);glEnable(GL_DEPTH_TEST);// calculate the camera positionvec3 camPos(60.0f, 0.0f, 0.0f);context->setUniform("CAM_POS", camPos);// the world is formed from time-based rotationsmat4 world = mat4::rotation(t*10.0f, vec3(1,1,0))*mat4::rotation(t, vec3(1,0,1));// the view is constructed from the camera location and viewing orientationsmat4 view = mat4::lookAt( camPos, vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f) );// the perspective transform is specified by field-of-view, aspect ratio,// near plane, and far planemat4 proj = mat4::perspective(270.0f, 4.0f/3.0f, 1.0f, 500.0f);// combine the calculated matrices to a world-view-projection matrixwvpMatrix = proj * view * world;// set the uniform values to pass to the shaderscontext->setUniformMatrix("WORLD_VIEW_PROJECTION", wvpMatrix);context->setUniformMatrix("WORLD", world.toMat3());The vertex shader reads the vertex values and calculates the lighting as shown in Example 6-9:Example 6-9 Using a matrix uniform in the vertex shaderuniform mat4 WORLD_VIEW_PROJECTION;uniform mat3 WORLD;uniform vec3 LIGHT_POSITION;uniform vec3 CAMERA_POSITION;attribute vec4 POSITION;attribute vec3 NORMAL;varying vec3 vNormal;varying vec3 vLightVector;varying vec3 vCameraVector;void main(void){gl_Position = WORLD_VIEW_PROJECTION * POSITION;ARM DUI 0527A-02a Copyright © 2010 ARM. All rights reserved. 6-10ID070710Non-Confidential - Draft - Beta

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

Saved successfully!

Ooh no, something went wrong!