OBJ model is not rendering whole ( only part of it)

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

My code :

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 :

http://pokit.org/get/img/f5d0766bee7c177aea920425a3aa83e5.png

Thanks in advance ! :slight_smile:

You’ve already asked this question, and people have already given you a number of solutions.

Well the solution I got did fix one part of the problem (it helped me set up bytebuffer and how parse .obj), but I still have some issues.
And I waited some time hoping someone will answer/help on my old topic, but no one did :confused: -> I also tried to solve problem by myself.
So I opened a new topic, with similar question hoping this time I will resolve this issue :slight_smile:

But if you think that this is not necessary, you may deleted ( if you have ability to do so ) :slight_smile:

So I googled around and managed to find decent tutorial on glDrawElements() and I managed to implement it and it actually renders in 3D :slight_smile:
But now I have encountered new problem, my cube (for some reason) looks deformed :confused:

My code :

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;
	FloatBuffer vertexData;
	int indiciesCount = 0; 
	

	public int vaoID = 0;
	public int vboID = 0;
	public int vboiID = 0;
	public ObjectLoad(String objfolder, String objfile, int size)
	{
		 
		ArrayList<Float> verticesL = new ArrayList<Float>();
		ArrayList<Byte> indicesL = new ArrayList<Byte>();
		
		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(" ");
		           
		            //verticesL.add(new Vector3f(Float.parseFloat(parts[1]),Float.parseFloat(parts[2]), Float.parseFloat(parts[3])));
		            verticesL.add(Float.parseFloat(parts[1]));
		            verticesL.add(Float.parseFloat(parts[2]));
		            verticesL.add(Float.parseFloat(parts[3]));
		            System.out.println(Float.parseFloat(parts[1]) + " " + Float.parseFloat(parts[2]) + " " + Float.parseFloat(parts[3]));
		         }
		         
		         
		         if (line.startsWith("f"))
		         {
		            String parts[] = line.split(" ");
		            if ( parts.length == 5)
		            {
		      
		           indicesL.add(Byte.parseByte(parts[1]));
		           indicesL.add(Byte.parseByte(parts[2]));
		           indicesL.add(Byte.parseByte(parts[3]));
		           indicesL.add(Byte.parseByte(parts[4]));
		           System.out.println(Byte.parseByte(parts[1]) + " " + Byte.parseByte(parts[2]) + " " + Byte.parseByte(parts[3]) + " "+ Byte.parseByte(parts[4]));
		               
		            }
		              
		         }
		        
		             

		      }
		      float verts[] = new float[verticesL.size()];
		      byte indices[] = new byte[indicesL.size()];
		         for(int i=0;i<verticesL.size();i++)
		        	 verts[i] = verticesL.get(i);
		         for(int i=0;i<indicesL.size();i++)
		          indices[i] = indicesL.get(i);
		       	
		      for(int i=0;i<verts.length;i++)
		    	  System.out.println(i+". : " + verts[i]);
		      for(int i=0;i<indicesL.size();i++)
		    	  System.out.println(i+". : " + indices[i]);
		      vertexData = BufferUtils.createFloatBuffer(verticesL.size());
		      vertexData.put(verts);
		      vertexData.flip();
		      inx = BufferUtils.createByteBuffer(indicesL.size()); 
		      inx.put(indices);
		      inx.flip();
		      indiciesCount = indicesL.size();
		      }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, vertexData, GL15.GL_STATIC_DRAW);
		      GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
		      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0 );
		      //GL20.glEnableVertexAttribArray(0); 
		      GL30.glBindVertexArray(0);
		      
		      vboiID = GL15.glGenBuffers();
		      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiID);
		      GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, inx, GL15.GL_STATIC_DRAW);
		      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0 );
		        
		   }
		   public void render()
		   {
		   
		      GL30.glBindVertexArray(vaoID);
		      GL20.glEnableVertexAttribArray(0);
		      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiID );
		      //GL11.glDrawArrays(GL11.GL_QUADS, 0, amountofv * 3);
		      GL11.glDrawElements(GL11.GL_QUADS, indiciesCount, GL11.GL_UNSIGNED_BYTE, 0);
		      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0 );
		      GL20.glDisableVertexAttribArray(0);
		      GL30.glBindVertexArray(0);
		      
		      
		   }
		   public void cleanMemory()
		   {
		      GL20.glDisableVertexAttribArray(0);
		      
		      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
		      GL15.glDeleteBuffers(vboID);
		      
		      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0 );
		      GL15.glDeleteBuffers(vboiID);
		      
		      GL30.glBindVertexArray(0);
		      GL30.glDeleteVertexArrays(vaoID);
		   }


}

And when I run it :

http://pokit.org/get/img/e3436bc726d6f017e33d9e3a254e3250.png

http://pokit.org/get/img/9b2edd5993ac947ed8107b3e1c057eb7.png

I solved the issue :smiley:

The problem was with parsing the .obj -> I made mistake at indices. The Blender saved them form 1 to 8 while openGL works from 0-7
Only this part of the code is changed

FROM

if (line.startsWith("f"))
		         {
		            String parts[] = line.split(" ");
		            if ( parts.length == 5)
		            {
		            	indicesL.add(Integer.parseInt(parts[1]));
		            	indicesL.add(Integer.parseInt(parts[2]));
		            	indicesL.add(Integer.parseInt(parts[3]));
		            	indicesL.add(Integer.parseInt(parts[4]));
		            	 //System.out.println(Byte.parseByte(parts[1]) + " " + Byte.parseByte(parts[2]) + " " + Byte.parseByte(parts[3]) + " "+ Byte.parseByte(parts[4]));
		               
		            }
		            else
		            {
		            	indicesL.add(Integer.parseInt(parts[1]));
		            	indicesL.add(Integer.parseInt(parts[2]));
		            	indicesL.add(Integer.parseInt(parts[3]));
		            	 //System.out.println(Byte.parseByte(parts[1]) + " " + Byte.parseByte(parts[2]) + " " + Byte.parseByte(parts[3]));
		            }
		              
		         }

TO

if (line.startsWith("f"))
		         {
		            String parts[] = line.split(" ");
		            if ( parts.length == 5)
		            {
		            	indicesL.add(Integer.parseInt(parts[1])-1);
		            	indicesL.add(Integer.parseInt(parts[2])-1);
		            	indicesL.add(Integer.parseInt(parts[3])-1);
		            	indicesL.add(Integer.parseInt(parts[4])-1);
		            	 //System.out.println(Byte.parseByte(parts[1]) + " " + Byte.parseByte(parts[2]) + " " + Byte.parseByte(parts[3]) + " "+ Byte.parseByte(parts[4]));
		               
		            }
		            else
		            {
		            	indicesL.add(Integer.parseInt(parts[1])-1);
		            	indicesL.add(Integer.parseInt(parts[2])-1);
		            	indicesL.add(Integer.parseInt(parts[3])-1);
		            	 //System.out.println(Byte.parseByte(parts[1]) + " " + Byte.parseByte(parts[2]) + " " + Byte.parseByte(parts[3]));
		            }
		              
		         }