Specifying a non-full-screen view of OpenGL ES

Does anyone know how to create an OpenGL ES application that does not use all of the iPhone screen sizes as its drawing surface?

Everything I have seen so far has an EAGLView drawing (or something else) at 0, 0, 320, 480.

I would like to have my EAGLView at 100, 100, 100, 100. Is this possible? It seems that all I need to do is change the call to glViewport (), but I'm not sure ... glViewport just set the clipping rectangle for the full-screen view of OpenGL ES? Or does it really determine where the start is (0, 0)?

This is very confusing, especially when you try to think about it in terms of the UIViews that I am. I would like to pass CGRect to my initialization function so that it sets glViewport and everything else to be in that rectangle.

Thanks!

+5
source share
1 answer

All you have to do is call [super initWithFrame: frame] with the frame you want from your EAGLView class when you initiate it. This example creates a transparent OpenGL view in the upper left corner and adds a view of the controllers to it.

I have the following code in the EAGLView class:

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // get the layer
        CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
        eaglLayer.opaque = NO;
        eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithBool:NO], 
                                        kEAGLDrawablePropertyRetainedBacking, 
                                        kEAGLColorFormatRGBA8, 
                                        kEAGLDrawablePropertyColorFormat, 
                                        nil];
        context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];     
        if (!context || ![EAGLContext setCurrentContext:context]) {
            [self release];
            return nil;
        }
        [EAGLContext setCurrentContext:context];
        [self destroyFramebuffer];
        [self createFramebuffer];           
    }
    return self;
}

And then I create an EAGLView from my controller as follows:

theEAGLView = [[EAGLView alloc] initWithFrame:CGRectMake(30.0f, 30.0f, 
                                                         90.0f, 70.0f)];
theEAGLView.opaque = NO;
[self.view addSubview:theEAGLView];

, EAGLView, :

- (void)render {
    glViewport(0, 0, backingWidth, backingHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrthof(0.0f, backingWidth, 0.0f, backingHeight, 1000.0f, -1000.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    // this is where we draw our stuff
    // ...
    // ...

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}
+6

All Articles