def create_stanchions(self): from pandac.PandaModules import GeomVertexReader, CollisionNode, CollisionTube self.stanchions = self.stage.findAllMatches('**/=Stanchion') for stanchion in self.stanchions: geomnode = stanchion.node() radius = float(stanchion.getTag('Stanchion')) geom = geomnode.getGeom(0) vdata = geom.getVertexData() for gp in range(geom.getNumPrimitives()): vreader = GeomVertexReader(vdata, 'vertex') prim = geom.getPrimitive(gp) prim = prim.decompose() for p in range(prim.getNumPrimitives()): start = prim.getPrimitiveStart(p) end = prim.getPrimitiveEnd(p) vertices = [] for v in range(start, end): vi = prim.getVertex(v) vreader.setRow(vi) vertex = vreader.getData3f() vertices.append(vertex) vertices.append(vertices[0]) for i in range(1, len(vertices)): a, b = vertices[i-1], vertices[i] stanchion_np = stanchion.attachNewNode(CollisionNode('stanchion')) print 'creating cyl with radius %f from %s to %s' % (radius, a, b) stanchion_np.node().addSolid(CollisionTube(a[0], a[1], a[2], b[0], b[1], b[2], radius)) stanchion_np.node().setFromCollideMask(OBSTACLE_MASK) geomnode.removeAllGeoms()
def create_stanchions(self): from pandac.PandaModules import GeomVertexReader, CollisionNode, CollisionTube self.stanchions = self.stage.findAllMatches('**/=Stanchion') for stanchion in self.stanchions: geomnode = stanchion.node() radius = float(stanchion.getTag('Stanchion')) geom = geomnode.getGeom(0) vdata = geom.getVertexData() for gp in range(geom.getNumPrimitives()): vreader = GeomVertexReader(vdata, 'vertex') prim = geom.getPrimitive(gp) prim = prim.decompose() for p in range(prim.getNumPrimitives()): start = prim.getPrimitiveStart(p) end = prim.getPrimitiveEnd(p) vertices = [] for v in range(start, end): vi = prim.getVertex(v) vreader.setRow(vi) vertex = vreader.getData3f() vertices.append(vertex) vertices.append(vertices[0]) for i in range(1, len(vertices)): a, b = vertices[i - 1], vertices[i] stanchion_np = stanchion.attachNewNode( CollisionNode('stanchion')) print 'creating cyl with radius %f from %s to %s' % ( radius, a, b) stanchion_np.node().addSolid( CollisionTube(a[0], a[1], a[2], b[0], b[1], b[2], radius)) stanchion_np.node().setFromCollideMask(OBSTACLE_MASK) geomnode.removeAllGeoms()
def processVertexData(vdata): vertex = GeomVertexReader(vdata, 'vertex') texcoord = GeomVertexReader(vdata, 'texcoord') while not vertex.isAtEnd(): v = vertex.getData3f() t = texcoord.getData2f() print "v = %s, t = %s" % (repr(v), repr(t))
def _set_z_on_shaft(self, z): for vdata in self.shaft_vdata: reader = GeomVertexReader(vdata, 'vertex') writer = GeomVertexWriter(vdata, 'vertex') while not reader.isAtEnd(): v = reader.getData3f() if v[2] > 0: writer.setData3f(v[0],v[1],z) else: writer.setData3f(v) # I have to call the writer setData method in any case
def set_bar_height(self, height): """ Sets the height of the bar to the given value. The given height should be between 0 and 1. """ reader = GeomVertexReader(self.bar_vdata, 'vertex') writer = GeomVertexWriter(self.bar_vdata, 'vertex') while not reader.isAtEnd(): v = reader.getData3f() if v[2] > 0: writer.setData3f(v[0],v[1],height) else: writer.setData3f(v) # I have to call the writer setData method in any case
def set_bar_height(self, height): """ Sets the height of the bar to the given value. The given height should be between 0 and 1. """ reader = GeomVertexReader(self.bar_vdata, 'vertex') writer = GeomVertexWriter(self.bar_vdata, 'vertex') while not reader.isAtEnd(): v = reader.getData3f() if v[2] > 0: writer.setData3f(v[0], v[1], height) else: writer.setData3f( v) # I have to call the writer setData method in any case
def _set_z_on_shaft(self, z): for vdata in self.shaft_vdata: reader = GeomVertexReader(vdata, 'vertex') writer = GeomVertexWriter(vdata, 'vertex') while not reader.isAtEnd(): v = reader.getData3f() if v[2] > 0: writer.setData3f(v[0], v[1], z) else: writer.setData3f( v ) # I have to call the writer setData method in any case
def processPrimitive(prim, vdata): vertex = GeomVertexReader(vdata, 'vertex') prim = prim.decompose() for p in range(prim.getNumPrimitives()): s = prim.getPrimitiveStart(p) e = prim.getPrimitiveEnd(p) for i in range(s, e): vi = prim.getVertex(i) vertex.setRow(vi) v = vertex.getData3f() print "prim %s has vertex %s: %s" % (p, vi, repr(v)) print
def scale_vertices(self, multiplier): """ Scale the working vertices as the given multiplier to the original vertices. @type multiplier: number @param multiplier: Coefficient to apply to original vertices. """ for i,geom in self.__get_geoms(): orig_vdata = self.orig_vertices[i] working_vdata = self.working_vertices[i] reader = GeomVertexReader(orig_vdata, 'vertex') writer = GeomVertexWriter(working_vdata, 'vertex') while not reader.isAtEnd(): data = reader.getData3f() writer.setData3f(data[0] * multiplier, data[1] * multiplier, data[2] * multiplier)
def draw_body(self, position, vector_list, radius = 1, keep_drawing = True, num_vertices = 8): circle_geom = Geom(self.vdata) vertex_writer = GeomVertexWriter(self.vdata, "vertex") color_writer = GeomVertexWriter(self.vdata, "color") normal_writer = GeomVertexWriter(self.vdata, "normal") draw_rewriter = GeomVertexRewriter(self.vdata, "drawFlag") tex_rewriter = GeomVertexRewriter(self.vdata, "texcoord") start_row = self.vdata.getNumRows() vertex_writer.setRow(start_row) color_writer.setRow(start_row) normal_writer.setRow(start_row) sCoord = 0 if start_row != 0: tex_rewriter.setRow(start_row - num_vertices) sCoord = tex_rewriter.getData2f().getX() + 1 draw_rewriter.setRow(start_row - num_vertices) if draw_rewriter.getData1f() == False: sCoord -= 1 draw_rewriter.setRow(start_row) tex_rewriter.setRow(start_row) angle_slice = 2 * math.pi / num_vertices current_angle = 0 perp1 = vector_list[1] perp2 = vector_list[2] # write vertex information for i in range(num_vertices): adjacent_circle = position + (perp1 * math.cos(current_angle) + perp2 * math.sin(current_angle)) * radius normal = perp1 * math.cos(current_angle) + perp2 * math.sin(current_angle) normal_writer.addData3f(normal) vertex_writer.addData3f(adjacent_circle) tex_rewriter.addData2f(sCoord, (i + 0.001) / (num_vertices - 1)) color_writer.addData4f(0.5, 0.5, 0.5, 1.0) draw_rewriter.addData1f(keep_drawing) current_angle += angle_slice draw_reader = GeomVertexReader(self.vdata, "drawFlag") draw_reader.setRow(start_row - num_vertices) # we can't draw quads directly so use Tristrips if start_row != 0 and draw_reader.getData1f() != False: lines = GeomTristrips(Geom.UHStatic) half = int(num_vertices * 0.5) for i in range(num_vertices): lines.addVertex(i + start_row) if i < half: lines.addVertex(i + start_row - half) else: lines.addVertex(i + start_row - half - num_vertices) lines.addVertex(start_row) lines.addVertex(start_row - half) lines.closePrimitive() lines.decompose() circle_geom.addPrimitive(lines) circle_geom_node = GeomNode("Debug") circle_geom_node.addGeom(circle_geom) circle_geom_node.setAttrib(CullFaceAttrib.makeReverse(), 1) self.get_model().attachNewNode(circle_geom_node)
def _set_threshold(self, vdata, t_value): reader = GeomVertexReader(vdata, 'vertex') writer = GeomVertexWriter(vdata, 'vertex') while not reader.isAtEnd(): v = reader.getData3f() writer.setData3f(v[0], v[1], t_value + self.padding)
def from_triangles(cls, triangle_geom_node): _log = logging.getLogger("HalfEdgeMesh") tri_geom = triangle_geom_node.getGeom(0) # assume that the triangle list is in the first geom in the node tri_primitive = tri_geom.getPrimitive(0) # assume that the triangle list is the first primitive in the geom vdata = tri_geom.getVertexData() vertex_reader = GeomVertexReader(vdata, "vertex") normal_reader = GeomVertexReader(vdata, "normal") # halfedges = zeros((tri_primitive.getNumPrimitives()*3,), dtype=cls.halfedge_dtype) # faces = zeros((tri_primitive.getNumPrimitives() ,), dtype=cls.face_dtype) # temp_vertices = zeros((vdata.getNumRows(),), dtype=cls.vertex_dtype) halfedges = [] faces = [] vertices = [] identity_threshold = 0.00001 _log.debug( u"Creating mesh from %d vertices and %d faces..." % (vdata.getNumRows(), tri_primitive.getNumPrimitives()) ) vertex_count = 0 vertex_index = 0 edge_index = 0 face_index = 0 for triangle_index in range(tri_primitive.getNumPrimitives()): # add vertices vertex_start = tri_primitive.getPrimitiveStart(triangle_index) vertex_end = tri_primitive.getPrimitiveEnd(triangle_index) edge_base = edge_index for prim_vertex_index in range(vertex_start, vertex_end): vertex_reader.setRow(prim_vertex_index) coordinates = array(vertex_reader.getData3f()) # find duplicate within a threshold # same_vertices = [ vertex_index for vertex_index, vertex_data in enumerate(temp_vertices[:vertex_count]) if not any(fabs(vertex_data['coord'] - vertex) > identity_threshold) ] vertex = None for vertex_data in vertices: if not any(fabs(vertex_data.coordinates - coordinates) > identity_threshold): vertex = vertex_data break if vertex is None: # new vertex normal_reader.setRow(prim_vertex_index) normal = array(normal_reader.getData3f()) vertex = Vertex(coordinates, normal, edge_index) vertices.append(vertex) vertex_count += 1 # add edge halfedges.append(Halfedge(vertex, edge_base + (edge_index - edge_base + 1) % 3, None, None)) edge_index += 1 # add face face = Face(halfedges[edge_base]) faces.append(face) face_index += 1 # determine matching face for edge_data in halfedges[edge_base:]: edge_data.face = face _log.debug(u"Found %d unique vertices with %d halfedges and %d faces." % (vertex_count, edge_index, face_index)) for vertex_data in vertices: vertex_data.edge = halfedges[vertex_data.edge] # resolve 'next' edges for edge_data in halfedges: edge_data.next = halfedges[edge_data.next] # determine 'opposite' edges for edge_data in halfedges: if edge_data.opposite == None: v1 = edge_data.origin v2 = edge_data.next.origin for opposite_edge_data in halfedges: if opposite_edge_data.origin == v2 and opposite_edge_data.next.origin == v1: edge_data.opposite = opposite_edge_data opposite_edge_data.opposite = edge_data break _log.debug(u"Done creating mesh.") return cls(vertices, halfedges, faces)
def drawBody(nodePath, vdata, pos, vecList, radius=1, keepDrawing=True,numVertices=8): circleGeom=Geom(vdata) vertWriter=GeomVertexWriter(vdata, "vertex") colorWriter=GeomVertexWriter(vdata, "color") normalWriter=GeomVertexWriter(vdata, "normal") drawReWriter=GeomVertexRewriter(vdata, "drawFlag") texReWriter=GeomVertexRewriter(vdata, "texcoord") startRow=vdata.getNumRows() vertWriter.setRow(startRow) colorWriter.setRow(startRow) normalWriter.setRow(startRow) sCoord=0 if (startRow!=0): texReWriter.setRow(startRow-numVertices) sCoord=texReWriter.getData2f().getX()+1 drawReWriter.setRow(startRow-numVertices) if(drawReWriter.getData1f()==False): sCoord-=1 drawReWriter.setRow(startRow) texReWriter.setRow(startRow) angleSlice=2*math.pi/numVertices currAngle=0 #axisAdj=Mat4.rotateMat(45, axis)*Mat4.scaleMat(radius)*Mat4.translateMat(pos) perp1=vecList[1] perp2=vecList[2] #vertex information is written here for i in range(numVertices): adjCircle=pos+(perp1*math.cos(currAngle)+perp2*math.sin(currAngle))*radius normal=perp1*math.cos(currAngle)+perp2*math.sin(currAngle) normalWriter.addData3f(normal) vertWriter.addData3f(adjCircle) texReWriter.addData2f(sCoord,(i+0.001)/(numVertices-1)) colorWriter.addData4f(0.5,0.5,0.5,1) drawReWriter.addData1f(keepDrawing) currAngle+=angleSlice drawReader=GeomVertexReader(vdata, "drawFlag") drawReader.setRow(startRow-numVertices) #we cant draw quads directly so we use Tristrips if (startRow!=0) & (drawReader.getData1f()!=False): lines=GeomTristrips(Geom.UHStatic) half=int(numVertices*0.5) for i in range(numVertices): lines.addVertex(i+startRow) if i< half: lines.addVertex(i+startRow-half) else: lines.addVertex(i+startRow-half-numVertices) lines.addVertex(startRow) lines.addVertex(startRow-half) lines.closePrimitive() lines.decompose() circleGeom.addPrimitive(lines) circleGeomNode=GeomNode("Debug") circleGeomNode.addGeom(circleGeom) #I accidentally made the front-face face inwards. Make reverse makes the tree render properly and #should cause any surprises to any poor programmer that tries to use this code circleGeomNode.setAttrib(CullFaceAttrib.makeReverse(),1) global numPrimitives numPrimitives+=numVertices*2 nodePath.attachNewNode(circleGeomNode)
def _set_threshold(self, vdata, t_value): reader = GeomVertexReader(vdata, 'vertex') writer = GeomVertexWriter(vdata, 'vertex') while not reader.isAtEnd(): v = reader.getData3f() writer.setData3f(v[0],v[1],t_value+self.padding)