def create_hexagon(radius): """ Creates a hexagon shape that is centered at (0,0,0) with the corners having a distance of radius to the center and the normals pointing in direction (0,-1,0). Returns the tuple (PandaNode, GeomVertexData). """ format = GeomVertexFormat.getV3n3c4t2() vdata=GeomVertexData('hexagon', format, Geom.UHStatic) vertex=GeomVertexWriter(vdata, 'vertex') normal=GeomVertexWriter(vdata, 'normal') # create the vertices vertex.addData3f(0,0,0) normal.addData3f(0,-1,0) # add the other vertices for phi in range(0,360,60): # right-hand-rule (with middle finger pointing upwards): the y-axis points towards the screen, # therefore the hexagon will be created in the x,z plane, with x-axis pointing to the right # and the z-axis pointing up # get the next vertex coordinates by rotating the point (0,0,radius) in the x,z plane x,z = rotate_phi_degrees_counter_clockwise(phi, (0,radius)) #print (x,z) vertex.addData3f(x,0,z) normal.addData3f(0,-1,0) # the normal vector points away from the screen # add the vertices to a geometry primitives prim = GeomTrifans(Geom.UHStatic) for i in range(7): prim.addVertex(i) prim.addVertex(1) prim.closePrimitive() geom = Geom(vdata) geom.addPrimitive(prim) hex_node = GeomNode('') hex_node.addGeom(geom) return hex_node, vdata
def makeCircle(vdata, numVertices=40,offset=Vec3(0,0,0), direction=1): circleGeom=Geom(vdata) vertWriter=GeomVertexWriter(vdata, "vertex") normalWriter=GeomVertexWriter(vdata, "normal") colorWriter=GeomVertexWriter(vdata, "color") uvWriter=GeomVertexWriter(vdata, "texcoord") drawWriter=GeomVertexWriter(vdata, "drawFlag") #make sure we start at the end of the GeomVertexData so we dont overwrite anything #that might be there already startRow=vdata.getNumRows() vertWriter.setRow(startRow) colorWriter.setRow(startRow) uvWriter.setRow(startRow) normalWriter.setRow(startRow) drawWriter.setRow(startRow) angle=2*math.pi/numVertices currAngle=angle for i in range(numVertices): position=Vec3(math.cos(currAngle)+offset.getX(), math.sin(currAngle)+offset.getY(),offset.getZ()) vertWriter.addData3f(position) uvWriter.addData2f(position.getX()/2.0+0.5,position.getY()/2.0+0.5) colorWriter.addData4f(1.0, 1.0, 1.0, 1.0) position.setZ(position.getZ()*direction) position.normalize() normalWriter.addData3f(position) #at default Opengl only draws "front faces" (all shapes whose vertices are arranged CCW). We #need direction so we can specify which side we want to be the front face currAngle+=angle*direction circle=GeomTrifans(Geom.UHStatic) circle.addConsecutiveVertices(startRow, numVertices) circle.closePrimitive() circleGeom.addPrimitive(circle) return circleGeom