示例#1
0
        public void AddTextureCoordinates()
        {
            bool texCoordsAdded = false;

            foreach (var face in this.Meshes.SelectMany(t => t.FaceGroups).SelectMany(t => t.Faces))
            {
                ObjIndex verticesIndex = face.VerticesIndex;
                ObjIndex texCoord      = face.VertexTexCoordsIndex;

                if (texCoord.A < 0 || texCoord.B < 0 || texCoord.C < 0 || (verticesIndex.D >= 0 && texCoord.D < 0))
                {
                    if (!texCoordsAdded)
                    {
                        this.VertexTexCoords.Add(new ObjVector2(0, 0));
                        this.VertexTexCoords.Add(new ObjVector2(1, 0));
                        this.VertexTexCoords.Add(new ObjVector2(1, 1));
                        this.VertexTexCoords.Add(new ObjVector2(0, 1));
                        texCoordsAdded = true;
                    }

                    texCoord.A = this.VertexTexCoords.Count - 4;
                    texCoord.B = this.VertexTexCoords.Count - 3;
                    texCoord.C = this.VertexTexCoords.Count - 2;
                    texCoord.D = verticesIndex.D < 0 ? -1 : this.VertexTexCoords.Count - 1;

                    face.VertexTexCoordsIndex = texCoord;
                }
            }
        }
示例#2
0
        public void AddVertexNormals()
        {
            foreach (var face in this.Meshes.SelectMany(t => t.FaceGroups).SelectMany(t => t.Faces))
            {
                ObjIndex verticesIndex      = face.VerticesIndex;
                ObjIndex vertexNormalsIndex = face.VertexNormalsIndex;

                if (vertexNormalsIndex.A < 0 || vertexNormalsIndex.B < 0 || vertexNormalsIndex.C < 0 || (verticesIndex.D >= 0 && vertexNormalsIndex.D < 0))
                {
                    ObjVector3 normal = ObjVector3.Normal(
                        this.Vertices.ElementAtOrDefault(verticesIndex.A),
                        this.Vertices.ElementAtOrDefault(verticesIndex.B),
                        this.Vertices.ElementAtOrDefault(verticesIndex.C));

                    this.VertexNormals.Add(normal);

                    vertexNormalsIndex.A = this.VertexNormals.Count - 1;
                    vertexNormalsIndex.B = this.VertexNormals.Count - 1;
                    vertexNormalsIndex.C = this.VertexNormals.Count - 1;
                    vertexNormalsIndex.D = verticesIndex.D < 0 ? -1 : this.VertexNormals.Count - 1;

                    face.VertexNormalsIndex = vertexNormalsIndex;
                }
            }
        }
示例#3
0
        private void CompactVertexNormalsBuffer()
        {
            if (this.VertexNormals.Count == 0)
            {
                return;
            }

            bool[] isUsed = new bool[this.VertexNormals.Count];

            foreach (int i in this.Meshes
                     .SelectMany(t => t.FaceGroups)
                     .SelectMany(t => t.Faces)
                     .Select(t => t.VertexNormalsIndex)
                     .SelectMany(t => new int[] { t.A, t.B, t.C, t.D }))
            {
                if (i >= 0 && i < this.VertexNormals.Count)
                {
                    isUsed[i] = true;
                }
            }

            List <ObjVector3> newValues = new List <ObjVector3>(this.VertexNormals.Count);

            int[] newIndices = new int[this.VertexNormals.Count];

            for (int i = 0; i < this.VertexNormals.Count; i++)
            {
                if (!isUsed[i])
                {
                    continue;
                }

                ObjVector3 value = this.VertexNormals[i];
                int        index = -1;

                for (int j = 0; j < newValues.Count; j++)
                {
                    if (newValues[j] == value)
                    {
                        index = j;
                        break;
                    }
                }

                if (index == -1)
                {
                    newIndices[i] = newValues.Count;
                    newValues.Add(value);
                }
                else
                {
                    newIndices[i] = index;
                }
            }

            newValues.TrimExcess();

            this.VertexNormals = newValues;

            foreach (var face in this.Meshes
                     .SelectMany(t => t.FaceGroups)
                     .SelectMany(t => t.Faces))
            {
                ObjIndex index = face.VertexNormalsIndex;

                face.VertexNormalsIndex = new ObjIndex(
                    index.A < 0 ? -1 : newIndices[index.A],
                    index.B < 0 ? -1 : newIndices[index.B],
                    index.C < 0 ? -1 : newIndices[index.C],
                    index.D < 0 ? -1 : newIndices[index.D]);
            }
        }