# 3D graphic functions

## rotateAroundAxis

``````  rotateAroundAxis(angle:float, axis:vector3D)
``````

In more complex application it is basically impossible to rotate around a specific rotation axis using only rotate3D(). For this reason the rotateAroundAxis() function was implemented.

This function will rotate the current frame along a user specified axis (3D vector) by a given angle (in rad).

It also works in either LOCAL or GLOBAL mode based on the current set rotationMode().

``````axis = vector3D(x=1,y=2,z=0.5)
rotateAroundAxis(PI/4, axis)

``````

## setWorldRotation3D

``````  setWorldRotation3D(rotation:rotation3D)
``````

For more elaborate 3D applications, it is helpful to be able to write & read the current world rotation.

This functions writes a previously read world rotation.

``````rotation:rotation3D
rotation = getWorldRotation3D()
setWorldRotation3D(rotation)

``````

## getWorldRotation3D

``````  getWorldRotation3D()->rotation3D
``````

For more elaborate 3D applications, it is helpful to be able to write & read the current world rotation.

This functions returns a rotation3D object/class (by reference).

``````rotation:rotation3D
rotation = getWorldRotation3D()

``````

## rotationMode

``````  rotationMode(mode:byte)
``````

Sometimes it’s easier to think in the world frame (especially when dealing with consecutive rotations).

You can set the rotation’s reference frame to LOCAL (default) or GLOBAL.

``````rotationMode(LOCAL)
rotationMode(GLOBAL)

``````

## translationMode

``````  translationMode(mode:byte)
``````

Sometimes it’s easier to think in the world frame (especially when dealing with consecutive rotations).

You can set the translation’s reference frame to LOCAL (default) or GLOBAL.

``````translationMode(LOCAL)
translationMode(GLOBAL)

``````

## render

``````  render()
``````

All 3D shapes are drawn on top of 2D elements. This is because of the automatic call of render() within the update() function.

If you would like to draw 2D elements on top of 3D shapes, you can call render() manually after the drawing of 3D shapes.

``````fill(230, 204, 57)
background(162, 9, 176)
textFont(FONT_ROBOTO_BOLD_64)
drawSphere(3)
stroke(0, 0, 0)
drawText(26, 90, "Sphere") # this black text will be behind the sphere
render()
stroke(255, 255, 255)
drawText(20, 80, "Sphere") # this white text will be in front of the sphere
update()

``````

``````  addVertex3D(x:float, y:float, z:float)
``````

This function adds a point at location x/y/z in a polygon definition.

Example: see beingShape3D()

## endShape3D

``````  endShape3D()
``````

This function closes a polygon definition.

When endShape3D() is called, the polygon is drawn into the screen buffer.

Example: see beginShape3D()

## beginShape3D

``````  beginShape3D()
``````

Starts a polygon definition. This consists of a beginShape3D(), several calls to addVertex3D() and a termination with endShape3D().

Example:

``````const l = 1.06 # 0.1 .. 2.0

fill(255,255,255)

def onDraw():
clear()
rotateY(0.02)
beginShape3D() # face 1
addVertex3D(-l, -l, l) # face 2
addVertex3D(l, -l, -l) # face 3
addVertex3D(l, -l, -l) # face 4
endShape3D()
update()

``````

This example draws a simple tetrahedron with four faces rotating in space.

## drawShape3D

``````  drawShape3D(vertices:float[], triangles:int[])
``````

You can create and render custom 3D shapes using the drawShape3D() function.

Every shape consists of a list of vertices (locations in the 3D space) and a list of triangles/faces between those vertices. Example:

``````verts:float = [
-1,-1,-1,
1,-1,-1,
1,1,-1,
-1,1,-1,
-1,-1,1,
1,-1,1,
1,1,1,
-1,1,1,
0,-1,2,
0,1,2
]
tris:int = [
0,1,4,
1,5,4,
1,2,5,
2,6,5,
2,3,6,
3,7,6,
3,0,7,
0,4,7,
0,3,2,
0,2,1,
4,5,8,
6,7,9,
5,6,8,
7,4,9,
4,8,9,
6,9,8
]

