iphone - 指定非全屏 OpenGL ES View

标签 iphone opengl-es coordinates

有人知道如何创建不使用整个 iPhone 屏幕尺寸作为绘图表面的 OpenGL ES 应用程序吗?

到目前为止,我所看到的所有内容都将 EAGLView(或其他)绘制为 0、0、320、480。

我希望我的 EAGLView 为 100、100、100、100。 这可能吗?看来我需要做的就是更改 glViewport() 调用,但不确定... glViewport 是否只是为全屏 OpenGL ES View 设置一个剪切矩形?或者它实际上定义了原点 (0, 0) 在哪里?

这非常令人困惑,尤其是当您尝试从 UIView 角度思考它时,我就是这样。我想将 CGRect 传递到我的初始化函数中,以便它将 glViewport 和其他所有内容设置在该矩形中。

谢谢!

最佳答案

您需要做的就是在启动它时使用 EAGLView 类中您想要的框架调用 [super initWithFrame:frame] 。此示例将在左上角创建一个透明的 OpenGL View 并将其添加到您的 Controller View 中。

我的 EAGLView 类中有以下代码:

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

然后我从我的 Controller 创建 EAGLView,如下所示:

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

在我的 EAGLView 类中进行实际绘图的代码是从 Controller 调用的,代码如下:

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

关于iphone - 指定非全屏 OpenGL ES View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1391501/

相关文章:

iphone - 影响应用程序性能的 AVAudioPlayer 声音

Android OpenGL - 帮助进行 C++ 转换

android - android中的 Canvas 在不同设备上是成比例的

ios - 如何从这种形式的xml中获取坐标 "lat,long"

使用 iOS 11 的 iPhone 使用相机并锁定屏幕后 iOS 应用程序崩溃

iphone - iOS 上的自定义手势。 iPad

iphone - iBooks 应用程序如何格式化不同页面上的文本?

iphone - 在iPhone上使用OpenGL ES 2.0渲染大小> 1024的UIImage

android - OpenGL ES 2.0 - Matrix.looktAtM 究竟如何与 glOrtho 交互?

google-maps - 如何将 MTM(加拿大)坐标转换为纬度和经度