当前位置: 动力学知识库 > 问答 > 编程问答 >

android - OpenGL ES Vertex / Indicies

问题描述:

I'm just starting to learn OpenGL ES but am having some trouble understanding how the vertex and indices work. My current understanding is that a Vertex is a point on the shape itself, and that the indices represent the 'triangles' within the vertex points. I'm following a tutorial that has me define the vertex and indices points as below...

Vertex data

-1.0f, -1.0f

1.0f, -1.0f

-1.0f, 1.0f

1.0f, 1.0f

indices data

0,3,1,

0,2,3

I understand that defining indices should always start at one vertex but to me these numbers just dont add up. When I draw this on paper it looks like the actual image drawn should be two triangles together that create a 'crown' shape. Can someone explain why this is actually drawing a square instead of the 'crown' that I am expecting?

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 more info is needed...

网友答案:

You defined four vertices:

2    3

0    1

Your indices then defined two triangles, 0-3-1:

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

and 0-2-3:

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

put together they form a square.

网友答案:

I don't think your indexes are correct, try drawing the bottom line then moving to the top verts. If I am picturing your indexes correctly, they really are trying to draw a square.

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

Instead

Edit: Even I got the order mixed up, fixed for a mistake

分享给朋友:
您可能感兴趣的文章:
随机阅读: