microgoodies

By Johnnie Rose, Jr.

Don't Be a Primitive, Use Primitives

Every once in a while VRML gives you, the developer, a little break.  You can consider the addition of primitives to the language as one of these breaks.  Primitives are predefined shapes that have already been made into nodes for easy use, so you wouldn't have to continuously make your own shapes even if they were very simple.  These primitives include the box, cone, cylinder, and sphere.  All of the nodes that represent these shapes fit inside a field of the Shape node:
Shape {
   appearance   NULL   # exposedField   SFNode
   geometry     NULL   # exposedField   SFNode
}
  • The Shape node is used to tell VRML that a shape is coming up and defines the attributes that will go along with it.
  • For example, the appearance field always contains an Appearance node that sets the color, among other things, of the shape.  This will be covered in a later tutorial.
  • You actually tell VRML what primitive you want to use in the geometry field.
As mentioned above, every primitive has its own node.  First up, the box:

The Box

Box {
   size   2.0 2.0 2.0   # field   SFVec3f
}

The size field sets the various sizes of the box in all your directions.  The first value sets the width of the box in the X axis, the second the height in the Y axis, and the third the depth of the box in the Z axis.  When you don't set any sizes, VRML reverts back to the defaults, 2.0, 2.0, 2.0.

The Cone

Cones are a little bit more exciting than simple boxes.  First of all, they have curved sides, all of which you can choose to build or not to build:

Cone {
   bottomRadius   1.0    # field   SFFloat
   height         2.0    # field   SFFloat
   side           TRUE   # field   SFBool
   bottom         TRUE   # field   SFBool
}

As you might expect, bottomRadius specifies the radius of the circle that creates the bottom of the cone.  The default is a radius of one.  The height determines the height of the cone in the Y axis.  If you don't use the height field, VRML defaults to two, where one unit of the cone is below the origin, and the other unit is above the origin.  Things get a little interesting with the side field.  If the value of the field is true, the sides of the cone (curved surfaces) will be built; if it's false, the opposite happens.  The bottom field follows the same directive.  Use the values true and false to tell the browser if the bottom is supposed to be built.  You can choose to have no sides or a bottom built, in which case the cone will pull a disappearing act and become invisible.

The Cylinder

The Cylinder node has just one more attribute:

Cylinder {
   radius   1.0    # field   SFFloat
   height   2.0    # field   SFFloat
   side     TRUE   # field   SFBool
   top      TRUE   # field   SFBool
   bootom   TRUE   # field   SFBool
}

The radius field defines the radius of the circles that make up the top and bottom of the cylinder.  VRML defaults to one unit.  Use the height field to specify the cylinder's height in the Y axis.  Determine if the curved sides of the cylinder are to be built by using the side nodes.  The same is true with the top and bottom fields, so you can choose whether to build the top and bottom circles of the cylinder, respectively.  Just as above, if no sides are to be built, you'll get an invisible cylinder.  These things might not be so primitive after all.

The Sphere

The last primitive is the sphere:

Sphere {
   radius   1.0   # field   SFFloat
}

The sphere has only one field, radius, which determines the radius of the sphere.

Experimenting with the Primitives

And no, this isn't some socio-political survey on the behavior of people on a distant island.  Let's attempt something easy, like a default box:

#VRML V2.0 utf8

Shape {
   # Make the box gray
   appearance Appearance {
      material Material {}
   }
   geometry Box {}
}

Easy enough, right?  Now let's make this box taller and thinner by manipulating its attribute:

#VRML V2.0 utf8

Shape {
   appearance Appearance {
      material Material {}
   }
   geometry Box {
      size 1.0 3.0 2.0
   }
}

The cylinder can be manipulated just as easily.  Here's one that's been flattened:

#VRML V2.0 utf8

Shape {
   appearance Appearance {
      material Material {}
   }
   geometry Cylinder {
      height 0.5
   }
}

If you then disable the top and bottom of the cylinder, you get a circular ribbon with some pretty interesting shading:

#VRML V2.0 utf8

Shape {
   appearance Appearance {
      material Material {}
   }
   geometry Cylinder {
      height 0.5
      top FALSE
      bottom FALSE
   }
}

In closing, don't be a primitive.  Use primitives.

johnnie2 AT hal-pc DOT org

This site and all contents herein are Copyright ©1998-2006 Johnnie Rose, Jr. and may not be copied or reposted without written permission from the founder.
View the Microgoodies Privacy Policy. Fight Spam!