Lights Out Cube VML Polygons 3D VML Home Home


How to use the 3D VML Library

The 3D VML Library is a minimalist 3D library; it only renders collections of well-formed set of polygons. A polygon set cannot for example be rendered correctly if there are intersecting polygons.

VML is essentially a 2D graphics library, as a result the 3D VML library cannot make any use of your powerful 3D graphics card. Therefore it is unlikely to be useful for writing Quake like games. The 3D VML library is a useful way of creating 3D effects on your web pages, such as rotating 3D text or spinning geometric shapes.

One of the goals of the 3D VML library was to be small, so as to be quickly downloadable. The only way to create polygons is by defining them in an XML file. This is no too much of a constraint as defining polygons programmatically is a tricky task and ultimately involves transforming data anyway.

XML data islands

IE5 has a rather nice extension known as XML data islands. An XML data island can exist anywhere in a web page or as link to a downloadable file. Once loaded these objects can be accessed via the standard DOM API.

3D VML polygons are defined in XML data islands. The syntax consists of our XML consists of:

<XML> <SCALE>11 </SCALE><POINTS></p><p><X>-11</X><Y>-11</Y><Z>-11</Z></p><p><X>-11</X><Y>11</Y><Z>-11</Z></p><p><X>11</X><Y>11</Y><Z>-11</Z></p><p><X>11</X><Y>-11</Y><Z>-11</Z></p></POINTS><FACES><F id="aFace" color="red"><V><p>0</V><V>1</V><V>2</V><V>3</V></F></FACES></XML> The XML syntax is hopefully self-documenting. Each polygon document must contain three root level sections:

XML data islands are a really flexible feature of IE5; the cube example uses them to load the data on demand, to change the polygon without having to reload the web page.

The JScript and HTML

To enable VML you must as your opening HTML tag have:

</p> <p>VML is implemented as a name space and using what Microsoft call behaviours. To enable the VML behaviour add the following style: <xmp>

The 3D VML library needs a small amount of Script to tie the XML data island to the 3D VML library. Before the VML Library can be used you need insert some where in the HTML page:

The following JScript can tie the XML data island and the DIV:

<script> var fullyloaded=false; function fnStartInit() { // Never load unless everything is ready if (vcube.readyState=="complete" && fullyloaded) { pframe.loadObject(vcube,1000); } } function init(){ // Create a polyframe pframe=new polyframe(); // set the name of our DIV and our polygons pframe.init("leftvml","cube"); fullyloaded=true; // we can in general never be sure of the order of things so call the StartInit function fnStartInit(); // Transform the points and draw pframe.polys.transformPoints(.05,.05,0,0,0,0); pframe.polys.perspTransform(); pframe.polys.draw(); } // set up the on load handler onload=init; </script>

The fnStartInit function is called every time the state of the XML file changes. We are only interested when it has completely loaded. As it's possible that the XML file may have finished loading before our HTML file, we are working over a network and strange things may happen after all, we do not allow the polyframe to load the object until the HTML file is also fully loaded. We call fnStartInit during the init function in case this series of events has occurred.

This short example ties these ideas together.

Click here to download the files needed to make the simple example work

Web site (c) 2000 Gareth Richards