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 
  8 */
  9 c3dl.gooch_callback = function (renderingObj) {
 10   var renderer = renderingObj.getRenderer();
 11   var gl = renderingObj.getContext();
 12   var geometry = renderingObj.getGeometry();
 13   var effect = geometry.getEffect();
 14   var programObjID = renderingObj.getProgramObjectID();
 15   
 16   gl.useProgram(programObjID);
 17 
 18   var modelViewMatrix = c3dl.peekMatrix();
 19   c3dl.matrixMode(c3dl.PROJECTION);
 20   var projectionMatrix = c3dl.peekMatrix();
 21   c3dl.matrixMode(c3dl.MODELVIEW);
 22 
 23   var modelViewProjMatrix = c3dl.multiplyMatrixByMatrix(projectionMatrix, modelViewMatrix);
 24   renderer.setUniformMatrix(programObjID, "modelViewMatrix", modelViewMatrix);
 25   renderer.setUniformMatrix(programObjID, "modelViewProjMatrix", modelViewProjMatrix);
 26   
 27   // Commenting out until a fix is solved for the 2-object outline bug
 28 
 29   // Only render the outline, which is a single pixel thick if the user
 30   // wants it and the effect exists.
 31   if (effect.getParameter("outline") == true && renderer.SOLID_COLOR_EFFECT_ID) {
 32     gl.enable(gl.POLYGON_OFFSET_FILL);
 33     gl.polygonOffset(2.0, 2.0);
 34 
 35     // use the solid color effect since it's fast and we don't have
 36     // to send unnecessary data.
 37     var outlineProgID = renderer.SOLID_COLOR_EFFECT_ID;
 38 
 39     gl.enable(gl.CULL_FACES);
 40     gl.cullFace(gl.FRONT);
 41     gl.useProgram(outlineProgID);
 42     renderer.setUniformf(outlineProgID, "color", [0, 0, 0]);
 43 
 44     var modelViewMatrix = c3dl.peekMatrix();
 45     c3dl.matrixMode(c3dl.PROJECTION);
 46     var projectionMatrix = c3dl.peekMatrix();
 47     c3dl.matrixMode(c3dl.MODELVIEW);
 48 
 49     var MVPMatrix = c3dl.multiplyMatrixByMatrix(projectionMatrix, modelViewMatrix);
 50     renderer.setUniformMatrix(outlineProgID, "modelViewProjMatrix", MVPMatrix);
 51 
 52 
 53 
 54 
 55     var contextWidth = renderer.getContextWidth();
 56     var contextHeight = renderer.getContextHeight();
 57 
 58     for (var primSet = 0; primSet < geometry.getPrimitiveSets().length; primSet++) {
 59       var currColl = geometry.getPrimitiveSets()[primSet];
 60 
 61     // Prevent C3DL from reporting an error, so check if attrib exsits
 62     // before trying to set it.
 63     // This is  a kludge for Safari and Chrome since they want these attributes
 64     ////////////////////////////
 65     var normalAttribLoc = gl.getAttribLocation(outlineProgID,"Normal");
 66 		if(normalAttribLoc != -1 && currColl.getNormals())
 67 		{
 68       renderer.setVertexAttribArray(outlineProgID, "Normal", 3, currColl.getVBONormals());
 69 		}
 70 		var texAttribLoc = gl.getAttribLocation(outlineProgID, "Texture");
 71 		if(texAttribLoc != -1 && currColl.getTexCoords())
 72 		{
 73 			renderer.setVertexAttribArray(outlineProgID, "Texture", 2, currColl.getVBOTexCoords());
 74 		}
 75     ////////////////////////// End kludge
 76     
 77       renderer.setVertexAttribArray(outlineProgID, "Vertex", 3, currColl.getVBOVertices());
 78 
 79       gl.viewport(1, -1, contextWidth, contextHeight);
 80       gl.drawArrays(renderer.getFillMode(), 0, currColl.getVertices().length / 3);
 81 
 82       gl.viewport(-1, -1, contextWidth, contextHeight);
 83       gl.drawArrays(renderer.getFillMode(), 0, currColl.getVertices().length / 3);
 84 
 85       gl.viewport(-1, 1, contextWidth, contextHeight);
 86       gl.drawArrays(renderer.getFillMode(), 0, currColl.getVertices().length / 3);
 87 
 88       gl.viewport(1, 1, contextWidth, contextHeight);
 89       gl.drawArrays(renderer.getFillMode(), 0, currColl.getVertices().length / 3);
 90     }
 91 
 92     // restore normal backface culling
 93     gl.cullFace(gl.BACK);
 94     gl.viewport(0, 0, contextWidth, contextHeight);
 95 
 96     gl.disable(gl.POLYGON_OFFSET_FILL);
 97     gl.polygonOffset(0.0, 0.0);
 98   }
 99   
100   gl.useProgram(programObjID);
101 
102   //renderer.setUniformi(programObjID, "lightingOn", true);
103   // render all the collation elements. Every collation element in an object will
104   // have the same tranformation
105   for (var coll = 0; coll < geometry.getPrimitiveSets().length; coll++) {
106     var currColl = geometry.getPrimitiveSets()[coll];
107 
108     var dummyAttribLoc = gl.getAttribLocation(programObjID, "dummyAttrib");
109     if (dummyAttribLoc !== -1 && currColl.getNormals()) {
110       renderer.setVertexAttribArray(programObjID, "dummyAttrib", 3, currColl.getVBONormals());
111     }
112 
113     var normalAttribLoc = gl.getAttribLocation(programObjID, "Normal");
114 
115     // if the object acutally has normals and the normal attribute was found
116     //
117     if (normalAttribLoc !== -1 && currColl.getNormals()) {
118       // the top matrix is the modelview matrix.
119       var NormalMatrix = c3dl.inverseMatrix(modelViewMatrix);
120       NormalMatrix = c3dl.transposeMatrix(NormalMatrix);
121       renderer.setUniformMatrix(programObjID, "normalMatrix", NormalMatrix);
122 
123       renderer.setVertexAttribArray(programObjID, "Normal", 3, currColl.getVBONormals());
124     } else {
125       gl.disableVertexAttribArray(normalAttribLoc);
126     }
127 
128     renderer.setUniformf(programObjID, "warmColor", effect.getParameter("warmColor"));
129     renderer.setUniformf(programObjID, "coolColor", effect.getParameter("coolColor"));
130     renderer.setUniformf(programObjID, "surfaceColor", effect.getParameter("surfaceColor"));
131 
132     renderer.setVertexAttribArray(programObjID, "Vertex", 3, currColl.getVBOVertices());
133     gl.drawArrays(renderer.getFillMode(), 0, currColl.getVertices().length / 3);
134   }
135 }