OBJ loader for opengl (LWJGL)

Started by
3 comments, last by SillyCow 10 years, 8 months ago

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

nvm wrong language :S

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

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

My Oculus Rift Game: RaiderV

My Android VR games: Time-Rider& Dozer Driver

My browser game: Vitrage - A game of stained glass

My android games : Enemies of the Crown & Killer Bees

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

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: Vitrage - A game of stained glass

My android games : Enemies of the Crown & Killer Bees

This topic is closed to new replies.

Advertisement