gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:25:49 25 4
gpt4 key购买 nike

我正在尝试绘制引用 here 的程序球体.

我稍微修改了一下,以便可以使用 OpenGL ES 2.0 的 glDrawElements 方法

这是我的 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;
}

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

最后在 drawInRect 中:

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

现在我在这里做错了什么?我没有看到任何视觉输出。任何帮助将不胜感激。谢谢。

最佳答案

我可以看到两件事:

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

最后一个参数应该是 0,因为顶点已经缓冲并绑定(bind)在 VBO 中。

OpenGL ES - glVertexAttribPointer documentation

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...

还有,

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

你应该使用 GL_UNSIGNED_SHORT 因为你有超过 255 个顶点。


至于索引,这是你索引它们的方式:

                 (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

所以,从这个图表我们可以看出两个问题:

1) 三角形不闭合多边形

2) 注意绕组,第一个三角形是 CCW,第二个是 CW。

试试这段代码:

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

关于iphone - 无法在 OpenGL ES 2.0 中通过 glDrawElements 绘制三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9765986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com