Draw a Box, Sphere, Tube, Teapot ...

Is there a class in Panda3D that is able to draw boxes, spheres, tubes, cones, teapots, … Sure I know you can create this on your own. But for programmers it may help if you don’t have to create some basic primitives in an modeling software first. IMO it is also useful to show a problem here in this forum because you don’t have to upload your models somewhere only to explain a simple thing.
If there is no such class I’ll write something like a PrimitiveMaker but only if

  • one or two think that would be useful (also add your wishes)
  • the class is added to Panda3D distribution (Whatever license anyone want). It is useless if not.

panda already comes with a set of basic geometry models. like the teapot,box,sphere etc. so i see little sense in writing an extra class for generating such things (aside from coding practice)
but feel free to do it if you want to. shouldn’t be that hard to do.

You can tesselate it as high as you want, scale it as you want (setScale is not always a good solution because nonuniform scaling of normals is problematic, you can’t just normalize). Beside I’ve a parametric primitive in mind.

Egg files support NURBS representation, and egg-qtess can triangulate a NURBS into arbitrary resolution. So all you’d need to have a multi-resolution teapot is a NURBS model of the teapot in an egg file. Same is true of any parametric primitive.

Normal scaling isn’t really an issue in practice. Panda generally auto-scales normals to compensate for any setScale() you have done.

If you’d like to provide a few standard parametric primitives in NURBS form, that would certainly be useful and easy to integrate.

If you’d prefer to write code to generate these primitives, I suggest writing it as an egg filter, for instance, we have a program called egg-make-tube that makes the “tube” shapes similar to those used by CollisionTube. I can imagine a suite of similar programs like egg-make-box and egg-make-cone and so on.


If I have to first call a program to generate my primitives I’m not sure if it is as handy. Here is a short example:

import sys
import math

import direct.directbase.DirectStart
from pandac.PandaModules import GeomVertexFormat, GeomVertexData, GeomVertexWriter, GeomTriangles, Geom, GeomNode, NodePath, GeomPoints

base.setBackgroundColor(0.0, 0.0, 0.0)

camera.setPos(0.0, 0.0, 20.0)
camera.setHpr(0.0, -90.0, 0.0)

class CubeMaker:
    def __init__(self):
        # self.smooth = True/False
        # self.uv = True/False or Spherical/Box/...
        # self.color = Method1/Method2/...
        # self.subdivide = 0/1/2/...
        self.size = 1.0

    def generate(self):
        format = GeomVertexFormat.getV3()
        data = GeomVertexData("Data", format, Geom.UHStatic)
        vertices = GeomVertexWriter(data, "vertex")

        size = self.size
        vertices.addData3f(-size, -size, -size)
        vertices.addData3f(+size, -size, -size)
        vertices.addData3f(-size, +size, -size)
        vertices.addData3f(+size, +size, -size)
        vertices.addData3f(-size, -size, +size)
        vertices.addData3f(+size, -size, +size)
        vertices.addData3f(-size, +size, +size)
        vertices.addData3f(+size, +size, +size)

        triangles = GeomTriangles(Geom.UHStatic)

        def addQuad(v0, v1, v2, v3):
            triangles.addVertices(v0, v1, v2)
            triangles.addVertices(v0, v2, v3)

        addQuad(4, 5, 7, 6) # Z+
        addQuad(0, 2, 3, 1) # Z-
        addQuad(3, 7, 5, 1) # X+
        addQuad(4, 6, 2, 0) # X-
        addQuad(2, 6, 7, 3) # Y+
        addQuad(0, 1, 5, 4) # Y+

        geom = Geom(data)

        node = GeomNode("CubeMaker")

        return NodePath(node)

cube = CubeMaker().generate()
cube.setColor(1.0, 0.0, 1.0)

base.accept("escape", sys.exit)
base.accept("a", render.analyze)
base.accept("o", base.oobe)


My more generic class is based on

local.wasp.uwa.edu.au/~pbourke/s … upershape/

and can create images like (source is a bit longer therefore I only post it on request):

same class other parameters:

better tessellation:

But based on your feedback I don’t think it is worth the trouble.

With my own shaders too (till yet I always used the transposed inverse modelview matrix to get correct lighting)?

1 Like

I guess it depends on your application. If you’re writing a program that needs to have dynamically-generated shapes chosen at runtime, sure, you really want to have something like this built into code. But if you just want a bunch of shapes to play with, you don’t really care whether they were generated at runtime or loaded from an egg file.

A good point–with your own shader, sure, Panda can’t help you out, and you’ll have to deal with the scale yourself, for instance with that transposed inverse matrix. Though if you call flattenLight(), Panda will apply the scale to the vertices and then properly transform the normals.

So, to the larger question of whether a tool to generate dynamic shapes at runtime will be generally useful, the answer is, I don’t know. Maybe. In general, I’m happy to accept code submissions, no matter how esoteric; but on the other hand, I don’t want to be stuck maintaining code needlessly–and someone would have to maintain once it becomes part of the codebase.


The overwhelming response pretty well answered the question :slight_smile:


If you take your idea to the next level of abstraction, you would have a very interesting tool. A primitive generator that accepts some basic parameters such as platonic solid type and number of vertices.

So a type of sphere would be defined as an object with vertices arrayed equidistant from a common center point. It would accept a minimum of 4 vertices (tetrahedron) and scale up to some arbitrary maximum.

A type of column would be defined as a planar circle extended along the center point axis at right angles to the base plane. It would accept a minimum of three planar points and scale up to some arbitrary maximum.

The type would define the topology of the base point array and the additional parameters would refine how they were presented.

Something for your consideration.

Didn’t realize how recent this post was when I PM’d u, Azraiyl - I assumed it was months old.

I require something exactly like this for my game, and would like to (heavily) modify it for this purpose, and adopt it into my game engine as part of a random terrain generator.

Could you please release the well-tesselated version of the source to me for this purpose?


Where is that information found. Wasn’t able to find it in the manual.

You can find some basic models in the models/ dir in the panda3d distribution.

I have need for exactly the same thing. I’d like to be able to generate simple geometric primitives in a flexible manner, e.g. specify a sphere either by center and radius, or 3 points on surface, etc. I would also like the resulting object instances to allow modification of the generated geometry (vertices, etc.). For example, increase the length of a cylinder without having to create a completely new one.

For the guts of it (e.g. how to ‘nicely’ generate vertices for a sphere) there must already be some open source code out there than can be borrowed from (Blender perhaps).

Happy to collaborate on this if you want.

I also support some sort of primitives library or auto generation. Right now i just use teapot as defacto default primitive maybe chaining that would be a good thing.