Ejemplo n.º 1
0
    def __init__(self, width=1, depth=1, height=1, origin=Point3(0, 0, 0)):

        # Create vetex data format
        gvf = GeomVertexFormat.getV3n3()
        gvd = GeomVertexData("vertexData", gvf, Geom.UHStatic)

        # Create vetex writers for each type of data we are going to store
        gvwV = GeomVertexWriter(gvd, "vertex")
        gvwN = GeomVertexWriter(gvd, "normal")

        # Write out all points
        for p in GetPointsForBox(width, depth, height):
            gvwV.addData3f(Point3(p) - origin)

        # Write out all the normals
        for n in ((-1, 0, 0), (1, 0, 0), (0, -1, 0), (0, 1, 0), (0, 0, -1), (0, 0, 1)):
            for i in range(4):
                gvwN.addData3f(n)

        geom = Geom(gvd)
        for i in range(0, gvwV.getWriteRow(), 4):

            # Create and add both triangles
            geom.addPrimitive(GetGeomTriangle(i, i + 1, i + 2))
            geom.addPrimitive(GetGeomTriangle(i, i + 2, i + 3))

        # Init the node path, wrapping the box
        geomNode = GeomNode("box")
        geomNode.addGeom(geom)
        NodePath.__init__(self, geomNode)
Ejemplo n.º 2
0
    def __init__(self, subdivides=3, scale=1.0):
        super(SphereNode, self).__init__('sphere')
        uniform = True
        # see if scale is a tuple
        try:
            xs, ys, zs = scale
            uniform = False
        except TypeError:
            # no, it's a scalar
            xs, ys, zs = scale, scale, scale

        north = (0.0, 1.0, 0.0)
        g = Octahedron()

        for i in range(subdivides):
            g.UniformSubdivide(midpointdisplace=NormalizeVert)

        #print "%d faces per sphere"%len(g.faces)

        # okay, we're gonna use setShaderInput to set constants for
        # surface coverages and planetary seed, so all we need per
        # vertex is position and normal
        # and we kind of don't need normal for a unit sphere, but
        # we want to use the same shader for other thangs
        format = GeomVertexFormat.getV3n3()
        vdata = GeomVertexData('sphere', format, Geom.UHDynamic)

        vertex = GeomVertexWriter(vdata, 'vertex')
        normal = GeomVertexWriter(vdata, 'normal')

        for (x, y, z) in g.verts:
            vertex.addData3f(x * xs, y * ys, z * zs)
            if uniform:
                normal.addData3f(x, y, z)
            else:
                n = NormalizeVert(
                    (x / (xs * xs), y / (ys * ys), z / (zs * zs)))
                normal.addData3f(n[0], n[1], n[2])

        trilist = GeomTriangles(Geom.UHDynamic)

        for (a, b, c) in g.faces:
            trilist.addVertex(a)
            trilist.addVertex(b)
            trilist.addVertex(c)

        trilist.closePrimitive()
        self.geom = Geom(vdata)
        self.geom.addPrimitive(trilist)

        self.addGeom(self.geom)
