OBJ loading messed up tex coords

Started by ic5y, August 09, 2013, 12:22:38

Previous topic - Next topic

ic5y

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);
        }

Fool Running

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
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

ic5y

According to 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

Fool Running

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?

Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D