GRW
GRW

Reputation: 615

How do I display multiple OpenGL ES 2.0 views using iOS?

I have several different 3D elements I would like to display using OpenGL in different views.

I have been playing with the code from this <excellent tutorial>. Things display just fine when I only have a single element to display using a single view, but if I have multiple elements it only displays one.

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

Displays fine with only A or only Z, but with both it only displays what closest to the screen by the Z coordinate. I do explicitly set things to be non-opaque.

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

The methods are mostly straight from the tutorial with minimal non-relevant modifications (I think).

Is there something I need to do in order to display all of the different views? Will this approach work or should I be doing something else?

Upvotes: 0

Views: 1832

Answers (1)

polyclick
polyclick

Reputation: 2703

I think the problem has to do with the creation of two seperate gl contexts in the two seperate views. If you're creating two glviews you should share the same context between the two views (nice to know: this forces the views to be in the same thread otherwise you'll run into trouble later on). A second option would be to constantly reset the context for each view. I must say I don't like both of these solutions and if you're serious on developing a deeper knowledge for OpenGL i strongly advise to merge the two in one glview.

More info here: https://stackoverflow.com/a/8134346/341358

Upvotes: 2

Related Questions