Skip to main content
edited title
Link

ObjFile loading not properly loading

deleted 30 characters in body
Source Link

i am losing my mind. I am trying to create an obj parser has a way to learn opengl but i have ran into a brick wall. with DMGregory help i can seem to getgot the textures to map to work i think but know the mesh correctly. its been two weeks if anyone here knows the answer please tell me :)just looks like a blob

i am losing my mind. I am trying to create an obj parser has a way to learn opengl but i have ran into a brick wall. i can seem to get textures to map to the mesh correctly. its been two weeks if anyone here knows the answer please tell me :)

i am losing my mind. I am trying to create an obj parser has a way to learn opengl but i have ran into a brick wall. with DMGregory help i got the textures to work i think but know the mesh just looks like a blob

modified code has DMGregory instructed
Source Link

What HappensHmmm

using OpenTK.Mathematics;
using System.Runtime.InteropServices;

using Core.Common.Objects;

namespace Core.Common
{
    public class LoadModel
    {
        public (List<Vector3> verts, List<Vector2>vertex[] texvertices, int[] indices) LoadObj(string path)
        {
            List<Vector3> Positions = new List<Vector3>();
            List<Vector2> texCoords = new List<Vector2>();


            List<int> position_Indices = new List<int>();
            List<int> texture_Indices = new List<int>();


            int[] indicesArray = null;
            vertex[] vertices = null;

            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("loading " + path + " ... ");
            Console.ResetColor();

            string[] lines = File.ReadAllLines(path);

            foreach (string line in lines)
            {
                string[] parts = line.Split(' ');

                switch (parts[0])
                {
                    case "v":
                        Vector3 pos = new Vector3(Convert.ToSingle(parts[1]), Convert.ToSingle(parts[2]), Convert.ToSingle(parts[3]));
                        Positions.Add(pos);
                        break;

                    case "vt":
                        Vector2 texture = new Vector2(Convertfloat.ToSingleParse(parts[1]), Convertfloat.ToSingleParse(parts[2]));
                        texCoords.Add(texture);
                        break;

                    case "f":
                        string[] vertex1 = parts[1].Split('/');
                        string[] vertex2 = parts[2].Split('/');
                        string[] vertex3 = parts[3].Split('/');

                        int index1 = Int32int.Parse(vertex1[0]) - 1;
                        int index2 = Int32int.Parse(vertex2[0]) - 1;
                        int index3 = Int32int.Parse(vertex3[0]) - 1;

                        int texIndex1 = Int32.Parse(vertex1[1]) - 1;
                        int texIndex2 = Int32.Parse(vertex2[1]) - 1;
                        int texIndex3 = Int32.Parse(vertex3[1]) - 1;

                        position_Indices.Add(index1);
                        position_Indices.Add(index2);
                        position_Indices.Add(index3);

                        texture_Indices.Add(texIndex1);
                        texture_Indices.Add(texIndex2);
                        texture_Indices.Add(texIndex3);



                        break;
                }
            }
            
            // bellow is modified\\

            indicesArray = new int[position_Indices.Count];
            vertices = new vertex[position_Indices.Count];

            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].pos = Positions[position_Indices[i]];
                vertices[i].tex = texCoords[texture_Indices[i]];
            }

            for (int i = 0; i < position_Indices.Count; i++)
            {
                indicesArray[i] = position_Indices[i];i;
            }

            foreach (var item in vertices)
            return{
                Console.WriteLine(Positions,item.pos);
 texCoords           }


            return (vertices, indicesArray); //return modeldata and faces
        }




    }
}
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using System.Runtime.InteropServices;


using Core.Common.Objects;
using Core.Render;

namespace Core.Common
{
    public class Loader
    {
        private List<int> vbos = new List<int>();
        private List<int> vaos = new List<int>();
    

