Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualRobeeZ

Posted 06 August 2013 - 04:14 PM

Hello guys.

 

I came across a problem today. I exported a single cube from blender, and I tried to wrote a simple .obj loader to use. I think my parsing method is okay, but I have problem putting the data into a vbo/ibo. The vertex and fragment shader is working, I used them before. What I get is a deformated cube. Like faces (quad) only got one of the tris drawn.

 

Vertex class contains fields for position, texcoords, and normals

 

FaceData just holds the ID for vertex, texcoord and normal.

public class Model {

    ArrayList<Vertex> vertices = new ArrayList<Vertex>();
    ArrayList<Vector3f> pos = new ArrayList<Vector3f>();
    ArrayList<Vector2f> tex = new ArrayList<Vector2f>();
    ArrayList<Vector3f> norm = new ArrayList<Vector3f>();
    ArrayList<FaceData> faces = new ArrayList<FaceData>();

    int vbo;
    int ibo;

    public int indexCount;

    public Model(String name)
    {
        parseObj(name);

        FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(vertices.size() * 8);

        for(Vertex v : vertices)
        {
            vertBuffer.put(v.getElements());
        }

        vertBuffer.flip();

        vbo = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

        IntBuffer indices = BufferUtils.createIntBuffer(faces.size());

        for(FaceData f : faces)
        {
            indices.put(f.vertexID - 1);
            indexCount++;
        }

        indices.flip();

        ibo = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    }

    public void render()
    {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

        GL20.glEnableVertexAttribArray(0);
        GL20.glEnableVertexAttribArray(1);
        GL20.glEnableVertexAttribArray(2);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
        GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 32, 0);
        GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 32, 12);
        GL20.glVertexAttribPointer(2, 3, GL11.GL_FLOAT, false, 32, 20);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
        GL11.glDrawElements(GL11.GL_TRIANGLES, indexCount, GL11.GL_UNSIGNED_INT, 0);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);
        GL20.glDisableVertexAttribArray(2);

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    }

    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].split("/");

                        FaceData data = new FaceData();
                        data.vertexID = Integer.parseInt(verStr[0]);
                        //data.texID = Integer.parseInt(verStr[1]);
                        data.normalID = Integer.parseInt(verStr[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);
            v.texCoord = new Vector2f(0,0);
            v.normal = norm.get(faces.get(i).normalID - 1);

            vertices.add(v);
        }
    }

}


#1RobeeZ

Posted 06 August 2013 - 04:14 PM

Hello guys.

 

I came across a problem today. I exported a single cube from blender, and I tried to wrote a simple .obj loader to use. I think my parsing method is okay, but I have problem putting the data into a vbo/ibo. The vertex and fragment shader is working, I used them before. What I get is a deformated cube. Like faces (quad) only got one of the tris drawn.

public class Model {

    ArrayList<Vertex> vertices = new ArrayList<Vertex>();
    ArrayList<Vector3f> pos = new ArrayList<Vector3f>();
    ArrayList<Vector2f> tex = new ArrayList<Vector2f>();
    ArrayList<Vector3f> norm = new ArrayList<Vector3f>();
    ArrayList<FaceData> faces = new ArrayList<FaceData>();

    int vbo;
    int ibo;

    public int indexCount;

    public Model(String name)
    {
        parseObj(name);

        FloatBuffer vertBuffer = BufferUtils.createFloatBuffer(vertices.size() * 8);

        for(Vertex v : vertices)
        {
            vertBuffer.put(v.getElements());
        }

        vertBuffer.flip();

        vbo = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertBuffer, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

        IntBuffer indices = BufferUtils.createIntBuffer(faces.size());

        for(FaceData f : faces)
        {
            indices.put(f.vertexID - 1);
            indexCount++;
        }

        indices.flip();

        ibo = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    }

    public void render()
    {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

        GL20.glEnableVertexAttribArray(0);
        GL20.glEnableVertexAttribArray(1);
        GL20.glEnableVertexAttribArray(2);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
        GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 32, 0);
        GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 32, 12);
        GL20.glVertexAttribPointer(2, 3, GL11.GL_FLOAT, false, 32, 20);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
        GL11.glDrawElements(GL11.GL_TRIANGLES, indexCount, GL11.GL_UNSIGNED_INT, 0);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);
        GL20.glDisableVertexAttribArray(2);

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    }

    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].split("/");

                        FaceData data = new FaceData();
                        data.vertexID = Integer.parseInt(verStr[0]);
                        //data.texID = Integer.parseInt(verStr[1]);
                        data.normalID = Integer.parseInt(verStr[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);
            v.texCoord = new Vector2f(0,0);
            v.normal = norm.get(faces.get(i).normalID - 1);

            vertices.add(v);
        }
    }

}


PARTNERS