-
Notifications
You must be signed in to change notification settings - Fork 1
/
p3dprims.py
80 lines (68 loc) · 2.1 KB
/
p3dprims.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# A bunch of Panda3D primitives
# HanishKVC, 2021
# GPL
from panda3d.core import GeomVertexFormat
from panda3d.core import GeomVertexData, Geom, GeomVertexWriter
from panda3d.core import GeomTriangles, GeomNode
class Polyhedron():
def __init__(self, name, numVertices=None):
self.name = name
self.gvf = GeomVertexFormat.get_v3t2()
self.vdata = GeomVertexData(name, self.gvf, Geom.UHStatic)
if numVertices != None:
self.vdata.setNumRows(numVertices)
self.gvwVertex = GeomVertexWriter(self.vdata, 'vertex')
self.gvwTexCo = GeomVertexWriter(self.vdata, 'texcoord')
self.prim = GeomTriangles(Geom.UHStatic)
def add_vertex(self, xyz, uv=None):
if uv == None:
uv = (xyz[0], xyz[1])
self.gvwVertex.addData3(xyz)
self.gvwTexCo.addData2(uv)
def add_vertices(self, va):
for v in va:
self.add_vertex(v)
def add_triangle(self, vi0, vi1, vi2):
self.prim.addVertices(vi0, vi1, vi2)
self.prim.closePrimitive()
def add_triangles(self, ta):
for t in ta:
self.add_triangle(t[0], t[1], t[2])
def generate(self):
self.geom = Geom(self.vdata)
self.geom.addPrimitive(self.prim)
self.geomNode = GeomNode(self.name)
self.geomNode.addGeom(self.geom)
return self.geomNode
def create_cube(name, bMagic=False):
m1 = Polyhedron(name)
# Vertices array
va = [ (0,0,0),
(0,1,0),
(1,1,0),
(1,0,0),
(0,0,1),
(0,1,1),
(1,1,1),
(1,0,1) ]
m1.add_vertices(va)
# Triangle Faces array
mta =[ [0,2,1], [0,3,2],
[4,6,5], [4,7,6],
[0,4,5], [0,5,1],
[3,7,6], [3,6,2],
[0,3,7], [0,7,4],
[1,2,6], [1,6,5] ]
nta =[ [0,1,2], [0,2,3],
[4,6,5], [4,7,6],
[0,4,5], [0,5,1],
[3,6,7], [3,2,6],
[0,3,7], [0,7,4],
[1,6,2], [1,5,6] ]
if bMagic:
ta = mta
else:
ta = nta
m1.add_triangles(ta)
m1gn = m1.generate()
return m1gn