Good evening fellow nerds.
I’ve been searching through the depths of the internet but couldn’t really find a solution for my problem.
I’m experimenting with VBOs since you hear every that glBegin()/glEnd() is a big no-no in terms of performance. I’m trying to put a neat texture (org.newdawn.slick.opengl.Texture) on my square.
This worked just fine with the no-no method as seen below.
public void draw(float xPos, float yPos){
texture.bind();
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f((float)width, 0);
glTexCoord2f(1, 1);
glVertex2f((float)width, (float)height);
glTexCoord2f(0, 1);
glVertex2f(0, (float)height);
glEnd();
glLoadIdentity();
}
But now that I’m trying to do the same with a VBO I’m kind of running against a wall.
I have the following so far…
public class Tile extends Entity {
final int amountOfVertices = 4;
final int vertexSize = 3;
int vboVertexHandle;
public Tile(float x, float y, int width, int height, Texture texture) {
super(x, y, width, height, texture);
FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);
vertexData.put(new float[]{x, y, 0, x + width, y, 0, x + width, y + height, 0, x, y + height, 0});
vertexData.flip();
vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
@Override
public void draw(float xPos, float yPos){
glLoadIdentity();
glTranslatef(xPos, yPos, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_QUADS, 0, amountOfVertices);
glDisableClientState(GL_VERTEX_ARRAY);
}
}
Honestly, I’d lie if I’d say that I understand what exactly this code does but I guess I understand the “most important part”
vertexData.put(new float[]{x, y, 0, x + width, y, 0, x + width, y + height, 0, x, y + height, 0});
Now I assume that I have to specify the Texture’s coordinates just like the above, but everything I tried so far didn’t seem to work.
Simply texture.bind() (of course) doesn’t do the trick since I haven’t specified it’s coordinates. It just paints the whole Quad in the color of the first pixel of the .png file (i think?!?).
So yeah, help very much appreciated, I hope my post was detailed enough ;D
Brb, shower.