Beispiel #1
0
def _parseModelNode(xmlSegments, xmlMaterials, lights, name, node):
    """
    parses a model node and creates a model instance.
    """
    
    params = {}
    
    segments = []
    sLights = []
    for child in list(node):
        if child.tag == "segment":
            segmentName = child.get('name')
            
            segment = loadXMLSegment(xmlSegments, segmentName)
            # load additional attributes into the datatype (rotation,translation,..)
            segment = _parseModelSegmentNode(xmlMaterials, lights, segment, child)
            segments.append(segment)
                
        elif child.tag == "state":
            xmlParseStateParam(child, params)
        
        elif child.tag == "light":
            name = child.get('name')
            for l in lights:
                if l.name==name:
                    sLights.append(l)
                    break
        else:
            print "WARNING: unknown model tag '%s'" % child.tag
    
    m = Model(segments=segments, params=params)
    m.setLights(sLights)
    
    return m
def _parseMaterialNode(name, node, xmlStateGroups):
    """
    create a GLMaterial instance from xml node.
    """
    
    params = {}
    textures = []
    
    for child in list(node):
        if child.tag == "state":
            xmlParseStateParam(child, params)
        
        elif child.tag == "stateGroup":
            xmlParseStateGroupParam(child, params, xmlStateGroups)
        
        elif child.tag == "texture":
            texName = child.get('image')
            tex = ImageTexture(texName)
            
            wrapMode = str(child.get('wrapping'))
            if wrapMode=="repeat":
                tex.wrapMode = gl.GL_REPEAT
            elif wrapMode=="clamp":
                tex.wrapMode = gl.GL_CLAMP
            
            tex.brightness = xmlFloat(child.get('brightness'), tex.brightness)
            tex.contrast = xmlFloat(child.get('contrast'), tex.contrast)
            tex.colfac = xmlFloat(child.get('colfac'), tex.colfac)
            tex.norfac = xmlFloat(child.get('norfac'), tex.norfac)
            tex.warpfac = xmlFloat(child.get('warpfac'), tex.warpfac)
            tex.dispfac = xmlFloat(child.get('dispfac'), tex.dispfac)
            tex.col = xmlFloatTuple(child.get('col'),
                                    default=tex.col)
            tex.rgbCol = xmlFloatTuple(child.get('rgbCol'),
                                       default=tex.rgbCol)
            tex.useMipMap = xmlBool(child.get('useMipMap'), tex.useMipMap)
            tex.noRGB = xmlBool(child.get('noRGB'), tex.noRGB)
            tex.stencil = xmlBool(child.get('stencil'), tex.stencil)
            tex.neg = xmlBool(child.get('neg'), tex.neg)
            tex.tangentSpace = xmlBool(child.get('tangentSpace'), tex.tangentSpace)
            tex.setBlendMode(child.get('blendMode'))
            tex.setMapInput(child.get('mapInput'))
            tex.setMapping(child.get('mapping'))
            tex.setMapTo(xmlStringList(child.get('mapTo'),
                                       default=['col']))
            
            if tex.useMipMap:
                # mipmapping filter mode
                tex.minFilterMode = gl.GL_LINEAR_MIPMAP_LINEAR
            
            textures.append(tex)
    
    params['textures'] = textures
    mat = GLMaterial(params)
    
    return mat
Beispiel #3
0
def xmlParseStateGroup(node):
    params = {}
    
    name = node.get('name')
    dynamic = xmlBool(node.get('dynamic'), False)
    
    for child in list(node):
        if child.tag!="state": continue
        xmlParseStateParam(child, params)
    
    return GLStateGroup(params, name, dynamic)
Beispiel #4
0
def _parseLightNode(node, xmlStateGroups):
    """
    parses a light node and creates a model instance.
    """
    
    params = { 'name': node.get('name') }
    
    for child in list(node):
        if child.tag == "state":
            xmlParseStateParam(child, params)
        elif child.tag == "stateGroup":
            xmlParseStateGroupParam(child, params, xmlStateGroups)
        else:
            print "WARNING: unknown light tag '%s'." % child.tag
    
    return Light(params)
