OS X的CAOpenGLLayer中如何启用OpenGL3.2 core profile


在OS X的openGL编程中,我们有时为了想在自己的OpenGL图层上再加些自己的某些涂层,必须得用CAOpenGLLayer而不是NSOpenGLView,由于在NSOpenGLView上添加任何子视图都会变得无效。

其实,在CAOpenGLLayer自定义的子类中要追加支持OpenGL Core Profile很简单,只需要重写其

- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask

方法即可。如以下代码所示:

// 重写父类的方法,提供自己的CGLPixelFormatObj
- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask
{
    CGLPixelFormatAttribute attribs[] =  {
       
        kCGLPFADisplayMask, 0,
        kCGLPFAColorSize, 24,
        kCGLPFAAccelerated,
        kCGLPFADoubleBuffer,
       
        // Use OpenGL 3.2 Core Profile
        kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core,
       
        // Use multi-sample
        kCGLPFAMultisample,
        kCGLPFASampleBuffers, (CGLPixelFormatAttribute)1,
        kCGLPFASamples, (CGLPixelFormatAttribute)4,
       
        0
    };
    // 将kCGLPFADisplayMask设置为传递过来的display mask。这个步骤是必须的!
    attribs[1] = mask;
   
    CGLPixelFormatObj pixFormatObj = NULL;
    GLint numPixFormats = 0;
    CGLChoosePixelFormat(attribs, &pixFormatObj, &numPixFormats);
   
    return pixFormatObj;
}

 

 

然后,我们可以在自己的子类中添加对CGLContextObj对象的引用来做一些标记。另外,我们必须重写CAOpenGLLayer的这个方法:

- (void)drawInCGLContext:(CGLContextObj)glContext

            pixelFormat:(CGLPixelFormatObj)pixelFormat

            forLayerTime:(CFTimeInterval)timeInterval

            displayTime:(const CVTimeStamp *)timeStamp

比如像以下代码所示:

- (void)drawInCGLContext:(CGLContextObj)glContext
            pixelFormat:(CGLPixelFormatObj)pixelFormat
            forLayerTime:(CFTimeInterval)timeInterval
            displayTime:(const CVTimeStamp *)timeStamp
{
    // 这里必须先将传进来的上下文作为当前OpenGL执行上下文对象,否则后续对OpenGL的状态设置都将无效
    CGLSetCurrentContext(glContext);
   
    [self setupContext];
   
    // mContext主要用于判别当前OpenGL上下文是否已经设置好,以及在render方法中的引用
    mContext = glContext;
   
    [self render];
}

 

- (void)setupContext是自定义方法,在里面做顶点设置、全局启用某些OpenGL功能状态,并设置viewport等等。

- (void)render也是自定义方法,用来做真正的图形绘制。

如以下代码所示:

- (void)render
{
    // render
    glClear(GL_COLOR_BUFFER_BIT);
   
    glDrawArrays(GL_LINE_STRIP, 0, 721);
   
    glFlush();
   
    CGLFlushDrawable(mContext);
}

随后,我们自己提供shader代码,并进行加载即可。这些可以放在setupContext自定义方法中实现。

当然,在OS X中要使用OpenGL core profile必须引入<OpenGL/gl3.h>这个头文件。目前,3.2 core profile以及4.1 core profile都是用此头文件。

OpenGL编程指南(原书第7版)中文扫描版PDF 下载

-------------------------------------分割线-------------------------------------

OpenGL 渲染篇

Ubuntu 13.04 安装 OpenGL

OpenGL三维球体数据生成与绘制【附源码】

Ubuntu下OpenGL编程基础解析

如何在Ubuntu使用eclipse for c++配置OpenGL

《OpenGL超级宝典》学习笔记

本文永久更新链接地址

相关内容