\chapter[DLP and Virtual Worlds]{DLP and Virtual Worlds}
\label{dlp-on-vw}
\label{dlpve}
\section{VRML EAI and DLP}
VRML EAI stands for the external authoring interface of the virtual reality modeling
language. The EAI allows developers to control the contents of a VRML world.
A VRML specification can be loaded in a browser by an application, such as a Java
applet. As mentioned, DLP programs are compiled to Java classes, which can be used
as Java applets in Web Browsers.
DLP has been extended with a VRML EAI library, called (\inx{bcilib}).
For instance, in the \inx{DLP VRML library}, the predicate
$getSFVec3f(Object,Field,X,Y,Z)$ gets the $SFVec3f$ value
(which consists of three float numbers $X$, $Y$, and $Z$) of the $Field$ of
object $Object$,
and $setSFVec3f(Object,Field,X,Y,Z)$ assigns $X$, $Y$, and $Z$ values
to the $SFVec3f$ $Field$ in $Object$, where $Object$ refers to an object in
a 3D VRML world.
A DLP program can manipulate VRML virtual worlds by using the VRML EAI library
predicates. Before we introduce more details of the DLP VRML predicates, we discuss
first how to design VRML worlds that can be accessed from DLP programs.
\section{Design 3D Virtual Worlds for DLP}
3D virtual worlds are implemented in VRML and the VRML EAI predicates refer
to objects in the current virtual world which is loaded into a web browser.
Each VRML file contains a \inx{scene graph hierarchy}\inxx{scene graph} which
consists of \inx{VRML nodes}.
Node statements may contain \inx{SFNode} or \inx{MFNode} \inx{field statements}
that contain other node statements.
Objects which can be manipulated by DLP programs are nodes which have names
defined by \inx{DEF statements} in VRML.
We use DEF to define object names of VRML nodes, and use DLP VRML predicates to
manipulate the values of the fields of the defined nodes.
For example, the following specification defines a yellow cylinder in VRML.
\begin{verbatim}
#VRML V2.0 utf8
Transform {
translation 0 0 0
rotation 0 1 0 0
children [Shape {appearance Appearance {
material Material {diffuseColor 1.0 1.0 0.0}}
geometry Cylinder { height 2.0 radius 1.0}}]}
\end{verbatim}
In order to manipulate the values of the field 'translation' and the field 'rotation' of
the cylinder, we have to define a name for the node 'Transform' as follows.
\begin{verbatim}
#VRML V2.0 utf8
DEF cylinder Transform {
translation 0 0 0
rotation 0 1 0 0
children [Shape {appearance Appearance {
material Material {diffuseColor 1.0 1.0 0.0}}
geometry Cylinder { height 2.0 radius 1.0}}]}
\end{verbatim}
Note that we do not define the name of fields.
More generally, we can define a prototype of a partial hierarchy first,
so we can use such a prototype to create multiple instances of 3D objects.
For example, the specification of a bus whose position and orientation can be
controlled by DLP programs, consists of the following three steps:
\begin{enumerate}
\item Design a prototype for a bus;
\item Instantiate the bus prototype;
\item Use DEF to define the name of the bus.
\end{enumerate}
The corresponding VRML description is as follows:
\begin{verbatim}
PROTO Bus [
exposedField SFVec3f translation 0 0 0
exposedField SFRotation rotation 0 1 0 0]
{
Transform {
translation IS translation
rotation IS rotation
children [
......
]}}
Transform {children [ DEF bus1 Bus {
translation -5 0 -1.5
rotation 0 1 0 0} ] }
\end{verbatim}
Thus, $setSFVec3f(bus1, translation, 15, 0, -1.5)$ sets $bus1$ to
a new position $\langle -15, 0, -1.5\rangle$.
Similarly, we can use the same method to manipulate a viewpoint in a
VRML world, namely, we define a viewpoint first and then use the viewpoint
predicates
\[getViewpointPosition(Viewpoint, X, Y,Z)\]
and
\[setViewpointPosition(Viewpoint, X,Y,Z)\]
to set and get the values of a viewpoint that's defined as:
\begin{verbatim}
DEF myviewpoint Viewpoint { position -10 1.75 0
orientation 0 1 0 -1.5708
set_bind TRUE}
\end{verbatim}
Since VRML allows multiple viewpoints in a virtual world,
we can only get the field values of the initial viewpoint unless we use
explicitly the corresponding set-predicates. In the example above,
calling $getViewpointPosition(myviewpoint, X,Y,Z)$, gets the result
$X=-10.0$, $Y=1.75$, and $Z=0.0$ no matter how the user changes the
viewpoint in the virtual world.
In most applications, we want to get the current position of
the user's viewpoint which may be changed by using the keyboard or mouse for
navigation.
This can be done by adding a proximity sensor to the virtual world:
\begin{verbatim}
DEF proxSensor ProximitySensor {center 0 0 0
size 1000 1000 1000
enabled TRUE
isActive TRUE}
\end{verbatim}
Here we specify a proximity sensor with the size ($1000 \times 1000 \times 1000)$.
Of course, the parameters should be changed for different applications.
Getting the position and the rotation of the proximity sensor means getting the
current values of the user's viewpoint. Therefore, we can define the extended
get-viewpoint predicates as:
\begin{verbatim}
getViewpointPositionEx(_,X,Y,Z) :-
getSFVec3f(proxSensor,position,X,Y,Z).
getViewpointOrientationEx(_,X,Y,Z,R):-
getSFRotation(proxSensor,orientation,X,Y,Z,R).
\end{verbatim}
\section{Loading 3D Virtual Worlds}
3D virtual worlds have to be loaded in a web browser for program manipulation, which can
be done as follows:
\begin{enumerate}
\item Virtual worlds embedded in html files:
\begin{verbatim}
DLP-BCI example 1