【问题标题】:OpenGL ES Vertex / IndiciesOpenGL ES 顶点/索引
【发布时间】:2013-09-14 11:02:33
【问题描述】:

我刚开始学习 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);

}

}

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

【问题讨论】:

  • 有一点需要注意,这并不是一个 OpenGL-ES 问题,您可以在任何 OpenGL 上下文中遇到这个问题,无论它是标准 OpenGL、WebGL、GLES 还是其他任何东西。跨度>
  • 好消息,我实际上想知道在不同版本的 OpenGL 中能掌握多少知识

标签: android opengl-es


【解决方案1】:

你定义了四个顶点:

2    3

0    1

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

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

和0-2-3:

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

把它们放在一起形成一个正方形。

【讨论】:

  • 那么为什么顶点会这样排列呢?我以为他们会在广场周围顺时针或逆时针
  • OpenGL 就像一个连接点的游戏——您指定了四个点(顶点),然后将它们连接起来。顶点定义每个三角形的角(不是其中的一个点),每个索引都指定一个顶点(不是三角形)。 CW/CCW 用于去除背面;如果你做了 0-1-3 和 0-3-2 你什么都看不到。
  • CW/CCW 一般用于人脸方向。它在大多数应用程序中的主要用途是剔除面部的某一侧,但它也有其他用途。例如,有些模板操作仅适用于单面。
  • 我现在明白了,我定义我的顶点的顺序实际上很重要(请参阅我原始帖子中的 Square 数组)并将准确地指定点映射的位置,如上面所示的 fadden!知道这一点,索引很容易映射!谢谢大家!
【解决方案2】:

我认为您的索引不正确,请尝试绘制底线,然后移至顶部顶点。如果我正确地描绘了您的索引,那么他们确实是在尝试绘制正方形。

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

相反

编辑:即使我把顺序弄混了,修正了一个错误

【讨论】:

  • 我使用的当前索引实际上画了一个正方形,但我不知道为什么。您的索引在通常绘制正方形的右下角形成一个 90 度三角形。右下角是 90 度角。
  • 其中一个三角形是不可见的,因为它是逆时针缠绕的。
  • 哇,即使我混淆了 base 0 v. base 1 数组/索引位置,我以为我正在绘制地平线的底部 2 个索引,但我正在绘制对角线。我现在就修。
猜你喜欢
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多