// // VS.txt is distributed under the FreeBSD License // // Copyright (c) 2012, Carlos Rafael Gimenes das Neves // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. // // http://carlosrafaelgn.com.br/WebGL/VS.txt // uniform mat4 modelViewProjectionMatrix, normalMatrix; uniform vec3 lightDirection; uniform bool useLight; uniform float diffuseLightIntensity, ambientLightIntensity; attribute vec4 attribPos; //0 attribute vec3 attribNormal; //1 attribute vec4 attribColor; //2 varying vec4 color; void main() { //calcula a posição de saída do vértice gl_Position = modelViewProjectionMatrix * attribPos; //calcula a cor do vértice, utilizando o algoritmo abaixo, porém em uma linha //em seguida, envia a cor para o próximo shader /*if (useLight) { // //NÃO precisaria normalizar novamente o vetor normal caso fosse possível //GARANTIR que a matriz utilizada para criar normalMatrix não contém //NENHUM tipo de redimensionamento! Infelizmente não pode ignorar as matrizes //de redimensionamento na hora de criar a matriz normal, pois elas alteram a //posição dos pontos, o que altera o ângulo das superfícies! // vec4 transNormal = normalize(normalMatrix * attribNormal.xyzx); float diffuse = max(dot(transNormal.xyz, lightDirection), 0.0); vec2 tmp = vec2((diffuseLightIntensity * diffuse) + ambientLightIntensity, 1.0); color = attribColor.rgba * tmp.xxxy; } else { color = attribColor; }*/ color = (useLight ? (attribColor.rgba * vec2((diffuseLightIntensity * max(dot(normalize(normalMatrix * attribNormal.xyzx).xyz, lightDirection), 0.0)) + ambientLightIntensity, 1.0).xxxy) : attribColor); }