2012-08-05 56 views
0

我有几个不同的3D元素,我想在不同的视图中使用OpenGL显示。如何使用iOS显示多个OpenGL ES 2.0视图?

我一直在玩这个代码< excellent tutorial>。当我只有一个元素可以使用单个视图显示时,情况显示得很好,但是如果我有多个元素,它只会显示一个元素。

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]; 
} 

这些方法大部分都是直接从教程中得到,并且最小的不相关的修改(我认为)。

有什么我需要做的,以显示所有不同的意见?这种方法会起作用还是应该做其他事情?

回答

2

我认为这个问题与在两个独立视图中创建两个独立的gl上下文有关。如果你创建两个glview,你应该在两个视图之间共享相同的上下文(很好知道:这会迫使视图在同一个线程中,否则你将在稍后遇到麻烦)。第二个选择是不断地重置每个视图的上下文。我必须说我不喜欢这两种解决方案,并且如果您认真研究OpenGL的深层知识,我强烈建议将两者融合在一起。

此处了解详情:https://stackoverflow.com/a/8134346/341358

+0

终于找到了解决我的问题,当我获得的OpenGL更多的知识,我能够返工的东西,并把一切都在一个单一的视图,并把事情的工作就是我想要的。 – GRW 2012-09-05 17:34:37