Ejemplo n.º 1
0
        private static List <MeshPartInfo> LoadHeadMeshes(ObjItem objModel, float scale, ref int lastTriangle)
        {
            var result                   = new List <MeshPartInfo>();
            var vertexPositions          = new List <float>();
            var vertexNormals            = new List <float>();
            var vertexTextureCoordinates = new List <float>();
            var vertexBoneIndices        = new List <float>();
            var vertexBoneWeights        = new List <float>();
            var indeces                  = new List <uint>();

            var indexer = 0;

            foreach (var modelGroup in objModel.Groups)                                                                         // one group - one mesh
            {
                if (modelGroup.Key.Name == "Tear" || modelGroup.Key.Name == "Cornea" || modelGroup.Key.Name == "EyeReflection") // очень плохие материалы. ИЗ-за них ломаются глазки.
                {
                    continue;
                }

                vertexPositions.Clear();
                vertexNormals.Clear();
                vertexTextureCoordinates.Clear();
                vertexBoneIndices.Clear();
                vertexBoneWeights.Clear();
                indeces.Clear();

                foreach (var face in modelGroup.Value.Faces)          //  combine all meshes in group - to one mesh.
                {
                    GetObjFace(face, objModel, ref vertexPositions, ref vertexNormals, ref vertexTextureCoordinates, ref vertexBoneWeights, ref vertexBoneIndices, ref indeces, ref lastTriangle);
                }

                var positions = new List <Vector3>();
                var texCoords = new List <Vector2>();
                var index     = 0;
                for (var i = 0; i < vertexPositions.Count / 3; ++i)
                {
                    index = i * 3;
                    positions.Add(new Vector3(vertexPositions[index], vertexPositions[index + 1], vertexPositions[index + 2]));
                    texCoords.Add(new Vector2(vertexTextureCoordinates[i * 2], 1.0f - vertexTextureCoordinates[i * 2 + 1]));
                }



                var meshPartInfo = new MeshPartInfo
                {
                    VertexPositions = GetScaledVertices(positions, scale),
                    MaterialName    = modelGroup.Key.Name,
                    TextureCoords   = texCoords,
                    PartName        = modelGroup.Key.Name == "default" ? string.Empty : modelGroup.Key.Name,
                    Color           =
                        new Vector4(modelGroup.Key.DiffuseColor.X, modelGroup.Key.DiffuseColor.Y,
                                    modelGroup.Key.DiffuseColor.Z, modelGroup.Key.Transparency),
                    Texture                = modelGroup.Key.Texture,
                    TransparentTexture     = modelGroup.Key.TransparentTexture,
                    TextureName            = modelGroup.Key.DiffuseTextureMap,
                    TransparentTextureName = modelGroup.Key.TransparentTextureMap
                };

                result.Add(meshPartInfo);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public bool Create(MeshPartInfo info, Vector3 Offset)
        {
            if (info.VertexPositions.Count == 0)
            {
                return(false);
            }

            Color              = info.Color;
            Texture            = info.Texture;
            TransparentTexture = info.TransparentTexture;

            Indices.Clear();
            var positions = new List <Vector3>();
            var texCoords = new List <Vector2>();

            var pointnsDict   = new Dictionary <VertexInfo, int>(new VectorEqualityComparer());
            var positionsDict = new Dictionary <Vector3, int>(new VectorEqualityComparer());

            for (var i = 0; i < info.VertexPositions.Count; i++)
            {
                var vertexInfo = new VertexInfo
                {
                    Position  = info.VertexPositions[i] + Offset,
                    TexCoords = info.TextureCoords[i]
                };
                if (!pointnsDict.ContainsKey(vertexInfo))
                {
                    var index = positions.Count;

                    if (!positionsDict.ContainsKey(vertexInfo.Position))
                    {
                        positionsDict.Add(vertexInfo.Position, MorphPoints.Count);
                        PointIndices.Add(MorphPoints.Count);
                        MorphPoints.Add(new MorphingPoint
                        {
                            Indices = new List <int> {
                                index
                            },
                            Position = vertexInfo.Position
                        });
                    }
                    else
                    {
                        var id = positionsDict[vertexInfo.Position];
                        PointIndices.Add(id);
                        MorphPoints[id].Indices.Add(index);
                    }

                    pointnsDict.Add(vertexInfo, index);
                    Indices.Add((uint)index);
                    positions.Add(vertexInfo.Position);
                    texCoords.Add(vertexInfo.TexCoords);
                }
                else
                {
                    PointIndices.Add(positionsDict[vertexInfo.Position]);
                    Indices.Add((uint)pointnsDict[vertexInfo]);
                }
            }

            CountIndices = Indices.Count;
            Vertices     = new Vertex3d[positions.Count];

            var normals = Normal.CalculateNormals(positions, Indices);

            for (var i = 0; i < Vertices.Length; i++)
            {
                Vertices[i].OriginalPosition = Vertices[i].Position = positions[i];
                Vertices[i].Normal           = normals[i];
                Vertices[i].TexCoord         = texCoords[i];
                Vertices[i].Color            = Vector4.One;
            }

            Destroy();
            GL.GenBuffers(1, out VertexBuffer);
            GL.GenBuffers(1, out IndexBuffer);

            return(true);
        }