Download demo executable - 46 Kb
Download project source - 118 Kb
Online documentation - 14 Kb
This article presents:
-
CVRMLScene
a "Scene graph" and all the associated classes, including a full VRML 1.
x parser.
-
COGLCanvas
my own YAOWM (yet another OpenGL wrapper for MFC ;)).
-
CVRMLCanvas
a descendant of COGLCanvas
a sample application that renders a limited set of VRML primitives using OpenGL. Samples for the kind of
models supported by this canvas can be freely downloaded at
http://www.rosl.com (site which I have nothing to do with, at least yet ;)).
The .wrl files included in .zip files are from that URL (except allnodes.wrl
).
-
vrmltest
, the
sample application of all the above and which screenshots you can see at the
top.
Index
Introduction
I'm developing a CAD-alike environment and debugger for industrial robot
programming as the final project of my five year degree in Computer Science. It has to resemble up to some extent
Robotech's Workspace . One of its required capabilites is to
load VRML models of the robots, so hence this code.
This article is far from exhaustive but you can peep at the documentation
of all the classes, which is autogenerated from the code with
Eric Artzt's Autoduck, an incode documentation tool
(that's why the weird comments you see in the code). I plan to release an article on this autodocumentation
tool some day (or another).
Full VRML 1.x parser
I'm not going to say too much about this parser, except that I'm very proud of it :*).
The main class is CVRMLScene
, where resides the VRML file parsing and loading capabilities.
All the other classes define the structure needed to traverse the built scene graph (and do whatever you
want with it).
The parser is done with Bison and Flex, so if you modify the sources, take care not to modify the
autogenerated files vrml1.cpp
(Flex generated) or vrml1_tab.cpp
(Bison generated).
Only if you don't have at range Bison or Flex, or if you don't want to deal with those beasts, it would be
convenient to modify them directly.
Just to state it clear: you don't need Bison and Flex at all to use
and compile this classes, only if you want to modify vrml.l
(Flex's token definition file)
or vrml.y
(Bison's grammar) you will need to regenerate vrml1.cpp
and
vrml1_tab.cpp
. Anyway, only because of bugs you should need to modify those files, as the
main functionality of these classes reside in VRML.cpp
file.
I've based the design of the parser in SGI's VRML 2.x parser, which is one of the simplest and smartest things
I've seen on the matter: The parser only knows about simple types of VRML (SFxxxx
and
MFxxxx
) and definition nodes
, and knows nothing about the nodes themselves.
Then it loads a library of default nodes (defined in a totally compliant VRML file, which has the peculiarity
that it only defines nodes and not instances of them), and presto!, you are ready
to load any VRML 1.x compliant file.
The included file allnodes.wrl
is that library of standard VRML 1.x nodes. The normal operation is
to load that library as if it was a normal VRML file (and it IS!), so standard nodes get defined, and then
load your desired VRML file/s (you can load as many VRML files as you want in the same scene).
The error detecting capabilities of this class are quite weak, it only returns FALSE
if file
is invalid, and I wouldn't find it strange if it hanged with corrupt VRML files :-m.
The parser has two non-stantard properties: the VRML file needn't to be a unique VRML node (there are
plenty of VRML files that have more than one root node), and any node (and not only group nodes)
can have children nodes. The latter is a side effect of not knowing beforehand the standard nodes
(anyway, this is VRML 1.x standard's fault, because user defined nodes can't mark in any way if they have
children nodes or not ... VRML 1.x is quite a dumb language, if I may say it: it's not LALR, and some
hacks are needed to parse it with Bison).
MFC OpenGL wrapper
This wrapper exports the overridable function Render
, so descendants only have to implement
that function and this class handles everything else:
- interaction with mouse: pan (right button), zoom (left button, moving up) and rotation (both buttons)
- wireframe, solid and smooth rendering
- lighting (it sets what VRML calls a headlight)
Someday it will support switching between orthographic and perspective (is just adding the methods, right
now the perspective code is commented) and OpenGL selection modes.
Oh, yes, I almost forget it: the mouse buttons get sticked sometimes.
VRML partial viewer
The viewer CVRMLCanvas
is based on the classes COGLCanvas
and
CVRMLScene
. It takes a simple CVRMLScene
and renders it the best it can ;).
The supported nodes are Material, IndexedFaceSet and Coordinate3.
As I said above, you can find free sample supported VRML files at the robots library of
Robot Simulations Ltd. and some of them are included in .zip files.
Sample application
In the directory ./wrl of the .zip you'll find sample files. Just load one of them and play
with it.