fill(255,255,255)
rotateX(-PI_DIV2)
scaleZ(0.8)

def onDraw():
clear()
rotateZ(0.02)
drawOrigin()
drawShape3D(verts, tris)
update()

``````

## rotateCameraZ

``````  rotateCameraZ(z:float)
``````

Same as rotateCamera3D(x, y, z) but only rotating the camera around the Z axis.

``````fill(255,255,255)
def onDraw():
clear()
rotateCameraZ(0.1)
drawCube(2)
update()

``````

## rotateCameraY

``````  rotateCameraY(y:float)
``````

Same as rotateCamera3D(x, y, z) but only rotating the camera around the Y axis.

``````fill(255,255,255)
def onDraw():
clear()
rotateCameraY(0.1)
drawCube(2)
update()

``````

## rotateCameraX

``````  rotateCameraX(x:float)
``````

Same as rotateCamera3D(x, y, z) but only rotating the camera around the X axis.

``````fill(255,255,255)
def onDraw():
clear()
rotateCameraX(0.1)
drawCube(2)
update()

``````

## rotateCamera3D

``````  rotateCamera3D(x:float, y:float, z:float))
``````

Besides using rotate3D() to rotate the whole frame (world), you can also use this function to rotate the camera.

The function is additive. Each additional call moves the origin relative to the previous position. If you don’t want this, you can use push()/pop(), which will restore the original state.

Unlike 2D objects, translate and rotate functions are the only option to set the position of 3D shapes.

## scaleZ

``````  scaleZ(z:float)
``````

Same as scale3D(x, y, z) but only scaling on the Z axis.

``````fill(255,255,255)
rotateY(PI/2)
def onDraw():
push()
for i in 100:
clear()
scaleZ(0.99)
drawSphere(4)
update()
pop()

``````

## scaleY

``````  scaleY(y:float)
``````

Same as scale3D(x, y, z) but only scaling on the Y axis.

``````fill(255,255,255)
def onDraw():
push()
for i in 100:
clear()
scaleY(0.99)
drawSphere(4)
update()
pop()

``````

## scaleX

``````  scaleX(x:float)
``````

Same as scale3D(x, y, z) but only scaling on the X axis.

``````fill(255,255,255)
def onDraw():
push()
for i in 100:
clear()
scaleX(0.99)
drawSphere(4)
update()
pop()

``````

## scale3D

``````  scale3D(x:float, y:float, z:float)
``````

Scales all subsequent drawing commands by the specified value. For example, the size doubles with the value 2 or halves with 0.5.

Note: scale3D() is multiplicative. This means that, for example, scale3D(2) and scale3D(1.5) together achieve the same effect as a single scale3D(3).

Example:

``````fill(255,255,255)
drawSphere(1)
update()
delay(1000)

scale3D(2,2,2)
drawSphere(1)
update()
delay(1000)

scale3D(1.5,1.5,1.5)
drawSphere(1)
update()

``````

## translateCameraZ

``````  translateCameraZ(z:float)
``````

Same as translateCamera3D(x, y, z) but only translating the Z axis of the camera.

``````fill(255,255,255)
translateCameraZ(-5)
def onDraw():
for i in 100:
clear()
translateCameraZ(0.1)
drawCube(2)
update()
translateCameraZ(-10)

``````

## translateCameraY

``````  translateCameraY(y:float)
``````

Same as translateCamera3D(x, y, z) but only translating the Y axis of the camera.

``````fill(255,255,255)
translateCameraY(-5)
def onDraw():
for i in 100:
clear()
translateCameraY(0.1)
drawCube(2)
update()
translateCameraY(-10)

``````

## translateCameraX

``````  translateCameraX(x:float)
``````

Same as translateCamera3D(x, y, z) but only translating the X axis of the camera.

``````fill(255,255,255)
translateCameraX(-5)
def onDraw():
for i in 100:
clear()
translateCameraX(0.1)
drawCube(2)
update()
translateCameraX(-10)

``````

