Bind Nodes from a .smls/.wrl file to the Source Code for Seamless3d

This tutorial shows how anyone can utilize the source files for seamless3d to bind nodes from a .smls or .wrl file to a C++ file that gets compiled with all the seamless source files (as part of the program)
This should be a much better way to go for developing non triveal scripts before attempting to use the built in Seamless compiler (at least at this stage) because you will have professional aids at your disposal such as being able to single step through your code and view the value of any variable in frozen time.
Working with a standard compiler at this stage of seamless's development will also have benefits by verifying first how the C++ code is meant to behave for seamless.
The main challenge for beginners will be setting up the C++ compiler to compile Win32 and DirectX files. After this it should be a breeze for anyone use to VRML and JavaScript.

Have VC 9 Express Setup

To do this you will need the Microsoft Visual C++ 2008 Express Compiler set up to compile the Seamless3d source files. To do this click here.

A Simple Rotating Box
We wont need to do all this just to rotate a box but to keep things simple this is what we will use for an example showing how to bind nodes from a smls file.

Create a new smls file called rotate_box_bind.smls and replace the Seamless node in the scene tree with the following VRML97 nodes as shown:

The names are necessary for any node we want to bind to.

Set the Viewpoint node to the following values:

Save this file.

Select the scene node and click on the outputNodeBind button.
This should result in a text file named rotate_box_bind.cpp being created in the same folder the rotate_box_bind.smls is located in.

Put the file rotate_box_bind.cpp in the folder:


so that its located with the rest of the seamless source files.
In VC9 open the solution explorer and right click on the Source Files folder and select:

Add / Existing Item

select the file rotate_box_bind.cpp and click on the Add button.

Now you should see rotate_box_bind.cpp added to the Source Files. Double click on rotate_box_bind.cpp file from the solution explorer to view the code for this file.
At the top of this file we will see the String variable g_bindSmlsFile contains the url for the smls file. To make it so that Seamless opens this file and run the code in our rotate_box_bind.cpp file open the seamless3d.h file (from the solution browser located in the Header Files folder) and go to the first line of code after the copyright licence notice:


and delete the 2 forward slash characters (//) to un-comment this line for the compiler.

Now when we run the compiler via the Start Debugging command (select Yes to rebuild when prompted):


we will run our cpp file. However because our cpp file does not yet do anything to alter our nodes we will see nothing different at this stage in the 3d window.

The initialize Function

The initialize function in the rotate_box_bind.cpp is called only once and before all other user defined script functions are called whenever Seamless3d is run.

To witness the initialize function do something lets add the following line of code:

myMaterial.diffuseColor = Color3f(1, 0, 0);

to colour the box red and the following line of code:

myTransform.translation = Vec3f(-3, 0, -5);

to change the position of the box -3 meters left and 5 meters back.

Our initialize function should now look like this:

void initialize(){
    myMaterial.diffuseColor = Color3f(1, 0, 0);
    myTransform.translation = Vec3f(-3, 0, -5);

Press F5 to Run (select Yes to rebuild when prompted)


and we should see the changes to the box displayed in the 3d window.


The onFrame function simulates an Anim's onFrame function.
This function gets called for every frame enabling us to do animation.
To rotate our box using C++ code, add the following line of code:

myTransform.rotation = Rotation(0, 1, 0, PI*v);

to the onFrame function so that we now see:

void onFrame(float v){
    myTransform.rotation = Rotation(0, 1, 0, PI*v);

Now when we run (F5) we should see a rotating box.

Copying the Code from the .cpp file to the .smls

To be able to use any code we write in the cpp file for online purposes we must copy it to a smls file so that the C++ instructions can be compiled by seamless3d's built in compiler.
For our box example make a copy of the rotate_box_bind.smls and rename it to simply rotate_box.smls
Open this file in seamless the normal way using the exe from the seamless site as opposed to running VC9 (which has BIND_DEVELOPER_SCRIPT set(so would need to be commented out)).
Add a Function
name it to initialize
Add an Anim node.
Add a MemeberFunction to the Anim node.
Open Settings and uncheck saveGZipped.
save and open the rotate_box.smls file into VC9 (just for editing)
and copy and paste the initialize and onFrame functions from the rotate_box_bind.cpp file to the rotate_box.smls file so that thse functions get repleced in the rotate_box.smls file.

Now the rotate_box.smls file should behave exactly the same when opened online and compiled by seamless as it is when compiled by VC9.

Copyright 2000-2006 Graham Perrett