        public RawModel loadModel(List<Vector3>vertex[] verts, List<Vector2> tex, int[] indices, string texPath)
        {
            int vao = createVAO();
            vbos.Add(vao);
            bindIndicesBuffer(indices);
            storeDataInPosAttribList(0, 3, verts, vertex.Stride);
            storeDataInTexAttribList(1, 2, texverts, vertex.Stride);
            loadTexture(texPath);
            unbindVAO();
            return new RawModel(vao, indices.Length);
        }

        private int createVAO()
        {
            int vao = GL.GenVertexArray();
            GL.BindVertexArray(vao);
            return vao;
        }

        private void loadTexture(string path)
        {
            Texture texture;
            texture = Texture.LoadFromFile(path);
            texture.Use(TextureUnit.Texture0);
        }
        
        private void storeDataInPosAttribList(int attribNum, int size, List<Vector3>vertex[]  verts, int stride)
        {
            int vbo = GL.GenBuffer();
            vbos.Add(vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            // bellow is modified\\
            GL.BufferData(BufferTarget.ArrayBuffer, stride * Vector3dVector3.SizeInBytes, vertsref verts[0].ToArray()pos, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(attribNum, size, VertexAttribPointerType.Float, false, 0, 0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }

        private void storeDataInTexAttribList(int attribNum, int size, List<Vector2>vertex[] tex, int stride)
        {
            int vbo = GL.GenBuffer();
            vbos.Add(vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            // bellow is modified\\
            GL.BufferData(BufferTarget.ArrayBuffer, stride * Vector2.SizeInBytes, texref tex[0].ToArray()tex, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(attribNum, size, VertexAttribPointerType.Float, false, 0, 0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }

        private void unbindVAO()
        {
            GL.BindVertexArray(0);
        }

        private void bindIndicesBuffer(int[] indices)
        {
            int vbo = GL.GenBuffer();
            vbos.Add(vbo);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo);
            GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(int), indices, BufferUsageHint.StaticDraw);
        }



        public void cleanUP()
        {
            foreach (var item in vaos)
            {
                GL.DeleteVertexArray(item);
            }

            foreach (var item in vbos)
            {
                GL.DeleteBuffer(item);
            }

        }

    }
}

What Happens

using OpenTK.Mathematics;
using System.Runtime.InteropServices;

using Core.Common.Objects;

namespace Core.Common
{
    public class LoadModel
    {
        public (List<Vector3> verts, List<Vector2> tex, int[] indices) LoadObj(string path)
        {
            List<Vector3> Positions = new List<Vector3>();
            List<Vector2> texCoords = new List<Vector2>();


            List<int> position_Indices = new List<int>();
            List<int> texture_Indices = new List<int>();


            int[] indicesArray = null;
            vertex[] vertices = null;

            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("loading " + path + " ... ");
            Console.ResetColor();

            string[] lines = File.ReadAllLines(path);

            foreach (string line in lines)
            {
                string[] parts = line.Split(' ');

                switch (parts[0])
                {
                    case "v":
                        Vector3 pos = new Vector3(Convert.ToSingle(parts[1]), Convert.ToSingle(parts[2]), Convert.ToSingle(parts[3]));
                        Positions.Add(pos);
                        break;

                    case "vt":
                        Vector2 texture = new Vector2(Convert.ToSingle(parts[1]), Convert.ToSingle(parts[2]));
                        texCoords.Add(texture);
                        break;

                    case "f":
                        string[] vertex1 = parts[1].Split('/');
                        string[] vertex2 = parts[2].Split('/');
                        string[] vertex3 = parts[3].Split('/');

                        int index1 = Int32.Parse(vertex1[0]) - 1;
                        int index2 = Int32.Parse(vertex2[0]) - 1;
                        int index3 = Int32.Parse(vertex3[0]) - 1;

                        int texIndex1 = Int32.Parse(vertex1[1]) - 1;
                        int texIndex2 = Int32.Parse(vertex2[1]) - 1;
                        int texIndex3 = Int32.Parse(vertex3[1]) - 1;

                        position_Indices.Add(index1);
                        position_Indices.Add(index2);
                        position_Indices.Add(index3);

                        texture_Indices.Add(texIndex1);
                        texture_Indices.Add(texIndex2);
                        texture_Indices.Add(texIndex3);



                        break;
                }
            }

            indicesArray = new int[position_Indices.Count];
            vertices = new vertex[position_Indices.Count];

            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].pos = Positions[position_Indices[i]];
                vertices[i].tex = texCoords[texture_Indices[i]];
            }

            for (int i = 0; i < position_Indices.Count; i++)
            {
                indicesArray[i] = position_Indices[i];
            }


            return (Positions, texCoords, indicesArray); //return modeldata and faces
        }




    }
}
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using System.Runtime.InteropServices;


using Core.Common.Objects;
using Core.Render;

namespace Core.Common
{
    public class Loader
    {
        private List<int> vbos = new List<int>();
        private List<int> vaos = new List<int>();
    

