繁体   English   中英

OpenGL ES 2.0如何正确设置glVertexAttribPointers

[英]OpenGL ES 2.0 How to Correctly Setup glVertexAttribPointers

我正在尝试使用基本的XCode OpenGL ES 2.0模板渲染一些自定义3D对象。 我一直在阅读Ray Wenderlich在OpenGL ES 2.0上的博客以尝试入门。 不管我做什么,我的简单“领域”都是这样的:

在此处输入图片说明

我相信我的问题是像下面这样设置缓冲区:

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

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

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

}

有人可以告诉我我在做什么错吗? 谢谢!

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

暂无
暂无

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

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