def makeCylinder(vdata,numVertices=40): topCircleGeom=makeCircle(vdata, numVertices,Vec3(0,0, 1)) bottomCircleGeom=makeCircle(vdata, numVertices,Vec3(0,0,0),-1) body=GeomTristrips(Geom.UHStatic) j=40 i=0 while i < numVertices+1: body.addVertex(i) body.addVertex(j) i+=1 if j==40: j=2*numVertices-1 else: j-=1 body.addVertex(i) body.addVertex(j) j-=1 i+=1 body.addVertex(numVertices-1) body.addVertex(0) body.addVertex(numVertices) body.closePrimitive() #print body cylinderGeom=Geom(vdata) cylinderGeom.addPrimitive(body) cylinderGeom.copyPrimitivesFrom(topCircleGeom) cylinderGeom.copyPrimitivesFrom(bottomCircleGeom) cylinderGeom.decomposeInPlace() cylinderGeom.unifyInPlace() return cylinderGeom
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 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 addGeometry(self, geomData): debugGui = dict() format = GeomVertexFormat.getV3n3t2() vdata = GeomVertexData('name', format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') prim = GeomTristrips(Geom.UHStatic) postphonedTriangles = list() vtxTargetId0 = vtxTargetId1 = vtxTargetId2 = None vtxDataCounter = 0 for vtxSourceId0, vtxSourceId1, vtxSourceId2 in geomData.triangles: vx0, vy0, vz0 = v0 = geomData.getVertex(vtxSourceId0) vx1, vy1, vz1 = v1 = geomData.getVertex(vtxSourceId1) vx2, vy2, vz2 = v2 = geomData.getVertex(vtxSourceId2) # prepare the vertices uvx0, uvy0 = uv0 = geomData.getUv(vtxSourceId0) uvx1, uvy1 = uv1 = geomData.getUv(vtxSourceId1) uvx2, uvy2 = uv2 = geomData.getUv(vtxSourceId2) # n0 = geomData.getNormal(vtxSourceId0) n1 = geomData.getNormal(vtxSourceId1) n2 = geomData.getNormal(vtxSourceId2) # make it wrap nicely if min(uvx0, uvx1, uvx2) < .25 and max(uvx0, uvx1, uvx2) > 0.75: if uvx0 < 0.25: uvx0 += 1.0 if uvx1 < 0.25: uvx1 += 1.0 if uvx2 < 0.25: uvx2 += 1.0 vertex.addData3f(*v0) normal.addData3f(*n0) texcoord.addData2f(*uv0) vtxTargetId0 = vtxDataCounter vtxDataCounter += 1 vertex.addData3f(*v1) normal.addData3f(*n1) texcoord.addData2f(*uv1) vtxTargetId1 = vtxDataCounter vtxDataCounter += 1 vertex.addData3f(*v2) normal.addData3f(*n2) texcoord.addData2f(*uv2) vtxTargetId2 = vtxDataCounter vtxDataCounter += 1 prim.addVertex(vtxTargetId0) prim.addVertex(vtxTargetId1) prim.addVertex(vtxTargetId2) prim.closePrimitive() if False: if vtxSourceId0 not in debugGui: i = InfoTextBillaboarded(render) i.setScale(0.05) i.billboardNodePath.setPos(Vec3(x0, y0, z0) * 1.1) i.setText('%i: %.1f %.1f %.1f\n%.1f %.1f' % ( vtxSourceId0, x0, y0, z0, nx0, ny0)) debugGui[vtxSourceId0] = i if vtxSourceId1 not in debugGui: i = InfoTextBillaboarded(render) i.setScale(0.05) i.billboardNodePath.setPos(Vec3(x1, y1, z1) * 1.1) i.setText('%i: %.1f %.1f %.1f\n%.1f %.1f' % ( vtxSourceId1, x1, y1, z1, nx1, ny1)) debugGui[vtxSourceId1] = i if vtxSourceId2 not in debugGui: i = InfoTextBillaboarded(render) i.setScale(0.05) i.billboardNodePath.setPos(Vec3(x2, y2, z2) * 1.1) i.setText('%i: %.1f %.1f %.1f\n%.1f %.1f' % ( vtxSourceId2, x2, y2, z2, nx2, ny2)) debugGui[vtxSourceId2] = i geom = Geom(vdata) geom.addPrimitive(prim) node = GeomNode('gnode') node.addGeom(geom) nodePath = self.attachNewNode(node) return nodePath