Thursday, February 25, 2010

How to create a 3D wireframe with X3D

All I wanted to do was display some lines in a 3D coordinate system to make sure everything was as it should be. That can't be hard, can it?

Well, it seems it is hard. At least, it's hard to figure out how to do it, because there aren't that many tools that will show you a 3D scene from a file you generate by hand/script.

I quickly found out about the VRML/X3D stuff. It looked ancient. Rotten. Neglected. All sad. A bunch of tutorials made me sure that I didn't want to go that route: None of the tutorials talked about how to display a line in a 3D scene. Instead, it seemed VRML was all about showing shapes: Boxes, cylinders, spheres and stuff like that.

Ugh! Ok. I guess I could just draw really thin boxes and call them lines. But it just gets worse: All shapes are defined in distinces from an origin - so your sphere has a radius. That's it. It is stuck at [0, 0, 0]. But you then translate your shape to where you want it to go. Move it around, rotate it. Etc.

Argh! I had all these nice 3D coordinates for my lines ready. But the docs said I would have to transform them all into this complicated scheme. Which, I am sure makes a lot of sense for 3D modelling. It's just for simple wireframe output that it seems a little bloated.

Well, It's not really that bad: X3D has an element called LineSet that lets you specify... lines! You just write a list of 3D points to the file and off you go!

So, if you are the next person to try creating a simple wireframe with X3D, you might want to have a look at this stub file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN"
  "http://www.web3d.org/specifications/x3d-3.0.dtd">
<X3D profile="Immersive" >
  <Scene>

    <Transform>
      <Shape>
        <LineSet vertexCount="5">
            <Coordinate point="1 0 0
                               1 1 0
                               0 1 0
                               0 0 0
                               1 0 0"
                               />
        </LineSet>
    </Shape>
    <Shape>
        <LineSet vertexCount="5">
            <Coordinate point="1 0 1
                               1 1 1
                               0 1 1
                               0 0 1
                               1 0 1"
                               />
        </LineSet>
    </Shape>
    <Shape>
        <LineSet vertexCount="5">
            <Coordinate point="0 0 1
                               1 0 1
                               1 0 0
                               0 0 0
                               0 0 1"
                               />
        </LineSet>
    </Shape>
    <Shape>
        <LineSet vertexCount="5">
            <Coordinate point="0 1 1
                               1 1 1
                               1 1 0
                               0 1 0
                               0 1 1"
                               />
        </LineSet>
      </Shape>
    </Transform>
  </Scene>
</X3D>


This will create a simple 1x1x1 cube. You can view this with an X3D browser. I used FreeWRL. It is supposed to be only for OSX and Linux, but they do have a windows binary that sort of works. At least for my purposes it is really enough. The other browsers out there (ones that also act as web browser plugins) seem scammy. They let you download a "test" version. What does that meen? Is this a trial version? Do I have to buy it? Where? How much?

So, here is a screenshot of FreeWRL rendering the X3D file above:
I rotated the cube a bit (this is the nice thing about having a dedicated browser - try writing your own!) to make it more recognizable.

3 comments:

  1. Have you ever tried Titania X3D Browser? It's also free and has a nice rendering engine.

    ReplyDelete
  2. Hi Holger! Awesome, I'll check it out!

    ReplyDelete