コード例 #1
0
        private void ParseJoint(idLexer lexer, idMD5Joint joint, ref idJointQuaternion defaultPose)
        {
            //
            // parse name
            //
            joint.Name = lexer.ReadToken().ToString();

            //
            // parse parent
            //
            int parentIndex = lexer.ParseInt();

            if (parentIndex >= 0)
            {
                if (parentIndex >= (_joints.Length - 1))
                {
                    lexer.Error("Invalid parent for joint '{0}'", joint.Name);
                }

                joint.Parent = _joints[parentIndex];
            }

            //
            // parse default pose
            //
            float[] tmp = lexer.Parse1DMatrix(3);
            defaultPose.Translation = new Vector3(tmp[0], tmp[1], tmp[2]);

            tmp = lexer.Parse1DMatrix(3);
            defaultPose.Quaternion   = new Quaternion(tmp[0], tmp[1], tmp[2], 0);
            defaultPose.Quaternion.W = idHelper.CalculateW(defaultPose.Quaternion);
        }
コード例 #2
0
        private void ParseBrushes(idLexer lexer, CollisionModel model)
        {
            idToken             token = lexer.CheckTokenType(TokenType.Number, 0);
            int                 planeCount;
            CollisionModelBrush b;

            float[] tmp;

            lexer.ExpectTokenString("{");

            while (lexer.CheckTokenString("}") == false)
            {
                // parse brush
                planeCount = lexer.ParseInt();

                b          = new CollisionModelBrush();
                b.Contents = ContentFlags.All;
                b.Material = _traceModelMaterial;
                b.Planes   = new Plane[planeCount];

                lexer.ExpectTokenString("{");

                for (int i = 0; i < planeCount; i++)
                {
                    tmp = lexer.Parse1DMatrix(3);

                    b.Planes[i].Normal = new Vector3(tmp[0], tmp[1], tmp[2]);
                    b.Planes[i].D      = lexer.ParseFloat();
                }

                lexer.ExpectTokenString("}");

                tmp          = lexer.Parse1DMatrix(3);
                b.Bounds.Min = new Vector3(tmp[0], tmp[1], tmp[2]);

                tmp          = lexer.Parse1DMatrix(3);
                b.Bounds.Max = new Vector3(tmp[0], tmp[1], tmp[2]);

                token = lexer.ReadToken();

                if (token.Type == TokenType.Number)
                {
                    b.Contents = (ContentFlags)token.ToInt32();                      // old .cm files use a single integer
                }
                else
                {
                    b.Contents = ContentsFromString(token.ToString());
                }

                b.CheckCount     = 0;
                b.PrimitiveCount = 0;

                // filter brush into tree
                FilterBrushIntoTree(model, model.Node, b);
            }
        }
コード例 #3
0
        private void ParsePolygons(idLexer lexer, CollisionModel model)
        {
            idToken token = lexer.CheckTokenType(TokenType.Number, 0);

            float[] tmp;
            Vector3 normal;

            lexer.ExpectTokenString("{");

            while (lexer.CheckTokenString("}") == false)
            {
                // parse polygon
                int edgeCount = lexer.ParseInt();

                CollisionModelPolygon p = new CollisionModelPolygon();
                p.Material = _traceModelMaterial;
                p.Contents = ContentFlags.All;
                p.Edges    = new int[edgeCount];

                lexer.ExpectTokenString("(");

                for (int i = 0; i < edgeCount; i++)
                {
                    p.Edges[i] = lexer.ParseInt();
                }

                lexer.ExpectTokenString(")");

                tmp    = lexer.Parse1DMatrix(3);
                normal = new Vector3(tmp[0], tmp[1], tmp[2]);

                p.Plane.Normal = normal;
                p.Plane.D      = lexer.ParseFloat();

                tmp          = lexer.Parse1DMatrix(3);
                p.Bounds.Min = new Vector3(tmp[0], tmp[1], tmp[2]);

                tmp          = lexer.Parse1DMatrix(3);
                p.Bounds.Max = new Vector3(tmp[0], tmp[1], tmp[2]);

                token = lexer.ExpectTokenType(TokenType.String, 0);

                // get material
                p.Material   = idE.DeclManager.FindMaterial(token.ToString());
                p.Contents   = p.Material.ContentFlags;
                p.CheckCount = 0;

                // filter polygon into tree
                FilterPolygonIntoTree(model, model.Node, p);
            }
        }
