Пример #1
0
def generate_sphere(name, radius, resolution):
    """
    Generates a sphere with the provided resolution.

    @type name: string
    @param name: Name of this sphere.

    @type radius: number
    @param radius: Radius of sphere in kilometers.

    @type resolution: number
    @param resolution: Resolution of sphere (minimum 2)

    @rtype: GeomNode
    @return: A GeomNode with the given sphere.
    """

    if resolution < 2:
        raise ValueError, "resolution must be >= 2"

    horizBands = resolution*2
    vertBands = horizBands*2

    vertexFormat = GeomVertexFormat.getV3n3c4t2()
    vdata = GeomVertexData('%s_vdata' % name, vertexFormat, Geom.UHDynamic)

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

    vertDelta = omath.TWOPI / vertBands
    horizDelta = omath.TWOPI / horizBands

    numVertices = 0

    for i in range(vertBands+1):
        lowTheta = i * vertDelta
        highTheta = (i+1) * vertDelta

        cosLowTheta = math.cos(lowTheta)
        sinLowTheta = math.sin(lowTheta)
        cosHighTheta = math.cos(highTheta)
        sinHighTheta = math.sin(highTheta)

        for j in range(horizBands):
            horizTheta = j * horizDelta

            cosHorizTheta = math.cos(horizTheta)
            sinHorizTheta = math.sin(horizTheta)

            ex = cosLowTheta*cosHorizTheta
            ey = sinLowTheta
            ez = cosLowTheta*sinHorizTheta

            vertex.addData3f(ex*radius, ey*radius, ez*radius)
            normal.addData3f(ex, ey, ez)
            color.addData4f(.75, .75, .75, 1)
            texcoord.addData2f(i / vertBands, j / horizBands)

            ex = cosHighTheta*cosHorizTheta
            ey = sinHighTheta
            ez = cosHighTheta*sinHorizTheta

            vertex.addData3f(ex*radius, ey*radius, ez*radius)
            normal.addData3f(ex, ey, ez)
            color.addData4f(.75, .75, .75, 1)
            texcoord.addData2f(i / vertBands, j / horizBands)

            numVertices += 2

    prim = GeomTristrips(Geom.UHStatic)
    prim.addConsecutiveVertices(0, numVertices)
    prim.closePrimitive()

    geom = Geom(vdata)
    geom.addPrimitive(prim)

    geomNode = GeomNode(name)
    geomNode.addGeom(geom)

    return GeomScaler(geomNode)