Skip to main content
GameDev.net gamedev.net
🔒 Locked

OBJ loader for opengl (LWJGL)

Started by robee00 Aug 9, 2013 at 12:47 PM 3 replies 3.6k views
Original Post
robee00
robee00

Hi

I wrote a simple OBJ loader but the texcoords are messed up somehow. I can't figure out what is wrong. I tried flipping the texture on the Y-Axis, no success. (Here is how my object looks like: http://imgur.com/d8edSyw

Code: (FaceData holds the IDs for vertices, normals and texcoords, Vertex class holds the data)


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)
            {
                Vector2f t = new Vector2f();
                t = tex.get(faces.get(i).texID - 1);
                t.y = t.y;

                v.texCoord.set(t);
            }
            else
                v.texCoord = new Vector2f(0,0);

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

            vertices.add(v);
        }
    }
SillyCow
SillyCow

Did you check if there is a W component in your obj? some programs export objs with U and W and V=0.

Can you upload your model & texture?

Other suspicious looking parts of the code:


t.y = t.y;

and:

why do you manually set the value for vertex id=0?


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

Ooops that t.y = t.y stayed in the code, I wrote t.y = 1f - t.y to flip the image on the y-axis. The texID's value is 0 by default, so I can check if there is no tex coords for that vertex.

https://mega.co.nz/#!8FtkXDzZ!R6S_71KMoKIhS4edYjq1VnnimMZ7TvLDL6OSmW_Zxuk <- link for obj and texture

It is not a cube, it's a sh*tty hallway, just made something to try the loader. Here is how it looks like http://i.imgur.com/sWXw7Lp.png

http://pastebin.com/38ECG3ge <- code for the class

SillyCow
SillyCow

I still can't compile and run your code, you didn't include supporting classes. (I am assuming you are using lwjgl 2.9)

When loading textured objects into a new engine, there are usually many problems. All kinds of coordinates either get flipped, or miss-indexed. If I had to make a bet you have an indexing problem.

The best way to solve this is to take a simple model like a 2D square, and see that the textures map correctly. It's even better if you take an asymetric shape like a trapeze, you will see everything much more clearly. The model should be simple enough that you can go figure out by eye what is wrong, and then go over the verticies with a debugger. Starting out with a complex model like this is a recipe for problems.

If you want me to be able to run your code you're going to have to supply some more of it:

  1. symbol: class Vertex
  2. symbol: class FaceData
  3. public ModelTransformation transformation;
  4. Texture texture;
  5. public LightingTechnique effect;
  6. symbol: class FaceData
  7. transformation = new ModelTransformation();
  8. effect.setEyeWorldPos(Main.cam.pos);
My Oculus Rift Game: RaiderV My Android VR games: Time-Rider& Dozer Driver My browser game: 

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.