コード例 #4
0
        private void ParseVertices(idLexer lexer, CollisionModel model)
        {
            lexer.ExpectTokenString("{");

            int vertexCount = lexer.ParseInt();

            model.Vertices = new CollisionModelVertex[vertexCount];

            for (int i = 0; i < vertexCount; i++)
            {
                float[] tmp = lexer.Parse1DMatrix(3);

                model.Vertices[i].Point      = new Vector3(tmp[0], tmp[1], tmp[2]);
                model.Vertices[i].Side       = 0;
                model.Vertices[i].SideSet    = 0;
                model.Vertices[i].CheckCount = 0;
            }

            lexer.ExpectTokenString("}");
        }
コード例 #5
0
        public void Parse(idLexer lexer, idJointMatrix[] joints)
        {
            lexer.ExpectTokenString("{");

            //
            // parse name
            //
            if (lexer.CheckTokenString("name") == true)
            {
                lexer.ReadToken();
            }

            //
            // parse shader
            //
            lexer.ExpectTokenString("shader");

            idToken token        = lexer.ReadToken();
            string  materialName = token.ToString();

            _material = idE.DeclManager.FindMaterial(materialName);

            //
            // parse texture coordinates
            //
            lexer.ExpectTokenString("numverts");
            int count = lexer.ParseInt();

            if (count < 0)
            {
                lexer.Error("Invalid size: {0}", token.ToString());
            }

            _texCoords = new Vector2[count];

            int[] firstWeightForVertex = new int[count];
            int[] weightCountForVertex = new int[count];
            int   maxWeight            = 0;
            int   coordCount           = _texCoords.Length;

            _weightCount = 0;

            for (int i = 0; i < coordCount; i++)
            {
                lexer.ExpectTokenString("vert");
                lexer.ParseInt();

                float[] tmp = lexer.Parse1DMatrix(2);

                _texCoords[i] = new Vector2(tmp[0], tmp[1]);

                firstWeightForVertex[i] = lexer.ParseInt();
                weightCountForVertex[i] = lexer.ParseInt();

                if (weightCountForVertex[i] == 0)
                {
                    lexer.Error("Vertex without any joint weights.");
                }

                _weightCount += weightCountForVertex[i];

                if ((weightCountForVertex[i] + firstWeightForVertex[i]) > maxWeight)
                {
                    maxWeight = weightCountForVertex[i] + firstWeightForVertex[i];
                }
            }

            //
            // parse tris
            //
            lexer.ExpectTokenString("numtris");
            _triangleCount = lexer.ParseInt();

            if (_triangleCount < 0)
            {
                lexer.Error("Invalid size: {0}", _triangleCount);
            }

            int[] tris = new int[_triangleCount * 3];

            for (int i = 0; i < _triangleCount; i++)
            {
                lexer.ExpectTokenString("tri");
                lexer.ParseInt();

                tris[i * 3 + 0] = lexer.ParseInt();
                tris[i * 3 + 1] = lexer.ParseInt();
                tris[i * 3 + 2] = lexer.ParseInt();
            }

            //
            // parse weights
            //
            lexer.ExpectTokenString("numweights");
            count = lexer.ParseInt();

            if (count < 0)
            {
                lexer.Error("Invalid size: {0}", count);
            }

            if (maxWeight > count)
            {
                lexer.Warning("Vertices reference out of range weights in model ({0} of {1} weights).", maxWeight, count);
            }

            VertexWeight[] tempWeights = new VertexWeight[count];

            for (int i = 0; i < count; i++)
            {
                lexer.ExpectTokenString("weight");
                lexer.ParseInt();

                int jointIndex = lexer.ParseInt();

                if ((jointIndex < 0) || (jointIndex >= joints.Length))
                {
                    lexer.Error("Joint index out of range({0}): {1}", joints.Length, jointIndex);
                }

                tempWeights[i].JointIndex  = jointIndex;
                tempWeights[i].JointWeight = lexer.ParseFloat();

                float[] tmp = lexer.Parse1DMatrix(3);

                tempWeights[i].Offset = new Vector3(tmp[0], tmp[1], tmp[2]);
            }

            // create pre-scaled weights and an index for the vertex/joint lookup
            _scaledWeights = new Vector4[_weightCount];
            _weightIndex   = new int[_weightCount * 2];

            count      = 0;
            coordCount = _texCoords.Length;

            for (int i = 0; i < coordCount; i++)
            {
                int num         = firstWeightForVertex[i];
                int weightCount = weightCountForVertex[i];

                for (int j = 0; j < weightCount; j++, num++, count++)
                {
                    Vector3 tmp = tempWeights[num].Offset * tempWeights[num].JointWeight;

                    _scaledWeights[count].X = tmp.X;
                    _scaledWeights[count].Y = tmp.Y;
                    _scaledWeights[count].Z = tmp.Z;
                    _scaledWeights[count].W = tempWeights[num].JointWeight;

                    _weightIndex[count * 2 + 0] = tempWeights[num].JointIndex;
                }

                _weightIndex[count * 2 - 1] = 1;
            }

            lexer.ExpectTokenString("}");

            // update counters
            idConsole.Warning("TODO: idRenderModel_MD5 update counters");

            /*c_numVerts += texCoords.Num();
             * c_numWeights += numWeights;
             * c_numWeightJoints++;
             * for ( i = 0; i < numWeights; i++ ) {
             *      c_numWeightJoints += weightIndex[i*2+1];
             * }*/

            //
            // build the information that will be common to all animations of this mesh:
            // silhouette edge connectivity and normal / tangent generation information
            //
            Vertex[] verts     = new Vertex[_texCoords.Length];
            int      vertCount = verts.Length;

            for (int i = 0; i < vertCount; i++)
            {
                verts[i].TextureCoordinates = _texCoords[i];
            }

            TransformVertices(verts, joints);

            idConsole.Warning("TODO: idMD5Mesh Deform");
            //_deformInfo = idE.RenderSystem.BuildDeformInformation(verts, tris, _material.UseUnsmoothedTangents);
        }
