使用多个顶点和索引缓冲区对象进行渲染(ios - OpenGLES 2.0)

时间:2012-08-11 00:16:12

标签: iphone ios ipad opengl-es opengl-es-2.0

我无法使用一对以上的顶点和索引缓冲区对象渲染我的所有对象。为了检查所有内容,我只初始化了3个对象并进行渲染。这导致前两个几何形状失真,第三个对象几何渲染稍微好一些(不完美)。

当我刚刚初始化所有3但仅渲染时,它再次显示扭曲的几何体,并且不知何故第三个几何体更加可见(即使我没有渲染它)。

但是,如果我正在初始化并渲染其中任何一个,它渲染得很好(完美)。 这是我的代码:

float tempAngles[4] = {0, 60, 180, 360};
pieOne = [[IVNode alloc]initWithPieGeometry:0.75 thickness:0.20 startAngle:tempAngles[0]*M_PI/180   andEndAngle:tempAngles[1]*M_PI/180];

glGenBuffers(1, &vertexBuffer1);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer1);//vertexBuffer[i]);
glBufferData(GL_ARRAY_BUFFER, [pieOne.pie getVertexSize]*sizeof(GLfloat), [pieOne.pie returnVertexArray] , GL_STATIC_DRAW);

glGenBuffers(1, &indexBuffer1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer1);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, [pieOne.pie getIndicesSize]*sizeof(GLushort),[pieOne.pie returnIndexArray], GL_STATIC_DRAW);

pieTwo = [[IVNode alloc]initWithPieGeometry:1.0 thickness:0.20 startAngle:tempAngles[1]*M_PI/180 andEndAngle:tempAngles[2]*M_PI/180];

glGenBuffers(1, &vertexBuffer2);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer2);//vertexBuffer[i]);
glBufferData(GL_ARRAY_BUFFER, [pieTwo.pie getVertexSize]*sizeof(GLfloat), [pieTwo.pie returnVertexArray] , GL_STATIC_DRAW);

glGenBuffers(1, &indexBuffer2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer2);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, [pieTwo.pie getIndicesSize]*sizeof(GLushort),[pieTwo.pie returnIndexArray], GL_STATIC_DRAW);


pieThree = [[IVNode alloc]initWithPieGeometry:0.75 thickness:0.20 startAngle:tempAngles[2]*M_PI/180 andEndAngle:tempAngles[3]*M_PI/180];

glGenBuffers(1, &vertexBuffer3);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer3);//vertexBuffer[i]);
glBufferData(GL_ARRAY_BUFFER, [pieThree.pie getVertexSize]*sizeof(GLfloat), [pieThree.pie returnVertexArray] , GL_STATIC_DRAW);

glGenBuffers(1, &indexBuffer3);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer3);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, [pieThree.pie getIndicesSize]*sizeof(GLushort),[pieThree.pie returnIndexArray], GL_STATIC_DRAW);

//#define BUFFER_OFFSET(i) ((char *)NULL + (i))
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), BUFFER_OFFSET(0));
glEnableVertexAttribArray(GLKVertexAttribNormal);
glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 6*sizeof(GLfloat), BUFFER_OFFSET(12));

现在在我的渲染功能中,我有:

-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{

glClearColor(0.77f, 0.88f, 1.0f, 1.0f);     
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


float yellow[3][4] = {1.0f, 1.0f, 0.0f, 1.0f,
    1.0f, 0.0f, 0.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f};

glUseProgram(_program);
//1st   
GLKMatrix4 model = GLKMatrix4Identity;// GLKMatrix4MakeTranslation(1.5, 0, 0);
_modelViewProjectionMatrix = GLKMatrix4Multiply(_modelViewProjectionMatrix, model);

glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0,_normalMatrix.m);
glUniform4f(uniforms[COLOR_VECTOR], yellow[0][0], yellow[0][1], yellow[0][2], yellow[0][3]);   

//bind corresponding buffer before drawing
glBindBuffer(GL_ARRAY_BUFFER, indexBuffer1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer1);
glDrawElements(GL_TRIANGLES, [pieOne.pie getIndicesSize], GL_UNSIGNED_SHORT, (void*)0);

//2nd    
model = GLKMatrix4MakeTranslation(0, -1, 0);
_modelViewProjectionMatrix = GLKMatrix4Multiply(_modelViewProjectionMatrix, model);

glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0,_normalMatrix.m);//
glUniform4f(uniforms[COLOR_VECTOR], yellow[1][0], yellow[1][1], yellow[1][2], yellow[1][3]);   

//bind corresponding buffer before drawing
glBindBuffer(GL_ARRAY_BUFFER, indexBuffer2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer2);
glDrawElements(GL_TRIANGLES, [pieTwo.pie getIndicesSize], GL_UNSIGNED_SHORT,(void*)0);

//3rd
model = GLKMatrix4MakeTranslation(-1.5, 0, 0);
_modelViewProjectionMatrix = GLKMatrix4Multiply(_modelViewProjectionMatrix, model);
glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0,_normalMatrix.m);
glUniform4f(uniforms[COLOR_VECTOR], yellow[2][0], yellow[2][1], yellow[2][2], yellow[2][3]);   

//bind corresponding buffer before drawing
glBindBuffer(GL_ARRAY_BUFFER, indexBuffer3);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer3);
glDrawElements(GL_TRIANGLES, [pieThree.pie getIndicesSize], GL_UNSIGNED_SHORT, (void*)0);

}

getIndicesSize 返回indexArray中的元素数。 getVertexSize 返回vertexArray中的元素数。 returnVertexArray 返回几何体的顶点数组。 returnIndexArray 返回几何的索引数组。 顶点数组的类型为GLfloat。 index数组的类型为GLushort。

生成的顶点数组中的元素数是24522。 生成的索引数组中的元素数为22680。 在互联网上,我发现只有多个VBO / IBO的例子没有使用GLkit,他们在初始化缓冲区和渲染方面完全正如我所做的那样。 我在这个问题上花了将近2天的时间。我觉得有一些非常基本的东西遗失了。不知何故,最后一个绑定缓冲区会影响我感觉到的所有其他几何(基于我上面解释的结果)。这可能是一个模拟器问题(我不这么认为)?快速回复将不胜感激。感谢

1 个答案:

答案 0 :(得分:1)

您似乎对glVertexAttribPointer函数应如何工作有些困惑。当你致电glVertexAttribPointer时,它告诉OpenGL:

  

在下一次绘制调用中,从当前绑定缓冲区的地址开始渲染,再加上提供的缓冲区偏移量。

在此之后,如果更改绑定的缓冲区并不重要,因为指针仍然指向函数调用时绑定的缓冲区。

因此在此代码中:

glBindBuffer(GL_ARRAY_BUFFER, indexBuffer1);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer1);
glDrawElements(GL_TRIANGLES, [pieOne.pie getIndicesSize], GL_UNSIGNED_SHORT, (void*)0);

调用glBindBuffer(GL_ARRAY_BUFFER)在这里没有任何作用,因为你没有更新指针。

glBindBuffer(GL_ARRAY_BUFFER, indexBuffer1);
//need to set glVertexAttribPointers here
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer1);
glDrawElements(GL_TRIANGLES, [pieOne.pie getIndicesSize], GL_UNSIGNED_SHORT, (void*)0);