[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Use ray tracing to draw a three-dimensional spiral in Python

[A three-dimensional spiral drawn with ray tracing in Python]

This example uses the techniques from my book Build Your Own Ray Tracer With Python to draw a sort of three-dimensional spiral shape. The ray tracing code is too long to show here. Buy the book to see how that works.

However, this post does show the code that builds the three-dimensional scene.

The following code shows how the program defines its scene. (This is pretty typical of such methods used in the book.)

# Define the scene. def define_scene(self): # Make the camera. camera_x = float(self.camera_x.get()) camera_y = float(self.camera_y.get()) camera_z = float(self.camera_z.get()) position = Vector3(camera_x, camera_y, camera_z) direction = -position up = Vector3(0, 1, 0) distance = direction.length() camera = RtCamera(position, direction, up, distance) viewing_angle = float(self.viewing_angle.get()) camera.viewing_angle = viewing_angle # Make the scene. self.scene = RtScene(RtColor.BLACK, camera) # Make lights. dim_gray = RtColor(0.3, 0.3, 0.3) self.scene.lights.append(RtAmbientLight(dim_gray)) self.scene.lights.append(RtDirectionalLight(RtColor.GRAY, Vector3(-1, -2, -3))) # Only draw faces toward the camera. do_xmin = do_ymin = do_zmin = False do_xmax = do_ymax = do_zmax = True # Define the colors. use_colors = False if use_colors: colors = [ RtColor.from_name('pink'), RtColor.from_name('light green'), RtColor.from_name('light blue'), ] else: colors = [ RtColor.from_name('orange'), RtColor.from_name('orange'), RtColor.from_name('orange'), ] # Make materials from the colors. materials = [ RtMaterial(color, specular_intensity=1, specular_power=30) for color in colors ] # Make meshes from the materials. meshes = [RtMesh('mesh', material) for material in materials] for mesh in meshes: self.scene.objects.append(mesh) # Create the objects. for i in range(5): center = Vector3(-1, 1, -1) + i * Vector3( 0, 0, -2) meshes[0].add_partial_box(center, 1 + 2 * i, 1 + 2 * i, 1, do_xmin, do_xmax, do_ymin, do_ymax, do_zmin, do_zmax) center = Vector3(-1, -1, 1) + i * Vector3(-2, 0, 0) meshes[1].add_partial_box(center, 1, 1 + 2 * i, 1 + 2 * i, do_xmin, do_xmax, do_ymin, do_ymax, do_zmin, do_zmax) center = Vector3( 1, -1, -1) + i * Vector3( 0, -2, 0) meshes[2].add_partial_box(center, 1 + 2 * i, 1, 1 + 2 * i, do_xmin, do_xmax, do_ymin, do_ymax, do_zmin, do_zmax)

The code first gets the desired camera position from its text boxes and uses position values to build the camera. It then creates a scene and adds some lights to it.

The book defines an RtMesh class that can hold a collection of triangles. For this example, I added an add_partial_box method to draw parts of a box. To save drawing time, the box only includes some of the box's sides. The do_xmin, do_xmax, and other similar values determine whether the program draws the box's various sides. In this example, it only draws the sides face the positive X, Y, and Z directions.

After it defines variables to determine which sides are drawn, the code builds a list of colors. From that it creates a list of materials, which it then uses to make a list of mesh objects. Set the use_colors variable to True to give the mesh's different colors.

If you look at the picture at the top of this post, you might think this is a very complicated scene. It's actually pretty simple once you realize that it's just three pyramids properly arranged with their apexes touching. The following picture shows the three pyramids drawn in different colors.

[This three-dimensional spiral is just three properly aligned pyramids]
(I think it's interesting that color makes the result in my post Use ray tracing to draw an interesting collection of colored blocks in Python harder to understand but it makes this post easier to understand.)

The code uses a loop to build each pyramid's levels and put them in their corresponding meshes.

The rest of the program draws the scene. Download the example to see additional details or, better still, read my book Build Your Own Ray Tracer With Python see how the ray tracing code works.

© 2024 - 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.