コード例 #6
0
        public bool LoadAnimation(string fileName)
        {
            idToken token;
            idLexer lexer = new idLexer(LexerOptions.AllowPathNames | LexerOptions.NoStringEscapeCharacters | LexerOptions.NoStringConcatination);

            if (lexer.LoadFile(fileName) == false)
            {
                return(false);
            }

            Clear();

            _name = fileName;

            lexer.ExpectTokenString(idRenderModel_MD5.VersionString);
            int version = lexer.ParseInt();

            if (version != idRenderModel_MD5.Version)
            {
                lexer.Error("Invalid version {0}.  Should be version {1}", version, idRenderModel_MD5.Version);
            }

            // skip the commandline
            lexer.ExpectTokenString("commandline");
            lexer.ReadToken();

            // parse num frames
            lexer.ExpectTokenString("numFrames");
            int frameCount = lexer.ParseInt();

            if (frameCount <= 0)
            {
                lexer.Error("Invalid number of frames: {0}", frameCount);
            }

            // parse num joints
            lexer.ExpectTokenString("numJoints");
            int jointCount = lexer.ParseInt();

            if (jointCount <= 0)
            {
                lexer.Error("Invalid number of joints: {0}", jointCount);
            }

            // parse frame rate
            lexer.ExpectTokenString("frameRate");
            _frameRate = lexer.ParseInt();

            if (_frameRate < 0)
            {
                lexer.Error("Invalid frame rate: {0}", _frameRate);
            }

            // parse number of animated components
            lexer.ExpectTokenString("numAnimatedComponents");
            _animatedComponentCount = lexer.ParseInt();

            if ((_animatedComponentCount < 0) || (_animatedComponentCount > (jointCount * 6)))
            {
                lexer.Error("Invalid number of animated components: {0}", _animatedComponentCount);
            }

            // parse the hierarchy
            _jointInfo = new JointAnimationInfo[jointCount];

            lexer.ExpectTokenString("hierarchy");
            lexer.ExpectTokenString("{");

            for (int i = 0; i < jointCount; i++)
            {
                token = lexer.ReadToken();

                _jointInfo[i]           = new JointAnimationInfo();
                _jointInfo[i].NameIndex = idR.AnimManager.GetJointIndex(token.ToString());

                // parse parent num
                _jointInfo[i].ParentIndex = lexer.ParseInt();

                if (_jointInfo[i].ParentIndex >= i)
                {
                    lexer.Error("Invalid parent num: {0}", _jointInfo[i].ParentIndex);
                }

                if ((i != 0) && (_jointInfo[i].ParentIndex < 0))
                {
                    lexer.Error("Animations may have only one root joint");
                }

                // parse anim bits
                _jointInfo[i].AnimationBits = (AnimationBits)lexer.ParseInt();

                if (((int)_jointInfo[i].AnimationBits & ~63) != 0)
                {
                    lexer.Error("Invalid anim bits: {0}", _jointInfo[i].AnimationBits);
                }

                // parse first component
                _jointInfo[i].FirstComponent = lexer.ParseInt();

                if ((_animatedComponentCount > 0) && ((_jointInfo[i].FirstComponent < 0) || (_jointInfo[i].FirstComponent >= _animatedComponentCount)))
                {
                    lexer.Error("Invalid first component: {0}", _jointInfo[i].FirstComponent);
                }
            }

            lexer.ExpectTokenString("}");

            // parse bounds
            lexer.ExpectTokenString("bounds");
            lexer.ExpectTokenString("{");

            _bounds = new idBounds[frameCount];

            for (int i = 0; i < frameCount; i++)
            {
                float[] tmp  = lexer.Parse1DMatrix(3);
                float[] tmp2 = lexer.Parse1DMatrix(3);

                _bounds[i] = new idBounds(
                    new Vector3(tmp[0], tmp[1], tmp[2]),
                    new Vector3(tmp2[0], tmp2[1], tmp2[2])
                    );
            }

            lexer.ExpectTokenString("}");

            // parse base frame
            _baseFrame = new idJointQuaternion[jointCount];

            lexer.ExpectTokenString("baseframe");
            lexer.ExpectTokenString("{");

            for (int i = 0; i < jointCount; i++)
            {
                float[] tmp  = lexer.Parse1DMatrix(3);
                float[] tmp2 = lexer.Parse1DMatrix(3);

                idCompressedQuaternion q = new idCompressedQuaternion(tmp2[0], tmp2[1], tmp2[2]);


                _baseFrame[i]             = new idJointQuaternion();
                _baseFrame[i].Translation = new Vector3(tmp[0], tmp[1], tmp[2]);
                _baseFrame[i].Quaternion  = q.ToQuaternion();
            }

            lexer.ExpectTokenString("}");

            // parse frames
            _componentFrames = new float[_animatedComponentCount * frameCount];
            int frameOffset = 0;

            for (int i = 0; i < frameCount; i++)
            {
                lexer.ExpectTokenString("frame");
                int count = lexer.ParseInt();

                if (count != i)
                {
                    lexer.Error("Expected frame number {0}", i);
                }

                lexer.ExpectTokenString("{");

                for (int j = 0; j < _animatedComponentCount; j++, frameOffset++)
                {
                    _componentFrames[frameOffset] = lexer.ParseFloat();
                }

                lexer.ExpectTokenString("}");
            }

            // get total move delta
            if (_animatedComponentCount == 0)
            {
                _totalDelta = Vector3.Zero;
            }
            else
            {
                int componentOffset = _jointInfo[0].FirstComponent;

                if ((_jointInfo[0].AnimationBits & AnimationBits.TranslationX) == AnimationBits.TranslationX)
                {
                    for (int i = 0; i < frameCount; i++)
                    {
                        _componentFrames[componentOffset + (_animatedComponentCount * i)] -= _baseFrame[0].Translation.X;
                    }

                    _totalDelta.X = _componentFrames[componentOffset + (_animatedComponentCount * (frameCount - 1))];
                    componentOffset++;
                }
                else
                {
                    _totalDelta.X = 0;
                }

                if ((_jointInfo[0].AnimationBits & AnimationBits.TranslationY) == AnimationBits.TranslationY)
                {
                    for (int i = 0; i < frameCount; i++)
                    {
                        _componentFrames[componentOffset + (_animatedComponentCount * i)] -= _baseFrame[0].Translation.Y;
                    }

                    _totalDelta.Y = _componentFrames[componentOffset + (_animatedComponentCount * (frameCount - 1))];
                    componentOffset++;
                }
                else
                {
                    _totalDelta.Y = 0;
                }

                if ((_jointInfo[0].AnimationBits & AnimationBits.TranslationZ) == AnimationBits.TranslationZ)
                {
                    for (int i = 0; i < frameCount; i++)
                    {
                        _componentFrames[componentOffset + (_animatedComponentCount * i)] -= _baseFrame[0].Translation.Z;
                    }

                    _totalDelta.Z = _componentFrames[componentOffset + (_animatedComponentCount * (frameCount - 1))];
                }
                else
                {
                    _totalDelta.Z = 0;
                }
            }

            _baseFrame[0].Translation = Vector3.Zero;

            // we don't count last frame because it would cause a 1 frame pause at the end
            _animLength = ((frameCount - 1) * 1000 + _frameRate - 1) / _frameRate;

            // done
            return(true);
        }
