LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: vastrolorde on January 26, 2014, 17:15:00

Title: VBO drawElements
Post by: vastrolorde on January 26, 2014, 17:15:00
Im trying to draw a model with multiple textures. I have got a suggestion that drawElements to draw parts of the array is a good way to do it. But i cant seem to get drawElements to work. It doesetn draw the part it is supposed to draw properly. Can someone tell me where is the error or what am i doing wrong.

Render function
public void Render(){
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
if(textured){
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
glVertexPointer(3, GL_FLOAT,0,0);
glBindBuffer(GL_ARRAY_BUFFER, vboBach1ID);
glColorPointer(3, GL_FLOAT,32,0);
glNormalPointer(GL_FLOAT,32,12);

if(textured){
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, faces.get(0).get(0).material.texture.getTextureID());
glBindBuffer(GL_ARRAY_BUFFER, vboBach1ID);
glTexCoordPointer(2, GL_FLOAT, 32,24);

}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndeciesID);
glDrawElements(GL_TRIANGLES, koht.get(0), GL_UNSIGNED_INT, 0);
//glDrawArrays(GL_TRIANGLES, 0, koht.get(0));

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
if(textured){
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
}


VBO creation function
public void prepareVBO(){
vboBach1ID = glGenBuffers();
vboIndeciesID = glGenBuffers();
vboVertexID = glGenBuffers();
for(int i = 0; i < faces.size(); i++){
total += faces.get(i).size();
}

FloatBuffer bach1Buffer = BufferUtils.createFloatBuffer(33 * total);
FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(9* total);
IntBuffer indeciesBuffer = BufferUtils.createIntBuffer(33*total);

for(int i = 0; i < faces.size(); i++){
for(Face face : faces.get(i)){
Material material = face.material;
indeciesBuffer.put((int) (face.vertex.x - 1)).put((int) (face.vertex.y - 1)).put((int) (face.vertex.z - 1));


Vector3f v1 = vertices.get((int) face.vertex.x - 1);
vertexBuffer.put(v1.x).put(v1.y).put(v1.z);
bach1Buffer.put(material.getDiffuse().x)
      .put(material.getDiffuse().y)
      .put(material.getDiffuse().z);

Vector3f n1 = normals.get((int) face.normal.x -1);
bach1Buffer.put(n1.x).put(n1.y).put(n1.z);

if(textured){
Vector2f t1 = texture.get((int) face.texture.x - 1);
bach1Buffer.put(t1.x).put(1 - t1.y);
}

Vector3f v2 = vertices.get((int) face.vertex.y - 1);
vertexBuffer.put(v2.x).put(v2.y).put(v2.z);
bach1Buffer.put(material.getDiffuse().x)
        .put(material.getDiffuse().y)
        .put(material.getDiffuse().z);

Vector3f n2 = normals.get((int) face.normal.y -1);
bach1Buffer.put(n2.x).put(n2.y).put(n2.z);

if(textured){
Vector2f t2 = texture.get((int) face.texture.y - 1);
bach1Buffer.put(t2.x).put(1 - t2.y);
}

Vector3f v3 = vertices.get((int) face.vertex.z - 1);
vertexBuffer.put(v3.x).put(v3.y).put(v3.z);
bach1Buffer.put(material.getDiffuse().x)
        .put(material.getDiffuse().y)
        .put(material.getDiffuse().z);

Vector3f n3 = normals.get((int) face.normal.z -1);
bach1Buffer.put(n3.x).put(n3.y).put(n3.z);

if(textured){
Vector2f t3 = texture.get((int) face.texture.z - 1);
bach1Buffer.put(t3.x).put(1 - t3.y);
}
}
koht.add(vertexBuffer.position());
}
bach1Buffer.rewind();
indeciesBuffer.rewind();
vertexBuffer.rewind();

glBindBuffer(GL_ARRAY_BUFFER, vboBach1ID);
glBufferData(GL_ARRAY_BUFFER, bach1Buffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndeciesID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indeciesBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);


}
Title: Re: VBO drawElements
Post by: Fool Running on January 27, 2014, 15:08:14
Can you be more specific than "it doesn't draw the part it is supposed to draw properly"? Is it not drawing anything at all? Are the vertexes, normals, textures messed up? What is wrong when you draw?

Best guess is that your stride in your glColorPointer and glNormalPointer is wrong. You have a stride of 32, but you only put 12 bytes in. glTexCoordPointer also looks wrong. It has a stride of 32, but you are only putting in 8 bytes.
Title: Re: VBO drawElements
Post by: vastrolorde on January 27, 2014, 18:11:54
Mostly the texture is bugged. I can see through some parts and when i move some other parts disappear.
Title: Re: VBO drawElements
Post by: vastrolorde on January 29, 2014, 11:40:53
http://www.java-gaming.org/topics/lwjgl-using-multiple-textures/31879/30/view.html