After having used display lists for my programs since I started learning OpenGL, I've finally decided to switch to VBOs after experiencing a considerable amount of lag when I started work on a new game. I've nearly finished with my transition, but I'm still having a bit of trouble with rendering the textures to the screen. The way I'm working it is this: I have several sprites of the same dimensions for each type of "block" in the game (it's a Terraria port) which are loaded into the game via a convenience method, then after they're all loaded, they're compiled into a virtual texture atlas via Graphics2D, converted to a Slick texture, and their relative coordinates are saved to a HashMap. However, when it comes time to grab the textures out of the HashMap and render them, the game simply doesn't. Here's part of my code for adding a block to the VBO (this is executed 4 times per block, once for each corner):
// top left
// vertex
values.add(Float.valueOf((float)b.getLocation().getPixelX()));
values.add(Float.valueOf((float)b.getLocation().getPixelY()));
// light
values.add(Float.valueOf((float)b.getLightLevel() / 15));
values.add(Float.valueOf((float)b.getLightLevel() / 15));
values.add(Float.valueOf((float)b.getLightLevel() / 15));
// texture
values.add(tX);
values.add(tY);
(Note: values is a list which is later converted to an array.) Then comes my code for rendering the VBO:
public static void render(){
glPushMatrix();
glTranslatef(MineFlat.xOffset, MineFlat.yOffset, 0);
glBindTexture(GL_TEXTURE_2D, BlockUtil.atlas.getTextureID());
glVertexPointer(2, GL_FLOAT, 28, 0);
glColorPointer(3, GL_FLOAT, 28, 8);
glTexCoordPointer(2, GL_FLOAT, 28, 20);
glDrawArrays(GL_QUADS, 0, vertexArray.length / 7);
glBindTexture(GL_TEXTURE_2D, 0);
glPopMatrix();
}
The blocks are properly rendered with the correct lighting, but they lack any sort of texture. Any suggestions as to how to get the code working? I apologize if the answer is obvious, but I still consider myself a bit of an OpenGL noob, especially in the area of VBOs.
One last thing; I should mention that I'm not using any shaders in my current game.
EDIT: I seem to be a bit mistaken. Upon experimenting with the code by manually setting all texture coords, I discovered that the game simply converts the texture to a uniform color by averaging the RGB value of all pixels. I didn't recognize this before because the color of most blocks is grey. I recall having this problem before with display lists, but that was quite a while ago, and so I don't remember how I resolved it.
SECOND EDIT: Screenshots of the expected and actual result, respectively: https://i.stack.imgur.com/3t4r4.png https://i.stack.imgur.com/edv8v.png
glEnable(GL_TEXTURE_2D);
andglActiveTexture(GL_TEXTURE0);
\$\endgroup\$glTexParameteri
, the MIN / MAG filters or the mipmap level if you use mipmaps. \$\endgroup\$glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
andglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
in my code in order to remove interpolation while scaling, commenting them out didn't seem to affect anything either. \$\endgroup\$GL_NEAREST
should give you the least problems. If it's really the average color that sounds kind of weird. Got no clue, if that wouldn't be what happened I'd check if the relevantglEnableClientState()
calls have been made. You might also try to callglClientActiveTexture(GL_TEXTURE0)
but just likeglActiveTexture
it should be default anyway. \$\endgroup\$