### 3.5.2 Mesh2 Object

The `mesh2` is a representation of a mesh, that is much more like POV-Ray's internal mesh representation than the standard `mesh`. As a result, it parses faster and it file size is smaller.

Due to its nature, `mesh2` is not really suitable for building meshes by hand, it is intended for use by modelers and file format converters. An other option is building the meshes by macros. Yet, to understand the format, we'll do a small example by hand and go through all options.

to be written as mesh2

We will turn the mesh sketched above into a `mesh2` object. The mesh is made of 8 triangles, each with 3 vertices, many of these vertices are shared among the triangles. This can later be used to optimize the mesh. First we will set it up straight forward.

In `mesh2` all the vertices are listed in a list named `vertex_vectors{}`. A second list, `face_indices{}`, tells us how to put together three vertices to create one triangle, by pointing to the index number of a vertex. All lists in `mesh2` are zero based, the number of the first vertex is 0. The very first item in a list is the amount of vertices, normals or uv_vectors it contains. `mesh2` has to be specified in the order VECTORS..., LISTS..., INDICES....

Lets go through the mesh above, we do it counter clockwise. The total amount of vertices is 24 (8 triangle * 3 vertices).

    mesh2 {
       vertex_vectors {
          24,
          ...

Now we can add the coordinates of the vertices of the first triangle:

    mesh2 {
       vertex_vectors {
          24,
          <0,0,0>, <0.5,0,0>, <0.5,0.5,0>
          ..

Next step, is to tell the mesh how the triangle should be created; There will be a total of 8 face_indices (8 triangles). The first point in the first face, points to the first vertex_vector (0: <0,0,0>), the second to the second (1: <0.5,0,0>), etc...

    mesh2 {
       vertex_vectors {
          24,
          <0,0,0>, <0.5,0,0>, <0.5,0.5,0>
          ...
       }
       face_indices {
          8,
          <0,1,2>
          ...

The complete mesh:

    mesh2 {
       vertex_vectors {
          24,
          <0,0,0>, <0.5,0,0>, <0.5,0.5,0>, //1
          <0.5,0,0>, <1,0,0>, <0.5,0.5,0>, //2
          <1,0,0>, <1,0.5,0>, <0.5,0.5,0>, //3
          <1,0.5,0>, <1,1,0>, <0.5,0.5,0>, //4
          <1,1,0>, <0.5,1,0>, <0.5,0.5,0>, //5
          <0.5,1,0>, <0,1,0>, <0.5,0.5,0>, //6
          <0,1,0>, <0,0.5,0>, <0.5,0.5,0>, //7
          <0,0.5,0>, <0,0,0>, <0.5,0.5,0>  //8
       }
       face_indices {
          8,
          <0,1,2>,    <3,4,5>,       //1 2
          <6,7,8>,    <9,10,11>,     //3 4
          <12,13,14>, <15,16,17>,    //5 6
          <18,19,20>, <21,22,23>     //7 8
       }
       pigment {rgb 1}
    }

As mentioned earlier, many vertices are shared by triangles. We can optimize the mesh by removing all duplicate vertices but one. In the example this reduces the amount from 24 to 9.

    mesh2 {
       vertex_vectors {
          9,
          <0,0,0>, <0.5,0,0>, <0.5,0.5,0>,
          /*as 1*/ <1,0,0>,   /*as 2*/
          /*as 3*/ <1,0.5,0>, /*as 2*/
          /*as 4*/ <1,1,0>,   /*as 2*/
          /*as 5*/ <0.5,1,0>, /*as 2*/
          /*as 6*/ <0,1,0>,   /*as 2*/
          /*as 7*/ <0,0.5,0>, /*as 2*/
          /*as 8*/ /*as 0*/   /*as 2*/
       }
       ...
       ...

Next step is to rebuild the list of face_indices, as they now point to indices in the `vertex_vector{}` list that don't exist anymore.

       ...
       ...
       face_indices {
          8,
          <0,1,2>, <1,3,2>,
          <3,4,2>, <4,5,2>,
          <5,6,2>, <6,7,2>,
          <7,8,2>, <8,0,2>
       }
       pigment {rgb 1}
    }

#### 3.5.2.1 Smooth triangles and mesh2

In case we want a smooth mesh, the same steps we did also apply to the normals in a mesh. For each vertex there is one normal vector listed in `normal_vectors{}`, duplicates can be removed. If the number of normals equals the number of vertices then the `normal_indices{}` list is optional and the indexes from the `face_indices{}` list are used instead.

    mesh2 {
       vertex_vectors {
          9,
          <0,0,0>, <0.5,0,0>, <0.5,0.5,0>,
          <1,0,0>, <1,0.5,0>, <1,1,0>,
          <0.5,1,0>, <0,1,0>, <0,0.5,0>
       }
       normal_vectors {
          9,
         <-1,-1,0>,<0,-1,0>, <0,0,1>,
          /*as 1*/ <1,-1,0>, /*as 2*/
          /*as 3*/ <1,0,0>,  /*as 2*/
          /*as 4*/ <1,1,0>,  /*as 2*/
          /*as 5*/ <0,1,0>,  /*as 2*/
          /*as 6*/ <-1,1,0>, /*as 2*/
          /*as 7*/ <-1,0,0>, /*as 2*/
          /*as 8*/ /*as 0*/  /*as 2*/
       }
       face_indices {
          8,
          <0,1,2>, <1,3,2>,
          <3,4,2>, <4,5,2>,
          <5,6,2>, <6,7,2>,
          <7,8,2>, <8,0,2>
       }
       pigment {rgb 1}
    }

When a mesh has a mix of smooth and flat triangles a list of `normal_indices{}` has to be added, where each entry points to what vertices a normal should be applied. In the example below only the first four normals are actually used.

    mesh2 {
       vertex_vectors {
          9,
          <0,0,0>, <0.5,0,0>, <0.5,0.5,0>,
          <1,0,0>, <1,0.5,0>, <1,1,0>,
          <0.5,1,0>, <0,1,0>,   <0,0.5,0>
       }
       normal_vectors {
          9,
          <-1,-1,0>, <0,-1,0>, <0,0,1>,
          <1,-1,0>, <1,0,0>, <1,1,0>,
          <0,1,0>, <-1,1,0>, <-1,0,0>
       }
       face_indices {
          8,
          <0,1,2>, <1,3,2>,
          <3,4,2>, <4,5,2>,
          <5,6,2>, <6,7,2>,
          <7,8,2>, <8,0,2>
       }
       normal_indices {
          4,
          <0,1,2>, <1,3,2>,
          <3,4,2>, <4,5,2>
       }
       pigment {rgb 1}
    }

#### 3.5.2.2 UV mapping and mesh2

uv_mapping is a method of 'sticking' 2D textures on an object in such a way that it follows the form of the object. For uv_mapping on triangles imagine it as follows; First you cut out a triangular section of a texture form the xy-plane. Then stretch, shrink and deform the piece of texture to fit to the triangle and stick it on.

Now, in `mesh2` we first build a list of 2D-vectors that are the coordinates of the triangular sections in the xy-plane. This is the `uv_vectors{}` list. In the example we map the texture from the rectangular area `<-0.5,-0.5>, <0.5,0.5>` to the triangles in the mesh. Again we can omit all duplicate coordinates

    mesh2 {
       vertex_vectors {
          9,
          <0,0,0>, <0.5,0,0>, <0.5,0.5,0>,
          <1,0,0>, <1,0.5,0>, <1,1,0>,
          <0.5,1,0>, <0,1,0>,   <0,0.5,0>
       }
       uv_vectors {
          9
         <-0.5,-0.5>,<0,-0.5>,  <0,0>,
          /*as 1*/   <0.5,-0.5>,/*as 2*/
          /*as 3*/   <0.5,0>,   /*as 2*/
          /*as 4*/   <0.5,0.5>, /*as 2*/
          /*as 5*/   <0,0.5>,   /*as 2*/
          /*as 6*/   <-0.5,0.5>,/*as 2*/
          /*as 7*/   <-0.5,0>,  /*as 2*/
          /*as 8*/   /*as 0*/   /*as 2*/
       }
       face_indices {
          8,
          <0,1,2>, <1,3,2>,
          <3,4,2>, <4,5,2>,
          <5,6,2>, <6,7,2>,
          <7,8,2>, <8,0,2>
       }
       uv_mapping
       pigment {wood scale 0.2}
    }

Just as with the `normal_vectors`, if the number of `uv_vectors` equals the number of vertices then the `uv_indices{}` list is optional and the indices from the `face_indices{}` list are used instead.

In contrary to the `normal_indices` list, if the `uv_indices` list is used, the amount of indices should be equal to the amount of `face_indices`. In the example below only 'one texture section' is specified and used on all triangles, using the `uv_indices`.

    mesh2 {
       vertex_vectors {
          9,
          <0,0,0>, <0.5,0,0>, <0.5,0.5,0>,
          <1,0,0>, <1,0.5,0>, <1,1,0>,
          <0.5,1,0>, <0,1,0>,   <0,0.5,0>
       }
       uv_vectors {
          3
          <0,0>, <0.5,0>, <0.5,0.5>
       }
       face_indices {
          8,
          <0,1,2>, <1,3,2>,
          <3,4,2>, <4,5,2>,
          <5,6,2>, <6,7,2>,
          <7,8,2>, <8,0,2>
       }
       uv_indices {
          8,
          <0,1,2>, <0,1,2>,
          <0,1,2>, <0,1,2>,
          <0,1,2>, <0,1,2>,
          <0,1,2>, <0,1,2>
       }
       uv_mapping
       pigment {gradient x scale 0.2}
    }

#### 3.5.2.3 A separate texture per triangle

By using the `texture_list` it is possible to specify a texture per triangle or even per vertex in the mesh. In the latter case the three textures per triangle will be interpolated. To let POV-Ray know what texture to apply to a triangle, the index of a texture is added to the `face_indices` list, after the face index it belongs to.

    mesh2 {
       vertex_vectors {
          9,
          <0,0,0>, <0.5,0,0>, <0.5,0.5,0>,
          <1,0,0>, <1,0.5,0>, <1,1,0>
          <0.5,1,0>, <0,1,0>, <0,0.5,0>
       }
       texture_list {
          2,
          texture{pigment{rgb<0,0,1>}}
          texture{pigment{rgb<1,0,0>}}
       }
       face_indices {
          8,
          <0,1,2>,0,  <1,3,2>,1,
          <3,4,2>,0,  <4,5,2>,1,
          <5,6,2>,0,  <6,7,2>,1,
          <7,8,2>,0,  <8,0,2>,1
       }
    }

To specify a texture per vertex, three `texture_list` indices are added after the `face_indices`

    mesh2 {
       vertex_vectors {
          9,
          <0,0,0>, <0.5,0,0>, <0.5,0.5,0>,
          <1,0,0>, <1,0.5,0>, <1,1,0>
          <0.5,1,0>, <0,1,0>, <0,0.5,0>
       }
       texture_list {
          3,
          texture{pigment{rgb <0,0,1>}}
          texture{pigment{rgb 1}}
          texture{pigment{rgb <1,0,0>}}
       }
       face_indices {
          8,
          <0,1,2>,0,1,2,  <1,3,2>,1,0,2,
          <3,4,2>,0,1,2,  <4,5,2>,1,0,2,
          <5,6,2>,0,1,2,  <6,7,2>,1,0,2,
          <7,8,2>,0,1,2,  <8,0,2>,1,0,2
       }
    }

Assigning a texture based on the `texture_list` and texture interpolation is done on a per triangle base. So it is possible to mix triangles with just one texture and triangles with three textures in a mesh. It is even possible to mix in triangles without any texture indices, these will get their texture from a general `texture` statement in the `mesh2`. uv_mapping is supported for texturing using a `texture_list`.