1 /* 2 Copyright (c) 2008 Seneca College 3 Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/) 4 */ 5 6 c3dl.model_vs = 7 8 // 9 // vertex attributes 10 // 11 "attribute vec3 Vertex;" + 12 "attribute vec3 Normal;" + 13 "attribute vec3 Texture;" + 14 15 // for every model we multiply the projection, view and model matrices 16 // once to prevent having to do it for every vertex, however we still need 17 // the view matrix to calculate lighting. 18 "uniform mat4 modelViewMatrix;" + 19 20 // we can calculate this once per model to speed up processing done on the js side. 21 "uniform mat4 modelViewProjMatrix;" + 22 23 // matrix to transform the vertex normals 24 "uniform mat4 normalMatrix;" + 25 26 27 "void main(void){" + 28 29 // create a normal matrix 3x3 out of 4x4 30 " mat3 normalMatrix3x3 = mat3(normalMatrix[0][0],normalMatrix[0][1],normalMatrix[0][2]," + 31 " normalMatrix[1][0],normalMatrix[1][1],normalMatrix[1][2]," + 32 " normalMatrix[2][0],normalMatrix[2][1],normalMatrix[2][2]);" + 33 " vec3 transformNormal = normalize(normalMatrix3x3 * Normal);" + 34 35 // 36 " vec3 ambient = vec3(0.0, 0.0, 0.0);" + 37 " vec3 diffuse = vec3(0.0, 0.0, 0.0);" + 38 " vec3 specular = vec3(0.0, 0.0, 0.0);" + 39 40 // place the current vertex into view space 41 // ecPos = eye coordinate position. 42 " vec4 ecPos4 = modelViewMatrix * vec4(Vertex,1.0);" + 43 44 // the current vertex in eye coordinate space 45 // perspective divide 46 " vec3 ecPos = (vec3(ecPos4))/ecPos4.w;" + 47 48 " vec3 eye = vec3(0.0, 0.0, 1.0);" + 49 //" eye = -normalize(ecPos);" + 50 51 " float shine = 1.0;" + 52 " if(usingMaterial)" + 53 " {" + 54 " shine = material.shininess;" + 55 " }" + 56 57 " if(lightingOn)" + 58 " {" + 59 // iterate over all the lights, and keep incrementing color values 60 // the color values are passed by reference and modified. 61 " for(int i = 0; i < C3DL_MAX_LIGHTS; i++)" + 62 " {" + 63 " if(lights[i].isOn) " + 64 " {" + 65 " if(lights[i].type == 1)" + 66 " {" + 67 " c3dl_DirectionalLight(lights[i], transformNormal, ambient, diffuse, specular, shine);" + 68 " }" + 69 70 " else if(lights[i].type == 2) " + 71 " {" + 72 " c3dl_PointLight(lights[i], transformNormal, eye, ecPos, ambient, diffuse, specular, shine);" + 73 " }" + 74 75 " else if(lights[i].type == 3)" + 76 " {" + 77 " c3dl_SpotLight(lights[i], transformNormal, eye, ecPos, ambient, diffuse, specular, shine);" + 78 " }" + 79 " }" + 80 " }" + 81 " }" + 82 83 " if( usingMaterial ){" + 84 " gl_FrontColor = vec4(material.emission + " + 85 " ambientLightColor + " + 86 " ambient * material.ambient + " + 87 " diffuse * material.diffuse + " + 88 " specular * material.specular,1.0); " + 89 90 " }" + 91 " else{" + 92 " gl_FrontColor = vec4(ambientLightColor + ambient + diffuse + specular,1.0);" + 93 " }" + 94 95 " gl_Position = modelViewProjMatrix * vec4(Vertex, 1.0);" + 96 " gl_TexCoord[0] = vec4(Texture,1.0);" + 97 "}"; 98