ch6_Transparency_Initial.html
in your HTML5 web browser. We have two completely opaque objects: a cone behind a wall. Click-and-drag on the canvas to move the camera behind the wall and see the cone as shown in the following screenshot: ch6_Transparency_Initial.html
using your preferred source code editor. Scroll to the configure
function and below these lines:gl.enable(gl.DEPTH_TEST); gl.depthFunc(gl.LEQUAL);
Add:
gl.enable(gl.BLEND); gl.blendFunc(gl.SRC_ALPHA,gl.ONE_MINUS_SRC_ALPHA);
ch6_Transparency_Final.html
and load this page on your web browser. ch6_Transparency_Final.html
in your source code editor.The cone is the farthest object in the scene. Hence, it is loaded first. You can check that by looking at the load function:
Scene.loadObject('models/geometry/cone.json','cone'), Scene.loadObject('models/geometry/wall.json','wall',{diffuse:[0.5,0.5,0.2,1.0], ambient:[0.2,0.2,0.2,1.0]});
Therefore it occupies a lower index in the Scene.objects
list. In the draw
function, the objects are rendered in the order in which they appear in the Scene.objects
list like this:
for (var i = 0, max=Scene.objects.length; i < max; i++){ var object = Scene.objects[i]; ...
ch6_Transparency_Final.html
and rotate the scene such that the cone appears in front of the wall. Now decrease the alpha value of the cone while the alpha value of the wall remains at 1.0. ch6_Transparency_Final.html
the blending is enabled (you just enabled it on step 3). It has to do with the rendering order. Click on the Wall First button. The scene should appear consistent now.The Cone First and Wall First buttons use a couple of new functions that we have included in the Scene
object to change the rendering order. These functions are renderSooner
and renderFirst.
In total, we have added these functions to the Scene object to deal with rendering order:
renderSooner(objectName)
— moves the object with name objectName
one position before in the Scene.objects
list.renderLater(objectName)
— moves the object with name objectName
one position after in the Scene.objects
list.renderFirst(objectName)
— moves the object with name objectName
to the first position of the list (index 0).renderLast(objectName)
— moves the object with name objectName
to the last position of the list.renderOrder()
— lists the objects in the Scene.objects
list in the order in which they are rendered. This is the same order in which they are stored in the list. For any two given objects, the object with the lower index will be rendered first.You can use these functions from the JavaScript console in your browser and see what effect these have on the scene.
3.23.127.197