Canvas 3d JS Library

WebGL made easy!
  • rss
  • What is C3DL?
  • Download
  • Tutorials
    • Tutorial #1: WebGL Browsers
    • Tutorial #2: A simple scene
    • Tutorial #3: Callback
    • Tutorial #4: Models
    • Tutorial #5: Light effects
    • Tutorial #6: Picking
  • Development News
  • Documentation
  • Community
  • Resources
  • Contact
  • About

The Matrix Stack

Andor Salga | 15 October, 2008 | 21:26
First some background:
OpenGL is a state system which has a state variable called the matrix stack. This is a stack data structure in which each element is a 4×4 matrix. The stack is very useful when rendering a scene in which objects have transformations relative to other objects. For example, when drawing a car:
the car body:
- car matrix transform is pushed onto the stack
- car is drawn
- car body transform popped off

for each tire:
- tire transform pushed on
- tire is drawn
- tire transform popped off

Every time an object is drawn, OpenGL uses the topmost matrix for that object’s transformation. If the car moves, the car’s transform is changed, but since the tires have translations relative to the car and not the scene’s origin, they come along with it. This happens because when pushing on matrices, they are typically multiplied by the one underneath. Therefore the matrices act like reference frames which can be combined together. When a matrix lower in the stack is changed, that is propagated to the matrices on top. Using this system of reference frames, we can accomplish very useful things such as rotating the tires as the car moves, without having to worry about where the car is in the scene. I figured some screenies would be helpful to illustrate the problem. Some collada files are composed of only one node, so the problem isn’t present. In the case of the duck, when the modeler exported this, it was one node, therefore I just represent this with a Model class from the library. But the plane has 2 nodes. Since the propeller will likely be expected to rotate independently from the plane, it was placed in its own node. However since we are currently using the Model() class which does not have its own transform, it results in being placed at the origin. Thus we really need a scenegraph.


Scene graphs can make creating this setup easier as every node in the graph can have a transformation. When rendering is done, the graph is traversed and matrix transformations are pushed and popped off with each node. Scene graphs are very useful structures and are found in collada files. This means for our library to support collada, we need support for both the matrix stack and a scenegraph. The native matrix stack state variable in present in OpenGL ES 1.1, but since 1.1 will eventually be phased out and 2.0 does not have this variable, we need to implement it ourselves. I started working on the stack, but I ran into a problem: If there are multiple canvases per page, should each have its own stack? I spent some time devising a way to have a global array which each element was a matrix stack so every scene can have its own. The solutions I came up with and ones suggested to me were a bit messy, I wanted a simple solution. Eventually Andrew suggested to just use a global for all canvases. I initially didn’t want to do this as I thought firefox’s threads would interfere with the state of the stack. Would the stack be updated by one scene and another scene will try to use it? I did some testing and found this doesn’t seem to be the case so I went ahead and wrote the global functions for manipulating the stack. I like this method as I didn’t want the stack to be tied to the context, which in some cases is necessary.

Speed is always an issue, so I did some primitive testing, how many matrix multiplications could the browser compute? Seems like a hundred in a couple of seconds. But if this needs to be faster, offloading it to the GPU via shaders is one option. Now I just need to beg someone to write a scenegraph in JS.
Categories
c3dl development
Comments rss
Comments rss

« .obj to .dae DAE Scenegraph »

One response

Great! Really really great!

Edson Mattos | 6 November, 2008 | 14:22

Great! Really really great!

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Videos

Demos

  • Asteroids-3D
  • Particle Systems Demo
  • Cross-Browser Orbiter
  • Mocap Demo With Spheres
  • Google Maps-3D

C3DL Development News

C3DL 2.0-WebGL and beyond

It has been a long time coming but we have now updated all the core features of C3DL to use WebGL. You can dowload our 2.0 release here. We have also updated all our demos to use WebGL. Our tutorials have all been updated (tutorial 5 and 6 needs a better example [...]

Preliminary WebGL RTS Game

Cathy asked me to make a cool demo using our library. After thinking about, I started getting many ideas, but creating a preliminary real-time strategy game made the most sense. It not only demonstrates a lot of C3DL features such as model loading, transformations, lighting, shaders, picking, cameras, textures, etc, but since animation is kept [...]

Tutorials

  • Tutorial #1: WebGL Browsers
  • Tutorial #2: A simple scene
  • Tutorial #3: Callback
  • Tutorial #4: Models
  • Tutorial #5: Light effects
  • Tutorial #6: Picking

Documentation

Archives

Archives

C3DL Development News

Recent Comments

  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • C3DL 2.0-WebGL and beyond
  • Preliminary WebGL RTS Game
  • Asteroids in 3D… and a bit of 2D
  • Another demo updated
  • Simplifying the Interface
  • Updating Demos
  • Cross-browser progress update
  • let there be vars
  • Creating tester pages
  • Problems with porting
  • keep it coming ve... - gero3
  • congrats on a great... - Paul Brunt
  • c++ not c# actually... - Cathy Leung
  • It's unbelievable ho... - Paul
  • Wow, now that's a co... - Andor Salga
  • Hi, is the project... - Sascha Hendel
  • I agree with both co... - Cathy Leung
  • Your library is real... - Sascha Hendel
  • Hi, as a more gener... - Sascha Hendel
  • Thanks. I just pull... - peter



Canvas 3d JS Library

©2007- 2009 Canvas 3d JS Library

Disclaimer: This website is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Canada License.
The Canvas 3d JS Library and Demos found on this website are licenced under the MIT License

Creative Commons License