Now that you have a GeomVertexData with a set of vertices, you can
create one or more GeomPrimitive objects that use the vertices in your
GeomVertexData.
In general, you do this by first creating a GeomPrimitive of the
appropriate type, and then calling addVertex() for each vertex in your
primitive, followed by closePrimitive() after each primitive is
complete.
Different GeomPrimitive types have different requirements for the
number of vertices per primitive. For instance, for a GeomTriangles
object, whose primitive type (triangle) has exactly three vertices
each, you should call addVertex() three times, followed by
closePrimitive() after every three vertices. For a GeomTristrips
object, whose primitive type (triangle strip) may have three or more
vertices, you should call addVertex() once for each vertex in your
triangle strip, followed by closePrimitive() to mark the end of the
triangle strip. Then you can begin adding vertices for the second
triangle strip in the primitive, and so on.
For example:
prim = GeomTriangles(Geom.UHStatic)
prim.addVertex(0)
prim.addVertex(1)
prim.addVertex(2)
prim.closePrimitive()
prim.addVertex(2)
prim.addVertex(1)
prim.addVertex(3)
prim.closePrimitive()
prim.addVertex(0)
prim.addVertex(5)
prim.addVertex(6)
prim.closePrimitive()
|
Note that the GeomPrimitive constructor requires one parameter, which
is a usage hint, similar to the usage hint required for the
GeomVertexData constructor. Like that usage hint, this tells Panda
whether you will frequently adjust the vertex indices on this
primitive after it has been created. Since it is very unusual to
adjust the vertex indices on a primitive (usually, if you intend to
animate the vertices, you would operate on the vertices, not these
indices), this is almost always Geom.UHStatic, even if the primitive
is associated with a dynamic GeomVertexData. However, there may be
special rendering effects in which you actually do manipulate this
vertex index table in-place every few frames, in which case you should use
Geom.UHDynamic. As with the GeomVertexData, this is only a
performance hint; you're not required to adhere to the usage you
specify.
If you are unsure about this parameter, you should use Geom.UHStatic.
The above sample code defines a GeomTriangles object that looks like
this:
The actual positions of the vertices depends on the values of the vertices numbered 0, 1, 2, 3, and 5 in the associated GeomVertexData (you will associate your GeomPrimitives with a GeomVertexData in the next step, when you attach the GeomPrimitives to a Geom).
Finally, there are a few handy shortcuts for adding multiple vertices
at once:
prim.addVertices(v1, v2)
prim.addVertices(v1, v2, v3)
prim.addVertices(v1, v2, v3, v4)
|
Adds 2, 3, or 4 vertices in a single call.
|
prim.addConsecutiveVertices(start, numVertices)
|
Adds numVertices consecutive vertices, beginning at vertex start. For
instance, addConsecutiveVertices(5, 3) adds vertices 5, 6, 7.
|
prim.addNextVertices(numVertices)
|
Adds numVertices consecutive vertices, beginning with the next vertex
after the last vertex you added, or beginning at vertex 0 if these are the first vertices. For instance, prim.addVertex(10)
adds vertex 10. If you immediately call prim.addNextVertices(4), it
adds vertices 11, 12, 13, 14.
|
None of the above shortcut methods calls closePrimitive() for you; it is still your responsibility to call closePrimitive() each time you add the appropriate number of vertices.
|