コード例 #7
0
        public static idMapBrush ParseQ3(idLexer lexer, Vector3 origin)
        {
            int rotate;

            int[]   shift = new int[2];
            float[] scale = new float[2];

            Vector3[]             planePoints = new Vector3[3];
            List <idMapBrushSide> sides       = new List <idMapBrushSide>();
            idMapBrushSide        side;
            idToken token;

            do
            {
                if (lexer.CheckTokenString("}") == true)
                {
                    break;
                }

                side = new idMapBrushSide();
                sides.Add(side);

                // read the three point plane definition
                float[] tmp  = lexer.Parse1DMatrix(3);
                float[] tmp2 = lexer.Parse1DMatrix(3);
                float[] tmp3 = lexer.Parse1DMatrix(3);

                if ((tmp == null) || (tmp2 == null) || (tmp3 == null))
                {
                    lexer.Error("idMapBrush::ParseQ3: unable to read brush side plane definition");
                    return(null);
                }

                planePoints[0] = new Vector3(tmp[0], tmp[1], tmp[2]) - origin;
                planePoints[1] = new Vector3(tmp2[0], tmp2[1], tmp2[2]) - origin;
                planePoints[2] = new Vector3(tmp3[0], tmp3[1], tmp3[2]) - origin;

                side.Plane.FromPoints(planePoints[0], planePoints[1], planePoints[2]);

                // read the material
                token = lexer.ReadTokenOnLine();

                if (token == null)
                {
                    lexer.Error("idMapBrush::ParseQ3: unable to read brush side material");
                    return(null);
                }

                // we have an implicit 'textures/' in the old format
                side.Material = "textures/" + token.ToString();

                // read the texture shift, rotate and scale
                shift[0] = lexer.ParseInt();
                shift[1] = lexer.ParseInt();

                rotate = lexer.ParseInt();

                scale[0] = lexer.ParseFloat();
                scale[1] = lexer.ParseFloat();

                side.TextureMatrix[0] = new Vector3(0.03125f, 0.0f, 0.0f);
                side.TextureMatrix[1] = new Vector3(0.0f, 0.03125f, 0.0f);

                side.Origin = origin;

                // Q2 allowed override of default flags and values, but we don't any more
                if (lexer.ReadTokenOnLine() != null)
                {
                    if (lexer.ReadTokenOnLine() != null)
                    {
                        if (lexer.ReadTokenOnLine() != null)
                        {
                        }
                    }
                }
            }while(true);

            idMapBrush brush = new idMapBrush();

            for (int i = 0; i < sides.Count; i++)
            {
                brush.AddSide(sides[i]);
            }

            brush.Dict = new idDict();

            return(brush);
        }
