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.sepia_callback = function(renderingObj)
 10 {
 11 	var progObjID = renderingObj.getProgramObjectID();
 12 	var renderer = renderingObj.getRenderer();
 13 	var geometry = renderingObj.getGeometry();
 14 	var gl = renderingObj.getContext();
 15 	var effect = geometry.getEffect();
 16 	
 17 	gl.useProgram(progObjID);
 18 
 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 			renderer.setVertexAttribArray(progObjID, "Texture", 2, currColl.getVBOTexCoords());
101 			usingTexture = true;
102 		}
103 		else
104 		{
105 			//glCanvas3D.activeTexture(gl.TEXTURE0);
106 			gl.disableVertexAttribArray(texAttribLoc);
107 			//gl.bindTexture(gl.TEXTURE_2D,-1);
108 		}
109 
110 		// tell the fragment shader if we are using textures or not
111 		renderer.setUniformi(progObjID, "usingTexture", usingTexture);
112 
113 		// Vertices
114 		renderer.setVertexAttribArray(progObjID, "Vertex", 3, currColl.getVBOVertices());
115 		gl.drawArrays(renderer.getFillMode(), 0, currColl.getVertices().length/3);
116 	}
117 }
118