Exemple #1
0
 public RawModel(int _vaoID, int _vertexCount, List <int> _vboIDs, LoaderVertex data, string modelName)
 {
     vaoID        = _vaoID;
     vertexCount  = _vertexCount;
     vboIDs       = _vboIDs;
     modelData    = data;
     rawModelName = modelName;
 }
Exemple #2
0
        public static RawModel LoadToVAO(LoaderVertex vertexData, string vertexName)
        {
            if (rawModelTable.TryGetValue(vertexName, out var rawModel))
            {
                return(rawModel);
            }

            int        vaoID  = CreateVAO();
            List <int> vboIDs = new List <int>();

            BindIndicesBuffer(vertexData.indices);
            vboIDs.Add(FloatsToAttribute(0, 3, vertexData.positions));
            vboIDs.Add(FloatsToAttribute(1, 2, vertexData.textureCoords));
            vboIDs.Add(FloatsToAttribute(2, 3, vertexData.normals));

            UnbindVAO();

            var model = new RawModel(vaoID, vertexData.indices.Length, vboIDs, vertexData, vertexName);

            rawModelTable.Add(vertexName, model);

            return(model);
        }
Exemple #3
0
        public static LoaderVertex LoadOBJModel(string fileName)
        {
            LoaderVertex model = null;

            if (table.Count != 0)
            {
                if (fileName == null)
                {
                    table.TryGetValue("default_model", out model);
                }
                else
                {
                    table.TryGetValue(fileName, out model);
                }
            }
            if (model == null)
            {
                try
                {
                    if (fileName == null)
                    {
                        table.Add("default_model", Geometry.GetVertex(Geometries.Cube));
                        return(Geometry.GetVertex(Geometries.Cube));
                    }

                    var reader = new StringReader(Resources.GetFile(fileName));

                    var            line          = "Start";
                    List <Vertex>  vertices      = new List <Vertex>();
                    List <Vector2> textureCoords = new List <Vector2>();
                    List <Vector3> normals       = new List <Vector3>();
                    List <int>     indices       = new List <int>();

                    while (reader.Peek() != -1)
                    {
                        if (line != null && line.Length != 0)
                        {
                            if (line.Contains("  "))
                            {
                                line = line.Replace("  ", " ");
                            }
                            string[] currentLine = line.Split(" ");
                            if (line.StartsWith("v "))
                            {
                                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));
                                Vertex  newVertex = new Vertex(vertices.Count, vertex);
                                vertices.Add(newVertex);
                            }
                            else if (line.StartsWith("vt "))
                            {
                                Vector2 textureCoord = new Vector2(float.Parse(currentLine[1], CultureInfo.InvariantCulture.NumberFormat), float.Parse(currentLine[2], CultureInfo.InvariantCulture.NumberFormat));
                                textureCoords.Add(textureCoord);
                            }
                            else if (line.StartsWith("vn "))
                            {
                                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);
                            }
                        }
                        line = reader.ReadLine();
                    }

                    reader = new StringReader(Resources.GetFile(fileName));

                    while (reader.Peek() != -1)
                    {
                        if (line != null && line.Length != 0)
                        {
                            if (line.Contains("  "))
                            {
                                line = line.Replace("  ", " ");
                            }
                            if (line.StartsWith("f "))
                            {
                                string[] currentLine = line.Split(" ");
                                string[] vertex1     = currentLine[1].Split("/");
                                string[] vertex2     = currentLine[2].Split("/");
                                string[] vertex3     = currentLine[3].Split("/");
                                ProcessVertex(vertex1, vertices, indices);
                                ProcessVertex(vertex2, vertices, indices);
                                ProcessVertex(vertex3, vertices, indices);
                            }
                        }
                        line = reader.ReadLine();
                    }
                    reader.Close();
                    RemoveUnusedVertices(vertices);
                    float[] verticesArray      = new float[vertices.Count * 3];
                    float[] textureCoordsArray = new float[vertices.Count * 2];
                    float[] normalsArray       = new float[vertices.Count * 3];
                    float   furthest           = ConvertDataToArrays(vertices, textureCoords, normals, verticesArray, textureCoordsArray, normalsArray);
                    int[]   indicesArray       = ConvertIndicesListToArray(indices);

                    int vertexPointer = 0;
                    foreach (Vertex vertex in vertices)
                    {
                        verticesArray[vertexPointer++] = vertex.position.X;
                        verticesArray[vertexPointer++] = vertex.position.Y;
                        verticesArray[vertexPointer++] = vertex.position.Z;
                    }
                    for (int i = 0; i < indices.Count; i++)
                    {
                        indicesArray[i] = indices[i];
                    }

                    LoaderVertex data = new LoaderVertex(verticesArray, textureCoordsArray, normalsArray, indicesArray, furthest);
                    table.Add(fileName, data);
                    return(data);
                }
                catch (FileNotFoundException e)
                {
                    Console.WriteLine(e.Message);
                    return(Geometry.GetVertex(Geometries.Cube));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return(Geometry.GetVertex(Geometries.Cube));
                }
            }
            else
            {
                return(model);
            }
        }