Question

When I pull vertex data from the obj file using this method:

    public static ArrayList<Float> parseOBJ(File file) throws FileNotFoundException
    {
        //If a file is not given, it is null.
        if (file == null)
        {
            throw new FileNotFoundException("file not valid");
        }

        //If it is not a file, it is null.
        if (!file.isFile())
        {
            throw new FileNotFoundException("file not found");
        }

        ArrayList<Float> vertexData = new ArrayList<Float>();

        //Creates a scanner for the provided file.
        Scanner fileScanner = new Scanner(file);
        while (fileScanner.hasNextLine())
        {
            //Takes in a line
            String line = fileScanner.nextLine();

            //For lines that start with a "v"
            if (line.length() > 1 && line.charAt(0) == 'v')
            {
                //Lines split when a space is present.
                String[] lineElements = line.split(" ");
                for (int index = 1; index < lineElements.length; index += 1)
                {
                    //Checking to make sure the string is a number. Then pulls out the number.
                    float number = Float.parseFloat(lineElements[index]);
                    vertexData.add(number);
                }
            }
        }
        return vertexData;
    }

and render it using this preredner code:

        int vboVertexHandler = 0;
        int vboColorHandler = 0;
        float[] vertexData;
        float[] colorData;
        int len = 0;
        try
        {
            ArrayList<Float> verticies = ResourceParser.parseOBJ(
                    new File("C:\\Users\\Will Stuckey\\Desktop\\cubeTri.obj"));
            len = verticies.size();
            vertexData = new float[len];
            colorData = new float[len];
            for (int i = 0; i < vertexData.length; i++)
            {
                vertexData[i] = verticies.get(i);
                colorData[i] = 1f;
            }

            System.out.print("Initializing the vertex float buffer........... ");
            FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(len);
            vertexBuffer.put(vertexData);
            System.out.print("flipping..... ");
            vertexBuffer.flip();
            System.out.println("DONE. num->" + (len));

            System.out.print("Initializing the color float buffer............ ");
            FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(len);
            colorBuffer.put(colorData);
            System.out.print("flipping..... ");
            colorBuffer.flip();
            System.out.println("DONE. num->" + (len));

            System.out.print("Initializing the vertex handler................ ");
            vboVertexHandler = glGenBuffers();
            glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandler);
            glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
            System.out.println("DONE. vertexBuffer->bound, vertexBuffer->static");

            System.out.print("Initializing the color handler................. ");
            vboColorHandler = glGenBuffers();
            glBindBuffer(GL_ARRAY_BUFFER, vboColorHandler);
            glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
            System.out.println("DONE. colorBuffer->bound, colorBuffer->static");
        }

and this render loop code:

            glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandler);
        glVertexPointer(3, GL_FLOAT, 0, 0L);

        glBindBuffer(GL_ARRAY_BUFFER, vboColorHandler);
        glColorPointer(3, GL_FLOAT, 0, 0L);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);

        glDrawArrays(GL_TRIANGLES, 0, len);

        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);

the obj file contains this:

# Blender v2.69 (sub 0) OBJ File: ''
# www.blender.org
mtllib cubeTri.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
s off
f 1 2 4
f 5 8 6
f 1 5 2
f 2 6 3
f 3 7 4
f 5 1 8
f 2 3 4
f 8 7 6
f 5 6 2
f 6 7 3
f 7 8 4
f 1 4 8

The cube has been triangulated. When its renderd, about half the triangles are missing and several are not mapped correctly. I'm thinking I have a stupid mistake somewhere but perhaps I need normals? I know it's a lot of source, thanks for taking the time to look at this.

Cheers -Will

Was it helpful?

Solution

After some fairly in depth searching, Wavefront OBJ files do not export vertices as triangles or triangle strips. The vertices and normals are simply lists that must be compared to face definitions. I'm writing code to parse faces and will give it a shot soon. Hopefully someone in the future will see this. Will add code when done.

UPDATE:

I wrote and tested the OBJ parser. It works but only with models that have been exported with triangulated faces. Still working on getting non triangulated models to work. Anyways the code can be found here:

https://github.com/guyfleeman/rayburn/blob/master/src/rayburn/game/util/ResourceParser.java

And the implementation here:

https://github.com/guyfleeman/rayburn/blob/master/src/rayburn/engine/Engine.java

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top