So i have a method that creates an IntBuffer from an ByteBuffer:
public static IntBuffer directIntBuffer(int[]buffer){
ByteBuffer bb = ByteBuffer.allocateDirect(4*buffer.length);
bb.order(ByteOrder.nativeOrder());
IntBuffer ib = bb.asIntBuffer();
ib.put(buffer).flip();
return ib;
}
In another class's constructor i'm initialising one of these IntBuffers and filling it up with a nested for loop:
EDIT: so this is my full constructor. I'm filling up the FloatBuffer FBVertices with the actual vertex coordinates and the IntBuffer IBVerticesIndex with the indices. Later down, i draw these using GL11.glDrawElements(GL11.GL_TRIANGLES, IBVerticesIndex);
public sphereHelper(float fRadius, int iSlices, int iStacks)
{
FBVertices = GLDrawHelper.directFloatBuffer(new float[iSlices*iStacks*4*3]);
FBVertices.clear();
IBVerticesIndex = GLDrawHelper.directIntBuffer(new int[iSlices*iStacks*4]);
IBVerticesIndex.clear();
int index=0;
float verticalDegreePerStack= (float)Math.PI/iStacks; //1*Pi because we only want the height of the sphere once (from -PI/2 to PI/2)
float horizontalDegreePerSlice = 2.0f* (float)Math.PI/iSlices; //2*Pi because of unit circle/polar coordinates going once around the sphere
for (int k=0; k< iStacks; k++)
{
//overall vertical angle
float rho= (float)k*verticalDegreePerStack;
//sinus of that angle
float srho =(float) (Math.sin(rho));
float crho = (float) (Math.cos(rho));
//sinus and cosinus of vertical angle+ angle of 1 stack
float srhodrho= (float) (Math.sin(rho + verticalDegreePerStack));
float crhodrho = (float) (Math.cos(rho + verticalDegreePerStack));
for (int j=0; j<iSlices;j++)
{
//overall angle along the horizontal border (0 degree at full 2*PI circle)
float theta = (j==iSlices)? 0.0f : j*horizontalDegreePerSlice;
//sinus and cosinus
float stheta = (float)(-Math.sin(theta));
float ctheta = (float)(Math.cos(theta));
//coordinates of first vertex of current triangle
float x=stheta*srho;
float z=ctheta*srho;
float y=crho;
//put coordinates of first vertex into buffer
FBVertices.put(new float[]{x*fRadius,y*fRadius,z*fRadius});
//put index of first vertex of polygon in index array, increase index counter by one
if (IBVerticesIndex.position()<IBVerticesIndex.capacity())
{IBVerticesIndex.put(index);}
index++;
//second vertex
x = stheta*srhodrho;
z = ctheta*srhodrho;
y = crhodrho;
//put these coordinates into Buffer
FBVertices.put(new float[] {x*fRadius,y*fRadius,z*fRadius});
if (IBVerticesIndex.position()+2<IBVerticesIndex.capacity())
{
IBVerticesIndex.put(new int[] {index,index+1,index+2});}
index++;
System.out.println(IBVerticesIndex.position()+"/"+IBVerticesIndex.capacity());
}
IBVerticesIndex.flip();
FBVertices.flip();
}
}
I get an BufferOverflowException, at this output:
4/400 8/400 12/400 16/400 20/400 24/400 28/400 32/400 36/400 40/400
Exception in thread "main" java.nio.BufferOverflowException
So it is overflowing at a 10th of the buffers capacity? I cleared() the buffer in the beginning so the position should be reset.
Thanks for your input!
400/400
as output any time I run your code. – Arjan Jun 25 at 22:07IntBuffer
) ... can you edit your post and include the surrounding code? How are you getting those outputs exactly? Oh yourprintln
is inside the inner loop, not where you put it in the example code. And you're missing some curly... please clean up that mess, it's obviously confusing at least one reader of your question :P Still can't reproduce, I get all outputs from4/400
to400/400
in steps of 4. Your problem is elsewhere. What code did you omit between the declaration ofindex
and the loops? – Arjan Jun 25 at 22:18flip()
makes a buffer ready for a new sequence of channel-write or relative get operations: It sets the limit to the current position and then sets the position to zero. It also means you're overwriting data in the buffers every iteration of the outer loop, because the position is set to 0. I sincerely doubt that's what you want in this constructor? – Arjan Jun 25 at 23:31