# Source code for direct.showutil.BuildGeometry

```from panda3d.core import *
from math import *

GEO_ID = 0

[docs]def circleX(angle, radius, centerX, centerY):
x = radius * cos(angle) + centerX
return x

[docs]def circleY(angle, radius, centerX, centerY):
y = radius * sin(angle) + centerY
return y

[docs]def getCirclePoints(segCount, centerX, centerY, radius, wideX= 1.0, wideY = 1.0):
returnShape = []
for seg in range(0, segCount):
coordX = wideX * (circleX(((pi * 2.0) * float(float(seg) / float(segCount))), radius, centerX, centerY))
coordY = wideY * (circleY(((pi * 2.0) * float(float(seg) / float(segCount))), radius, centerX, centerY))
returnShape.append((coordX, coordY, 1))

coordX =  wideX * (circleX(((pi * 2.0) * float(0 / segCount)), radius, centerX, centerY))
coordY =  wideY * (circleY(((pi * 2.0) * float(0 / segCount)), radius, centerX, centerY))
returnShape.append((coordX, coordY, 1))
return returnShape

[docs]def addCircle(attachNode, vertexCount, radius, color = Vec4(1.0, 1.0, 1.0, 1.0), centerColor = None, layer = 0):
targetGN=GeomNode("target Circle")
if centerColor == None:
centerColor = color
zFloat = 0.025
targetCircleShape = getCirclePoints(5 + (vertexCount), 0.0, 0.0, radius)
gFormat = GeomVertexFormat.getV3cp()
targetCircleVertexData = GeomVertexData("holds my vertices", gFormat, Geom.UHDynamic)
targetCircleVertexWriter = GeomVertexWriter(targetCircleVertexData, "vertex")
targetCircleColorWriter = GeomVertexWriter(targetCircleVertexData, "color")
targetCircleVertexWriter.addData3f(0.0, 0.0, zFloat) #center
targetCircleColorWriter.addData4f(centerColor[0], centerColor[1], centerColor[2], centerColor[3])

for vertex in targetCircleShape:
targetCircleVertexWriter.addData3f(0.0 + vertex[0] , 0.0 + vertex[1] , zFloat)
targetCircleColorWriter.addData4f(color[0], color[1], color[2], color[3])
#targetCircleColorWriter.addData4f(1.0, 1.0, 1.0, 1.0)

targetTris=GeomTrifans(Geom.UHStatic) # triangle object

sizeTarget = len(targetCircleShape)
for countVertex in range(1, sizeTarget + 1):
targetTris.closePrimitive()

targetGeom=Geom(targetCircleVertexData)
return targetGeom

[docs]def addCircleGeom(rootNode,  vertexCount, radius, color = Vec4(1.0, 1.0, 1.0, 1.0), centerColor = None, layer = 0):
global GEO_ID
GN=GeomNode("Circle %s" % (GEO_ID))
GEO_ID += 1
NodePathGeom = rootNode.attachNewNode(GN)
geo = addCircle(GN, vertexCount, radius, color, centerColor,layer)
return NodePathGeom, GN, geo

[docs]def addSquare(attachNode, sizeX, sizeY, color = Vec4(1.0, 1.0, 1.0, 1.0), layer = 0):
targetGN=GeomNode("Square Geom")
sX = sizeX / 2.0
sY = sizeY / 2.0

color1 = color
color2 = color
color3 = color

gFormat = GeomVertexFormat.getV3n3cpt2()
boxVertexData = GeomVertexData("vertices", gFormat, Geom.UHDynamic)

boxVertexWriter = GeomVertexWriter(boxVertexData, "vertex")
boxNormalWriter = GeomVertexWriter(boxVertexData, "normal")
boxColorWriter = GeomVertexWriter(boxVertexData, "color")
boxTextureWriter = GeomVertexWriter(boxVertexData, "texcoord")

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxTris=GeomTristrips(Geom.UHStatic) # trianglestrip obejcet

boxTris.closePrimitive()

boxGeom=Geom(boxVertexData)

return boxGeom

[docs]def addSquareGeom(rootNode, sizeX, sizeY, color = Vec4(1.0, 1.0, 1.0, 1.0), layer = 0):
global GEO_ID
GN=GeomNode("Square %s" % (GEO_ID))
GEO_ID += 1
NodePathGeom = rootNode.attachNewNode(GN)
geo = addSquare(GN, sizeX, sizeY, color, layer)
return NodePathGeom, GN, geo

[docs]def addBox(attachNode, sizeX, sizeY, sizeZ, color = Vec4(1.0, 1.0, 1.0, 1.0), darken = 0):
targetGN=GeomNode("Box Geom")
sX = sizeX / 2.0
sY = sizeY / 2.0
sZ = sizeZ / 2.0

color1 = color
color2 = color
color3 = color
if darken:
color1 = color * 0.75 #Vec4(0.0, 0.0, 0.0, 1.0)
color2 = color * 0.50 #Vec4(0.0, 0.0, 0.0, 1.0)
color3 = color * 0.25 #Vec4(0.0, 0.0, 0.0, 1.0)

gFormat = GeomVertexFormat.getV3n3cp()
boxVertexData = GeomVertexData("vertices", gFormat, Geom.UHDynamic)

boxVertexWriter = GeomVertexWriter(boxVertexData, "vertex")
boxNormalWriter = GeomVertexWriter(boxVertexData, "normal")
boxColorWriter = GeomVertexWriter(boxVertexData, "color")

#Front

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color1[0], color1[1], color1[2], color1[3])

boxColorWriter.addData4f(color1[0], color1[1], color1[2], color1[3])

boxColorWriter.addData4f(color1[0], color1[1], color1[2], color1[3])

#Back

boxColorWriter.addData4f(color2[0], color2[1], color2[2], color2[3])

boxColorWriter.addData4f(color3[0], color3[1], color3[2], color3[3])

boxColorWriter.addData4f(color2[0], color2[1], color2[2], color2[3])

boxColorWriter.addData4f(color2[0], color2[1], color2[2], color2[3])

#Top

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

#Bottom

boxColorWriter.addData4f(color3[0], color3[1], color3[2], color3[3])

boxColorWriter.addData4f(color3[0], color3[1], color3[2], color3[3])

boxColorWriter.addData4f(color3[0], color3[1], color3[2], color3[3])

boxColorWriter.addData4f(color3[0], color3[1], color3[2], color3[3])

#Right

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color1[0], color1[1], color1[2], color1[3])

boxColorWriter.addData4f(color1[0], color1[1], color1[2], color1[3])

boxColorWriter.addData4f(color1[0], color1[1], color1[2], color1[3])

#Left

boxColorWriter.addData4f(color2[0], color2[1], color2[2], color2[3])

boxColorWriter.addData4f(color3[0], color3[1], color3[2], color3[3])

boxColorWriter.addData4f(color2[0], color2[1], color2[2], color2[3])

boxColorWriter.addData4f(color2[0], color2[1], color2[2], color2[3])

boxTris=GeomTristrips(Geom.UHStatic) # trianglestrip obejcet
boxTris.closePrimitive()

boxTris.closePrimitive()

boxTris.closePrimitive()

boxTris.closePrimitive()

boxTris.closePrimitive()

boxTris.closePrimitive()

boxGeom=Geom(boxVertexData)

return boxGeom

[docs]def addBoxGeom(rootNode, sizeX, sizeY, sizeZ, color = Vec4(1.0, 1.0, 1.0, 1.0), darken = 0):
global GEO_ID
GN=GeomNode("Box %s" % (GEO_ID))
GEO_ID += 1
nodePathGeom = rootNode.attachNewNode(GN)
geo = addBox(GN, sizeX, sizeY, sizeZ, color, darken)
return nodePathGeom, GN, geo

[docs]def addArrow(attachNode, sizeX, sizeY, color = Vec4(1.0, 1.0, 1.0, 1.0), layer = 0):
targetGN=GeomNode("Arrow Geom")
sX = sizeX / 2.0
sY = sizeY / 2.0

color1 = color
color2 = color
color3 = color

gFormat = GeomVertexFormat.getV3n3cp()
boxVertexData = GeomVertexData("vertices", gFormat, Geom.UHDynamic)

boxVertexWriter = GeomVertexWriter(boxVertexData, "vertex")
boxNormalWriter = GeomVertexWriter(boxVertexData, "normal")
boxColorWriter = GeomVertexWriter(boxVertexData, "color")

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxTris=GeomTristrips(Geom.UHStatic) # trianglestrip obejcet

boxTris.closePrimitive()

boxVertexWriter.addData3f(-sX * 2.0, sY, 0.0)
boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxVertexWriter.addData3f(sX * 2.0, sY, 0.0)
boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxVertexWriter.addData3f(0.0, sY * 2.0, 0.0)
boxColorWriter.addData4f(color[0], color[1], color[2], color[3])

boxTris.closePrimitive()

boxGeom=Geom(boxVertexData)