我正在尝试使用GLKit渲染两个对象。最初我试图这样做:
[Cube drawViewController:self.effect CameraMatrix:&lookatmatrix];
//turn off lighting for floor
self.effect.light0.enabled = GL_FALSE;
[Floor drawViewController:self.effect CameraMatrix:&lookatmatrix];
self.effect.light0.enabled = GL_TRUE;
然而,这似乎会导致内存泄漏。我读到了某个地方:
“基本”属性(lightingType,lightModelTwoSided,colorMaterialEnabled,...)的每次更改都将导致新的着色器程序加载下一个“prepareToDraw”调用。
如果我删除self.effect.light0.enabled更改并运行以下内容我没有内存泄漏问题:
[Cube drawViewController:self.effect CameraMatrix:&lookatmatrix];
//turn off lighting for floor
//self.effect.light0.enabled = GL_FALSE;
[Floor drawViewController:self.effect CameraMatrix:&lookatmatrix];
//self.effect.light0.enabled = GL_TRUE;
我尝试创建2个不同的GLKBaseEffects,但第二个对象不能正确渲染地板。
drawViewController调用prepareToDraw,如下所示:
-(void) drawViewController:(GLKBaseEffect *)effect CameraMatrix:(GLKMatrix4 *)cameramatrix
{
GLKMatrix4 modelViewMatrix = GLKMatrix4Multiply(*cameramatrix, modelMatrix);
effect.transform.modelviewMatrix = modelViewMatrix;
[effect prepareToDraw];
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, *TextureID);
glBindVertexArrayOES(modelvertdata.vertexArray);
glDrawArrays(GL_TRIANGLES, 0, modelvertdata.NumVerts);
}