Пример #1
0
 def OnInit(self):
     """Load the image on initial load of the application"""
     print """Should see two geodesic spheres over black background
     
 Sphere to left should be lit with normal-per-face,
 giving each face a hard-line edge with adjacent faces.
 
 Sphere to the right should be lit with normal-per-vertex
 which should make the lines between faces fuzzier."""
     points, indices = loadData(ICOSDATA)
     ##		light = basenodes.PointLight(
     ##			location = (2,10,10)
     ##		)
     self.sg = basenodes.sceneGraph(
         ##			lights = [
         ##				light
         ##			],
         children=[
             ##				light,
             basenodes.Transform(
                 translation=(-1.5, 0, 0),
                 children=[
                     basenodes.Shape(
                         appearance=basenodes.Appearance(
                             material=basenodes.Material(diffuseColor=(1, 0,
                                                                       0))),
                         geometry=basenodes.IndexedFaceSet(
                             coord=basenodes.Coordinate(point=points, ),
                             coordIndex=indices,
                         ),
                     ),
                 ],
             ),
             basenodes.Transform(
                 translation=(1.5, 0, 0),
                 children=[
                     basenodes.Shape(
                         appearance=basenodes.Appearance(
                             material=basenodes.Material(diffuseColor=(1, 0,
                                                                       0))),
                         geometry=basenodes.IndexedFaceSet(
                             coord=basenodes.Coordinate(point=points, ),
                             coordIndex=indices,
                             creaseAngle=3.14,
                             normalPerVertex=1,
                         ),
                     ),
                 ],
             ),
         ])
Пример #2
0
 def OnInit(self):
     """Load the image on initial load of the application"""
     print("""Should see 6-sided polygon with normal-per-vertex smoothing
 This polygon is produced using the GLU tesselator
 from a simple linear progression of indices across
 the 6 points of the polygon.""")
     points, indices = loadData(TESTDATA)
     self.sg = basenodes.sceneGraph(children=[
         basenodes.Transform(children=[
             basenodes.Shape(
                 appearance=basenodes.Appearance(
                     material=basenodes.Material(diffuseColor=(1, 0, 0))),
                 geometry=basenodes.IndexedFaceSet(
                     coord=basenodes.Coordinate(point=points, ),
                     coordIndex=indices,
                     creaseAngle=3.14,
                     normalPerVertex=1,
                 ),
             ),
         ], ),
     ])
    def parse( self, data, baseURL, *args, **named ):
        """Parse the loaded data (with the provided meta-information)
        
        This implementation simply creates VRML97 scenegraph nodes out 
        of the .obj format data. 
        """
        sg = basenodes.sceneGraph(
        )
        
        # these three are shared among all shapes
        hash = md5( baseURL ).hexdigest()
        coord = basenodes.Coordinate( DEF='Coord-%s'%(hash,) )
        normal = basenodes.Normal(DEF='Norm-%s'%(hash,))
        texCoord = basenodes.TextureCoordinate(DEF='TexCoord-%s'%(hash,))

        mesh = None # transforms
        group = None # shape
        material = None # appearance, material, texture
        
        materials = {}
        
        # indices are 1-based, the first values are never used...
        vertices = [[0., 0., 0.]] 
        normals = [[0., 0., 0.]]
        tex_coords = [[0., 0.]]
        
        current_vertex_indices = []
        current_normal_indices = []
        current_texcoord_indices = []

        for line in data.splitlines():
            if line.startswith('#'): 
                continue
            values = line.split()
            if not values: 
                continue

            if values[0] == 'v':
                vertices.append(map(float, values[1:4]))
            elif values[0] == 'vn':
                normals.append(map(float, values[1:4]))
            elif values[0] == 'vt':
                tex_coords.append(map(float, values[1:3]))
            elif values[0] == 'mtllib':
                self.load_material_library(values[1], materials, baseURL)
            elif values[0] in ('usemtl', 'usemat'):
                material = materials.get(values[1], None)
                if material is None:
                    log.warn('Unknown material: %s', values[1])
                    material = self.defaultMaterial()
                if mesh is not None:
                    if group and current_vertex_indices:
                        group.geometry.coordIndex = current_vertex_indices
                        group.geometry.texCoordIndex = current_texcoord_indices
                        group.geometry.normalIndex = current_normal_indices
                        current_vertex_indices = []
                        current_texcoord_indices = []
                        current_normal_indices = []
                    group = basenodes.Shape(
                        geometry = basenodes.IndexedFaceSet(
                            coord = coord,
                            normal = normal,
                            texCoord = texCoord,
                            solid=False,
                        ),
                        appearance = material,
                    )
                    mesh.children.append(group)
            elif values[0] == 'o':
                mesh = basenodes.Transform( DEF = values[1] )
                sg.children.append( mesh )
                sg.regDefName( values[1], mesh )
                # previous shape is no longer current...
                group = None
            elif values[0] == 's':
                # a smoothing-group definition...
                # not currently supported...
                pass
            elif values[0] == 'f':
                # adds a single face
                if mesh is None:
                    # anonymous transform
                    mesh = basenodes.Transform()
                    sg.children.append(mesh)
                if material is None:
                    material = self.defaultMaterial()
                if group is None:
                    group = basenodes.Shape( 
                        geometry = basenodes.IndexedFaceSet(
                            coord = coord,
                            normal = normal,
                            texCoord = texCoord,
                            solid=False,
                        ),
                        appearance = material,
                    )
                    mesh.children.append(group)

                for i, v in enumerate(values[1:]):
                    v_index, t_index, n_index = self._cleanIndex( v )
                    current_vertex_indices.append( v_index )
                    current_texcoord_indices.append( t_index )
                    current_normal_indices.append( n_index )
                current_vertex_indices.append( -1 )
                current_texcoord_indices.append( -1 )
                current_normal_indices.append( -1 )
            else:
                log.warn( """Unrecognized operation: %r""", values )
        if group and current_vertex_indices:
            group.geometry.coordIndex = current_vertex_indices
            group.geometry.texCoordIndex = current_texcoord_indices
            group.geometry.normalIndex = current_normal_indices
        coord.point = vertices
        normal.normal = normals
        texCoord.texCoord = tex_coords
        return True,sg