## translateCamera3D

``````  translateCamera3D(x:float, y:float, z:float)
``````

Besides using translate3D() to move the whole frame (world), you can also use this function to move the position of the camera.

The function is additive. Each additional call moves the origin relative to the previous position. If you don’t want this, you can use push()/pop(), which will restore the original state.

Unlike 2D objects, translate and rotate functions are the only option to set the position of 3D shapes.

## rotateZ

``````  rotateZ(z:float)
``````

Same as rotate3D(x, y, z) but only rotating around the Z axis.

``````fill(255,255,255)
def onDraw():
clear()
rotateZ(0.1)
drawCube(2)
update()

``````

## rotateY

``````  rotateY(y:float)
``````

Same as rotate3D(x, y, z) but only rotating around the Y axis.

``````fill(255,255,255)
def onDraw():
clear()
rotateY(0.1)
drawCube(2)
update()

``````

## rotateX

``````  rotateX(x:float)
``````

Same as rotate3D(x, y, z) but only rotating around the X axis.

``````fill(255,255,255)
def onDraw():
clear()
rotateX(0.1)
drawCube(2)
update()

``````

## rotate3D

``````  rotate3D(x:float, y:float, z:float)
``````

Use this function to rotate the current frame (world) around the origin point.

The function is additive. Each additional call moves the origin relative to the previous position. If you don’t want this, you can use push()/pop(), which will restore the original state.

Unlike 2D objects, translate and rotate functions are the only option to set the position of 3D shapes.

## translateZ

``````  translateZ(z:float)
``````

Same as translate3D(x, y, z) but only translating the Z axis.

``````fill(255,255,255)
translateZ(-5)
def onDraw():
for i in 100:
clear()
translateZ(0.1)
drawCube(2)
update()
translateZ(-10)

``````

## translateY

``````  translateY(y:float)
``````

Same as translate3D(x, y, z) but only translating the Y axis.

``````fill(255,255,255)
translateY(-5)
def onDraw():
for i in 100:
clear()
translateY(0.1)
drawCube(2)
update()
translateY(-10)

``````

## translateX

``````  translateX(x:float)
``````

Same as translate3D(x, y, z) but only translating the X axis.

``````fill(255,255,255)
translateX(-5)
def onDraw():
for i in 100:
clear()
translateX(0.1)
drawCube(2)
update()
translateX(-10)

``````

## translate3D

``````  translate3D(x:float, y:float, z:float)
``````

Use this function to move the current frame (world).

The function is additive. Each additional call moves the origin relative to the previous position. If you don’t want this, you can use push()/pop(), which will restore the original state.

Unlike 2D objects, translate and rotate functions are the only option to set the position of 3D shapes.

## drawTriangularPrism

``````  drawTriangularPrism(r:float, h:float)
``````

Draws 3D shape: Triangular Prism

``````fill(255,255,255)
rotate3D(-PI/4, -PI/4, 0)
drawTriangularPrism(2, 3)
update()

``````

## drawCylinder

``````  drawCylinder(r:float, h:float)
``````

Draws 3D shape: Cylinder

``````fill(255,255,255)
rotate3D(-PI/4, -PI/4, 0)
drawCylinder(2, 3)
update()

``````

## drawCuboid

``````  drawCuboid(w:float, l:float, h:float)
``````

Draws 3D shape: Cuboid

``````fill(255,255,255)
rotate3D(0, PI/4, 0)
drawCuboid(3, 2, 1)
update()

``````

## drawSphere

``````  drawSphere(size:float)
``````

Draws 3D shape: Sphere

``````fill(255,255,255)
drawSphere(2)
update()

``````

## drawCube

``````  drawCube(size:float)
``````

Draws 3D shape: Cube

``````fill(255,255,255)
rotate3D(0,PI/4,0)
drawCube(2)
update()

``````

## drawOrigin

``````  drawOrigin()
``````

This function draws a coordinate frame where each axis has a length of 1 meter/unit.

By convention x is red, y is green, and z is blue.

Use for example translate3D() or rotate3D() to change the position of the origin.