Mesh Object

### 3.5.1 Mesh Object

You know you have been raytracing too long when ...
... You think that the evolution theory was based on the triangular origin of the wheel.
-- Mark Kadela

Mesh objects are very useful because they allow us to create objects containing hundreds or thousands of triangles. Compared to a simple union of triangles the mesh object stores the triangles more efficiently. Copies of mesh objects need only a little additional memory because the triangles are stored only once.

Almost every object can be approximated using triangles but we may need a lot of triangles to create more complex shapes. Thus we will only create a very simple mesh example. This example will show a very useful feature of the triangles meshes though: a different texture can be assigned to each triangle in the mesh.

Now let's begin. We will create a simple box with differently colored sides. We create an empty file called `meshdemo.pov` and add the following lines. Note that a mesh is - not surprisingly - declared using the keyword `mesh`.

      camera {
        location <20, 20, -50>
        look_at <0, 5, 0>
      }
      light_source { <50, 50, -50> color rgb<1, 1, 1> }
      #declare Red = texture {
        pigment { color rgb<0.8, 0.2, 0.2> }
        finish { ambient 0.2 diffuse 0.5 }
      }
      #declare Green = texture {
        pigment { color rgb<0.2, 0.8, 0.2> }
        finish { ambient 0.2 diffuse 0.5 }
      }
      #declare Blue = texture {
        pigment { color rgb<0.2, 0.2, 0.8> }
        finish { ambient 0.2 diffuse 0.5 }
      }

We must declare all textures we want to use inside the mesh before the mesh is created. Textures cannot be specified inside the mesh due to the poor memory performance that would result.

Now we add the mesh object. Three sides of the box will use individual textures while the other will use the global mesh texture.

      mesh {
        /* top side */
        triangle {
          <-10, 10, -10>, <10, 10, -10>, <10, 10, 10>
          texture { Red }
        }
        triangle {
          <-10, 10, -10>, <-10, 10, 10>, <10, 10, 10>
          texture { Red }
        }
        /* bottom side */
        triangle { <-10, -10, -10>, <10, -10, -10>, <10, -10, 10> }
        triangle { <-10, -10, -10>, <-10, -10, 10>, <10, -10, 10> }
        /* left side */
        triangle { <-10, -10, -10>, <-10, -10, 10>, <-10, 10, 10> }
        triangle { <-10, -10, -10>, <-10, 10, -10>, <-10, 10, 10> }
        /* right side */
        triangle {
          <10, -10, -10>, <10, -10, 10>, <10, 10, 10>
          texture { Green }
        }
        triangle {
          <10, -10, -10>, <10, 10, -10>, <10, 10, 10>
          texture { Green }
        }
        /* front side */
        triangle {
          <-10, -10, -10>, <10, -10, -10>, <-10, 10, -10>
          texture { Blue }
        }
        triangle {
          <-10, 10, -10>, <10, 10, -10>, <10, -10, -10>
          texture { Blue }
        }
        /* back side */
        triangle { <-10, -10, 10>, <10, -10, 10>, <-10, 10, 10> }
        triangle { <-10, 10, 10>, <10, 10, 10>, <10, -10, 10> }
        texture {
          pigment { color rgb<0.9, 0.9, 0.9> }
          finish { ambient 0.2 diffuse 0.7 }
        }
      }

Tracing the scene at 320x240 we will see that the top, right and front side of the box have different textures. Though this is not a very impressive example it shows what we can do with mesh objects. More complex examples, also using smooth triangles, can be found under the scene directory as `chesmsh.pov`.