コード例 #8
0
        public static idMapBrush Parse(idLexer lexer, Vector3 origin, bool newFormat = true, float version = idMapFile.CurrentMapVersion)
        {
            idToken               token;
            idMapBrushSide        side;
            List <idMapBrushSide> sides = new List <idMapBrushSide>();
            idDict dict = new idDict();

            Vector3[] planePoints = new Vector3[3];

            if (lexer.ExpectTokenString("{") == false)
            {
                return(null);
            }

            do
            {
                if ((token = lexer.ReadToken()) == null)
                {
                    lexer.Error("idMapBrush::Parse: unexpected EOF");
                    return(null);
                }

                if (token.ToString() == "}")
                {
                    break;
                }

                // here we may have to jump over brush epairs ( only used in editor )
                do
                {
                    // if token is a brace
                    if (token.ToString() == "(")
                    {
                        break;
                    }

                    // the token should be a key string for a key/value pair
                    if (token.Type != TokenType.String)
                    {
                        lexer.Error("idMapBrush::Parse: unexpected {0}, expected ( or epair key string", token.ToString());
                        return(null);
                    }


                    string key = token.ToString();

                    if (((token = lexer.ReadTokenOnLine()) == null) || (token.Type != TokenType.String))
                    {
                        lexer.Error("idMapBrush::Parse: expected epair value string not found");
                        return(null);
                    }

                    dict.Set(key, token.ToString());

                    // try to read the next key
                    if ((token = lexer.ReadToken()) == null)
                    {
                        lexer.Error("idMapBrush::Parse: unexpected EOF");
                        return(null);
                    }
                }while(true);

                lexer.UnreadToken = token;

                side = new idMapBrushSide();
                sides.Add(side);

                if (newFormat == true)
                {
                    float[] tmp = lexer.Parse1DMatrix(4);

                    if (tmp == null)
                    {
                        lexer.Error("idMapBrush::Parse: unable to read brush side plane definition");
                        return(null);
                    }
                    else
                    {
                        side.Plane = new Plane(tmp[0], tmp[1], tmp[2], tmp[3]);
                    }
                }
                else
                {
                    // read the three point plane definition
                    float[] tmp, tmp2, tmp3;

                    if (((tmp = lexer.Parse1DMatrix(3)) == null) ||
                        ((tmp2 = lexer.Parse1DMatrix(3)) == null) ||
                        ((tmp3 = lexer.Parse1DMatrix(3)) == null))
                    {
                        lexer.Error("idMapBrush::Parse: unable to read brush side plane definition");
                        return(null);
                    }

                    planePoints[0] = new Vector3(tmp[0], tmp[1], tmp[2]) - origin;
                    planePoints[1] = new Vector3(tmp2[0], tmp2[1], tmp2[2]) - origin;
                    planePoints[2] = new Vector3(tmp3[0], tmp3[1], tmp3[2]) - origin;

                    side.Plane.FromPoints(planePoints[0], planePoints[1], planePoints[2]);
                }

                // read the texture matrix
                // this is odd, because the texmat is 2D relative to default planar texture axis
                float[,] tmp5 = lexer.Parse2DMatrix(2, 3);

                if (tmp5 == null)
                {
                    lexer.Error("idMapBrush::Parse: unable to read brush side texture matrix");
                    return(null);
                }

                side.TextureMatrix[0] = new Vector3(tmp5[0, 0], tmp5[0, 1], tmp5[0, 2]);
                side.TextureMatrix[1] = new Vector3(tmp5[1, 0], tmp5[1, 1], tmp5[1, 2]);
                side.Origin           = origin;

                // read the material
                if ((token = lexer.ReadTokenOnLine()) == null)
                {
                    lexer.Error("idMapBrush::Parse: unable to read brush side material");
                    return(null);
                }

                // we had an implicit 'textures/' in the old format...
                if (version < 2.0f)
                {
                    side.Material = "textures/" + token.ToString();
                }
                else
                {
                    side.Material = token.ToString();
                }

                // Q2 allowed override of default flags and values, but we don't any more
                if (lexer.ReadTokenOnLine() != null)
                {
                    if (lexer.ReadTokenOnLine() != null)
                    {
                        if (lexer.ReadTokenOnLine() != null)
                        {
                        }
                    }
                }
            }while(true);

            if (lexer.ExpectTokenString("}") == false)
            {
                return(null);
            }

            idMapBrush brush = new idMapBrush();

            foreach (idMapBrushSide s in sides)
            {
                brush.AddSide(s);
            }

            brush.Dict = dict;

            return(brush);
        }
コード例 #9
0
        public static idMapPatch Parse(idLexer lexer, Vector3 origin, bool patchDef3 = true, float version = idMapFile.CurrentMapVersion)
        {
            if (lexer.ExpectTokenString("{") == false)
            {
                return(null);
            }

            // read the material (we had an implicit 'textures/' in the old format...)
            idToken token = lexer.ReadToken();

            if (token == null)
            {
                lexer.Error("idMapPatch::Parse: unexpected EOF");
                return(null);
            }

            // Parse it
            float[] info;

            if (patchDef3 == true)
            {
                info = lexer.Parse1DMatrix(7);

                if (info == null)
                {
                    lexer.Error("idMapPatch::Parse: unable to Parse patchDef3 info");
                    return(null);
                }
            }
            else
            {
                info = lexer.Parse1DMatrix(5);

                if (info == null)
                {
                    lexer.Error("idMapPatch::Parse: unable to parse patchDef2 info");
                    return(null);
                }
            }

            idMapPatch patch = new idMapPatch((int)info[0], (int)info[1]);

            if (version < 2.0f)
            {
                patch.Material = "textures/" + token.ToString();
            }
            else
            {
                patch.Material = token.ToString();
            }

            if (patchDef3 == true)
            {
                patch.HorizontalSubdivisions = (int)info[2];
                patch.VerticalSubdivisions   = (int)info[3];
                patch.ExplicitlySubdivided   = true;
            }

            if ((patch.Width < 0) || (patch.Height < 0))
            {
                lexer.Error("idMapPatch::Parse: bad size");
                return(null);
            }

            // these were written out in the wrong order, IMHO
            if (lexer.ExpectTokenString("(") == false)
            {
                lexer.Error("idMapPatch::Parse: bad patch vertex data");
                return(null);
            }

            for (int j = 0; j < patch.Width; j++)
            {
                if (lexer.ExpectTokenString("(") == false)
                {
                    lexer.Error("idMapPatch::Parse: bad vertex row data");
                    return(null);
                }

                for (int i = 0; i < patch.Height; i++)
                {
                    float[] v = lexer.Parse1DMatrix(5);

                    if (v == null)
                    {
                        lexer.Error("idMapPatch::Parse: bad vertex column data");
                        return(null);
                    }

                    Vertex vert = new Vertex();
                    vert.Position.X         = v[0] - origin.X;
                    vert.Position.Y         = v[1] - origin.Y;
                    vert.Position.Z         = v[2] - origin.Z;
                    vert.TextureCoordinates = new Vector2(v[3], v[4]);

                    patch.SetVertex(i * patch.Width + j, vert);
                }

                if (lexer.ExpectTokenString(")") == false)
                {
                    lexer.Error("idMapPatch::Parse: unable to parse patch control points");
                    return(null);
                }
            }

            if (lexer.ExpectTokenString(")") == false)
            {
                lexer.Error("idMapPatch::Parse: unable to parse patch control points, no closure");
                return(null);
            }

            // read any key/value pairs
            while ((token = lexer.ReadToken()) != null)
            {
                if (token.ToString() == "}")
                {
                    lexer.ExpectTokenString("}");
                    break;
                }

                if (token.Type == TokenType.String)
                {
                    string key = token.ToString();
                    token = lexer.ExpectTokenType(TokenType.String, 0);

                    patch.Dict.Set(key, token.ToString());
                }
            }

            return(patch);
        }
コード例 #10
0
        public override bool Parse(string text)
        {
            if (this.Disposed == true)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            idLexer lexer = new idLexer(idDeclFile.LexerOptions);

            lexer.LoadMemory(text, this.FileName, this.LineNumber);
            lexer.SkipUntilString("{");

            int     defaultAnimationCount = 0;
            idToken token;
            idToken token2;
            string  tokenValue;
            string  fileName;
            string  extension;
            int     count;

            idMD5Joint[] md5Joints;

            while (true)
            {
                if ((token = lexer.ReadToken()) == null)
                {
                    break;
                }

                tokenValue = token.ToString();

                if (tokenValue == "}")
                {
                    break;
                }

                if (tokenValue == "inherit")
                {
                    idConsole.WriteLine("TODO: inherit");

                    /*if( !src.ReadToken( &token2 ) ) {
                     *      src.Warning( "Unexpected end of file" );
                     *      MakeDefault();
                     *      return false;
                     * }
                     *
                     * const idDeclModelDef *copy = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, token2, false ) );
                     * if ( !copy ) {
                     *      common->Warning( "Unknown model definition '%s'", token2.c_str() );
                     * } else if ( copy->GetState() == DS_DEFAULTED ) {
                     *      common->Warning( "inherited model definition '%s' defaulted", token2.c_str() );
                     *      MakeDefault();
                     *      return false;
                     * } else {
                     *      CopyDecl( copy );
                     *      numDefaultAnims = anims.Num();
                     * }*/
                }
                else if (tokenValue == "skin")
                {
                    if ((token2 = lexer.ReadToken()) == null)
                    {
                        lexer.Warning("Unexpected end of file");
                        MakeDefault();

                        return(false);
                    }

                    _skin = idE.DeclManager.FindSkin(token2.ToString());

                    if (_skin == null)
                    {
                        lexer.Warning("Skin '{0}' not found", token2.ToString());
                        MakeDefault();

                        return(false);
                    }
                }
                else if (tokenValue == "mesh")
                {
                    if ((token2 = lexer.ReadToken()) == null)
                    {
                        lexer.Warning("Unexpected end of file");
                        MakeDefault();

                        return(false);
                    }

                    fileName  = token2.ToString();
                    extension = Path.GetExtension(fileName);

                    if (extension != idRenderModel_MD5.MeshExtension)
                    {
                        lexer.Warning("Invalid model for MD5 mesh");
                        MakeDefault();

                        return(false);
                    }

                    _model = idE.RenderModelManager.FindModel(fileName);

                    if (_model == null)
                    {
                        lexer.Warning("Model '{0}' not found", fileName);
                        MakeDefault();

                        return(false);
                    }
                    else if (_model.IsDefault == true)
                    {
                        lexer.Warning("Model '{0}' defaulted", fileName);
                        MakeDefault();

                        return(false);
                    }

                    // get the number of joints
                    count = _model.JointCount;

                    if (count == 0)
                    {
                        lexer.Warning("Model '{0}' has no joints", fileName);
                    }

                    // set up the joint hierarchy
                    md5Joints = _model.Joints;

                    _joints           = new JointInfo[count];
                    _jointParents     = new int[count];
                    _channelJoints    = new int[(int)AnimationChannel.Count][];
                    _channelJoints[0] = new int[count];

                    for (int i = 0; i < count; i++)
                    {
                        _joints[i]         = new JointInfo();
                        _joints[i].Channel = AnimationChannel.All;
                        _joints[i].Index   = i;

                        if (md5Joints[i].Parent != null)
                        {
                            _joints[i].ParentIndex = _model.GetJointIndex(md5Joints[i].Parent);
                        }
                        else
                        {
                            _joints[i].ParentIndex = -1;
                        }

                        _jointParents[i]     = _joints[i].ParentIndex;
                        _channelJoints[0][i] = i;
                    }
                }
                else if (tokenValue == "remove")
                {
                    idConsole.Warning("TODO: remove");

                    // removes any anims whos name matches

                    /*if( !src.ReadToken( &token2 ) ) {
                     *      src.Warning( "Unexpected end of file" );
                     *      MakeDefault();
                     *      return false;
                     * }
                     * num = 0;
                     * for( i = 0; i < anims.Num(); i++ ) {
                     *      if ( ( token2 == anims[ i ]->Name() ) || ( token2 == anims[ i ]->FullName() ) ) {
                     *              delete anims[ i ];
                     *              anims.RemoveIndex( i );
                     *              if ( i >= numDefaultAnims ) {
                     *                      src.Warning( "Anim '%s' was not inherited.  Anim should be removed from the model def.", token2.c_str() );
                     *                      MakeDefault();
                     *                      return false;
                     *              }
                     *              i--;
                     *              numDefaultAnims--;
                     *              num++;
                     *              continue;
                     *      }
                     * }
                     * if ( !num ) {
                     *      src.Warning( "Couldn't find anim '%s' to remove", token2.c_str() );
                     *      MakeDefault();
                     *      return false;
                     * }*/
                }
                else if (tokenValue == "anim")
                {
                    if (_model == null)
                    {
                        lexer.Warning("Must specify mesh before defining anims");
                        MakeDefault();

                        return(false);
                    }
                    else if (ParseAnimation(lexer, defaultAnimationCount) == false)
                    {
                        MakeDefault();

                        return(false);
                    }
                }
                else if (tokenValue == "offset")
                {
                    float[] tmp = lexer.Parse1DMatrix(3);

                    if (tmp == null)
                    {
                        lexer.Warning("Expected vector following 'offset'");
                        MakeDefault();
                        return(false);
                    }

                    _offset = new Vector3(tmp[0], tmp[1], tmp[2]);
                }
                else if (tokenValue == "channel")
                {
                    if (_model == null)
                    {
                        lexer.Warning("Must specify mesh before defining channels");
                        MakeDefault();

                        return(false);
                    }

                    // set the channel for a group of joints
                    if ((token2 = lexer.ReadToken()) == null)
                    {
                        lexer.Warning("Unexpected end of file");
                        MakeDefault();

                        return(false);
                    }

                    if (lexer.CheckTokenString("(") == false)
                    {
                        lexer.Warning("Expected { after '{0}'", token2.ToString());
                        MakeDefault();

                        return(false);
                    }

                    int i;
                    int channelCount = (int)AnimationChannel.Count;

                    for (i = (int)AnimationChannel.All + 1; i < channelCount; i++)
                    {
                        if (ChannelNames[i].Equals(token2.ToString(), StringComparison.OrdinalIgnoreCase) == true)
                        {
                            break;
                        }
                    }

                    if (i >= channelCount)
                    {
                        lexer.Warning("Unknown channel '{0}'", token2.ToString());
                        MakeDefault();

                        return(false);
                    }

                    int           channel    = i;
                    StringBuilder jointNames = new StringBuilder();
                    string        token2Value;

                    while (lexer.CheckTokenString(")") == false)
                    {
                        if ((token2 = lexer.ReadToken()) == null)
                        {
                            lexer.Warning("Unexpected end of file");
                            MakeDefault();

                            return(false);
                        }

                        token2Value = token2.ToString();
                        jointNames.Append(token2Value);

                        if ((token2Value != "*") && (token2Value != "-"))
                        {
                            jointNames.Append(" ");
                        }
                    }

                    int[] jointList   = GetJointList(jointNames.ToString());
                    int   jointLength = jointList.Length;

                    List <int> channelJoints = new List <int>();

                    for (count = i = 0; i < jointLength; i++)
                    {
                        int jointIndex = jointList[i];

                        if (_joints[jointIndex].Channel != AnimationChannel.All)
                        {
                            lexer.Warning("Join '{0}' assigned to multiple channels", _model.GetJointName(jointIndex));
                            continue;
                        }

                        _joints[jointIndex].Channel = (AnimationChannel)channel;
                        channelJoints.Add(jointIndex);
                    }

                    _channelJoints[channel] = channelJoints.ToArray();
                }
                else
                {
                    lexer.Warning("unknown token '{0}'", token.ToString());
                    MakeDefault();

                    return(false);
                }
            }

            return(true);
        }