Hello,
(No XPing, this is completely separate Q)
Generally I regard myself as an almost decent coder, but occasionally i get humbled by seemingly simple looking stuff and here is one. I am hoping someone can see where the proplem is.
Anticipating (infact fully knowing) that the following program will eventually grow exponentially, i am trying to write a function that would take a FloatBuffer type as a parameter so that i won't have to write those 5 lines over again and again bcus i have got zillions of vertices.
Also another reason for doing it this way is that the actual vertices amount would not be known at compile time but decoded at runtime
so i would eventually do some dynamic memory allocation and read other stuff... but i the mean time when i call
...the function setupByteBuffer( linev0, linebff0 );
and by the time gl.glDrawArrays(GL10.GL_POINTS, 0, 4 ); , is called, the program stops working or crashes
I'm sure there is a way of doing this, does anyone know how.
I always feel my code can sometimes be too cluttered, so the code below is oversimplified so the actual problem can be easily spotted
Many thanks in advance
public class rampantjoiner {
private FloatBuffer colorBuffer;
private FloatBuffer linebff;
private FloatBuffer linebff0;
public rampantjoiner( int n ) {
setupByteBuffer( linev0, linebff0 );
ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4);
cbb.order(ByteOrder.nativeOrder());
colorBuffer = cbb.asFloatBuffer();
colorBuffer.put(colors);
colorBuffer.position(0);
}
public void setupByteBuffer( float f[], FloatBuffer lbff ){
linev = f; //EDit: still crashes , linebff is global to the class
linebff = lbff; //EDIT: this obvious error crept in will i was trying to isolate the error
ByteBuffer vbbc = ByteBuffer.allocateDirect(linev.length * 4); // as you will see from the top linebff isnt even a local variale
vbbc.order(ByteOrder.nativeOrder()); // its always been global (check previous unedited history -2)
linebff = vbbc.asFloatBuffer();
linebff.put(linev);
linebff.position(0);
}
private float[] linev0 = {
1.0f, -1.0f, 0.5f,
1.0f, 1.0f, 0.5f,
1.0f, 1.0f, -1.5f,
1.0f, -1.0f, -1.5f,
1.0f, -1.0f, 0.5f,
};
private float[] linev = {};
public void draw(GL10 gl) {
gl.glPointSize(5);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, linebff);
gl.glDrawArrays(GL10.GL_POINTS, 0, 4 );
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
private float[] colors = {
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
}