LWJGL Forum

Programming => OpenGL => Topic started by: MaV3r1cK on March 28, 2015, 20:24:11

Title: OBJ model is not rendered whole ( only part of it)
Post by: MaV3r1cK on March 28, 2015, 20:24:11
Hello everyone, I am new around here :)

So I have this issue with loading obj models where the model is only partially rendered
The model isn't anything complex, it is only a basic cube from blender :
# Blender v2.72 (sub 0) OBJ File: ''
# www.blender.org
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
s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8

Now here is my code, I am trying to use more "modern" way of rendering with use of VBO and VAO ( note that I am noob at this, but I really want to learn how to do it) :
package Objects;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;

import static org.lwjgl.opengl.ARBBufferObject.*;
import static org.lwjgl.opengl.ARBVertexBufferObject.*;
import Math.Vector3;

import com.jogamp.opengl.*;

import javax.swing.JOptionPane;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.*;
import org.lwjgl.util.vector.Vector3f;
public class ObjectLoad {

ByteBuffer inx;
int amountofv = 0;
int indices[];
float verts[];
public int vaoID = 0;
public int vboID = 0;
public ObjectLoad(String objfolder, String objfile, int size)
{

ArrayList<Vector3f> verticesL = new ArrayList<Vector3f>();


FileReader objectfile = null;
try {
objectfile = new FileReader(objfolder + "/" + objfile);
} catch (FileNotFoundException e2) {
System.out.println(" File not found !");
JOptionPane.showMessageDialog(null,
    "File not found.",
    "Error - Failed to Load Object",
    JOptionPane.ERROR_MESSAGE);
e2.printStackTrace();
}

String line;
try{
      BufferedReader br = new BufferedReader(objectfile);
      while( (line=br.readLine()) != null)
      {
         
         if( line.startsWith("v"))
         {
            String parts[] = line.split(" ");
            System.out.println("Parts length : " + parts.length);
            verticesL.add(new Vector3f(Float.parseFloat(parts[1]),Float.parseFloat(parts[2]), Float.parseFloat(parts[3])));
         }
         amountofv = verticesL.size();
         System.out.println(verticesL.size() * 36 + " " + amountofv);
         inx = BufferUtils.createByteBuffer(verticesL.size() * 36);
         if (line.startsWith("f"))
         {
            String parts[] = line.split(" ");
            if ( parts.length == 5)
            {
               System.out.println("1. " + (float) verticesL.get(Integer.parseInt(parts[1]) - 1).getX() + " ss " + (float) verticesL.get(Integer.parseInt(parts[1]) - 1).getY() + " ss "+(float) verticesL.get(Integer.parseInt(parts[1]) - 1).getZ());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[1]) - 1).getX());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[1]) - 1).getY());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[1]) - 1).getZ());
               System.out.println("2.  " + (float) verticesL.get(Integer.parseInt(parts[2]) - 1).getX() + " ss " + (float) verticesL.get(Integer.parseInt(parts[2]) - 1).getY() + " ss "+(float) verticesL.get(Integer.parseInt(parts[2]) - 1).getZ());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[2]) - 1).getX());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[2]) - 1).getY());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[2]) - 1).getZ());
               System.out.println("3.  " + (float) verticesL.get(Integer.parseInt(parts[3]) - 1).getX() + " ss " + (float) verticesL.get(Integer.parseInt(parts[3]) - 1).getY() + " ss "+(float) verticesL.get(Integer.parseInt(parts[3]) - 1).getZ());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[3]) - 1).getX());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[3]) - 1).getY());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[3]) - 1).getZ());
               System.out.println("4.  " + (float) verticesL.get(Integer.parseInt(parts[4]) - 1).getX() + " ss " + (float) verticesL.get(Integer.parseInt(parts[4]) - 1).getY() + " ss "+(float) verticesL.get(Integer.parseInt(parts[4]) - 1).getZ());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[4]) - 1).getX());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[4]) - 1).getY());
               inx.putFloat((float) verticesL.get(Integer.parseInt(parts[4]) - 1).getZ());
            }
             
         }
       
           

      }
      inx.flip();
      for(int i=0; i<48;i++)
      System.out.println(i + ". :" + inx.get(i));
      }catch(FileNotFoundException e){
         e.printStackTrace();
         
      }catch( Exception e1)
      {
         e1.printStackTrace();
     
      }
   
   
   }
   public void setUP()
   {
   
     
      vaoID = GL30.glGenVertexArrays();
      GL30.glBindVertexArray(vaoID);
      vboID = GL15.glGenBuffers();
     
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID );
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, inx , GL15.GL_STATIC_DRAW);
      GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
   
     
     
       
       
   }
   public void render()
   {
   
      GL30.glBindVertexArray(vaoID);
      GL20.glEnableVertexAttribArray(0);
      GL11.glDrawArrays(GL11.GL_QUADS, 0, amountofv * 3);
      GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
      GL20.glDisableVertexAttribArray(0);
      GL30.glBindVertexArray(0);
       
       
     
   }
   public void cleanMemory()
   {
      GL20.glDisableVertexAttribArray(0);
     
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
      GL15.glDeleteBuffers(vboID);
     
      GL30.glBindVertexArray(0);
      GL30.glDeleteVertexArrays(vaoID);
   }


}


And when I run the program I get only one face from whole cube :/
(http://pokit.org/get/img/f5d0766bee7c177aea920425a3aa83e5.png)

Any help is appreciated :)
Thanks in advance :D
Title: Re: OBJ model is not rendered whole ( only part of it)
Post by: Kai on March 29, 2015, 11:39:03
In line 63 of your code snippet you are creating the
inx = BufferUtils.createByteBuffer(verticesL.size() * 36);
for every line you read from the .OBJ file.
Thus, you end up only rendering the last 'f' line of your OBJ model.
You should allocate a sufficiently large ByteBuffer before the loop.