Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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!

share|improve this question
    
Unable to reproduce, I get 400/400 as output any time I run your code. – Arjan Jun 25 at 22:07
    
What type is IBVerticesIndex in your code (I simply declared it as IntBuffer) ... can you edit your post and include the surrounding code? How are you getting those outputs exactly? Oh your println 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 from 4/400 to 400/400 in steps of 4. Your problem is elsewhere. What code did you omit between the declaration of index and the loops? – Arjan Jun 25 at 22:18
1  
Yeah, sorry for the mess. I cut out the presumably irrelevant parts as i'm trying to fill an OPENGL index buffer for vertices of a sphere here. You are right about the curly bracket, the println should be outside the loops. I will update the question with mor of my code. – Don_M Jun 25 at 22:54
    
From the Buffer superclass, flip() 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
    
THATS IT! The flip() wasn't supposed to be within the loop. When you work with so much code you get blind for these little details. Thanks to both of you. – Don_M Jun 25 at 23:47

EDIT: +1 to Arjan's comment on the original question. I copied Don's code to my machine and it's working on Java 8, also get 400/400 as the output. Are you setting the limit elsewhere in the remarked-out code?

Have you read up on how the Buffer superclass works? Specifically, the class has a limit property which does not have a well documented initial value. Writing past the limit value throws a BufferOverflowException.

It seems to me that you want to use this IntBuffer like a primitive array, in which case in your initialization routine you need to set the limit to the capacity after the call to flip() which according to the docs resets the limit to the mark.

share|improve this answer
    
.clear() sets the limit to the capacity. – Arjan Jun 25 at 22:42
    
You're right. Missed that! – Andrew C. Jun 25 at 22:49
    
Not really to my observations. Requesting and comparing position()and capacity() shouldn't influence the pointer/limit right? I read that there is a write and a read mdoe on each ByteBuffer and that the limit is only equal the capacity when in write mode. The call .clear() should set it into write mode to my understanding. If it's not too much can you test the whole code and wheteher it's producing the same output? – Don_M Jun 25 at 23:24
    
I copied your new code over and I am getting similar output as you. Seems to hit 40/400 several times in my screen output. Still looking at it. – Andrew C. Jun 25 at 23:32
    
Found the problem, the flip() reset the buffer pointer each loop. Thanks! – Don_M Jun 25 at 23:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.