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. Some always have a fixed amount of
vertices, like GeomTriangles, GeomLines and GeomPoints. You should
simply add all of the vertices for these primitives. Some people call
close_primitive after adding every primitive, but this is not strictly
necessary.
Other GeomPrimitive types have a variable number of vertices, like
GeomTristrips, GeomTrifans and GeomLinestrips. Because you need to
tell Panda3D how many vertices are in every primitive, you should call
close_primitive() after adding every primitive.
For example:
prim = GeomTriangles(Geom.UHStatic)
prim.addVertex(0)
prim.addVertex(1)
prim.addVertex(2)
# thats the first triangle
# you can also add a few at once
prim.addVertices(2, 1, 3)
prim.addVertices(0, 5, 6)
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.