### A.2 Turtle and 3D

>From version 0.9.92, our turtle can leave its plane and move into 3D space. To switch to this mode, we use the primitive perspective. Welcome to a 3D world!

#### A.2.1 The perspective projection

To represent a 3D space on a 2D plane, XLOGO uses a projection perspective. A camera looks at the 3D scene, where the image from the projection screen is displaying. Here is a little scheme to explain this:

Some primitives allow us to set the camera position. The screen projection is half the distance from the camera.

#### A.2.2 Understanding orientation in a 3D World

In a 2D plane, the turtle’s orientation was only defined by its heading. In a 3D world, the turtle’s orientation is given by 3 angles:

• Roll: The turtle’s angle around axis (Oy)
• Pitch: The turtle’s angle around axis (Ox)
• Heading: The turtle’s angle around axis (Oz)

In fact, to move itself in the 3D World, the turtle is very similar to an aircraft. Here is a little illustration which represents these 3 values:

Roll

Pitch

It seems quite complex at first, but you will see that a lot of things stay very similar to moving in a 2D plane. Here are the basic primitives for moving in the 3D world:

 forward, fd, back, bk n

Same behaviour as in 2D plane.

 right, rt, left, lt n

Same behaviour as in 2D plane.

 rr, rightroll n

The turtle turns n degrees to the right around its longitudinal axis.

 lr, leftroll n

The turtle turns n degrees to the left around its longitudinal axis.

 up, uppitch n

The turtle goes n degrees up around its transversal axis.

 down, downpitch n

The turtle goes n degrees down around its transversal axis.

In the 2D plane, when we want to draw a square of side 200 steps, we write:
repeat 4[fd 200 rt 90]

These instructions are still available in the 3D world, where the square is drawn in perspective mode. If the turtle goes down 90 degrees, we can draw another square and we obtain:

cs
repeat 4[fd 200 rt 90]
down 90
repeat 4[fd 200 rt 90]

You just have to try some examples to understand these orientations and become an expert!
You must understand that the 3 rotation primitives are linked together, for example try this:

cs
leftroll 90 up 90 rightroll 90

The turtles movement is equivalent to left 90 (You can try with your hand simulating the turtle if you don’t understand)

#### A.2.3 Primitives available in 2D mode and 3D mode

The following primitives are available in 2D plane or in 3D world. The only difference is the arguments received by the primitives. For example, the primitive setpos or setposition is still waiting for a list as an argument but now, the list must contain three numbers (x;y;z) which represent the three point coordinates. Here are all those primitives:

 circle arc home towards distance setpos, setposition setx sety setheading label labellength dot pos, position heading

Primitives only available in 3D mode

 setxyz x y z

This primitive moves the turtle to the chosen point. This primitive is waiting for three arguments representing the point’s coordinates. setxyz is very similar to setpos but the coordinates are not written into a list.
Example, setxyz -100 200 50: move the turtle to the point x = -100;y = 200;z = 50

 setz z

This primitive moves the turtle to the point with the valid value z. setz is waiting for one number as an argument. This primitive is comparable to setx or sety.

 setorientation list

Set the turtle’s orientation. This primitive waits for a list which contains 3 numbers, the roll, the pitch and the heading.
Example, setorientation [100 0 58]: the turtle has roll: 100 degrees, pitch: 0 degree and heading: 58 degrees.

 orientation

Returns the turtle’s orientation in a list which contains: [ roll pitch heading ]. Note the number order, if for example, the orientation value is [100 20 90], this means that if you want the same orientation starting from the origin position (after a clearscreen instruction), you’ll have to write the following sequence:

rightroll 100 up 20 right 90

If you inverse the instruction’s order, you don’t obtain the valid orientation!

 setroll n

The turtle turns around its longitudinal axis to the chosen roll angle.

 roll

Returns the current roll value.

 setpitch n

The turtle turns around its transversal axis to the chosen pitch angle.

 pitch

Returns the current pitch value.

#### A.2.4 3D Viewer

A 3D Viewer is included in XLogo, it allows you to visualize your drawing in 3D. This module uses the JAVA3D library, so it’s necessary to have java3D fully installed.

Here are the rules to use the 3D Viewer:
When we create a geometric figure on the drawing area, we have to indicate to the 3D Viewer which shapes we want to record for future visualization. It’s possible to record polygons (surfaces), lines, points or text. To use this feature, here are the primitives:

 polystart

The following turtle’s moves are saved to create a polygon.

 polyend

Since the last polystart call, the turtle has gone through several vertices. This new polygon is recorded, its color is defined by all vertices color. This primitive finalizes the polygon.

 linestart

The following turtle’s moves are saved to create a strip line.

 lineend

Since the last linestart call, the turtle has gone through several vertices. This new line is recorded, its color is defined by all vertices color. This primitive finalizes the strip line.

 pointstart

The following turtle’s moves are saved to create a point set.

 pointend

This primitive finalizes the point set.

 textstart

Each time the user displays text on the drawing area with the primitive label, it will be recorded and then displayed by the 3D Viewer.

 textend

End of text recording.

 view3d polyview

Launch the 3D viewer, all recorded objects are drawn on this new window. You have control of the camera scene:
• You can rotate the scene by clicking on the mouse’s left button.
• You can translate the scene by clicking on the mouse’s right button.
• You can zoom the scene with the mouse’s wheel button.

#### A.2.5 Drawing a cube

All faces are 400 steps square. Here is the program:

to square
# we record the vertice square
polystart repeat 4[forward 400 right 90] polyend
end

to simpleCube
# yellow cube
clearscreen perspective setpencolor yellow
# lateral faces
repeat 4[square penup right 90 forward 400 left 90 rightroll 90 pendown]
# bottom face
downpitch 90 square uppitch 90
# upper face
forward 400 downpitch 90 square
# visualization
view3d
end

We launch with the command: simpleCube:

When we replace in the procedure square, polystart with linestart and polyend with lineend

If we had used pointstart and pointend instead of linestart and lineend, we would see on screen only the eight cube vertices. These primitives are very useful to display the point set in 3D Space.

#### A.2.6 Lighting the scene

You can specify four lights in your 3D scene. By default, the main 3D scene has only two ponctual lights enabled. Click on one of the 4 button lights in the 3D modeler, and this dialog box appears:

Several light type are available:

• Ambient light: uniform light, you just have to specify its color.
• Unidirectional light: diffuses according to a constant direction. It’s the same case as a ponctual light when the source is very very far from the observer. For example, the case of sun.
• Ponctual light: This light has a specified position. This light is similar to a headlight.
• Spot Light: it is a ponctual light but the light is only displayed in a light cone. You have to specify a value angle for this cone.

The best thing is to play with those lights to understand how they work!

#### A.2.7 Fog effect

You can add a fog effect on the main 3d scene. Click on the cloud button in the 3D scene and this dialog box appears.

Two fogs are available:

• Progressive fog: this fog’s opactity is progressive. You have to specify two parameters:
• The distance from which fog begins.
• The disatnce from which opacity is full
• uniform fog: This fog is uniform on the whole scene. You just have to specify the fog’s density.

Example with a progressive fog: