Example #1
0
        public VertexNM Duplicate(int newIndex)
        {
            VertexNM vertex = new VertexNM(newIndex, Position);

            vertex.tangents = tangents;
            return(vertex);
        }
Example #2
0
 private static VertexNM DealWithAlreadyProcessedVertex(VertexNM previousVertex, int newTextureIndex,
                                                        int newNormalIndex, List <int> indices, List <VertexNM> vertices)
 {
     if (previousVertex.HasSameTextureAndNormal(newTextureIndex, newNormalIndex))
     {
         indices.Add(previousVertex.Index);
         return(previousVertex);
     }
     else
     {
         VertexNM anotherVertex = previousVertex.DuplicateVertex;
         if (anotherVertex != null)
         {
             return(DealWithAlreadyProcessedVertex(anotherVertex, newTextureIndex,
                                                   newNormalIndex, indices, vertices));
         }
         else
         {
             VertexNM duplicateVertex = previousVertex.Duplicate(vertices.Count);
             duplicateVertex.TextureIndex   = newTextureIndex;
             duplicateVertex.NormalIndex    = newNormalIndex;
             previousVertex.DuplicateVertex = duplicateVertex;
             vertices.Add(duplicateVertex);
             indices.Add(duplicateVertex.Index);
             return(duplicateVertex);
         }
     }
 }
Example #3
0
        private static float ConvertDataToArrays(List <VertexNM> vertices, List <Vector2> textures, List <Vector3> normals, float[] verticesArray, float[] texturesArray, float[] normalsArray, float[] tangentsArray)
        {
            float furthestPoint = 0;

            for (int i = 0; i < vertices.Count; i++)
            {
                VertexNM currentVertex = vertices[i];
                if ((currentVertex.Length) > furthestPoint)
                {
                    furthestPoint = currentVertex.Length;
                }
                Vector3 position     = currentVertex.Position;
                Vector2 textureCoord = textures[currentVertex.TextureIndex];
                Vector3 normalVector = normals[currentVertex.NormalIndex];
                Vector3 tangent      = currentVertex.AveragedTangent;
                verticesArray[i * 3]     = position.X;
                verticesArray[i * 3 + 1] = position.Y;
                verticesArray[i * 3 + 2] = position.Z;
                texturesArray[i * 2]     = textureCoord.X;
                texturesArray[i * 2 + 1] = 1 - textureCoord.Y;
                normalsArray[i * 3]      = normalVector.X;
                normalsArray[i * 3 + 1]  = normalVector.Y;
                normalsArray[i * 3 + 2]  = normalVector.Z;
                tangentsArray[i * 3]     = tangent.X;
                tangentsArray[i * 3 + 1] = tangent.Y;
                tangentsArray[i * 3 + 2] = tangent.Z;
            }
            return(furthestPoint);
        }
Example #4
0
        //NEW
        private static void calculateTangents(VertexNM v0, VertexNM v1, VertexNM v2, List <Vector2> textures)
        {
            Vector3 delatPos1 = v1.Position - v0.Position;
            Vector3 delatPos2 = v2.Position - v0.Position;
            Vector2 uv0       = textures[v0.TextureIndex];
            Vector2 uv1       = textures[v1.TextureIndex];
            Vector2 uv2       = textures[v2.TextureIndex];
            Vector2 deltaUv1  = uv1 - uv0;
            Vector2 deltaUv2  = uv2 - uv0;

            float r = 1.0f / (deltaUv1.X * deltaUv2.Y - deltaUv1.Y * deltaUv2.X);

            delatPos1 *= deltaUv2.Y;
            delatPos2 *= deltaUv1.Y;
            Vector3 tangent = delatPos1 - delatPos2;

            tangent *= r;
            v0.AddTangent(tangent);
            v1.AddTangent(tangent);
            v2.AddTangent(tangent);
        }
Example #5
0
        private static VertexNM processVertex(string[] vertex, List <VertexNM> vertices, List <int> indices)
        {
            int.TryParse(vertex[0], out int index);
            index--;
            VertexNM currentVertex = vertices[index];

            int.TryParse(vertex[1], out int textureIndex);
            textureIndex--;
            int.TryParse(vertex[2], out int normalIndex);
            normalIndex--;
            if (!currentVertex.IsSet())
            {
                currentVertex.TextureIndex = textureIndex;
                currentVertex.NormalIndex  = normalIndex;
                indices.Add(index);
                return(currentVertex);
            }
            else
            {
                return(DealWithAlreadyProcessedVertex(currentVertex, textureIndex, normalIndex, indices,
                                                      vertices));
            }
        }
Example #6
0
        public static RawModel LoadOBJ(string objFileName, Loader loader)
        {
            string          line;
            List <VertexNM> vertices = new List <VertexNM>();
            List <Vector2>  textures = new List <Vector2>();
            List <Vector3>  normals  = new List <Vector3>();
            List <int>      indices  = new List <int>();

            using (StreamReader sr = new StreamReader($"Models/{objFileName}"))
            {
                while (true)
                {
                    line = sr.ReadLine();
                    if (line.StartsWith("v "))
                    {
                        string[] currentLine = line.Split(' ');
                        Vector3  vertex      = new Vector3(float.Parse(currentLine[1], CultureInfo.InvariantCulture.NumberFormat),
                                                           float.Parse(currentLine[2], CultureInfo.InvariantCulture.NumberFormat),
                                                           float.Parse(currentLine[3], CultureInfo.InvariantCulture.NumberFormat));
                        VertexNM newVertex = new VertexNM(vertices.Count, vertex);
                        vertices.Add(newVertex);
                    }
                    else if (line.StartsWith("vt "))
                    {
                        string[] currentLine = line.Split(' ');
                        Vector2  texture     = new Vector2(float.Parse(currentLine[1], CultureInfo.InvariantCulture.NumberFormat),
                                                           float.Parse(currentLine[2], CultureInfo.InvariantCulture.NumberFormat));
                        textures.Add(texture);
                    }
                    else if (line.StartsWith("vn "))
                    {
                        string[] currentLine = line.Split(' ');
                        Vector3  normal      = new Vector3(float.Parse(currentLine[1], CultureInfo.InvariantCulture.NumberFormat),
                                                           float.Parse(currentLine[2], CultureInfo.InvariantCulture.NumberFormat),
                                                           float.Parse(currentLine[3], CultureInfo.InvariantCulture.NumberFormat));
                        normals.Add(normal);
                    }
                    else if (line.StartsWith("f "))
                    {
                        break;
                    }
                }
                while (line != null && line.StartsWith("f "))
                {
                    String[] currentLine = line.Split(' ');
                    String[] vertex1     = currentLine[1].Split('/');
                    String[] vertex2     = currentLine[2].Split('/');
                    String[] vertex3     = currentLine[3].Split('/');
                    VertexNM v0          = processVertex(vertex1, vertices, indices);
                    VertexNM v1          = processVertex(vertex2, vertices, indices);
                    VertexNM v2          = processVertex(vertex3, vertices, indices);
                    calculateTangents(v0, v1, v2, textures);
                    line = sr.ReadLine();
                }
            }

            RemoveUnusedVertices(vertices);
            float[] verticesArray = new float[vertices.Count * 3];
            float[] texturesArray = new float[vertices.Count * 2];
            float[] normalsArray  = new float[vertices.Count * 3];
            float[] tangentsArray = new float[vertices.Count * 3];
            float   furthest      = ConvertDataToArrays(vertices, textures, normals, verticesArray, texturesArray, normalsArray, tangentsArray);

            uint[] indicesArray = ConvertIndicesListToArray(indices);

            return(loader.LoadToVao(verticesArray, texturesArray, normalsArray, tangentsArray, indicesArray));
        }