OpenGL ES Vertex / Indicies

I'm just starting to learn OpenGL ES, but I'm having trouble understanding the workings of vertices and indexes. My real understanding is that the vertex is the point of the figure itself and that the indices are “triangles” at the vertex points. I am following a tutorial in which I define vertex points and indexes as shown below ...

Vertex data

-1.0f, -1.0f 1.0f, -1.0f -1.0f, 1.0f 1.0f, 1.0f

index data

0.3.1, 0.2.3

I understand that defining indices should always start from one vertex, but to me these numbers simply do not add up. When I draw this on paper, it looks like the drawn image should consist of two triangles that create the shape of a “crown”. Can someone explain why this actually draws a square instead of the “crown” that I expect?

Source code for the Square class:

public class Square { private FloatBuffer mFVertexBuffer; private ByteBuffer mColorBuffer; private ByteBuffer mIndexBuffer; public Square() { // 2D Points float[] square = { -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, }; byte maxColor = (byte) 225; /** * Each line below represents RGB + Alpha transparency */ byte colors[] = { 0, maxColor, 0, maxColor, 0, maxColor, maxColor, maxColor, 0, 0, 0, maxColor, maxColor, 0, maxColor, maxColor, }; //triangles byte[] indicies = { 0,3,1, 0,2,3 }; /** * Make sure that bytes are in correct order, otherwise they might be * drawn backwards */ ByteBuffer byteBuffer = ByteBuffer.allocateDirect(square.length * 4); byteBuffer.order(ByteOrder.nativeOrder()); mFVertexBuffer = byteBuffer.order(ByteOrder.nativeOrder()) .asFloatBuffer(); mFVertexBuffer.put(square); mFVertexBuffer.position(0); mColorBuffer = ByteBuffer.allocateDirect(colors.length); mColorBuffer.put(colors); mColorBuffer.position(0); mIndexBuffer = ByteBuffer.allocateDirect(indicies.length); mIndexBuffer.put(indicies); mIndexBuffer.position(0); } public void draw(GL10 gl) { /** * Make open GL only draw the front of the triangle (GL_CW = Graphics * Library Clockwise) * * Back of triangle will not be drawn */ gl.glFrontFace(GL11.GL_CW); /** * specifies number of elements per vertex * * specifies floating point type * * Sets stride = 0 bytes* (Stride allows to use different types of data * interchangably with opengl ) */ gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer); // 4 because we are using 4 colors in our color bufer array gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer); /** * draws the image * * first argument specifies geomety format */ gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, mIndexBuffer); // Reset to CounterClockwise gl.glFrontFace(GL11.GL_CCW); } 

}

Let me know if you need more information ...

+4
source share
2 answers

You have identified four vertices:

 2 3 0 1 

Then your indices defined two triangles: 0-3-1:

  . ... .... ..... 

and 0-2-3:

 ..... .... ... . 

they form a square.

+4
source

I don’t think your indexes are correct, try to draw the bottom line and then move to the top vertices. If I draw your indexes correctly, they are really trying to draw a square.

Try:
0, 1, 3
0, 1, 2

Instead

Edit : even I received the order mixed, corrected for error

0
source

Source: https://habr.com/ru/post/1501513/


All Articles