Hello,
I my game has a tiled based top to down view.
I’ve been using immediate rendering which I think might be slower…
Now that I’m woried about performance and I want to make everything fast and bug free before I move on, I want to make it better… My thoughts using VBO…
I’m not sure if this is the best Idea or the right one to use but as far as I know it is… if not tell me please.
My code that doesn’t work:
public class QuadRender {
protected int vao, vbo, vio, vto;
protected float[] vertices;
protected byte[] indices = new byte[] {
0, 1, 2, //
2, 3, 0 //
};
protected byte[] texCoords = new byte[] {
0, 0, //
1, 0, //
1, 1, //
1, 1, //
0, 1, //
0, 0 //
};
public QuadRender(int size) {
GeneralSettings.quads.add(this);
vertices = new float[] {
0.0f, 0.0f, 0.0f, //
size, 0.0f, 0.0f, //
size, size, 0.0f, //
0.0f, size, 0.0f //
};
compile();
}
protected void compile() {
vao = glGenVertexArrays();
glBindVertexArray(vao);
{
vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
{
glBufferData(GL_ARRAY_BUFFER, Buffer.createFloatBuffer(vertices), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
vio = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vio);
{
glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(indices), GL_STATIC_DRAW);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(vto);
vto = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vto);
{
glBufferData(GL_ARRAY_BUFFER, Buffer.createByteBuffer(texCoords), GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_UNSIGNED_BYTE, false, 0, 1);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
glBindVertexArray(0);
}
public void render(int x, int y, int texture, Shader shade) {
glTranslatef(x, y, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(shade.getShader(), "texture"), 0);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
{
glBindBuffer(GL_ARRAY_BUFFER, vio);
glVertexPointer(2, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vto);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
glDrawArrays(GL_QUADS, 0, 4);
}
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glTranslatef(-x, -y, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
My code that does work:
public class QuadRender {
public QuadRender(int size) {
GeneralSettings.quads.add(this);
}
public void render(int x, int y, int texture, Shader shade) {
glTranslatef(x, y, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(glGetUniformLocation(shade.getShader(), "texture"), 0);
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(0, 1);
glVertex2f(0, 64);
glTexCoord2f(1, 1);
glVertex2f(64, 64);
glTexCoord2f(1, 0);
glVertex2f(64, 0);
}
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glTranslatef(-x, -y, 0);
}
}
With the code that works it looks like this:
http://www.joaolourenco.net/Img1.png
With the code that doesn’t work it looks like this:
http://www.joaolourenco.net/Img2.png
PS: the wooden bit uses another render engine for now thats why its not affected by the changes.
What the problem on the textures?
Thanks, Joao Lourenco.