def make_square(x1, y1, z1, x2, y2, z2, tex_coord): format = GeomVertexFormat.getV3n3t2() vdata = GeomVertexData("square", format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, "vertex") normal = GeomVertexWriter(vdata, "normal") texcoord = GeomVertexWriter(vdata, "texcoord") # make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y1, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y2, z2) else: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y2, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y1, z2) normal.addData3f(my_normalize(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1))) normal.addData3f(my_normalize(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1))) normal.addData3f(my_normalize(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1))) normal.addData3f(my_normalize(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1))) # adding different colors to the vertex for visibility u1, v1, u2, v2 = tex_coord texcoord.addData2f(u1, v2) texcoord.addData2f(u1, v1) texcoord.addData2f(u2, v1) texcoord.addData2f(u2, v2) # quads arent directly supported by the Geom interface # you might be interested in the CardMaker class if you are # interested in rectangle though tri1 = GeomTriangles(Geom.UHStatic) tri2 = GeomTriangles(Geom.UHStatic) tri1.addVertex(0) tri1.addVertex(1) tri1.addVertex(3) tri2.addConsecutiveVertices(1, 3) tri1.closePrimitive() tri2.closePrimitive() square = Geom(vdata) square.addPrimitive(tri1) square.addPrimitive(tri2) return square
def make_square(x1, y1, z1, x2, y2, z2, tex_coord): format = GeomVertexFormat.getV3n3t2() vdata = GeomVertexData('square', format, Geom.UHStatic) vertex = GeomVertexWriter(vdata, 'vertex') normal = GeomVertexWriter(vdata, 'normal') texcoord = GeomVertexWriter(vdata, 'texcoord') #make sure we draw the sqaure in the right plane if x1 != x2: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y1, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y2, z2) else: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y2, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y1, z2) normal.addData3f(my_normalize(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z1 - 1))) normal.addData3f(my_normalize(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z1 - 1))) normal.addData3f(my_normalize(Vec3(2 * x2 - 1, 2 * y2 - 1, 2 * z2 - 1))) normal.addData3f(my_normalize(Vec3(2 * x1 - 1, 2 * y1 - 1, 2 * z2 - 1))) #adding different colors to the vertex for visibility u1, v1, u2, v2 = tex_coord texcoord.addData2f(u1, v2) texcoord.addData2f(u1, v1) texcoord.addData2f(u2, v1) texcoord.addData2f(u2, v2) #quads arent directly supported by the Geom interface #you might be interested in the CardMaker class if you are #interested in rectangle though tri1 = GeomTriangles(Geom.UHStatic) tri2 = GeomTriangles(Geom.UHStatic) tri1.addVertex(0) tri1.addVertex(1) tri1.addVertex(3) tri2.addConsecutiveVertices(1, 3) tri1.closePrimitive() tri2.closePrimitive() square = Geom(vdata) square.addPrimitive(tri1) square.addPrimitive(tri2) return square
def createPlane(width,height): format=GeomVertexFormat.getV3() vdata=GeomVertexData("vertices", format, Geom.UHStatic) vertexWriter=GeomVertexWriter(vdata, "vertex") vertexWriter.addData3f(0,0,0) vertexWriter.addData3f(width,0,0) vertexWriter.addData3f(width,height,0) vertexWriter.addData3f(0,height,0) #step 2) make primitives and assign vertices to them tris=GeomTriangles(Geom.UHStatic) #have to add vertices one by one since they are not in order tris.addVertex(0) tris.addVertex(1) tris.addVertex(3) #indicates that we have finished adding vertices for the first triangle. tris.closePrimitive() #since the coordinates are in order we can use this convenience function. tris.addConsecutiveVertices(1,3) #add vertex 1, 2 and 3 tris.closePrimitive() #step 3) make a Geom object to hold the primitives squareGeom=Geom(vdata) squareGeom.addPrimitive(tris) #now put squareGeom in a GeomNode. You can now position your geometry in the scene graph. squareGN=GeomNode("square") squareGN.addGeom(squareGeom) terrainNode = NodePath("terrNode") terrainNode.reparentTo(render) terrainNode.attachNewNode(squareGN) terrainNode.setX(-width/2) texGrass = loader.loadTexture("textures/envir-ground.jpg") terrainNode.setTexture(texGrass)
def __init__(self, point_cloud_size): format = GeomVertexFormat.getV3c4t2() vdata = GeomVertexData('point', format, Geom.UHDynamic) self._pos_writer = GeomVertexWriter(vdata, 'vertex') self._color_writer = GeomVertexWriter(vdata, 'color') self._tex_writer = GeomVertexWriter(vdata, 'texcoord') self._point_cloud_size = point_cloud_size self._prev_point_cloud_size = 0 assert point_cloud_size > 0 vdata.setNumRows(point_cloud_size * 6) self._tex_writer.set_row(0) set_texture(self._tex_writer, point_cloud_size) pnts = GeomTriangles(Geom.UHStatic) pnts.addConsecutiveVertices(0, 3 * 2 * point_cloud_size) pnts.closePrimitive() points_geom = Geom(vdata) points_geom.addPrimitive(pnts) snode = GeomNode('points') snode.addGeom(points_geom) dir_name = osp.dirname(__file__) # print(osp.join(dir_name, 'pnts_vs.glsl')) vs_shader = osp.join(dir_name, 'pnts_vs.glsl') fs_shader = osp.join(dir_name, 'pnts_fs.glsl') myShader = Shader.load( Shader.SL_GLSL, vertex=Filename.fromOsSpecific(vs_shader).getFullpath(), fragment=Filename.fromOsSpecific(fs_shader).getFullpath()) assert myShader is not None self.points_node = base.render.attachNewNode(snode) self.points_node.setPos(0., 0., 0.) self.points_node.set_shader(myShader) self.points_node.set_shader_input( "view_size", (base.win.getXSize(), base.win.getYSize())) self.points_node.node().setBounds( BoundingBox((-1000., -1000., -1000.), (1000., 1000., 1000.))) self.points_node.setTransparency(TransparencyAttrib.MAlpha)
def renderCharts(facegraph, verts, vert_indices, lineset=None): from meshtool.filters.panda_filters.pandacore import getVertexData, attachLights, ensureCameraAt from meshtool.filters.panda_filters.pandacontrols import KeyboardMovement, MouseDrag, MouseScaleZoom, ButtonUtils from panda3d.core import GeomTriangles, Geom, GeomNode, GeomVertexFormat, GeomVertexData, GeomVertexWriter, LineSegs from direct.showbase.ShowBase import ShowBase vformat = GeomVertexFormat.getV3c4() vdata=GeomVertexData('tris', vformat, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') color=GeomVertexWriter(vdata, 'color') colors = gen_color3(len(facegraph)) numtris = 0 for chart, data in facegraph.nodes_iter(data=True): curcolor = next(colors) for tri in data['tris']: triv = verts[vert_indices[tri]] vertex.addData3f(triv[0][0], triv[0][1], triv[0][2]) vertex.addData3f(triv[1][0], triv[1][1], triv[1][2]) vertex.addData3f(triv[2][0], triv[2][1], triv[2][2]) color.addData4f(curcolor[0],curcolor[1], curcolor[2], 1) color.addData4f(curcolor[0],curcolor[1], curcolor[2], 1) color.addData4f(curcolor[0],curcolor[1], curcolor[2], 1) numtris += 1 tris=GeomTriangles(Geom.UHDynamic) tris.addConsecutiveVertices(0, 3*numtris) tris.closePrimitive() linenodes = [] if lineset: for lines in lineset: ls = LineSegs() ls.setThickness(4) curcolor = next(colors) ls.setColor(curcolor[0]/256.0, curcolor[1]/256.0, curcolor[2]/256.0, 1) tuples = False for blah in lines: if isinstance(blah, tuple): tuples = True break if tuples: for i, j in lines: frompt = verts[i] topt = verts[j] ls.moveTo(frompt[0], frompt[1], frompt[2]) ls.drawTo(topt[0], topt[1], topt[2]) else: for i in range(len(lines)-1): frompt = verts[lines[i]] topt = verts[lines[i+1]] ls.moveTo(frompt[0], frompt[1], frompt[2]) ls.drawTo(topt[0], topt[1], topt[2]) linenodes.append(ls.create()) pgeom = Geom(vdata) pgeom.addPrimitive(tris) node = GeomNode("primitive") node.addGeom(pgeom) p3dApp = ShowBase() #attachLights(render) geomPath = render.attachNewNode(node) for linenode in linenodes: geomPath.attachNewNode(linenode) #geomPath.setRenderModeWireframe() ensureCameraAt(geomPath, base.cam) boundingSphere = geomPath.getBounds() base.cam.setPos(boundingSphere.getCenter() + boundingSphere.getRadius()) base.cam.lookAt(boundingSphere.getCenter()) KeyboardMovement() ButtonUtils(geomPath) MouseDrag(geomPath) MouseScaleZoom(geomPath) #render.setShaderAuto() p3dApp.run()
def renderCharts(facegraph, verts, vert_indices, lineset=None): from meshtool.filters.panda_filters.pandacore import getVertexData, attachLights, ensureCameraAt from meshtool.filters.panda_filters.pandacontrols import KeyboardMovement, MouseDrag, MouseScaleZoom, ButtonUtils from panda3d.core import GeomTriangles, Geom, GeomNode, GeomVertexFormat, GeomVertexData, GeomVertexWriter, LineSegs from direct.showbase.ShowBase import ShowBase vformat = GeomVertexFormat.getV3c4() vdata = GeomVertexData('tris', vformat, Geom.UHDynamic) vertex = GeomVertexWriter(vdata, 'vertex') color = GeomVertexWriter(vdata, 'color') colors = gen_color3(len(facegraph)) numtris = 0 for chart, data in facegraph.nodes_iter(data=True): curcolor = next(colors) for tri in data['tris']: triv = verts[vert_indices[tri]] vertex.addData3f(triv[0][0], triv[0][1], triv[0][2]) vertex.addData3f(triv[1][0], triv[1][1], triv[1][2]) vertex.addData3f(triv[2][0], triv[2][1], triv[2][2]) color.addData4f(curcolor[0], curcolor[1], curcolor[2], 1) color.addData4f(curcolor[0], curcolor[1], curcolor[2], 1) color.addData4f(curcolor[0], curcolor[1], curcolor[2], 1) numtris += 1 tris = GeomTriangles(Geom.UHDynamic) tris.addConsecutiveVertices(0, 3 * numtris) tris.closePrimitive() linenodes = [] if lineset: for lines in lineset: ls = LineSegs() ls.setThickness(4) curcolor = next(colors) ls.setColor(curcolor[0] / 256.0, curcolor[1] / 256.0, curcolor[2] / 256.0, 1) tuples = False for blah in lines: if isinstance(blah, tuple): tuples = True break if tuples: for i, j in lines: frompt = verts[i] topt = verts[j] ls.moveTo(frompt[0], frompt[1], frompt[2]) ls.drawTo(topt[0], topt[1], topt[2]) else: for i in range(len(lines) - 1): frompt = verts[lines[i]] topt = verts[lines[i + 1]] ls.moveTo(frompt[0], frompt[1], frompt[2]) ls.drawTo(topt[0], topt[1], topt[2]) linenodes.append(ls.create()) pgeom = Geom(vdata) pgeom.addPrimitive(tris) node = GeomNode("primitive") node.addGeom(pgeom) p3dApp = ShowBase() #attachLights(render) geomPath = render.attachNewNode(node) for linenode in linenodes: geomPath.attachNewNode(linenode) #geomPath.setRenderModeWireframe() ensureCameraAt(geomPath, base.cam) boundingSphere = geomPath.getBounds() base.cam.setPos(boundingSphere.getCenter() + boundingSphere.getRadius()) base.cam.lookAt(boundingSphere.getCenter()) KeyboardMovement() ButtonUtils(geomPath) MouseDrag(geomPath) MouseScaleZoom(geomPath) #render.setShaderAuto() p3dApp.run()
um.polygonize() format=GeomVertexFormat.getV3n3() vdata=GeomVertexData("vertices", format, Geom.UHStatic) vertexWriter=GeomVertexWriter(vdata, "vertex") normalWriter=GeomVertexWriter(vdata, "normal") print len(um.tri_list), "vertices" print len(um.tri_list) / 3.0, "tris" for p in um.tri_list: vertexWriter.addData3f(p[0],p[1],p[2]) for p in um.normal_list: normalWriter.addData3f(p[0],p[1],p[2]) for p in reversed(um.tri_list): vertexWriter.addData3f(p[0],p[1],p[2]) for p in reversed(um.normal_list): normalWriter.addData3f(-1 * p[0], -1 * p[1], -1 *p[2]) tris = GeomTriangles(Geom.UHStatic) tris.addConsecutiveVertices(0, len(um.tri_list)) tris.closePrimitive() um_geom = Geom(vdata) um_geom.addPrimitive(tris) um_GN = GeomNode(shp) um_GN.addGeom(um_geom) app = MyApp() app.run()
def makeSquare(x1,y1,z1, x2,y2,z2): format=GeomVertexFormat.getV3n3cpt2() vdata=GeomVertexData('square', format, Geom.UHDynamic) vertex=GeomVertexWriter(vdata, 'vertex') normal=GeomVertexWriter(vdata, 'normal') color=GeomVertexWriter(vdata, 'color') texcoord=GeomVertexWriter(vdata, 'texcoord') #make sure we draw the sqaure in the right plane if x1!=x2: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y1, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y2, z2) normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1))) normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y1-1, 2*z1-1))) normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1))) normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y2-1, 2*z2-1))) else: vertex.addData3f(x1, y1, z1) vertex.addData3f(x2, y2, z1) vertex.addData3f(x2, y2, z2) vertex.addData3f(x1, y1, z2) normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z1-1))) normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z1-1))) normal.addData3f(myNormalize(Vec3(2*x2-1, 2*y2-1, 2*z2-1))) normal.addData3f(myNormalize(Vec3(2*x1-1, 2*y1-1, 2*z2-1))) #adding different colors to the vertex for visibility color.addData4f(1.0,0.0,0.0,1.0) color.addData4f(0.0,1.0,0.0,1.0) color.addData4f(0.0,0.0,1.0,1.0) color.addData4f(1.0,0.0,1.0,1.0) texcoord.addData2f(0.0, 1.0) texcoord.addData2f(0.0, 0.0) texcoord.addData2f(1.0, 0.0) texcoord.addData2f(1.0, 1.0) #quads arent directly supported by the Geom interface #you might be interested in the CardMaker class if you are #interested in rectangle though tri1=GeomTriangles(Geom.UHDynamic) tri2=GeomTriangles(Geom.UHDynamic) tri1.addVertex(0) tri1.addVertex(1) tri1.addVertex(3) tri2.addConsecutiveVertices(1,3) tri1.closePrimitive() tri2.closePrimitive() square=Geom(vdata) square.addPrimitive(tri1) square.addPrimitive(tri2) return square