def update_geometry(self):
     # The geosphere itself
     vertex = GeomVertexWriter(self.sphere_vdata, 'vertex')
     normal = GeomVertexWriter(self.sphere_vdata, 'normal')
     # u_map and v_map are in [-pi, pi]
     u_map_list = [(float(u) / float(self.res[0]) - 0.5) * 2.0 * pi for u in range(0, self.res[0] + 1)]
     v_map_list = [(float(v) / float(self.res[1]) - 0.5) * 2.0 * pi for v in range(0, self.res[1] + 1)]
     if self.unwrap_state == 0.0: # Flat map
         for v_map in v_map_list:
             for u_map in u_map_list:
                 vertex.addData3f(u_map, 0.0, v_map / 2.0)
                 normal.addData3f(0.0, -1.0, 0.0)
     else: # Non-flat map
         sphere_radius = 1.0 / self.unwrap_state
         sphere_offset = sphere_radius - self.unwrap_state
         for v_map in v_map_list:
             for u_map in u_map_list:
                 u_sphere = u_map / sphere_radius
                 v_sphere = v_map / sphere_radius
                 # And this, kids, is why you should pay attention in trigonometry.
                 v_x, v_y, v_z = sin(u_sphere) * cos(v_sphere/2.0) * sphere_radius, \
                                 -cos(u_sphere) * cos(v_sphere/2.0) * sphere_radius + sphere_offset, \
                                 sin(v_sphere / 2.0) * sphere_radius
                 n_x_un, n_y_un, n_z_un = v_x, sphere_offset - v_y, v_z # FIXME: This is a lie.
                 length = sqrt(n_x_un**2 + n_y_un**2 + n_z_un**2)
                 n_x, n_y, n_z = n_x_un / length, n_y_un / length, n_z_un / length
                 vertex.addData3f(v_x, v_y, v_z)
                 normal.addData3f(n_x, n_y, n_z)
                 
     # The connections between bases
     segs_per_connection = 30
     vertex = GeomVertexWriter(self.connections_vdata, 'vertex')
     color = GeomVertexWriter(self.connections_vdata, 'color')
     for c_1_uv, c_2_uv in self.connections:
         # s will be [0.0, 1.0]
         for s in [float(c)/float(segs_per_connection+1) for c in range(0, segs_per_connection+2)]:
             u = (c_1_uv[0] * s) + (c_2_uv[0] * (1.0 - s))
             v = (c_1_uv[1] * s) + (c_2_uv[1] * (1.0 - s))
             (v_x, v_y, v_z), (n_x, n_y, n_z) = self.uv_to_xyz(u, v)
             min_height = 0.0001 * (1.0 - self.unwrap_state)
             max_height = (0.2 - min_height) * self.unwrap_state
             seg_height = (1.0 - (abs(s-0.5) * 2.0)**2.0) * max_height + min_height
             vertex.addData3f(v_x + n_x*seg_height,
                              v_y + n_y*seg_height,
                              v_z + n_z*seg_height)
             color.addData4f(1, 1, 1, 1)
     for c in range(0, len(self.connections)):
         for s in range(0, segs_per_connection+1):
             seg = GeomLines(Geom.UHDynamic)
             seg.addVertices(c*(segs_per_connection+2) + s, c*(segs_per_connection+2) + s + 1)
             seg.closePrimitive()
             self.connections_geom.addPrimitive(seg)
Exemple #2
0
def makeGeomNode():
    vdata = GeomVertexData('handleData', GeomVertexFormat.getV3(),
            Geom.UHStatic)
    v = GeomVertexWriter(vdata, 'vertex')
    length = 1.0
    gapLen = 0.2
    coneLen = 0.18
    coneRad = 0.06
    circRes = 10

    v.addData3f(0, 0, -length/2.0) # back cone butt
    v.addData3f(0, 0, -length/2.0 + coneLen) # back cone point
    v.addData3f(0, 0, -gapLen/2.0)
    v.addData3f(0, 0, gapLen/2.0)
    v.addData3f(0, 0, length/2.0 - coneLen) # front cone butt
    v.addData3f(0, 0, length/2.0) # font cone point
    # Add vertices for the cone's circles.
    for z in [-length/2.0, length/2.0 - coneLen]:
        for i in xrange(circRes):
            theta = i*(2*pi/circRes)
            v.addData3f(coneRad*sin(theta), coneRad*cos(theta), z)

    lines = Geom(vdata)
    # Make prims for the two lines.
    for vertices in [(0, 2), (3, 5)]:
        line = GeomLines(Geom.UHStatic)
        line.addVertices(*vertices)
        line.closePrimitive()
        lines.addPrimitive(line)

    cones = Geom(vdata)
    # Make prims for the cones.
    for back, circ in [(0, 6), (4, 6 + circRes)]:
        point = back + 1
        cone = GeomTriangles(Geom.UHStatic)
        for i in xrange(circRes):
            if i + 1 == circRes:
                cone.addVertices(back, circ, circ + i)
                cone.addVertices(point, circ + i, circ)
            else:
                cone.addVertices(back, circ + i + 1, circ + i)
                cone.addVertices(point, circ + i, circ + i + 1)
        cone.closePrimitive()
        cones.addPrimitive(cone)
    
    node = GeomNode('geomnode')
    node.addGeom(lines)
    node.addGeom(cones)
    return node
def getGeomFromPrim(prim, matstate):
    if type(prim) is collada.triangleset.TriangleSet:
        (vdata, gprim) = getPrimAndDataFromTri(prim, matstate)
    elif type(prim) is collada.polylist.Polylist or type(prim) is collada.polygons.Polygons:
        triset = prim.triangleset()
        (vdata, gprim) = getPrimAndDataFromTri(triset, matstate)
    elif type(prim) is collada.lineset.LineSet:
        vdata, indexdata = getVertexData(prim.vertex, prim.vertex_index)
        gprim = GeomLines(Geom.UHStatic)
        gprim.setIndexType(Geom.NTUint32)
        gprim.setVertices(indexdata)
        gprim.closePrimitive()
    else:
        raise Exception("Error: Unsupported primitive type. Exiting.")

    pgeom = Geom(vdata)
    pgeom.addPrimitive(gprim)
    return pgeom