How it works...

We import the math modules (for angle calculations used to control the view based on mouse movements). We also import the demo module, which just provides the path to the shaders and textures in this example.

We start by defining some key elements that will be used by pi3d to generate and display our object. The space in which we shall place our object is the pi3d.Display object; this defines the size of the space and initializes the screen to generate and display OpenGL ES graphics.

Next, we define a pi3d.Camera object, which will allow us to define how we view the
object within our space. To render our object, we define a texture to be applied to the surface and a shader that will apply the texture to the object. The shader is used to apply all the effects and lighting to the object, and it is coded to use the GPU's OpenGL ES core instead of the ARM processor.

We define the keyboard and mouse object using pi3d.keyboard() and pi3d.mouse() so that we can respond to the keyboard and mouse input. The restrict flag of the mouse object allows the absolute mouse position to continue past the screen limits (so we can continuously rotate our 3D object). The main loop, when running, will check if the Esc key is pressed and will then close everything down (including calling DISPLAY.destroy() to release the screen). We use the try: finally: method to ensure that the display is closed correctly, even if there is an exception within main().

The mouse movement is collected in the main display loop using mymouse.position(), which returns the x and y coordinates. The difference in the x and y movement is used to rotate around the object.

The mouse movements determine the position and angle of the camera. Any adjustment to the forward/backward position of the mouse is used to move it over or under the object and change the angle of the camera (using tilt) so it remains pointing at the object. Similarly, any sideways movement will move the camera around the object using the CAMERA.reset() function. This ensures that the display updates the camera view with the new position, CAMERA.rotate(), to change the angle, and uses CAMERA.position() to move the camera to a position around the object, camRad units away from its center.

We will draw a three-dimensional object called a tetrahedron, a shape made up of four triangles to form a pyramid with a triangular base. The four corners of the shape (three around the base and one at the top) will be defined by the three-dimensional coordinates A, B, C, and D, as shown in the following diagram:

The tetrahedron placed within the 3D coordinates

The pi3d.Tetrahedron object is defined by specifying x, y, and z coordinates of four points to position it in the space and then specify the corners that will be joined to form the four triangles that make up the shape.

Using set_draw_details(flatsh,[text]), we apply the shader(s) we wish to use and the texture(s) for the object. In our example, we are just using a single texture, but some shaders can use several textures for complex effects.

To help highlight where the coordinates are, we will add some pi3d.String objects by setting the string text to specify the ID and coordinates next to them and placing it at the required location. We will create two string objects for each location, one facing forward and another facing backward (ry=180 rotates the object by 180 degrees on the y axis). The pi3d.String objects are single-sided, so if we only had one side facing forward, it wouldn't be visible from behind when the view was rotated and would just disappear (plus, if it was visible, the text would be backwards anyway). Again, we use the flatsh shader to render it using the set_shader() string object.

All that is left to do now is to draw our tetrahedron and the string objects while checking for any keyboard events. Each time the while loop completes, DISPLAY.loop_running() is called, which will update the display with any adjustments to the camera as required.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.209.131