        public RawModel loadModel(List<Vector3> verts, List<Vector2> tex, int[] indices, string texPath)
        {
            int vao = createVAO();
            vbos.Add(vao);
            bindIndicesBuffer(indices);
            storeDataInPosAttribList(0, 3, verts, vertex.Stride);
            storeDataInTexAttribList(1, 2, tex, vertex.Stride);
            loadTexture(texPath);
            unbindVAO();
            return new RawModel(vao, indices.Length);
        }

        private int createVAO()
        {
            int vao = GL.GenVertexArray();
            GL.BindVertexArray(vao);
            return vao;
        }

        private void loadTexture(string path)
        {
            Texture texture;
            texture = Texture.LoadFromFile(path);
            texture.Use(TextureUnit.Texture0);
        }
        
        private void storeDataInPosAttribList(int attribNum, int size, List<Vector3> verts, int stride)
        {
            int vbo = GL.GenBuffer();
            vbos.Add(vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.BufferData(BufferTarget.ArrayBuffer, stride * Vector3d.SizeInBytes, verts.ToArray(), BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(attribNum, size, VertexAttribPointerType.Float, false, 0, 0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }

        private void storeDataInTexAttribList(int attribNum, int size, List<Vector2> tex, int stride)
        {
            int vbo = GL.GenBuffer();
            vbos.Add(vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.BufferData(BufferTarget.ArrayBuffer, stride * Vector2.SizeInBytes, tex.ToArray(), BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(attribNum, size, VertexAttribPointerType.Float, false, 0, 0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }

        private void unbindVAO()
        {
            GL.BindVertexArray(0);
        }

        private void bindIndicesBuffer(int[] indices)
        {
            int vbo = GL.GenBuffer();
            vbos.Add(vbo);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo);
            GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(int), indices, BufferUsageHint.StaticDraw);
        }



        public void cleanUP()
        {
            foreach (var item in vaos)
            {
                GL.DeleteVertexArray(item);
            }

            foreach (var item in vbos)
            {
                GL.DeleteBuffer(item);
            }

        }

    }
}

Hmmm

using OpenTK.Mathematics;
using System.Runtime.InteropServices;

using Core.Common.Objects;

namespace Core.Common
{
    public class LoadModel
    {
        public (vertex[] vertices, int[] indices) LoadObj(string path)
        {
            List<Vector3> Positions = new List<Vector3>();
            List<Vector2> texCoords = new List<Vector2>();


            List<int> position_Indices = new List<int>();
            List<int> texture_Indices = new List<int>();


            int[] indicesArray = null;
            vertex[] vertices = null;

            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("loading " + path + " ... ");
            Console.ResetColor();

            string[] lines = File.ReadAllLines(path);

            foreach (string line in lines)
            {
                string[] parts = line.Split(' ');

                switch (parts[0])
                {
                    case "v":
                        Vector3 pos = new Vector3(Convert.ToSingle(parts[1]), Convert.ToSingle(parts[2]), Convert.ToSingle(parts[3]));
                        Positions.Add(pos);
                        break;

                    case "vt":
                        Vector2 texture = new Vector2(float.Parse(parts[1]), float.Parse(parts[2]));
                        texCoords.Add(texture);
                        break;

                    case "f":
                        string[] vertex1 = parts[1].Split('/');
                        string[] vertex2 = parts[2].Split('/');
                        string[] vertex3 = parts[3].Split('/');

                        int index1 = int.Parse(vertex1[0]) - 1;
                        int index2 = int.Parse(vertex2[0]) - 1;
                        int index3 = int.Parse(vertex3[0]) - 1;

                        int texIndex1 = Int32.Parse(vertex1[1]) - 1;
                        int texIndex2 = Int32.Parse(vertex2[1]) - 1;
                        int texIndex3 = Int32.Parse(vertex3[1]) - 1;

                        position_Indices.Add(index1);
                        position_Indices.Add(index2);
                        position_Indices.Add(index3);

                        texture_Indices.Add(texIndex1);
                        texture_Indices.Add(texIndex2);
                        texture_Indices.Add(texIndex3);



                        break;
                }
            }
            
            // bellow is modified\\

            indicesArray = new int[position_Indices.Count];
            vertices = new vertex[position_Indices.Count];

            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].pos = Positions[position_Indices[i]];
                vertices[i].tex = texCoords[texture_Indices[i]];
            }

            for (int i = 0; i < position_Indices.Count; i++)
            {
                indicesArray[i] = i;
            }

            foreach (var item in vertices)
            {
                Console.WriteLine(item.pos);
            }


            return (vertices, indicesArray); //return modeldata and faces
        }




    }
}
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using System.Runtime.InteropServices;


using Core.Common.Objects;
using Core.Render;

namespace Core.Common
{
    public class Loader
    {
        private List<int> vbos = new List<int>();
        private List<int> vaos = new List<int>();
    

        public RawModel loadModel(vertex[] verts, int[] indices, string texPath)
        {
            int vao = createVAO();
            vbos.Add(vao);
            bindIndicesBuffer(indices);
            storeDataInPosAttribList(0, 3, verts, vertex.Stride);
            storeDataInTexAttribList(1, 2, verts, vertex.Stride);
            loadTexture(texPath);
            unbindVAO();
            return new RawModel(vao, indices.Length);
        }

        private int createVAO()
        {
            int vao = GL.GenVertexArray();
            GL.BindVertexArray(vao);
            return vao;
        }

        private void loadTexture(string path)
        {
            Texture texture;
            texture = Texture.LoadFromFile(path);
            texture.Use(TextureUnit.Texture0);
        }
        
        private void storeDataInPosAttribList(int attribNum, int size, vertex[]  verts, int stride)
        {
            int vbo = GL.GenBuffer();
            vbos.Add(vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            // bellow is modified\\
            GL.BufferData(BufferTarget.ArrayBuffer, stride * Vector3.SizeInBytes, ref verts[0].pos, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(attribNum, size, VertexAttribPointerType.Float, false, 0, 0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }

        private void storeDataInTexAttribList(int attribNum, int size, vertex[] tex, int stride)
        {
            int vbo = GL.GenBuffer();
            vbos.Add(vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            // bellow is modified\\
            GL.BufferData(BufferTarget.ArrayBuffer, stride * Vector2.SizeInBytes, ref tex[0].tex, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(attribNum, size, VertexAttribPointerType.Float, false, 0, 0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }

        private void unbindVAO()
        {
            GL.BindVertexArray(0);
        }

        private void bindIndicesBuffer(int[] indices)
        {
            int vbo = GL.GenBuffer();
            vbos.Add(vbo);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo);
            GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(int), indices, BufferUsageHint.StaticDraw);
        }



        public void cleanUP()
        {
            foreach (var item in vaos)
            {
                GL.DeleteVertexArray(item);
            }

            foreach (var item in vbos)
            {
                GL.DeleteBuffer(item);
            }

        }

    }
}
Source Link
Loading