def _parseSegmentNode(name, node, xmlStateGroups):
    """
    parses a segment node and creates a segment instance.
    """
    
    segmentParams = {}
    
    for child in list(node):
        # parse vertices
        if child.tag == "vertices":
            _parseSegmentVertices(child, segmentParams)
        # parse faces
        elif child.tag == "faces":
            _parseSegmentFaces(child, segmentParams, xmlStateGroups)
        # parse evaluators
        elif child.tag == "evaluators":
            _parseSegmentEvaluators(child, segmentParams, xmlStateGroups)
        # parse nurbs
        elif child.tag == "nurbs":
            _parseSegmentNurbs(child, segmentParams, xmlStateGroups)
        elif child.tag == "state":
            xmlParseStateParam( child, segmentParams )
        elif child.tag == "stateGroup":
            xmlParseStateGroupParam(child, segmentParams, xmlStateGroups)
        elif child.tag == "vertexParam":
            xmlParseVertexParam( child, segmentParams , xmlStateGroups)
        else:
            print "WARNING: XML: unknown tag '%s'" % child.tag
    
    try:
        segmentCls = segmentParams['cls']
    except:
        print "WARNING: no segment class sepecified!"
        return None
    
    return (name, segmentParams, segmentCls)
Beispiel #6
0
from xml_material import loadXMLMaterial
from core.material import GLMaterial
from utils.util import unique
from xml_parser.xml_state import xmlParseStateParam, xmlParseVertexParam


def _parseModelSegmentNode(xmlMaterials, lights, (segmentName, segmentParams, segmentCls), node):
    """
    parse segment configuration specified by model.
    """
    
    baseImage = None
    
    for child in list(node):
        if child.tag == "state":
            xmlParseStateParam( child, segmentParams )
        elif child.tag == "vertexParam":
            xmlParseVertexParam( child, segmentParams )
        elif child.tag == "image":
            baseImage = str(child.get('val'))
        elif child.tag == "material":
            material = loadXMLMaterial(xmlMaterials, str(child.get('name')))
        else:
            print "WARNING: unknown model segment tag '%s'" % child.tag
    
    if material == None:
        # segment needs material
        material = GLMaterial()
    
    segmentParams['material'] = material
    
def _parseSegmentFaces(node, segmentParams, xmlStateGroups):
    segmentTypes = { 'Points':        (PointSegment, gl.GL_POINTS, 1)
                   , 'Lines':         (LineSegment, gl.GL_LINES, 2)
                   , 'LineStrip':     (LineSegment, gl.GL_LINE_STRIP, 2)
                   , 'LineLoop':      (LineSegment, gl.GL_LINE_LOOP, 2)
                   , 'Triangles':     (TriangleSegment, gl.GL_TRIANGLES, 3)
                   , 'TriangleStrip': (TriangleSegment, gl.GL_TRIANGLE_STRIP, 3)
                   , 'TriangleFan':   (TriangleSegment, gl.GL_TRIANGLE_FAN, 3)
                   , 'Quads':         (QuadSegment, gl.GL_QUADS, 4)
                   , 'QuadStrip':     (QuadSegment, gl.GL_QUAD_STRIP, 4)
                   , 'Polygon':       (PolygonSegment, gl.GL_POLYGON, -1)
                     }
    
    try:
        segmentCls, primitive, faceVertices = segmentTypes[node.get('primitive')]
    except:
        print "WARNING: unknown faces type '%s'" % node.get('primitive')
        return
    segmentParams['cls'] = segmentCls
    segmentParams['primitive'] = primitive
    
    faces = []
    indexes = []
    numIndexes = 0
    for fNode in list(node):
        if fNode.tag != "face": continue
        
        faceParams = {}
        
        for iNode in list(fNode):
            if iNode.tag == "i":
                if faceVertices==1 or faceVertices==2:
                    i = xmlIntList(iNode.get('val'))
                    if i!=None:
                        indexes += i
                        numIndexes += len(i)
                else:
                    faceParams['i'] = xmlIntListC(iNode.get('val'))
                    numIndexes += faceVertices
            elif iNode.tag == "state":
                if faceVertices==1 or faceVertices==2:
                    xmlParseStateParam( iNode, segmentParams )
                else:
                    xmlParseStateParam( iNode, faceParams )
            elif iNode.tag == "stateGroup":
                if faceVertices==1 or faceVertices==2:
                    xmlParseStateGroupParam( iNode, segmentParams, xmlStateGroups )
                else:
                    xmlParseStateGroupParam( iNode, faceParams, xmlStateGroups )
            elif iNode.tag == "uv":
                faceParams['uv'] = xmlFloatTupleListC(iNode.get('val'))
            elif iNode.tag == "col":
                faceParams['col'] = xmlFloatTupleListC(iNode.get('val'))
            elif iNode.tag == "nor":
                faceParams['nor'] = xmlFloatTupleListC(iNode.get('val'))
        
        if not (faceVertices==1 or faceVertices==2):
            faces.append(VArrayFace(faceParams))
        
    if faceVertices==1 or faceVertices==2:
        segmentParams['indexes'] = indexes
    segmentParams['numIndexes'] = numIndexes
    segmentParams['faces'] = (faces, primitive, faceVertices)