Ejemplo n.º 3
0
    def __init__(self,subdivides=3,scale=1.0):
        super(SphereNode,self).__init__('sphere')
        uniform = True
        # see if scale is a tuple
        try:
            xs,ys,zs = scale
            uniform = False
        except TypeError:
            # no, it's a scalar
            xs,ys,zs = scale,scale,scale
               
        north = (0.0,1.0,0.0)
        g = Octahedron()
        
        for i in range(subdivides):
            g.UniformSubdivide( midpointdisplace = NormalizeVert )
            
        #print "%d faces per sphere"%len(g.faces)
        
        # okay, we're gonna use setShaderInput to set constants for 
        # surface coverages and planetary seed, so all we need per 
        # vertex is position and normal
        # and we kind of don't need normal for a unit sphere, but 
        # we want to use the same shader for other thangs
        format=GeomVertexFormat.getV3n3()
        vdata=GeomVertexData('sphere', format, Geom.UHDynamic)

        vertex=GeomVertexWriter(vdata, 'vertex')
        normal=GeomVertexWriter(vdata, 'normal')


        for (x,y,z) in g.verts:
            vertex.addData3f( x*xs, y*ys, z*zs )
            if uniform:            
                normal.addData3f( x, y, z )
            else:
                n = NormalizeVert( (x/(xs*xs),y/(ys*ys),z/(zs*zs)) )
                normal.addData3f( n[0], n[1], n[2] )  
            
        trilist=GeomTriangles(Geom.UHDynamic)
        
        for (a,b,c) in g.faces:
            trilist.addVertex(a)
            trilist.addVertex(b)
            trilist.addVertex(c)
        
        trilist.closePrimitive()
        self.geom = Geom(vdata)
        self.geom.addPrimitive( trilist )
        
        self.addGeom( self.geom )
Ejemplo n.º 4
0
    def __init__(self, radius=1.0, height=1.0, numSegs=16, degrees=360, axis=Vec3(0, 0, 1), origin=Point3(0, 0, 0)):

        # Create vetex data format
        gvf = GeomVertexFormat.getV3n3()
        gvd = GeomVertexData("vertexData", gvf, Geom.UHStatic)

        # Create vetex writers for each type of data we are going to store
        gvwV = GeomVertexWriter(gvd, "vertex")
        gvwN = GeomVertexWriter(gvd, "normal")

        # Get the points for an arc
        points = GetPointsForArc(degrees, numSegs, True)
        for i in range(len(points) - 1):

            # Rotate the points around the desired axis
            p1 = Point3(points[i][0], points[i][1], 0) * radius
            p1 = RotatePoint3(p1, Vec3(0, 0, 1), axis) - origin
            p2 = Point3(points[i + 1][0], points[i + 1][1], 0) * radius
            p2 = RotatePoint3(p2, Vec3(0, 0, 1), axis) - origin

            cross = (p2 - axis).cross(p1 - axis)
            cross.normalize()

            gvwV.addData3f(p1)
            gvwV.addData3f(axis * height - origin)
            gvwV.addData3f(p2)
            gvwN.addData3f(cross)
            gvwN.addData3f(cross)
            gvwN.addData3f(cross)

            # Base
            gvwV.addData3f(p2)
            gvwV.addData3f(Point3(0, 0, 0) - origin)
            gvwV.addData3f(p1)
            gvwN.addData3f(-axis)
            gvwN.addData3f(-axis)
            gvwN.addData3f(-axis)

        geom = Geom(gvd)
        for i in range(0, gvwV.getWriteRow(), 3):

            # Create and add triangle
            geom.addPrimitive(GetGeomTriangle(i, i + 1, i + 2))

        # Init the node path, wrapping the box
        geomNode = GeomNode("cone")
        geomNode.addGeom(geom)
        NodePath.__init__(self, geomNode)
