Sign in to follow this  
madmax46

OpenGL attempting separate VertexBufferObject for vertex coordinates and texture coordinates

Recommended Posts

I am attempting to use 2 vertex buffer objects for rendering an image, one for the texture coordinates, and one for the vertex coordinates. The reason I am doing this is that I have many images in different locations on the screen and I want them all to share the texture coordinates and have their own vertex coordinates. I was able to get this working but am now wanting to move into use VBOs for speed and to keep the memory on the graphics card. This is what I have so far, I get no errors but no image either (using jogl with opengl 3.3) sharedTextureCoords and textureCoordVBO:

[code]
if (textureCoordVBO < 0) {
gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
int[] vbos = new int[1];
gl.glGenBuffers(1, vbos, 0);
textureCoordVBO = vbos[0];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, textureCoordVBO);
gl.glBufferData(GL.GL_ARRAY_BUFFER,
sharedTextureCoords.capacity(), sharedTextureCoords,
GL.GL_STATIC_DRAW);
System.out.println("ERROR CODE: " + gl.glGetError());
sharedTextureCoords = null;
gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);
}

if (vertexVbo < 0) {
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
int[] vbos = new int[1];
gl.glGenBuffers(1, vbos, 0);
vertexVbo = vbos[0];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexVbo);
gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexCoordinates.capacity(),
vertexCoordinates, GL.GL_STATIC_DRAW);
System.out.println("ERROR CODE: " + gl.glGetError());
vertexCoordinates = null;
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
}

if (textureCoordVBO > -1 && vertexVbo > -1) {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, textureCoordVBO);
gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexVbo);
gl.glVertexPointer(2, GL.GL_FLOAT, 0, 0);

// Draw each triangle strip
for (int i = 0; i < horizontalDivisions; ++i) {
gl.glDrawArrays(GL.GL_TRIANGLE_STRIP,
(i * 2 * verticalDivisions), verticalDivisions * 2);
}

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

System.out.println("ERROR CODE: " + gl.glGetError());
}
[/code]

Thanks for any help

Max

Share this post


Link to post
Share on other sites
You're disabling GL_VERTEX_ARRAY and GL_TEXTURE_COORD_ARRAY at the wrong times it looks like. The only time it matters when they are active is during the glDrawArrays call. You're enabling them before creating the buffer and disabling them after loading the data, which doesn't do anything, and leaves them disabled when you try to draw.

Share this post


Link to post
Share on other sites
Thanks for the quick response! I change that and still I get nothing. My code now looks like:

[code]
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);

if (textureCoordId < 0) {
int[] vbos = new int[1];
gl.glGenBuffers(1, vbos, 0);
textureCoordId = vbos[0];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, textureCoordId);
gl.glBufferData(GL.GL_ARRAY_BUFFER, sharedTextureCoords.capacity(),
sharedTextureCoords, GL.GL_STATIC_DRAW);
System.out.println("ERROR CODE: " + gl.glGetError());
sharedTextureCoords = null;
}

if (vertexId < 0) {
int[] vbos = new int[1];
gl.glGenBuffers(1, vbos, 0);
vertexId = vbos[0];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexId);
gl.glBufferData(GL.GL_ARRAY_BUFFER, vertexCoordinates.capacity(),
vertexCoordinates, GL.GL_STATIC_DRAW);
System.out.println("ERROR CODE: " + gl.glGetError());
vertexCoordinates = null;
}

if (textureCoordId > -1 && vertexId > -1) {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, textureCoordId);
gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexId);
gl.glVertexPointer(2, GL.GL_FLOAT, 0, 0);

// Draw each triangle strip
for (int i = 0; i < horizontalDivisions; ++i) {
gl.glDrawArrays(GL.GL_TRIANGLE_STRIP,
(i * 2 * verticalDivisions), verticalDivisions * 2);
}

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

System.out.println("ERROR CODE: " + gl.glGetError());
}

gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);
[/code]

I'm beginning to wonder if it isn't possible to use separate VBOs for texture and vertex coordinates and that they have to be interleaved in the one VBO. Any thoughts?

Max

Share this post


Link to post
Share on other sites
Got it working, realized "size" in glBufferData was in bytes, I was passing it in floats. I also needed to rewind my Buffer objects when passing them to that function.

working code:
[code]
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);

if (textureCoordId < 0) {
int[] vbos = new int[1];
gl.glGenBuffers(1, vbos, 0);
textureCoordId = vbos[0];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, textureCoordId);
gl.glBufferData(GL.GL_ARRAY_BUFFER,
sharedTextureCoords.capacity() * 4,
sharedTextureCoords.rewind(), GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
}

if (vertexId < 0) {
int[] vbos = new int[1];
gl.glGenBuffers(1, vbos, 0);
vertexId = vbos[0];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexId);
gl.glBufferData(GL.GL_ARRAY_BUFFER,
vertexCoordinates.capacity() * 4,
vertexCoordinates.rewind(), GL.GL_STATIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
vertexCoordinates = null;
}

long t0 = System.currentTimeMillis();
if (textureCoordId > -1 && vertexId > -1) {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexId);
gl.glVertexPointer(2, GL.GL_FLOAT, 0, 0);

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, textureCoordId);
gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);

// Draw each triangle strip
for (int i = 0; i < horizontalDivisions; ++i) {
gl.glDrawArrays(GL.GL_TRIANGLE_STRIP,
(i * 2 * verticalDivisions), verticalDivisions * 2);
}

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
}

System.out.println("Time to paint mesh: "
+ (System.currentTimeMillis() - t0));

gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);
[/code]

What really helped was the confirmation that it was in fact possible. Thanks!

Max

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this