glVertexPointer - help needed

Hey guys,

can anyone tell me why this code will not render (it should display a box)?


	public FloatBuffer createFloatBuffer(int length) {
		return ByteBuffer.allocateDirect(length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
	} 
	
....
// rendering code starts
	float boxnorms[] ={0,0,1, 0,0,1, 0,0,1, 0,0,1,
                0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1,
                0,1,0, 0,1,0, 0,1,0, 0,1,0,
                0,-1,0, 0,-1,0, 0,-1,0, 0,-1,0,
                1,0,0, 1,0,0, 1,0,0, 1,0,0,
                -1,0,0, -1,0,0, -1,0,0, -1,0,0};
		
	float x = 1, y = 1, z = 1;
		
	float points[] = new float[72];		
		
        //  front
        points[0] =  x; points[1] =  y; points[2] =  z; points[3] = -x; points[4] =  y; points[5] =  z;
        points[6] = -x; points[7] = -y; points[8] =  z; points[9] =  x; points[10] = -y; points[11] =  z;
        //  back
        points[12] =  x; points[13] = -y; points[14] = -z; points[15] = -x; points[16] = -y; points[17] = -z;
        points[18] = -x; points[19] =  y; points[20] = -z; points[21] =  x; points[22] =  y; points[23] = -z;
        //  top
        points[24] = -x; points[25] =  y; points[26] =  z; points[27] =  x; points[28] =  y; points[29] =  z;
        points[30] =  x; points[31] =  y; points[32] = -z; points[33] = -x; points[34] =  y; points[35] = -z;
        //  down
        points[36] = -x; points[37] = -y; points[38] = -z; points[39] =  x; points[40] = -y; points[41] = -z;
        points[42] =  x; points[43] = -y; points[44] =  z; points[45] = -x; points[46] = -y; points[47] =  z;
        //  right
        points[48] =  x; points[49] = -y; points[50] =  z; points[51] =  x; points[52] = -y; points[53] = -z;
        points[54] =  x; points[55] =  y; points[56] = -z; points[57] =  x; points[58] =  y; points[59] =  z;
        //  left
        points[60] = -x; points[61] = -y; points[62] =  z; points[63] = -x; points[64] =  y; points[65] =  z;
        points[66] = -x; points[67] =  y; points[68] = -z; points[69] = -x; points[70] = -y; points[71] = -z;
        
        // create buffers
        FloatBuffer vertexBuffer, normalBuffer;
        vertexBuffer = createFloatBuffer(72 * 3);
        normalBuffer = createFloatBuffer(72 * 3);       
        
        // draw vertices/normals
        gl.glVertexPointer (3, GL.GL_FLOAT, 0, vertexBuffer);
        gl.glNormalPointer (GL.GL_FLOAT, 0, normalBuffer);

Im sure im not doing the buffers right, I’ve tried a lot of searching and thats where i found that createFloatBuffer() method, but nothing is being drawn on screen :confused: Just to note If i draw using glBegin(…) and glVertex3f() etc the box is drawn fine.

Any help is really appreciated :slight_smile:

From a quick look over, I can see a few things wrong:

  1. You never actually fill your buffers with any data, you just create blank ones.
  2. You need to glEnableClientState for the vertex arrays you’ll be using (ie. vertex array and normal array).
  3. After you’ve specified your vertex arrays you need to use glDrawArrays (or similar) to actually use the data to render primitives.

ooh cheers for the fast reply :wink: Can’t believe i didnt notice that :open_mouth: Here’s the fixed code:


        FloatBuffer vertexBuffer, normalBuffer;
        vertexBuffer = createFloatBuffer(72 * 3);
        vertexBuffer.put(points);
        normalBuffer = createFloatBuffer(72 * 3);       
        normalBuffer.put(boxnorms);
        
        gl.glEnableClientState(GL.GL_VERTEX_ARRAY);        
        gl.glVertexPointer (3, GL.GL_FLOAT, 0, vertexBuffer);
        
        gl.glEnableClientState(GL.GL_NORMAL_ARRAY);
        gl.glNormalPointer (GL.GL_FLOAT, 0, normalBuffer);

        gl.glDrawArrays (GL.GL_QUADS, 0, 24);

You also need to rewind() your buffers before passing them to glVertexPointer, etc. (if you’re using one of the JSR-231 beta builds, which I strongly recommend) because the position of the buffers is now significant.

Thanks for the tip :slight_smile:

Hey guys,

I’ve wanted to expand my code a bit more so that I could draw different types of models, not just a cube like in my code above. I’m reading in model information for an IndexedFaceSet (so the points could make up any kind of model), and I have come across the following code to do the job:


        gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertexBuffer);
        gl.glDrawElements(GL.GL_TRIANGLES, 72, GL.GL_UNSIGNED_INT,  indices);

However the last param of glDrawElements is confusing. I have read it takes in the indices of the model, but as could be seen from the model I posted above I have no indices set. I have read this thread, where he has passed in a int buffer, but Im not sure how I should generate the indices first

I really appreciate any help I can get. Cheers :slight_smile: