gpt4 book ai didi

iphone - OpenGL ES 2.0 如何正确设置 glVertexAttribPointers

转载 作者:行者123 更新时间:2023-11-29 11:00:48 26 4
gpt4 key购买 nike

我正在尝试使用基本的 XCode OpenGL ES 2.0 模板渲染一些自定义 3D 对象。我一直在阅读 Ray Wenderlich 关于 OpenGL ES 2.0 的博客以尝试开始。无论我做什么,我的简单“球体”看起来都是这样的:

enter image description here

我认为我的问题在于像下面这样设置缓冲区:

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 8, BUFFER_OFFSET(6));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 8, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 8, BUFFER_OFFSET(3));

我有来自外部程序的顶点数据,如下所示:

// The vertex data is saved in the following format:
// u0,v0,normalx0,normaly0,normalz0,x0,y0,z0
// u1,v1,normalx1,normaly1,normalz1,x1,y1,z1
// u2,v2,normalx2,normaly2,normalz2,x2,y2,z2
// ...

#define Ball_vertexcount 559
#define Ball_polygoncount 960

float Ball_vertex[Ball_vertexcount][8]={
{0.03125, 0.00000, -0.00000, 1.00000, 0.00000, 0.00000, 1.00000, 0.00000},
{0.03125, 0.06250, 0.03806, 0.98079, 0.19132, 0.03806, 0.98079, 0.19134},
{0.00000, 0.06250, -0.00000, 0.98079, 0.19507, 0.00000, 0.98079, 0.19509},
{0.03125, 0.12500, 0.07465, 0.92389, 0.37530, 0.07466, 0.92388, 0.37533},
...

int Ball_index[Ball_polygoncount][3]={
{0, 1, 2},
{1, 3, 4},
{4, 2, 1},
{3, 5, 6},
...

Apple 有一个偏移量定义,如下所示:

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

我的 setupGl 函数包括:

glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);

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

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

// u0,v0,normalx0,normaly0,normalz0,x0,y0,z0

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 8, BUFFER_OFFSET(6));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 8, BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 8, BUFFER_OFFSET(3));

glBindVertexArrayOES(0);

我对 glVertexAttribPointers 的理解如下:

• The first parameter specifies the attribute name to set. We just use the pre- defined constants GLKit set up.
• The second parameter specifies how many values are present for each vertex. If you look back up at the Vertex struct, you'll see that for the position there are three floats (x,y,z) and for the color there are four floats (r,g,b,a).
• The third parameter specifies the type of each value - which is float for both Posi- tion and Color.
• The fourth parameter is always set to false.
• The fifth parameter is the size of the stride, which is a fancy way of saying "the size of the data structure containing the per-vertex data". So we can simply pass in sizeof(Vertex) here to get the compiler to compute it for us.
• The final parameter is the offset within the structure to find this data. We can use the handy offsetof operator to find the offset of a particular field within a struc-ture.

我的 glkView 看起来像这样:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Render the object with GLKit
[self.effect prepareToDraw];

glBindVertexArrayOES(_vertexArray);

glDrawElements(GL_TRIANGLES, sizeof(Ball_index)/sizeof(Ball_index[0]), GL_UNSIGNED_SHORT, 0);

}

有人可以阐明我做错了什么吗?谢谢!

最佳答案

第五个参数,stride的大小,应该以字节为单位。
8*sizeof(float)
最后一个参数offset应该是指向具体数据的指针,BUFFER_OFFSET应该是这样的
#define BUFFER_OFFSET(a) a*sizeof(float)

关于iphone - OpenGL ES 2.0 如何正确设置 glVertexAttribPointers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16005753/

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