2012-08-05 62 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