LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: ic5y on August 09, 2013, 12:22:38

Title: OBJ loading messed up tex coords
Post by: ic5y on August 09, 2013, 12:22:38
Hello fellow developers.

I ran into a problem when trying to write an OBJ loader that converts a model to VBO.

I can load vertices, and normals properly, but I got problems with texture coords.

Here is the load method: (FaceData just holds IDs for vertex, texcoord, and normal). Vertex class holds the data for each vertex. When I got a code from this forum that loaded an obj and draw it into a display list, it worked for the same model.


    public void parseObj(String path)
    {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File("").getAbsolutePath() + "/res/" + path));

            String line = reader.readLine();

            while(line != null)
            {
                if(line.startsWith("v "))
                {
                    int start = line.indexOf(" ");
                    String sub = line.substring(start + 1);

                    String[] vertexStr = sub.split(" " );

                    pos.add(new Vector3f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1]), Float.parseFloat(vertexStr[2])));
                }
                if(line.startsWith("vt "))
                {
                    int start = line.indexOf(" ");
                    String sub = line.substring(start + 1);

                    String[] vertexStr = sub.split(" " );

                    tex.add(new Vector2f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1])));
                }
                if(line.startsWith("vn "))
                {
                    int start = line.indexOf(" ");
                    String sub = line.substring(start + 1);

                    String[] vertexStr = sub.split(" " );

                    norm.add(new Vector3f(Float.parseFloat(vertexStr[0]), Float.parseFloat(vertexStr[1]), Float.parseFloat(vertexStr[2])));
                }
                if(line.startsWith("f "))
                {
                    int start = line.indexOf(" ");
                    String sub = line.substring(start + 1);

                    String[] vertexStr = sub.split(" ");

                    for(int i = 0; i < vertexStr.length; i++)
                    {
                        String verStr = vertexStr[i].replaceAll("//", "/0/");

                        String[] asd = verStr.split("/");

                        FaceData data = new FaceData();
                        data.vertexID = Integer.parseInt(asd[0]);

                        data.texID = Integer.parseInt(asd[1]);

                        data.normalID = Integer.parseInt(asd[2]);

                        faces.add(data);
                    }
                }

                line = reader.readLine();
            }

            reader.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        for(int i = 0; i < faces.size(); i++)
        {
            Vertex v = new Vertex();
            v.pos = pos.get(faces.get(i).vertexID - 1);

            if(faces.get(i).texID != 0)
            {
                v.texCoord.set(tex.get(faces.get(i).texID - 1));
            }
            else
                v.texCoord = new Vector2f(0,0);

            v.normal = norm.get(faces.get(i).normalID - 1);

            vertices.add(v);
        }
Title: Re: OBJ loading messed up tex coords
Post by: Fool Running on August 09, 2013, 12:46:30
When I was playing with .obj models, I think the texture coordinate (vt) and normals (vn) were defined as being in the same index of the vertexes (v). In other words, the first texture coordinate you found was supposed to be associated with the first vertex (same with the first normal). Then your faces (f) on each line are the 3 indexes that make up each polygon, not that the first index is the vertex index, second - texture index, third - normal index.

Hope that helps. ;D
Title: Re: OBJ loading messed up tex coords
Post by: ic5y on August 09, 2013, 12:49:25
According to https://en.wikipedia.org/wiki/Wavefront_.obj_file (https://en.wikipedia.org/wiki/Wavefront_.obj_file), when you read the face data each "part" is a vertex. If the line is: f v1 v2 v3, then its a triangle, so each part is a vertex index
Title: Re: OBJ loading messed up tex coords
Post by: Fool Running on August 12, 2013, 13:14:23
Yes, sorry, I read your code wrong. I didn't see the splitting of the face data by '/'. I see that handling now. :-\

What is the problem you are having with your texture coords? Is the texture upside down, blank, etc?