Create a simple OpenGL/C++ game with white_dune

You can create OpenGL-programs written in C++ with white_dune.
Here a simple example is shown, the needs only a few lines of programming code.
In the first step a prototype with X3D/javascript is created, that is extended with C++ later.

javascript/X3D prototype

As base, the in X3D available white_dune logo is used. It is a 3d model of a pinguin in a landscape of dunes and a pyramid.

For programming, it makes no difference, is the pinguin is replaced with a box and the landscape is replaced with a large thin box...
In the first step, the target (pinguin or box) is animated with a CurveAnimation.

Next, a Text-node with "Game over" is created.

Next a Switch-node is created and the target is moved into it.
To make the target visible, Switch.whichChoice must be set to 0.
Next another Switch-node is created and the text is moved into it.

Next a Script-node is created.
Next Routes/IS -> rebuild is used. A new window is shown, that shows the CurveAnimation.
Next, the Script-node is selected (clicked).
A SFVec3f-input-function "transform_in" is created.

In the same way. a SFVec3f-input-function "proximity_in" is created.

Next a SFInt32-output-variable "out_text" is created.

Next a SFInt32-output-variable "out_tux" is created.

Now a Proximity-Sensor-node is created and Routes/IS -> show node on top is used.
Now the script-node is selected and Routes/IS -> show node on top is used.
The node are moved and you connect ProxomitySensor.position_changed and Script.proximity_in
Next, you connect CurveAnimation1.position_changed and Script.translation_in

Next, you connect Script.out_text and Switch.set_whichChoice of the text Switch.

Next, you connect Script.out_tux and Switch.set_whichChoice of the target Switch.


Now the script-node is selected and a SFVec3f-data-variable transfrom_data is created.

Next a SFBool-data-variable game_over is created.

The script node is selected and Edit -> URL edit is used .
The following javascript programm has to be inserted:
"ecmascript:

        function transform_in(value)
        {
           transform_data = value;
        }

        function proximity_in(value)
        {
           if (!(game_over) &&
               (Math.abs(transform_data.x - value.x) < 2) &&
               (Math.abs(transform_data.y - value.y) < 3) &&
               (Math.abs(transform_data.z - value.z) < 2)) {
               game_over = true;
               out_text = 0; // show Text
               out_tux = -1; // hide TUX
               }
        }
"

To make the X3D-Game available, the X3D-Browser "FreeWRL" must be installed and Options -> preview settings must be configured for File -> Previe.

Now File -> Preview has to be used and via the FreeWRL navigation the target is reached, so the "show Text/hide TUX" code is executed.
Screenshots of the game:
Before the reach of the target:

After the reach of the target:


Extension with C++

Now the Script node is selected and Edit -> URL edit is used.
The programm is extended with a C++ part:

"ecmascript:

        function transform_in(value)
        {
           transform_data = value;
        }

        function proximity_in(value)
        {
           if (!(game_over) &&
               (Math.abs(transform_data.x - value.x) < 2) &&
               (Math.abs(transform_data.y - value.y) < 3) &&
               (Math.abs(transform_data.z - value.z) < 2)) {
               game_over = true;
               out_text = 0; // show Text
               out_tux = -1; // hide TUX 
               }
        }
"
"c++:

   X3dScript_Script1 *self = (X3dScript_Script1 *)node;
   self->transform_data[0] = self->transform_in[0];
   self->transform_data[1] = self->transform_in[1];
   self->transform_data[2] = self->transform_in[2];
   if (!(self->game_over) &&
       (fabsf(self->transform_data[0] - self->proximity_in[0]) < 2) &&
       (fabsf(self->transform_data[1] - self->proximity_in[1]) < 3) &&
       (fabsf(self->transform_data[2] - self->proximity_in[2]) < 2)) {
       self->game_over = true;
       self->out_text = 0; // show Text
       self->out_tux = -1; // hide TUX
   }
"
Now the C++-Export-Verzeichnis of white_dune need to be copied to the HOME-Verzeichnis:
$ cp -r wdune-*/docs/export_example_c++/opengl_example/ $HOME

The created X3D-Datei must be stored to $HOME/opengl_example/robot.x3dv
Change to the directory
$ cd $HOME/opengl_example/ 

And use
$ make && ./render

to compile and execute the programm.