Search code examples
javaopenglvbolwjglvertex-buffer

Texturing Vertex Buffer Objects


What I want to do is drawing a (large) terrain with OpenGL. So I have a set of vertices, lets say 256 x 256 which I store in a vertex buffer object in the VRAM. I properly triangulated them, so I've got an index buffer for the faces.

// vertexes
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBufferId);
glVertexPointer(3, GL_FLOAT, 0, 0);
// textures
glBindBufferARB(GL_ARRAY_BUFFER_ARB, texCoordBufferId);
glTexCoordPointer(2, GL_FLOAT, 0, 0);
// indexes
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indexBufferId);
// draw it
glDrawRangeElements(GL11.TRIANGLES, 0, size - 1, size, GL_UNSIGNED_INT, 0);

I also loaded a square texture which has to be applied to each triangle. So I've got a problem with the texture coords:

Each vertex is included in 4 triangles which means it needs 4 texture coords. But glDrawRangeElements() requires as much texture coords as vertexes.

So I don't the see the way how to do this with the VBOs. Maybe there is better concept for solving my problem or I'm just lacking a good idea.

Thanks in advance.


Solution

  • If your texture should repeat (or mirror) itself in each quad the best way would be to use texture coordinates that match the number of the (x, y) position in your array. E.g. for the first line of vertices use these texture coordinates: (0.0, 0.0), (1.0, 0.0), (2.0, 0.0)...(255.0, 0.0).