PDA

View Full Version : Correct implementation of Vertex Buffer Objects



ADP_Max
07-16-2012, 10:02 AM
Hi! I am a beginner and try to use VBOs in OpenGL ES for Android. My code was working before, but now i wanted to use VBOs instead of vertex buffers and I can't get it to work. It would be amazing if you could check the code for obvious errors.

Thank you!!!


public class Objects {

ArrayList<String> results; // String with :: containing all the results
String[] verticesString;
float verticesWalls[];
private FloatBuffer mFVertexBuffer;
private ByteBuffer mTstrip;
byte tstrip[];
int[] m_VertexVBO = new int[1]; //handle for VBO
int[] tStripBuffer= new int[1]; //handle for indices

public Objects() {
results = DataHandler.results; // Array with results from KML

// // 16 xyz-coordinates
verticesWalls = new float[results.size() * 3];

for (int j = 0; j < results.size(); j++) {
verticesString = results.get(j).split("::"); // separate

verticesWalls[j * 3] = Float.parseFloat(verticesString[0]);
verticesWalls[j * 3 + 1] = Float.parseFloat(verticesString[1]);
verticesWalls[j * 3 + 2] = -Float.parseFloat(verticesString[2]);
}

// Indices (TriangleStrip)
tstrip = new byte[results.size() * 3];
for (int i = 0; i < results.size(); i++) {
if (i == results.size() - 1) {
tstrip[i * 3] = (byte) i;
tstrip[i * 3 + 1] = (byte) 0;
tstrip[i * 3 + 2] = (byte) 1;
} else if (i == results.size() - 2) {
tstrip[i * 3] = (byte) i;
tstrip[i * 3 + 1] = (byte) (i + 1);
tstrip[i * 3 + 2] = (byte) 0;
} else {
tstrip[i * 3] = (byte) i;
tstrip[i * 3 + 1] = (byte) (i + 1);
tstrip[i * 3 + 2] = (byte) (i + 2);
}

}

ByteBuffer vbb = ByteBuffer.allocateDirect(verticesWalls.length * 4);
vbb.order(ByteOrder.nativeOrder());
mFVertexBuffer = vbb.asFloatBuffer();
mFVertexBuffer.put(verticesWalls);
mFVertexBuffer.position(0);

GLES11.glGenBuffers(1,m_VertexVBO,0);
GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, m_VertexVBO[0]);
GLES11.glBufferData(GLES11.GL_ARRAY_BUFFER,16* 3*4,mFVertexBuffer,GLES11.GL_STATIC_DRAW);
GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER,0);

mTstrip = ByteBuffer.allocateDirect(tstrip.length);
mTstrip.put(tstrip);
mTstrip.position(0);

GLES11.glGenBuffers(1,tStripBuffer,0);
GLES11.glBindBuffer(GLES11.GL_ELEMENT_ARRAY_BUFFER , tStripBuffer[0]);
GLES11.glBufferData(GLES11.GL_ELEMENT_ARRAY_BUFFER , 16*3, mTstrip, GLES11.GL_STATIC_DRAW);
GLES11.glBindBuffer(GLES11.GL_ELEMENT_ARRAY_BUFFER ,0);
}

public void draw(GL10 gl) {

GLES11.glBindBuffer(GL11.GL_ARRAY_BUFFER, m_VertexVBO[0]);

GLES11.glEnableClientState(GLES11.GL_VERTEX_ARRAY) ;
GLES11.glVertexPointer(3, GLES11.GL_FLOAT, 16*4, 0);

GLES11.glBindBuffer(GLES11.GL_ELEMENT_ARRAY_BUFFER ,tStripBuffer[0]);
GLES11.glDrawElements(GLES11.GL_TRIANGLE_STRIP, 16*3, GLES11.GL_UNSIGNED_BYTE, 0);
GLES11.glBindBuffer(GLES11.GL_ELEMENT_ARRAY_BUFFER ,0); // reset

}
}

ADP_Max
07-17-2012, 12:42 AM
Also I realized that i am getting an Error: call to OpenGL ES API with no current context (logged once per thread).

Does anybody know what that means?

thokra
07-17-2012, 01:14 AM
call to OpenGL ES API with no current context

That means that you either don't have any valid context at all or that no valid context is current. If you have a valid context, you need to call something like MakeCurrent() or makeCurrent(). For instance, I'm doing a lot of Qt4 based GL stuff using the QGLWidget which basically is nothing more than a convenience class providing automated context creation, init-, rendering-, resize-logic and the usual stuff you get out of a QWidget. However, depending on the order of operations, for instance when trying to call GL functions before using any of the functions provided by the interface, you need to call QGLWidget::makeCurrent() first to make the underlying GL context current in the current thread.