A mesh is one object that holds many triangles. It needs much less memory than a union of triangle objects, it is transformed in one step, and copies of it share the triangle data. There are two forms. mesh is a list of triangles, each with its own corners (and optionally normals, texture coordinates and textures). mesh2 is made of numbered lists: a list of vertices, and faces that refer to the vertices by number. This is the form that programs write. The mesh_camera is a camera, not an object, and is on Cameras. The single triangle and smooth_triangle are on Finite objects.
mesh
mesh {
triangle { <c1>, <c2>, <c3> [uv_vectors <uv1>, <uv2>, <uv3>] [texture { Name } | texture_list { Name1 Name2 Name3 }] }
smooth_triangle { <c1>, <n1>, <c2>, <n2>, <c3>, <n3> [uv_vectors ...] [texture ...] }
...
[inside_vector <direction>]
[hierarchy [on | off]]
[modifiers]
}
| Item | Default | Meaning |
|---|---|---|
triangle { ... } | at least one triangle is required | a flat triangle with three corners. Triangles of no area are skipped, without a message (the parser still reads their uv and texture items). A mesh in which no triangle is left is the error No triangles in triangle mesh. |
smooth_triangle { ... } | a triangle with a normal at each corner; the normal across the triangle is interpolated, so it looks curved. The normals are made unit vectors. A normal with no length is replaced by <1,0,0> with the warning Normal vector in mesh cannot be zero - changing it to <1,0,0>. (once per mesh). If the three normals are equal, the triangle is a flat triangle | |
uv_vectors <u1,v1>, <u2,v2>, <u3,v3> | three times <0,0> | the texture coordinates of the three corners, after the corners and before the texture. They are used when the mesh has uv_mapping; the coordinates are interpolated across the triangle |
texture { Name } | the texture of the mesh | the texture of this triangle. The braces must hold a texture identifier; an inline texture is not accepted, so the texture must be declared before the mesh. The parser stores each different texture once |
texture_list { Name1 Name2 Name3 } | three texture identifiers, one for each corner; the textures are blended across the triangle. It can replace texture on any triangle, and triangles with one texture and triangles with three can be mixed | |
inside_vector <d> | none: the mesh has no inside | gives the mesh an inside, so it can be used in the inside tests of CSG. A point is inside if a ray from it along this direction crosses an odd number of triangles. A vector of zero length (each component below a tiny tolerance) leaves the mesh without an inside. The direction is made a unit vector. The mesh must be a closed surface for this to give good results |
hierarchy | on | a tree of bounding boxes over the triangles. hierarchy off saves memory for a very small mesh. It takes an optional number: hierarchy off and hierarchy 0 are the same |
The items inside the braces may come in any order and any number of times; inside_vector and hierarchy go after the triangles in practice, before the other modifiers. Then follow the usual object modifiers.
A triangle that has no texture of its own uses the texture of the mesh (from texture, pigment and so on in the modifiers, or inherited from a CSG object). If every triangle has a texture, the mesh counts as textured and the default texture is not applied to it.
The corners that coincide are stored once: the parser keeps one list of vertices, one list of normals, one list of texture coordinates and one list of textures, and the triangles hold indices. Vertices and normals are stored as single precision numbers; indices are signed 32 bit integers; the parser stops with Too many triangles in triangle mesh. before the triangle count could overflow.
Loading a mesh
The parser reads a mesh only from the scene text. There is no keyword to read a mesh from a data file. A mesh stored in another file is a text file with POV-Ray syntax that is pulled in with #include. It usually declares the object, and the scene then places it:
// in scene.pov
#include "bunny.inc" // contains: #declare Bunny = mesh { triangle { ... } ... }
object { Bunny scale 2 pigment { rgb 1 } }
The same works for a mesh2 (the files that meshmaker.inc writes declare an object named Surface). Mesh files are usually written by converters. The #include directive is on Preprocessor.
mesh2
mesh2 {
vertex_vectors { n, <v0>, <v1>, ... }
[normal_vectors { n, <n0>, <n1>, ... }]
[uv_vectors { n, <uv0>, <uv1>, ... }]
[texture_list { n, texture { ... }, texture { ... }, ... }]
face_indices { n, <a, b, c> [, t [, t2, t3]], ... }
[normal_indices { n, <a, b, c>, ... }]
[uv_indices { n, <a, b, c>, ... }]
[inside_vector <direction>]
[modifiers]
}
The lists
| Block | Required | Content and checks |
|---|---|---|
vertex_vectors | yes | the count, then that many 3D points. A count of 0 or less is the error No vertices in triangle mesh. |
normal_vectors | no | the count, then that many normals. Each is made a unit vector; a normal with no length becomes <1,0,0> with the warning Normal vector in mesh2 cannot be zero - changing it to <1,0,0>. |
uv_vectors | no | the count, then that many 2D texture coordinates. Without it the mesh has one coordinate, <0,0>, and every corner uses it |
texture_list | only if a face names a texture | the count, then that many texture { ... } blocks. Here the textures may be written in full; they need not be identifiers |
The blocks vertex_vectors, normal_vectors and uv_vectors can come in any order. A block that is given twice replaces the first and gives the warning Duplicate vertex_vectors block; ignoring previous block. (and the same for the other two). The texture_list ends the lists: after it the parser expects face_indices, so write texture_list last among the lists. The manual says that it can also come before vertex_vectors; this parser does not accept that and stops with Vertex vectors not found in mesh2.
The faces
| Block | Required | Content and checks |
|---|---|---|
face_indices | yes | the number of faces (0 is the error No triangles in triangle mesh.), then for each face a vector <a, b, c> of three vertex numbers, counting from 0. A number below 0 or at least the number of vertices is the error Mesh face index out of range. After the vector, one, two or three more numbers may follow (with commas); they are texture numbers into the texture_list. One number gives the face one texture; all three give the three corners a texture each, which are blended. A texture number outside the list is the error Texture index out of range in mesh2. |
uv_indices | if there are uv vectors, unless the number of uv vectors equals the number of vertices (or is 1) | the number of faces, which must equal the number in face_indices (Number of uv indices must equal number of faces.), then a vector of three uv numbers per face. A number out of range is Mesh UV index out of range. Only one block is allowed (Only one uv_indices section is allowed in mesh2) |
normal_indices | if there are normals, unless the number of normals equals the number of vertices | a number of faces that may be less than the number of faces (more is an error: Number of normal indices cannot be more than the number of faces.), then a vector of three normal numbers per face for the first faces. A number out of range is Mesh normal index out of range. Only one block is allowed |
uv_indices and normal_indices can come in either order after face_indices; then comes inside_vector if there is one, and then the object modifiers.
The rules for leaving a list of indices out:
- No
uv_indices, and as many uv vectors as vertices: face corner i uses uv vector i. With exactly one uv vector (the default) all corners use it. Any other number is the errorMissing uv_indicies section in mesh2.(the spelling is the parser's). - No
normal_indices, and as many normals as vertices: face corner i uses normal i, for all faces. No normals at all: every face is flat. Any other number of normals is the errorMissing normal_indicies section in mesh2. - With
normal_indicesfor the first k faces, those faces are smooth triangles and the rest are flat. This lets smooth and flat triangles share one mesh: list the smooth ones first. A face whose three normals are equal is flat anyway.
A triangle may repeat a vertex or have no area; unlike mesh the parser does not drop such faces. The vertices and normals are stored as single precision numbers, as in mesh. hierarchy, inside_vector and the other modifiers act as in mesh. If every face has a texture number, the mesh counts as textured. Faces with no texture number use the texture of the mesh.
#declare Red = texture { pigment { rgb <1, 0, 0> } }
#declare Green = texture { pigment { rgb <0, 1, 0> } }
#declare Blue = texture { pigment { rgb <0, 0, 1> } }
mesh {
triangle {
<0, 0, 0>, <1, 0, 0>, <0, 1, 0>
texture { Red }
}
smooth_triangle {
<1, 0, 0>, <0, 0, -1>,
<1, 1, 0>, <0, 0, -1>,
<0, 1, 0>, <0.5, 0.5, -1>
uv_vectors <1, 0>, <1, 1>, <0, 1>
texture_list { Red Green Blue }
}
hierarchy on
}
// a cube: 8 vertices, 12 triangles, one texture per face pair
mesh2 {
vertex_vectors {
8,
<-1, -1, -1>, <1, -1, -1>, <1, 1, -1>, <-1, 1, -1>,
<-1, -1, 1>, <1, -1, 1>, <1, 1, 1>, <-1, 1, 1>
}
normal_vectors {
8,
<-1, -1, -1>, <1, -1, -1>, <1, 1, -1>, <-1, 1, -1>,
<-1, -1, 1>, <1, -1, 1>, <1, 1, 1>, <-1, 1, 1>
}
uv_vectors {
4, <0, 0>, <1, 0>, <1, 1>, <0, 1>
}
texture_list {
2,
texture { pigment { rgb <1, 0.4, 0> } }
texture { pigment { rgb <0, 0.4, 1> } }
}
face_indices {
12,
<0, 2, 1>, 0, <0, 3, 2>, 0,
<4, 5, 6>, 1, <4, 6, 7>, 1,
<0, 1, 5>, 0, <0, 5, 4>, 0,
<3, 6, 2>, 1, <3, 7, 6>, 1,
<0, 4, 7>, 0, <0, 7, 3>, 0,
<1, 2, 6>, 1, <1, 6, 5>, 1
}
normal_indices {
12,
<0, 2, 1>, <0, 3, 2>, <4, 5, 6>, <4, 6, 7>,
<0, 1, 5>, <0, 5, 4>, <3, 6, 2>, <3, 7, 6>,
<0, 4, 7>, <0, 7, 3>, <1, 2, 6>, <1, 6, 5>
}
uv_indices {
12,
<0, 2, 1>, <0, 3, 2>, <0, 1, 2>, <0, 2, 3>,
<0, 1, 2>, <0, 2, 3>, <0, 1, 2>, <0, 2, 3>,
<0, 1, 2>, <0, 2, 3>, <0, 1, 2>, <0, 2, 3>
}
inside_vector <0, 1, 0>
}
inside_vector it can be used as a solid in CSG.The obj reader
The parser source has a reader for Wavefront .obj files, in parser_obj.cpp, that would let you write:
mesh {
obj "model.obj"
[texture_list { "name" texture { ... } | "name" material { ... } ... [prefix "text"] [suffix "text"] }]
[inside_vector <d>]
}
It is switched off in the standard build: the switch POV_PARSER_EXPERIMENTAL_OBJ_IMPORT in configparser.h is 0, and then the word obj is not a keyword at all. With the program checked for this page, mesh { obj "x.obj" } gives No triangles in triangle mesh. The same switch also turns off the use of the mesh2 keywords inside a plain mesh block. So in practice POV-Ray 3.8 does not read any mesh file format; an obj file has to be converted to mesh2 text first.
When the reader is compiled in, it reads these lines of an obj file and skips or reports the rest:
| Obj line | What the reader does |
|---|---|
v x y z | a vertex |
vn x y z | a normal (a normal with no length becomes <1,0,0> with a warning) |
vt u v | a texture coordinate |
f a/b/c ... | a face with three or more vertices; the numbers are vertex, texture coordinate and normal. Faces with more than three vertices are cut into triangles, and the reader warns that they import properly only if they are convex and planar. The uv and normal parts must be used by all faces or by none. Numbers out of range, a face with too few vertices, and inconsistent use are errors with the line number |
usemtl name | selects a texture for the faces that follow. The reader first looks for the name in the texture_list of the mesh block. If it is not there, it looks for a declared texture or material whose identifier is the name with the optional prefix and suffix of that block put before and after it. If there is none, or the identifier is not a texture or a material, it stops with an error |
g, o, #, empty lines | skipped |
mtllib and anything else | skipped with the warning Unsupported command ... skipped in obj file: material library files are not read |
A word longer than 1023 characters, or extra data on a line, gives a possible-error message. A file with no vertices or no faces is an error.
meshmaker.inc
meshmaker.inc is an include file with macros that compute the vertices, normals and texture coordinates of a surface and then write a mesh2. Use it with #include "meshmaker.inc" and put the macro call inside an object { ... }, because a macro that builds a mesh gives back an object that you then transform and texture. The macros all build a grid of U by V quads (two triangles each) with (U+1)(V+1) vertices; the uv coordinates run over the square <0,0> to <1,1>, so use uv_mapping for textures that follow the surface.
| Macro | Builds |
|---|---|
BuildWriteMesh2(VecArr, NormArr, UVArr, U, V, FileName) | the core macro: from three arrays of the same length (vertices, normals, uv vectors) that lay out a grid, it emits the mesh2 with vertex_vectors, normal_vectors, uv_vectors and face_indices. U and V are the number of quads along the two directions. The index lists are left out because the three lists have as many entries as there are vertices (see the rules above). The other macros call it. |
TwoVarSurf(Fuv, Urange, Vrange, Iter_U, Iter_V, FileName) | a surface from a function of two variables Fuv(u, v) (the height), with the ranges as 2D vectors <min, max> |
Parametric(Fx, Fy, Fz, UVmin, UVmax, Iter_U, Iter_V, FileName) | a surface from three functions of u and v; UVmin and UVmax are 2D vectors. Use FromU(n), ToU(n), FromV(n), ToV(n) for a limit where the normal would be undefined (the poles of a sphere), as in <0, FromV(0)>. Without them the macro warns that it normalises a zero-length vector |
Prism(Spl, ResSpl, Height, HRes, FileName), Prism1(Spl, ResSpl, PSpl, PRes, FileName) | a spline curve extruded along the y axis; Prism1 scales it along the way by a second spline |
Lathe(Spl, ResSpl, Rot, ResRot, FileName) | a spline curve turned around the y axis through the angle Rot |
Coons(Spl1, Spl2, Spl3, Spl4, Iter_U, Iter_V, FileName) | a Coons surface bounded by four splines that are joined head to tail |
SweepSpline1(Track, Shape, Waist, U, V, FileName), SweepSpline2(...) | a cross-section spline swept along a track spline, with a third spline giving the radius |
MSM(SplineArray, SplRes, Interp_type, InterpRes, FileName) | a surface from an array of splines (BuildSpline(Arr, SplType) makes a spline of type C cubic, L linear, N natural or Q quadratic from an array of points) |
LInterpolate(Val, Min, Max), RangeMM(Val, Rmin, Rmax, Min, Max), CheckFileName(FileName) | helpers: a linear interpolation, a range mapping, and the file name check that the other macros use |
Every macro that builds a mesh ends with the argument FileName:
| FileName | Effect |
|---|---|
"" | the mesh is built and nothing is written |
a name that ends in .obj | the mesh is built and also written as a Wavefront file (this macro writes obj; POV-Ray itself cannot read it back, see above). An existing file is never overwritten |
a name that ends in .pcm | written as a compressed mesh file for the Compressed Mesh Macros. An existing file is not overwritten |
a name that ends in .arr | writes only the arrays (VertexVectors, NormalVectors, UVVectors, FaceIndices) as an include file |
| any other name | writes an include file that declares the mesh2 as Surface. If the file already exists, the macro includes it and uses its Surface instead of computing the mesh again, which saves the time of a slow build |
#include "meshmaker.inc"
#declare Wave = function(u, v) { 0.3 * sin(u * 3) * cos(v * 3) }
object {
TwoVarSurf(Wave, <-2, 2>, <-2, 2>, 20, 20, "")
uv_mapping
pigment { checker rgb <0.1, 0.1, 0.5>, rgb 1 scale 0.5 }
}
#declare Fx = function(u, v) { cos(u) * sin(v) }
#declare Fy = function(u, v) { cos(v) }
#declare Fz = function(u, v) { sin(u) * sin(v) }
object {
Parametric(Fx, Fy, Fz, <0, FromV(0)>, <2*pi, ToV(pi)>, 24, 12, "")
pigment { rgb <1, 0.5, 0> }
scale 0.8 translate x*3
}
mesh2 objects.Not described
Known, not yet covered
- Reference: the exact algorithms of
MSM,Coons,SweepSpline1andSweepSpline2(the splines they expect and how the normals are made) are in the comments and code ofmeshmaker.inc; only their arguments are listed here.