I have been developing code to display VBOs with textures. The problem is no texture gets displayed. I know the texture is loaded. I can display the model in wireframe. And I can display it in polygon mode, but when I add textures nothing appears. If I remove the line
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
it displays in polygon mode and I can change it to wire frame.
My Main Loop:
while(!Display.isCloseRequested())
{
//I just removed the camera stuff
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glLight(GL_LIGHT0, GL_POSITION, Utility.asFlippedFloatBuffer(new float[]{cam.getX(), cam.getY(), cam.getZ(), 1}));
cam.useView(); //<-- this is part of the camera stuff
for (VBO vbo : TexturedGroupedObjects) {
vbo.renderObject();
}
updateFPS();
Display.update();
}
This is my VBO class:
public class VBO {
int vertexHandle;
int normalHandle;
int textureHandle;
int facesCount;
private static Texture vboTexture;
private static boolean containsTexture;
public VBO(ArrayList<Group> group, String textureLocation) {
//Load texture
try {
vboTexture = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream(textureLocation));
} catch (IOException e) {
e.printStackTrace();
}
facesCount = 0;
//Get faces count in multiGroup
for (Group gg : group) {
facesCount += gg.Faces.size();
}
System.out.println(facesCount);
vertexHandle = glGenBuffers();
normalHandle = glGenBuffers();
textureHandle = glGenBuffers();
FloatBuffer vertices = reserveData(facesCount * 36);
FloatBuffer normals = reserveData(facesCount * 36);
FloatBuffer texture = reserveData(facesCount * 36);
//Load all the vertex data into the floatbuffer
for (Group g : group) {
for (Face f : g.Faces) {
for (Vertex3f v3f : f.vertexData) {
vertices.put(asFloatsVertex(v3f));
}
for (Normal3f n3f : f.normalData) {
normals.put(asFloatsNormal(n3f));
}
if(f.textureData != null) {
for (Texture3f t3f : f.textureData) {
normals.put(asFloatsTexture(t3f));
}
}
}
}
vertices.flip();
normals.flip();
texture.flip();
glBindBuffer(GL_ARRAY_BUFFER, vertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, normalHandle);
glBufferData(GL_ARRAY_BUFFER, normals, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, textureHandle);
glBufferData(GL_ARRAY_BUFFER, texture, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
public void renderObject() {
vboTexture.bind();
glBindBuffer(GL_ARRAY_BUFFER, vertexHandle);
glVertexPointer(3, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, normalHandle);
glNormalPointer(GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, textureHandle);
glTexCoordPointer(3, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, facesCount * 3);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vboTexture.release();
}
public void deleteBuffers() {
glDeleteBuffers(vertexHandle);
glDeleteBuffers(normalHandle);
glDeleteBuffers(textureHandle);
}
private static float[] asFloatsTexture(Texture3f v) {
return new float[]{v.v, v.u};
}
private static float[] asFloatsNormal(Normal3f v) {
return new float[]{v.x, v.y, v.z};
}
private static float[] asFloatsVertex(Vertex3f v) {
return new float[]{v.x, v.y, v.z};
}
private static FloatBuffer reserveData(int size) {
return BufferUtils.createFloatBuffer(size);
}