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