Drawing basic 3D primitive shapes

All the 2D graphical shapes can work in the 3D environment. In addition, GEM provides a number of 3D primitives, such as cone, cube, cuboid, curve3d, cylinder, disk, sphere, teapot, torus, and tube. Let us learn it step-by-step by following a similar procedure.

Create an empty patch and save it with name gem003.pd in your folder. Put the gemwin object, create and destroy messages, and the toggle box for rendering.

We start from the simplest 3D object, cube. Put the gemhead and cube objects and connect them together. Similar to 2D shapes, we can also have the draw fill and draw line messages. Here is the patch and the resulting window. By using the draw line message, the 3D perspective projection of the cube is more obvious:

Here is the wire-frame rendering of the cube in the three-dimensional space:

Control for the cube object is simple. It has only one number inlet for its size. The next object is sphere. It has two parameters. One for the size and another for the resolution, that is, the number of straight line segments to form the circle. If you have an extremely small value for the last inlet, such as three or four, you may end up with a polygonal 3D object, instead of a sphere object:

Here is the wire-frame rendering of the sphere. Note that the last inlet of the sphere object defines its resolution:

In order to show the 3D rendering of the sphere, we can make use of lighting. In the gemwin object, we can turn on the lighting effect by the message lighting 1. To turn it off, specify the message lighting 0. We also introduce a new object world_light to light up the environment.

Here is the result without turning on the lighting:

Here is the result with lighting turned on:

In the patch, we have to use two messages to turn lighting on and off for the GEM window. Actually, we can simplify it with one message and a toggle box. In the message box, we type lighting $1, instead of putting 1 or 0, and then we connect a toggle box before it:

In the previous chapter, we learned that a toggle box sends out either 0 or 1. The lighting $1 message has one argument, similar to the use of function arguments in other programming language. The notation $1 is the first argument. This message box will package the text lighting together with 0 or 1 from the toggle box and send it out to the gemwin object to control the lighting effect.

The world_light object defines a point light at infinity distance to the scene. Without the light, your scene will be invisible once you turn on the lighting message. Note that the world_light object has another gemhead object for it. You can also add color information in the second inlet of the world_light object. Again, the color is the RGB number values, ranging from 0 to 1: