Esempio n. 1
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);
        }
Esempio n. 2
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);
		}