简体   繁体   English

在OpenGL ES 2.0中无法通过glDrawElements绘制三角形

[英]Unable to Draw Triangles Through glDrawElements in OpenGL ES 2.0

I'm trying to draw a procedural sphere referenced here . 我正在尝试绘制此处引用的程序范围。

I modified it a bit so that I can use the glDrawElements method of OpenGL ES 2.0 我对其进行了一些修改,以便可以使用OpenGL ES 2.0的glDrawElements方法

Here's my version of createSphere: 这是我的createSphere版本:

GLfloat sphereVerticies[10000]={0.0};
GLubyte triangleIndices[15000]={0};

int createSphere (GLfloat spherePoints[], GLubyte triangleIndices[], GLfloat fRadius, GLfloat step)
{
    int points = 0;

    GLfloat uStep = DEGREES_TO_RADIANS (step);
    GLfloat vStep = uStep;

    unsigned long index=0;

    for (GLfloat u = 0.0f; u <= (2 * M_PI); u += uStep) 
    {
        for (GLfloat v = -M_PI_2; v <= M_PI_2; v += vStep) 
        {               
            triangleIndices[index++]=points;
            triangleIndices[index++]=points+1;
            triangleIndices[index++]=points+2;
            triangleIndices[index++]=points+2;
            triangleIndices[index++]=points+3;
            triangleIndices[index++]=points;

            points++;
            spherePoints[(points - 1) * 3] = fRadius * cosf(v) * cosf(u);             // x
            spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v) * sinf(u);       // y
            spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v);                 // z

            points++;
            spherePoints[(points - 1) * 3] = fRadius * cosf(v) * cosf(u + uStep);             // x
            spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v) * sinf(u + uStep);       // y
            spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v);                         // z              

            points++;
            spherePoints[(points - 1) * 3] = fRadius * cosf(v + vStep) * cosf(u);                  // x
            spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v + vStep) * sinf(u);            // y
            spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v + vStep);                      // z

            points++;
            spherePoints[(points - 1) * 3] = fRadius * cosf(v + vStep) * cosf(u + uStep);           // x
            spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v + vStep) * sinf(u + uStep);     // y
            spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v + vStep);                       // z

        }
    }        
    return points;
}

In SetupGL I have: 在SetupGL中,我有:

..
glEnable(GL_CULL_FACE);
..
..
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);

 numPoints=createSphere(sphereVerticies, triangleIndices, 30.0f, 20.0f);

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(sphereVerticies), sphereVerticies, GL_STATIC_DRAW);

    glGenBuffers(1, &_indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangleIndices), triangleIndices, GL_STATIC_DRAW);

    // New lines (were previously in draw)
    glEnableVertexAttribArray(GLKVertexAttribPosition);        
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) sphereVerticies);

glBindVertexArrayOES(0);

and finally in drawInRect: 最后在drawInRect中:

glBindVertexArrayOES(_vertexArray);   
    glDrawElements(GL_TRIANGLES, (int)(numPoints*1.5), GL_UNSIGNED_BYTE, 0);

Now what am I doing wrong here? 现在我在这里做错了什么? I don't see any visual output. 我看不到任何视觉输出。 Any help will be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

I can see two things: 我可以看到两件事:

glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) sphereVerticies);

the last argument there should be 0 , since the vertices are already buffered and bound in the VBO. 最后一个参数应该为0 ,因为顶点已经在VBO中进行了缓冲和绑定。

OpenGL ES - glVertexAttribPointer documentation OpenGL ES-glVertexAttribPointer文档

If a non-zero named buffer object is bound to the GL_ARRAY_BUFFER target (see glBindBuffer) while a generic vertex attribute array is specified, pointer is treated as a byte offset into the buffer object's data store... 如果在指定通用顶点属性数组的同时将非零命名缓冲区对象绑定到GL_ARRAY_BUFFER目标(请参阅glBindBuffer),则将指针视为缓冲区对象数据存储区中的字节偏移量。

And, 和,

glDrawElements(GL_TRIANGLES, (int)(numPoints*1.5), GL_UNSIGNED_BYTE, 0);

you should use GL_UNSIGNED_SHORT because you have more than 255 vertices. 您应该使用GL_UNSIGNED_SHORT因为您有超过255个顶点。


As for the indices, this is how you index them: 至于索引,这是您如何对其进行索引:

                 (0,1,2)        (2,3,0)
 2       3      2              2       3
 o-------o      o              o-------o
 |       |      | \            |     /
 |       |  =>  |   \          |   /
 |       |      |     \        | /
 o-------o      o-------o      o
 0       1      0       1      0

So, from this chart we can see two problems: 因此,从该图表我们可以看到两个问题:

1) The triangles do not close up the polygon 1)三角形不会关闭多边形

2) Notice the winding, the first triangle is CCW, the second CW. 2)注意绕组,第一个三角形为CCW,第二个为CW。

Try this code: 试试这个代码:

        triangleIndices[index++]=points;
        triangleIndices[index++]=points+1;
        triangleIndices[index++]=points+2;
        triangleIndices[index++]=points+3;
        triangleIndices[index++]=points+2;
        triangleIndices[index++]=points+1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM