1 /* 2 Copyright (c) 2008 Seneca College 3 Licenced under the MIT License (http://www.c3dl.org/index.php/mit-license/) 4 */ 5 6 /** 7 @private 8 */ 9 c3dl.greyscale_callback = function(renderingObj) 10 { 11 // 12 var progObjID = renderingObj.getProgramObjectID(); 13 var renderer = renderingObj.getRenderer(); 14 var geometry = renderingObj.getGeometry(); 15 var gl = renderingObj.getContext(); 16 var effect = geometry.getEffect(); 17 18 gl.useProgram(progObjID); 19 renderer.setUniformf(progObjID, "color", effect.getParameter("color")); 20 21 var modelViewMatrix = c3dl.peekMatrix(); 22 c3dl.matrixMode(c3dl.PROJECTION); 23 var projectionMatrix = c3dl.peekMatrix(); 24 c3dl.matrixMode(c3dl.MODELVIEW); 25 26 // create a ModelViewProjection matrix. By doing this, we can multiply 27 // 3 matrices together once per model instead of once per vertex 28 var modelViewProjMatrix = c3dl.multiplyMatrixByMatrix(projectionMatrix, modelViewMatrix); 29 renderer.setUniformMatrix(progObjID, "modelViewMatrix", modelViewMatrix); 30 renderer.setUniformMatrix(progObjID, "modelViewProjMatrix", modelViewProjMatrix); 31 32 // render all the collation elements. Every collation element in an object will 33 // have the same tranformation 34 for(var coll = 0; coll < geometry.getPrimitiveSets().length; coll++) 35 { 36 var currColl = geometry.getPrimitiveSets()[coll]; 37 38 // MATERIAL 39 var mat = currColl.getMaterial(); 40 41 if(mat) 42 { 43 // every primitive collection can have a material associated with it. 44 // currColl.material.getEmission() 45 renderer.setUniformf(progObjID, "material.emission", mat.getEmission()); 46 renderer.setUniformf(progObjID, "material.ambient", mat.getAmbient()); 47 renderer.setUniformf(progObjID, "material.diffuse", mat.getDiffuse()); 48 renderer.setUniformf(progObjID, "material.specular", mat.getSpecular()); 49 renderer.setUniformf(progObjID, "material.shininess", mat.getShininess()); 50 renderer.setUniformi(progObjID, "usingMaterial", true); 51 } 52 else 53 { 54 renderer.setUniformi(progObjID, "usingMaterial", false); 55 } 56 57 // NORMAL 58 var normalAttribLoc = gl.getAttribLocation(progObjID, "Normal"); 59 60 // if the object acutally has normals and the normal attribute was found 61 // 62 if(currColl.getNormals()) 63 { 64 // the top matrix is the modelview matrix. 65 var NormalMatrix = c3dl.inverseMatrix(modelViewMatrix); 66 NormalMatrix = c3dl.transposeMatrix(NormalMatrix); 67 renderer.setUniformMatrix(progObjID, "normalMatrix", NormalMatrix); 68 renderer.setVertexAttribArray(progObjID, "Normal", 3, currColl.getVBONormals()); 69 } 70 else 71 { 72 gl.disableVertexAttribArray(normalAttribLoc); 73 } 74 75 // TEXTURE 76 var usingTexture = false; 77 78 var texAttribLoc = gl.getAttribLocation(progObjID, "Texture"); 79 80 // 81 var texID = renderer.getTextureID(currColl.getTexture()); 82 83 // if the texture isn't loaded, but this collation element has one, 84 // queue one up 85 if(texID == -1 && currColl.getTexture()) 86 { 87 renderer.addTexture(currColl.getTexture()); 88 } 89 90 // The following must be fixed: If a collada object was specified as having 91 // a texture, but the texture file isn't found, the object will be textured. 92 // However, this following condition will run, because the object 'thinks' 93 // it has a texture. the result is the object will not get lit and will 94 // appear completely black. We need to insert another condition which 95 // states if the texture is actually present, this should run... 96 if(texID != -1 && currColl.getTexture() && currColl.getTexCoords() && texAttribLoc != -1 ) 97 { 98 gl.activeTexture(gl.TEXTURE0); 99 gl.bindTexture(gl.TEXTURE_2D, texID); 100 101 //gl.vertexAttribPointer(texAttribLoc, 2, gl.FLOAT, false, 0, currColl.getTexCoords()); 102 //gl.enableVertexAttribArray(texAttribLoc); 103 renderer.setVertexAttribArray(progObjID, "Texture", 2, currColl.getVBOTexCoords()); 104 usingTexture = true; 105 } 106 else 107 { 108 //gl.activeTexture(gl.TEXTURE0); 109 gl.disableVertexAttribArray(texAttribLoc); 110 //gl.bindTexture(gl.TEXTURE_2D,-1); 111 } 112 113 // tell the fragment shader if we are using textures or not 114 renderer.setUniformi(progObjID, "usingTexture", usingTexture); 115 116 // Vertices 117 renderer.setVertexAttribArray(progObjID, "Vertex", 3, currColl.getVBOVertices()); 118 gl.drawArrays(renderer.getFillMode(), 0, currColl.getVertices().length/3); 119 } 120 } 121