zoukankan      html  css  js  c++  java
  • OpenGLES入门笔记二

    #import <UIKit/UIKit.h>
    #import <QuartzCore/QuartzCore.h>
    #import <OpenGLES/ES2/gl.h>
    #import <OpenGLES/ES2/glext.h>
    
    @interface OpenGLView : UIView
    {
        CAEAGLLayer * _eaglLayer;
        EAGLContext * _context;
        GLuint _colorRenderBuffer;
        GLuint _frameBuffer;
    }
    
    @end
    // https://developer.apple.com/library/ios/documentation/OpenGLES/Reference/EAGLContext_ClassRef/index.html
    #import "OpenGLView.h"
    
    @implementation OpenGLView
    
    //!> 1.设置layer class 为 CAEAGLayer
    + (Class)layerClass
    {
        return [CAEAGLLayer class];
    }
    
    //!> 2.设置layer为Opaque
    - (void)setupLayer
    {
        _eaglLayer = (CAEAGLLayer *)self.layer;
        _eaglLayer.opaque = YES;
    }
    
    //!> 3.创建OpenGL context
    - (void)setupContext
    {
        //!> 渲染上下文提供的 OpenGL ES 的版本
        EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES2;
        _context = [[EAGLContext alloc] initWithAPI:api];
        if (!_context)
        {
            NSLog(@"Failed to initialize OpenGLES 2.0 context");
            exit(1);
        }
        if (![EAGLContext setCurrentContext:_context])
        {
            NSLog(@"Failed to set current OpenGL context");
            exit(1);
        }
    }
    
    //!> 4.创建render buffer(渲染缓冲区)
    - (void)setupRenderBuffer
    {
        // generate renderbuffer object names
        glGenRenderbuffers(1, &_colorRenderBuffer);
        // bind a renderbuffer to a renderbuffer target
        glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer);
        [_context renderbufferStorage:GL_RENDERBUFFER fromDrawable:_eaglLayer];
    }
    
    //!> 5.创建一个frame buffer(帧缓冲区)
    - (void)setupFrameBuffer
    {
        glGenFramebuffers(1, &_frameBuffer);
        glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderBuffer);
    }
    
    //!> 6.清理屏幕
    - (void)render
    {
        glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);//ClearBufferMask
        [_context presentRenderbuffer:GL_RENDERBUFFER];
    }
    
    //!> 实际操作
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            [self setupLayer];
            [self setupContext];
            [self setupRenderBuffer];
            [self setupFrameBuffer];
            [self render];
        }
        return self;
    }
    
    - (void)dealloc
    {
        _context = nil;
        
    }
    
    @end
  • 相关阅读:
    Android多种打包方式
    Service、HandlerThread、IntentService、Broadcast
    Handler、Looper、MessageQueue、Message的联系
    深入了解ANR
    深入了解OOM
    laravel的核心概念:服务提供者provider解析
    简单理解laravel框架中的服务容器,服务提供者以及怎样调用服务
    Laravel框架数据库CURD操作、连贯操作总结
    Laravel数据库操作的三种方式
    Python操作MySQL数据库9个实用实例
  • 原文地址:https://www.cnblogs.com/R0SS/p/5091619.html
Copyright © 2011-2022 走看看