So, I know I just had a post with 40 replies, but I want to clarify one thing.
I got VBO’s to work. I got them to render a texture and move, but I don’t know exactly how to set a different texture for each object.
I’m assuming that with VBO’s, you want to use a sprite sheet for animations and what not (switch Texture Coordinates), but what about a completely different texture for each object? I think I got it all working ok up to this point, but it’s being weird.
So I have an “Abstract Creature” Class which just has default stuff for every creature.
public void initVBO()
{
glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
vertexData.put(new float[]{x, y, x + width, y, x + width, y + height, x, y + height});
vertexData.flip();
textureData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
textureData.put(new float[]{0, 1, 1, 1, 1, 0, 0, 0});
textureData.flip();
vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
vboTexCoordHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboTexCoordHandle);
glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
public void updateVBOCoords()
{
vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
vertexData.put(new float[]{x, y, x + width, y, x + width, y + height, x, y + height});
vertexData.flip();
vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
public void updateVBO()
{
updateVBOCoords();
GL11.glColor3f(0.5f,0.5f,1.0f);
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, vboTexCoordHandle);
glTexCoordPointer(vertexSize, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS, 0, amountOfVertices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
As you can see, this is all of the code regarding creating/rendering the VBO. At the moment, I just draw it every frame through this code:
public void drawCreature()
{
getInput();
currentWeapon.draw();
if (hasinit == false)
{
setTexture("PNG", "Enemy.png");
initVBO();
hasinit = true;
}
updateVBO();
}
setTexture just uses slick to get a texture in my source file and sets it to a local variable within my “Tile” Class. The Abstract Creature Class Inherits the Tile class. So, whenever I create a new creature, I just copy the drawCreature method, and change the setTexture to the correct image.
The problem I’m having is… everything is staying as the Enemy Picture. This wasn’t happening before in immediate mode, so I feel like it’s once again something that I don’t understand with VBO’s. I never create the VBO until drawCreature, so I don’t know why it always uses the Enemy picture.
If anyone has better advice for how I go about Textures in VBO’s, let me know.
To be clear, this is for a 2D Side Scoller Game, and I’m using OpenGL 2. I don’t even have spritesheets at the moment, I just want to be able to set the textures according to each creature I create. (So Enemy1 may have a Enemy1.png… and so on)
When I comment this line out:
glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
It still renders the Enemy.png. So that just confuses me