Login  Register

Re: glDrawElements with TRIANGLE_STRIP

Posted by millerni456 on Sep 28, 2011; 3:33am
URL: https://forum.jogamp.org/glDrawElements-with-TRIANGLE-STRIP-tp3374872p3374878.html

The next step is to set up my indices:

short[] indices = new short[vertices.length/3]; //vertices is actually 3 * the number of vertices because each vertex has xyz components. So this should be the equivalent to the number of triangles times 3 for each point.

                int k = 0; //aids in specifying indices.
                for(int i = 0; i<indices.length-1; i+=3)
                {
                        indices[i] = (short)k;
                        indices[i+1] = (short) (k+1);
                        indices[i+2] = (short) (k+2);
                        k++;
                }

This simple loop should place similar values into a short array:
{0, 1, 2,       1, 2, 3,         2, 3, 4             3, 4, 5,        4, 5, 6,         5, 6, 7.....}
And do this until it reaches the end of the number of vertices. This is also the same order
for triangle strip:

http://www.codesampler.com/d3dbook/chapter_05/chapter_05_files/image007.jpg

So I load my buffers like such:
Note that vertexBuffer is a FloatBuffer and that indexBuffer is a ShortBuffer.

//create buffers.
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
               
ByteBuffer ibb = ByteBuffer.allocateDirect(vertices.length/3 * 2);
ibb.order(ByteOrder.nativeOrder());
ndexBuffer = ibb.asShortBuffer();

//fill them
vertexBuffer.put(vertices); //this uses the data from earlier
vertexBuffer.position(0);  

indexBuffer.put(indices); //this also uses the data from earlier
indexBuffer.position(0);

____________________________________________________________________________
So all set up code is done. Now I have to draw them:
this uses the buffers and data mentioned above.
GL10 because this is for android. (This is a GLES)
Also am using UNSIGNED_SHORT because I have a short array for indices.

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
                 
                 // Point to our vertex buffer
                 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
               
                 // Draw the vertices as triangle strip
                 gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
                         
                 //Disable the client state before leaving
 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);


So I should get truncated cone that is 3D, without any cuts (meaning that the cone makes a complete circle).
The top and bottom should be empty but the walls of the cone should appear. At least that's what I thought. For some reason not even half of the cone is rendering.

Any help is greatly appreciated, sorry if this is confusing. I was trying to say all there was fast and concise.
Let me know if you have any questions.

-Nick.