• 笔谈OpenGL ES(二)


            昨晚回家也看了OpenGL ES 2.0 iOS教程的第一篇,对于其中涉及的一些基本知识罗列下,虽然自己做iOS开发一年多了,但是对于一些细节没有注意,真正的把自己当成“应用”工程师了 ,不仅要会用,也需要深入的理解,这样进步的才快,所以是需要经常做笔记的。

             [OpenGL ES 01]OpenGL ES之初体验 中提到了 + (Class)layerClass 方法,来看看这玩意到底有什么用。

            @interface OpenGLView : UIView 中 OpenGLView 继承于 UIView 类,所以可以访问到 UIView 类的成员变量和方法。UIView 类中有个 layer 属性,用来返回主 CALayer 实例,UIView 类中+ (Class)layerClass 方法,可以返回主 layer 所使用的类。进入iOS SDK 中的 UIKit.h中查看  UIView 类

    NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace> 

    @property(nonatomic,readonly,retain)                 CALayer  *layer;              // returns view's layer. Will always return a non-nil value. view is layer's delegate

    + (Class)layerClass;                        // default is [CALayer class]. Used when creating the underlying layer for the view.

            重写+ (Class)layerClass 方法可以来让UIView使用不同的CALayer来显示,所以就有了

    + (Class)layerClass {
        // 只有 [CAEAGLLayer class] 类型的 layer 才支持在其上描绘 OpenGL 内容。
        return [CAEAGLLayer class];
    }

    使用 CAEAGLLAYER 类型的 layer 进行内容的绘制。

            用 CALayer 进行图形绘制的时候,常用到其背景色 @property CGColorRef backgroundColor 

    /* The background color of the layer. Default value is nil. Colors
     * created from tiled patterns are supported. Animatable. */

    @property CGColorRef backgroundColor;

    CALayer 的背景色的设置与UIView 的背景色设置的方式为

    yellowLayer = [[CALayer alloc] init];
    yellowLayer.backgroundColor = [UIColor yellowColor].CGColor; // CALayer 的背景色设置

    blackView = [[UIView alloc] init];
    blackView.backgroundColor = [UIColor yellowColor]; // UIView 的背景色设置

            这里顺便提一下 UIColor,有的小公司的笔试题神经病啊,问 UIColor 是不是对象?呵呵,UIColor 的声明为

    NS_CLASS_AVAILABLE_IOS(2_0) @interface UIColor : NSObject <NSSecureCoding, NSCopying> {
      @private
    }

            UIColor类方法有很多,列举一个看下就行了,

    // Convenience methods for creating autoreleased colors
    + (UIColor *)colorWithWhite:(CGFloat)white alpha:(CGFloat)alpha;

            关于更多 UIView 与 CALayer 之间的关联可以看下这篇文章UIView与CALayer的区别,很详细

            还有一个有意思的值得研究的地方,就是指针类型之间的强转,父类指针强转为子类指针、子类指针强转为父类指针,这里面涉及的东西就很有意思了,更多的时候,知道是那么回事,但为什么就要那么做,什么原理?去腾讯面试过iOS工程师,被问及很多原理层面的东西,不求甚解这种做法是弊端很多的,摆在眼前的是,那肯定是进不了牛X的互联网公司的。

    - (void)setupLayer
    {
        _eaglLayer = (CAEAGLLayer*) self.layer;
        
        // CALayer 默认是透明的,必须将它设为不透明才能让其可见
        _eaglLayer.opaque = YES;
        
        // 设置描绘属性,在这里设置不维持渲染内容以及颜色格式为 RGBA8
        _eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
    }

    这里 _eaglLayer = (CAEAGLLayer*) self.layer; 转化的依据是什么?这就跟指针与内存分配扯上关系了。

    - (void)layoutSubviews {
        [self setupLayer];        
        [self setupContext];
        
        [self destoryRenderAndFrameBuffer];
        [self setupRenderBuffer];        
        [self setupFrameBuffer];    
        
        [self render];
    }

            layoutSubviews 会在什么时候触发?具体可以看下 解析LayoutSubviews

             OpenGL ES 2.0 iOS教程 中引用了OpenGL ES 2.0 for iPhone 、 OpenGL ES 2.0 Programming Guide ,虽然我研究生毕业,也是科班出身,但是看中文肯定比看英文效率高嘛,还是到网上搜搜看看有没有中文版的,还真有。感谢有奉献精神的程序员们,这2篇英文文档对应的中文版为:OpenGL ES2.0 – Iphone开发指引OpenGL ES 2.0 Programming Guide 编程指南 中文版 。所以,我可不能浪费了这干货了。

            还有一篇值得看,网友在学习《OpenGL ES Profile Specification 2.0.24 (Difference Specification) (April 1, 2009) (Annotated)》的过程中提取出来了其中的一些内容,是中文哦。CSDN下载地址:OpenGL ES 2.0规范(中文版),为防止资源失效,我把它存到了百度网盘:http://pan.baidu.com/s/1mgCCdUK

  • 相关阅读:
    LabVIEW入门第六天(布尔控件及布尔量)
    LabVIEW如何加载显示GIF动画
    C#打开EXCEL或保存文件时报错:System.InvalidOperationException:未在本地计算机上注册” Microsoft.ACE.OLEDB.12.0“提供程序。...
    SQL 怎么设置自定义服务器名称登录
    C#打开EXCEL或保存文件时报错:System.InvalidOperationException:未在本地计算机上注册” Microsoft.ACE.OLEDB.12.0"提供程序。
    半平面交
    poj1038
    poj2318 && poj2398
    poj1696
    poj2430
  • 原文地址:https://www.cnblogs.com/sunminmin/p/4451879.html
Copyright © 2020-2023  润新知