2

我无法使用超过 1 对顶点和索引缓冲区对象来渲染我的所有对象。为了检查一切,我只初始化了 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。索引数组是 GLushort 类型。

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

4

1 回答 1

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);
于 2012-08-11T00:26:31.920 回答