class CartesianBasis(GeomNode): def __init__(self, length=1., tickness=3.): GeomNode.__init__(self, "Basis") self.vertexData = GeomVertexData("Basis", GeomVertexFormat.getV3c4(), Geom.UHStatic) self.vertex = GeomVertexWriter(self.vertexData, 'vertex') self.color = GeomVertexWriter(self.vertexData, 'color') self.mesh = Geom(self.vertexData) self.lines = GeomLines(Geom.UHStatic) self.vertex.addData3f(0.0, 0.0, 0.0) self.color.addData4f(1.0, 0.0, 0.0, 1.0) self.vertex.addData3f(length, 0.0, 0.0) self.color.addData4f(1.0, 0.0, 0.0, 1.0) self.lines.add_vertices(0, 1) self.vertex.addData3f(0.0, 0.0, 0.0) self.color.addData4f(0.0, 1.0, 0.0, 1.0) self.vertex.addData3f(0.0, length, 0.0) self.color.addData4f(0.0, 1.0, 0.0, 1.0) self.lines.add_vertices(2, 3) self.vertex.addData3f(0.0, 0.0, 0.0) self.color.addData4f(0.0, 0.0, 1.0, 1.0) self.vertex.addData3f(0.0, 0.0, length) self.color.addData4f(0.0, 0.0, 1.0, 1.0) self.lines.add_vertices(4, 5) self.lines.closePrimitive() self.mesh.addPrimitive(self.lines) self.addGeom(self.mesh) NodePath(self).setRenderModeThickness(tickness) NodePath(self).setLightOff() NodePath(self).setColorOff() NodePath(self).set_bin('fixed', 9)
def draw_shape(angles, width, radius): res = 10 if radius == 0: point = GeomNode('gnode') vdata = GeomVertexData('occ', GeomVertexFormat.getV3(), Geom.UHStatic) vdata.setNumRows(1) vertex = GeomVertexWriter(vdata, 'vertex') vertex.addData3f(0, 0, 0) geom = Geom(vdata) g = GeomPoints(Geom.UHStatic) g.addVertex(0) geom.addPrimitive(g) point.addGeom(geom) return point, point #first, sort angles in ascending order #if negative is given, translate to range of 2pi for i in range(len(angles)): if angles[i] < 0: angles[i] = 360 + angles[i] angles.sort() angles = [math.radians(a) for a in angles] angles.append(angles[0]) #read first angle to account for wrapping around #function = 'w/sin(theta)' vdata = GeomVertexData('occ', GeomVertexFormat.getV3(), Geom.UHStatic) vdata.setNumRows(1) vertex = GeomVertexWriter(vdata, 'vertex') numverts = [3] * (len(angles) - 1 ) #center, and two points on function lines for i in range(len(angles) - 1): #find center point #L = [None,None] #sign = [-1,1] #for p in range(2): # theta1 = angles[i+p] - sign[p]*math.pi/2 # theta2 = angles[i+p] + sign[p]*math.pi/4 # x1 = width*math.cos(theta1) # y1 = width*math.sin(theta1) # r = math.sqrt(2*width**2) # x2 = r*math.cos(theta2) # y2 = r*math.sin(theta2) # L[p] = line([x1,y1], [x2,y2]) #R = intersection(L[0],L[1]) #if R is False: # R = [x1,y1] #if parallel, shift both lines down by y_width #vertex.addData3f(R[0],R[1],1) difang = (angles[i + 1] - angles[i]) / 2 l = width / math.sin(difang) avgang = (angles[i] + angles[i + 1]) / 2 x0 = l * math.cos(avgang) y0 = l * math.sin(avgang) vertex.addData3f(x0, y0, 1) newang1 = angles[i] + (math.pi / 2 - math.acos(width / radius)) x = radius * math.cos(newang1) y = radius * math.sin(newang1) vertex.addData3f(x, y, 1) newang2 = angles[i + 1] - (math.pi / 2 - math.acos(width / radius)) for angle in arc(newang1, newang2, res): vertex.addData3f(radius * math.cos(math.radians(angle)), radius * math.sin(math.radians(angle)), 1) numverts[i] = numverts[i] + 1 x = radius * math.cos(newang2) y = radius * math.sin(newang2) vertex.addData3f(x, y, 1) #copy all data to the bottom, moving it down to z = -1 vertread = GeomVertexReader(vdata, 'vertex') while not vertread.isAtEnd(): v = vertread.getData3f() vertex.addData3f(v[0], v[1], -1) #draw geom = Geom(vdata) #draw top for i in range(len(angles) - 1): for j in range(numverts[i] - 2): ind = sum(numverts[0:i]) + j g = GeomTriangles(Geom.UHStatic) g.add_vertices(ind - j, ind + 1, ind + 2) geom.addPrimitive(g) #draw bottom for i in range(len(angles) - 1): for j in range(numverts[i] - 2): ind = sum(numverts) + sum(numverts[0:i]) + j g = GeomTriangles(Geom.UHStatic) g.add_vertices(ind - j, ind + 2, ind + 1) geom.addPrimitive(g) #draw edges for i in range(len(angles) - 1): for j in range(numverts[i] - 1): ind = sum(numverts[0:i]) + j g = GeomTriangles(Geom.UHStatic) g.add_vertices(ind, ind + sum(numverts), ind + 1) g.add_vertices(ind + sum(numverts), ind + 1 + sum(numverts), ind + 1) geom.addPrimitive(g) g = GeomTriangles(Geom.UHStatic) ind = sum(numverts[0:i]) indx = sum(numverts[0:i + 1]) g.add_vertices(indx - 1, indx + sum(numverts) - 1, ind) g.add_vertices(indx + sum(numverts) - 1, sum(numverts) + ind, ind) geom.addPrimitive(g) #outline object lines = Geom(vdata) l = GeomLines(Geom.UHStatic) for i in range(len(angles) - 1): for j in range(numverts[i] - 1): ind = sum(numverts[0:i]) + j l.add_vertices(ind, ind + 1) l.add_vertices(sum(numverts) + ind, sum(numverts) + ind + 1) ind = sum(numverts[0:i]) indx = sum(numverts[0:i + 1]) l.add_vertices(ind, indx - 1) l.add_vertices(sum(numverts) + ind, sum(numverts) + indx - 1) l.add_vertices(ind + 1, sum(numverts) + ind + 1) l.add_vertices(indx - 1, sum(numverts) + indx - 1) lines.addPrimitive(l) node = GeomNode('gnode') node.addGeom(geom) nodeL = GeomNode('gnode') nodeL.addGeom(lines) return node, nodeL
class Grid(GeomNode): def __init__(self, x_extend=None, y_extend=None, x_size=1, y_size=1, z=-0.01, tickness=1., name='Grid', x_color=None, y_color=None): GeomNode.__init__(self, name) if x_color is None: x_color = LVector4f(1.0, 1.0, 1.0, 1.0) if y_color is None: y_color = LVector4f(1.0, 1.0, 1.0, 1.0) if x_extend is None: x_extend = [0, 10] if y_extend is None: y_extend = [0, 10] self.vertexData = GeomVertexData("Chunk", GeomVertexFormat.getV3c4(), Geom.UHStatic) self.vertex = GeomVertexWriter(self.vertexData, 'vertex') self.color = GeomVertexWriter(self.vertexData, 'color') self.mesh = Geom(self.vertexData) self.lines = GeomLines(Geom.UHStatic) nb_lines_x = int((x_extend[1] - x_extend[0]) / x_size) nb_lines_y = int((y_extend[1] - y_extend[0]) / y_size) vertex_nb = 0 for ix in range(nb_lines_x): for iy in range(nb_lines_y): x = x_extend[0] + ix * x_size y = y_extend[0] + iy * y_size self.vertex.addData3f(x, y, z) self.color.addData4f(x_color) self.vertex.addData3f(x + x_size, y, z) self.color.addData4f(x_color) self.vertex.addData3f(x, y, z) self.color.addData4f(y_color) self.vertex.addData3f(x, y + y_size, z) self.color.addData4f(y_color) self.lines.add_vertices(vertex_nb, vertex_nb + 1, vertex_nb + 2, vertex_nb + 3) vertex_nb += 4 self.lines.closePrimitive() self.mesh.addPrimitive(self.lines) self.addGeom(self.mesh) NodePath(self).setRenderModeThickness(tickness) NodePath(self).setLightOff() NodePath(self).setColorOff() NodePath(self).set_bin('fixed', 8)