我有几个不同的 3D 元素,我想使用 OpenGL 在不同的视图中显示。
我一直在玩这个<优秀教程>中的代码。当我只有一个元素要使用单个视图显示时,事情显示得很好,但如果我有多个元素,它只会显示一个。
IBOutlet UIView *openGL;
openGLA = [[OpenGLView alloc] initWithFrame:screenBounds Vertices:[self renderVertices:[self getBucketA]] Indices:[self renderIndices:[self getBucketA]]];
openGLZ = [[OpenGLView alloc] initWithFrame:screenBounds Vertices:[self renderVertices:[self getBucketZ]] Indices:[self renderIndices:[self getBucketZ]]];
[openGL addSubview:openGLA];
[openGL addSubview:openGLZ];
[openGLA render];
[openGLZ render];
[openGLA release];
[openGLZ release];
仅使用 A 或仅显示 Z 可以正常显示,但两者都仅显示 Z 坐标最接近屏幕的内容。我确实明确地将事物设置为非透明的。
@interface OpenGLView : UIView
- (void)setupLayer
{
_eaglLayer = (CAEAGLLayer*) self.layer;
_eaglLayer.opaque = NO;
}
- (id)initWithFrame:(CGRect)frame Vertices:(NSMutableArray *)vertices Indices:(NSMutableArray *)indices
{
self = [super initWithFrame:frame];
if (self)
{
[self setupLayer];
[self setupContext];
[self setupDepthBuffer];
[self setupRenderBuffer];
[self setupFrameBuffer];
[self compileShaders];
[self setupVBOs];
}
return self;
}
- (void)render
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:10];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
[modelView populateFromTranslation:CC3VectorMake(0, 0, -7)];
[modelView rotateBy:CC3VectorMake(20, -45, -20)];
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
glDrawElements(GL_TRIANGLES, indicesSize/sizeof(Indices[0]), GL_UNSIGNED_SHORT, 0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
这些方法大多直接来自本教程,并进行了最少的非相关修改(我认为)。
为了显示所有不同的视图,我需要做些什么吗?这种方法会奏效还是我应该做其他事情?