Pedro
June 8, 2007, 10:07am
1
Hi everyone !
I’d like to know if there is a way of creating objects on the fly. I have method which computes the object vertex position and an other one which computes the faces of the object. I could create an egg file (although all my attempts to create a model with a egg, even a really simple model have failed - I think I did not get how it works) but I have a lot of objects, and writing into a file then loading them seems a bit slow.
What I would like to do is to create the object directly from my program. Is this possible ?
enn0x
June 8, 2007, 10:48am
2
Yes, it is possible. There is a full section of the manual dedicated to this topic: “Procedurally Generating 3D Models”.
Or you can use the egg library and then directly load a NodePath from the egg data without writing the data to a file by using loadEggData( eggData ).
enn0x
Pedro
June 8, 2007, 11:47am
3
Thanks, I’m going to take a look at it.
Pedro
June 8, 2007, 1:30pm
4
I tried this after reading the manual, but nothing shows up on screen. Any idea ?
import direct.directbase.DirectStart
from direct.showbase import DirectObject
from pandac.PandaModules import *
class Test(DirectObject.DirectObject):
def __init__(self):
self.create()
run()
def create(self):
array = GeomVertexArrayFormat()
array.addColumn(InternalName.make('vertex'), 3, Geom.NTFloat32, Geom.CPoint)
format = GeomVertexFormat()
format.addArray(array)
format = GeomVertexFormat.registerFormat(format)
vdata = GeomVertexData('name', format, Geom.UHStatic)
vertex = GeomVertexWriter(vdata, 'vertex')
vertex.addData3f(1, 0, 0)
vertex.addData3f(1, 1, 0)
vertex.addData3f(0, 1, 0)
vertex.addData3f(0, 0, 0)
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()
geom = Geom(vdata)
geom.addPrimitive(prim)
node = GeomNode('gnode')
node.addGeom(geom)
nodePath = render.attachNewNode(node)
nodePath.setColor(1,0,0,1)
nodePath.setPos(0,-5,0)
t = Test()
rdb
June 8, 2007, 4:51pm
5
It works fine for me. You just have to reposition the camera a bit to see it. Drag with the right mouse a bit upwards to zoom out, then drag the middle mouse button around to rotate. You will see two perfect red triangles.
enn0x
June 8, 2007, 5:59pm
6
Yes, it works for me too.
This is your first triangle, visible only from “above”:
2----1
\ |
\ |
\ |
\|
3 0
And this your second, visible only from “below”:
2----1
| /
| /
| /
|/
3 0
But I assume you wanted to do something like this:
2----1
|\ |
| \ |
| \ |
| \|
3----0
You have to create the triangle indices in counter-clockwise order, like this:
prim.addVertex(0)
prim.addVertex(1)
prim.addVertex(2)
prim.closePrimitive()
prim.addVertex(0)
prim.addVertex(2)
prim.addVertex(3)
prim.closePrimitive()
enn0x
yep, addition :
your node is behind your camera
if it’s still hard to see, use nodePath.showBounds()
Pedro
June 11, 2007, 7:24am
8
Sorry guys. I didn’t even thought that the camera position might be the problem. I must have been tired last friday…
Thanks anyway !