def _parseSegmentNurbs(node, segmentParams, xmlStateGroups):
    evaluators = []
    
    vertexEvaluator = None
    normalEvaluator = None
    needsNormals = False
        
    segmentParams['cls'] = EvaluatorSegment
    
    for child in list(node):
        if child.tag != "nurb":
            continue
        
        target = child.get('target', 'vertex')
        uKnot = [0.0]*4 + [1.0]*4
        vKnot = [0.0]*4 + [1.0]*4
        
        params = {}
        
        for n in list(child):
            if n.tag == "u":
                uKnot = xmlFloatListC(n.get('knot'), uKnot)
            elif n.tag == "v":
                vKnot = xmlFloatListC(n.get('knot'), vKnot)
            elif n.tag == "ctrls":
                params['ctrls'] = _parseEvaluatorCtrls(n)
            elif n.tag == "state":
                xmlParseStateParam(n, params)
            elif child.tag == "stateGroup":
                xmlParseStateGroupParam(n, params, xmlStateGroups)
            elif n.tag == "vertexParam":
                xmlParseVertexParam( n, params , xmlStateGroups)
        
        try:
            params['ctrls'][0][0][0]
            isCurve = False
        except:
            isCurve = True
        
        params['tolerance'] = xmlFloat(child.get('tolerance'), 25.0)
        
        if isCurve:
            params['target'] = _curveTargets()[target]
            params['knots'] = [uKnot]
            nurbCls = NURBCurveEvaluator
        else:
            params['target'] = _patchTargets()[target]
            params['knots'] = [uKnot, vKnot]
            nurbCls = NURBPatchEvaluator
        
        evaluator = nurbCls(params)
        
        if target=="vertex" or target=="vertex4":
            vertexEvaluator = evaluator
            # curves dont need normals
            needsNormals = not isCurve
        if target=="normal":
            normalEvaluator = evaluator
        
        evaluators.append(evaluator)
    
    if needsNormals and normalEvaluator==None and vertexEvaluator!=None:
        vertexEvaluator.generateNormals = True
    
    segmentParams['evaluators'] = evaluators
def _parseSegmentEvaluators(node, segmentParams, xmlStateGroups):
    evaluators = []
    
    def _readParam(n, name, param):
        param.min = xmlFloat(n.get('min'), 0.0)
        param.max = xmlFloat(n.get('max'), 1.0)
        param.steps = xmlInt(n.get('steps'), 20)
    
    vertexEvaluator = None
    normalEvaluator = None
    needsNormals = False
    
    for child in list(node):
        if child.tag != "evaluator":
            continue
        
        segmentParams['cls'] = EvaluatorSegment
        evalParams = {}
        
        target = child.get('target', 'vertex')
        u = EvalParam("u")
        v = EvalParam("v")
        
        for n in list(child):
            if n.tag == "u":
                _readParam(n, n.tag, u)
            elif n.tag == "v":
                _readParam(n, n.tag, v)
            elif n.tag == "ctrls":
                evalParams['ctrls'] = _parseEvaluatorCtrls(n)
            elif n.tag == "state":
                xmlParseStateParam(n, evalParams)
            elif child.tag == "stateGroup":
                xmlParseStateGroupParam(n, evalParams, xmlStateGroups)
            elif n.tag == "vertexParam":
                xmlParseVertexParam( n, evalParams , xmlStateGroups)
        
        try:
            evalParams['ctrls'][0][0][0]
            isCurve = False
        except:
            isCurve = True
        
        if isCurve:
            evalParams['target'] = _curveTargets()[target]
            evalParams['params'] = [u]
            evaluatorCls = CurveEvaluator
        else:
            evalParams['target'] = _patchTargets()[target]
            evalParams['params'] = [u, v]
            evaluatorCls = PatchEvaluator
        
        evaluator = evaluatorCls(evalParams)
        
        if target=="vertex" or target=="vertex4":
            vertexEvaluator = evaluator
            # curves dont need normals
            needsNormals = not isCurve
        if target=="normal":
            normalEvaluator = evaluator
        
        evaluators.append(evaluator)
    
    if needsNormals and normalEvaluator==None and vertexEvaluator!=None:
        vertexEvaluator.generateNormals = True
    
    segmentParams['evaluators'] = evaluators