OpenGL ES顶点/索引

时间:2013-09-10 16:10:49

标签: android opengl-es

我刚刚开始学习OpenGL ES,但在理解顶点和索引的工作方式时遇到了一些麻烦。我目前的理解是顶点是形状本身上的一个点,并且索引代表顶点内的“三角形”。我正在按照下面的教程定义顶点和索引点......

顶点数据

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

指数数据

0,3,1, 0,2,3

据我所知,定义索引应始终从一个顶点开始,但对我来说这些数字不会加起来。当我在纸上绘制时,看起来绘制的实际图像应该是两个三角形,形成“冠”形状。有人可以解释为什么这实际上是在绘制一个正方形而不是我期待的“王冠”吗?

Square类的源代码:

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);

}

}

如果需要更多信息,请告诉我......

2 个答案:

答案 0 :(得分:4)

您定义了四个顶点:

2    3

0    1

然后您的索引定义了两个三角形,0-3-1:

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

和0-2-3:

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

将它们组合成一个正方形。

答案 1 :(得分:0)

我不认为你的索引是正确的,尝试绘制底线然后移动到顶部顶点。如果我正确地描绘你的索引,他们真的试图画一个正方形。

尝试:
0,1,3 0,1,2

相反

编辑:即使我将订单混淆了,也因为错误而修复