Hello Guest

VBO texture coordinates not working?

  • 2 Replies
  • 2123 Views
VBO texture coordinates not working?
« on: August 26, 2014, 13:26:44 »
I've been trying to get textures to work with VBOs for a while, and I finally got all the code done, but instead of a texture the quad is just white. Here's the code I'm using: (The full thing is here: https://github.com/skyress3000/OpenGL-Project/blob/master/OpenGLTesting.java)
Code to load textures (I've confirmed this works using glGetTexImage)
Code: [Select]
public int loadTex(File file) {
BufferedImage img = null;
try {
img = ImageIO.read(file);
} catch(IOException e) {
e.printStackTrace();
}

int[] pixels = new int[img.getWidth() * img.getHeight()];
img.getRGB(0, 0, img.getWidth(), img.getHeight(), pixels, 0, img.getWidth());

ByteBuffer buffer = BufferUtils.createByteBuffer(img.getWidth() * img.getHeight() * 4);

for(int y = 0; y < img.getHeight(); y++){ //shizznuggets this is fancy
for(int x = 0; x < img.getWidth(); x++){
int pixel = pixels[y * img.getWidth() + x];
buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
buffer.put((byte) (pixel & 0xFF)); // Blue component
buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component. Only for RGBA
}
}

buffer.flip();

int texId = glGenTextures();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, texId);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_3D, 0, GL_RGBA, img.getWidth(), img.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

return texId;
}

Load test.png:
Code: [Select]
testTexid = loadTex(new File("resources/tex/test.png"));

Create VBOs:
Code: [Select]
vertexData = BufferUtils.createFloatBuffer(12);
vertexData.put(new float[]{x-1, y-1, z, x+1, y-1, z, x+1, y-1, z-100, x-1, y-1, z-100});
vertexData.flip();

colorData = BufferUtils.createFloatBuffer(12);
colorData.put(new float[]{0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.5f, 1.0f, 0.0f, 1.0f});
colorData.flip();

texData = BufferUtils.createFloatBuffer(8);
texData.put(new float[]{0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f});
texData.flip();

vboVertexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

vboColorHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

vboTexHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboTexHandle);
glBufferData(GL_ARRAY_BUFFER, texData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

Render VBOs:
Code: [Select]
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(3, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, vboColorHandle);
glColorPointer(3, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, vboTexHandle);
glTexCoordPointer(3, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindTexture(GL_TEXTURE_3D, testTexid);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindTexture(GL_TEXTURE_3D, 0);
« Last Edit: August 26, 2014, 14:25:44 by skyress3000 »

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: VBO texture coordinates not working?
« Reply #1 on: August 26, 2014, 14:14:05 »
That is too much code to ask people to look through for bugs. If you want people to check code, the code must be a small use case. The reason for this is two -fold. 1) In making the small use case, you often solve the bug anyway because the bug is generally just a typo which if you missed, we are never going to notice. 2) If there is a small amount of code, we can spend more time helping you and less time pointlessly reading through lines and lines of code with which there is nothing wrong.

My advice is either do as I have said above, making a small use case, or follow the tutorials here http://lwjgl.org/wiki/index.php?title=Main_Page under "OpenGL 3.2 and newer." Which take you through the whole process and have code at the end you can compare.

Also, your github repo contains no actual code, just compiled class and jar files.

Re: VBO texture coordinates not working?
« Reply #2 on: August 27, 2014, 16:20:07 »
You should not use vertexAttribPointer for color and texture pointers. Those are simple buffer objects.