LWJGL Forum

Programming => OpenGL => Topic started by: SirJavaGaming on October 02, 2014, 18:05:41

Title: OBJ Loader indices connection
Post by: SirJavaGaming on October 02, 2014, 18:05:41
Hey guys,
Im trying to import some .obj models to my LWJGL engine.
Im using the model as used here: https://www.youtube.com/watch?v=izKAvSV3qk0
Drawing it using GL_POINTS works great, but GL_TRIANGLE causes this:
(http://s14.directupload.net/images/141002/3oqbjtfw.png)

The triangles are connected in a way they shouldnt. There are no textures applied, just black color.

My Code:
public void render() {
GL30.glBindVertexArray(vaoId);
GL20.glEnableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboId);
GL11.glDrawElements(GL11.GL_TRIANGLES, lenght, GL11.GL_UNSIGNED_INT, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
}



public void load() { //Called once to load the model
ArrayList<Float> vertices = new ArrayList<Float>();
ArrayList<Integer> indices = new ArrayList<Integer>();
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("data/meshes/" + file)));
String line;
while((line = reader.readLine()) != null) {
if(line.startsWith("v ")) {
String[] params = line.split("\\s+");
vertices.add(Float.valueOf(params[1]));
vertices.add(Float.valueOf(params[2]));
vertices.add(Float.valueOf(params[3]));
}
if(line.startsWith("f")) {
String[] params = line.split("\\s+");
indices.add(Integer.valueOf(params[1].split("/")[0]));
indices.add(Integer.valueOf(params[2].split("/")[0]));
indices.add(Integer.valueOf(params[3].split("/")[0]));
}
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
lenght = indices.size();
FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.size()); //vertices loaded from file put into a FloatBffer
for(float f : vertices) {
verticesBuffer.put(f);
}
verticesBuffer.flip();
IntBuffer indicesBuffer = BufferUtils.createIntBuffer(indices.size()); //indices from file put into IntBuffer
for(int i : indices) {
indicesBuffer.put(i);
}
indicesBuffer.flip();

vaoId = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vaoId);

int vboiId = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboiId);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesBuffer, GL15.GL_STATIC_DRAW);

GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

GL30.glBindVertexArray(0);

vboId = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboId);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

}


Thanks for your help!

UPDATE: If you have the same problem, check that you subtract 1 from each index!

indices.add(Integer.parseInt(params[1].split("/")[0]) - 1);
indices.add(Integer.parseInt(params[2].split("/")[0]) - 1);
indices.add(Integer.parseInt(params[3].split("/")[0]) - 1);
Title: Re: OBJ Loader indices connection
Post by: abcdef on October 03, 2014, 07:59:42
What do you think is wrong? (I have no idea what you are expecting). You told open gl to draw triangles with out telling it to draw textures or colors and it has done just that. If you passed it the same set of indices for triangles and points then its going to look different because the vertex data will be used in two different ways (maybe not in the way you expect)
Title: Re: OBJ Loader indices connection
Post by: SirJavaGaming on October 03, 2014, 08:52:16
I want it to simply draw the mesh. The color is set to black in vertex shader.
The only error is that opengl connects the triangles wrong. If I use points, it works because there is nothing corrected.
I think I have set a wrong variable somewhere or forget a command, but I cant find it.
Title: Re: OBJ Loader indices connection
Post by: abcdef on October 03, 2014, 15:15:18
lenght = indices.size();

Shouldn't that be

lenght = indices.size()/3;

As you are using triangles now, the length is the number of items you are drawing, your length is currently the number of points.
Title: Re: OBJ Loader indices connection
Post by: SirJavaGaming on October 03, 2014, 17:24:52
That is a good idea, but is doesnt change anything.  :'(