Ejemplo n.º 5
0
    def __init__(self, inner, outer, sectors):
        super(RingNode, self).__init__('ring')
        self.inner = inner
        self.outer = outer
        self.sectors = sectors

        ringgeo = RawGeometry()

        # inner and outer radii are the true circular limits, so expand a little bit
        # for the sectored mesh
        mesh_inner = self.inner * 0.9
        mesh_outer = self.outer * 1.1

        angular_width = 2.0 * math.pi / self.sectors
        for sector in range(self.sectors):
            start = sector * angular_width
            end = (sector + 1) * angular_width
            # add a quad
            x0 = math.sin(start) * mesh_inner
            x1 = math.sin(start) * mesh_outer
            x2 = math.sin(end) * mesh_inner
            x3 = math.sin(end) * mesh_outer

            z0 = math.cos(start) * mesh_inner
            z1 = math.cos(start) * mesh_outer
            z2 = math.cos(end) * mesh_inner
            z3 = math.cos(end) * mesh_outer

            index = len(ringgeo.verts)
            ringgeo.verts.append((x0, 0, z0))
            ringgeo.verts.append((x1, 0, z1))
            ringgeo.verts.append((x2, 0, z2))
            ringgeo.verts.append((x3, 0, z3))

            # double-side the faces so they render from either side
            # top pair...
            ringgeo.faces.append((index + 0, index + 1, index + 2))
            ringgeo.faces.append((index + 1, index + 3, index + 2))
            # bottom pair...
            ringgeo.faces.append((index + 0, index + 2, index + 1))
            ringgeo.faces.append((index + 1, index + 2, index + 3))

        format = GeomVertexFormat.getV3n3()
        vdata = GeomVertexData('ring', format, Geom.UHDynamic)

        vertex = GeomVertexWriter(vdata, 'vertex')
        normal = GeomVertexWriter(vdata, 'normal')

        for (x, y, z) in ringgeo.verts:
            vertex.addData3f(x, y, z)
            normal.addData3f(0, 1, 0)

        trilist = GeomTriangles(Geom.UHDynamic)

        for (a, b, c) in ringgeo.faces:
            trilist.addVertex(a)
            trilist.addVertex(b)
            trilist.addVertex(c)

        trilist.closePrimitive()
        ring = Geom(vdata)
        ring.addPrimitive(trilist)

        self.addGeom(ring)
Ejemplo n.º 6
0
    def __init__(self,inner,outer,sectors):
        super(RingNode,self).__init__('ring')
        self.inner = inner
        self.outer = outer
        self.sectors = sectors
        
        ringgeo = RawGeometry()
        
        # inner and outer radii are the true circular limits, so expand a little bit 
        # for the sectored mesh
        mesh_inner = self.inner * 0.9
        mesh_outer = self.outer * 1.1
        
        angular_width = 2.0 * math.pi / self.sectors
        for sector in range( self.sectors ):
            start = sector * angular_width
            end = (sector+1) * angular_width
            # add a quad
            x0 = math.sin(start) * mesh_inner
            x1 = math.sin(start) * mesh_outer
            x2 = math.sin(end  ) * mesh_inner
            x3 = math.sin(end  ) * mesh_outer
            
            z0 = math.cos(start) * mesh_inner
            z1 = math.cos(start) * mesh_outer
            z2 = math.cos(end  ) * mesh_inner
            z3 = math.cos(end  ) * mesh_outer
            
            index = len(ringgeo.verts)
            ringgeo.verts.append( (x0, 0, z0) )
            ringgeo.verts.append( (x1, 0, z1) )
            ringgeo.verts.append( (x2, 0, z2) )
            ringgeo.verts.append( (x3, 0, z3) )
            
            # double-side the faces so they render from either side
            # top pair...
            ringgeo.faces.append( (index+0,index+1,index+2) )
            ringgeo.faces.append( (index+1,index+3,index+2) )
            # bottom pair...
            ringgeo.faces.append( (index+0,index+2,index+1) )
            ringgeo.faces.append( (index+1,index+2,index+3) )
            
        format=GeomVertexFormat.getV3n3()
        vdata=GeomVertexData('ring', format, Geom.UHDynamic)

        vertex=GeomVertexWriter(vdata, 'vertex')
        normal=GeomVertexWriter(vdata, 'normal')


        for (x,y,z) in ringgeo.verts:
            vertex.addData3f( x, y, z )
            normal.addData3f( 0, 1, 0 )
            
        trilist=GeomTriangles(Geom.UHDynamic)
        
        for (a,b,c) in ringgeo.faces:
            trilist.addVertex(a)
            trilist.addVertex(b)
            trilist.addVertex(c)
        
        trilist.closePrimitive()
        ring = Geom(vdata)
        ring.addPrimitive( trilist )
        
        self.addGeom( ring )