OpenGL学习笔记:iOS下GLES环境配置
Posted 计算机科学家的世界
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenGL学习笔记:iOS下GLES环境配置相关的知识,希望对你有一定的参考价值。
1,OpenGL 与 OpenGLES(GLES)
在OpenGL学习笔记:(1)中已经讲清楚了OpenGL是什么以及一些想关的基础知识,此小节主要讲GLES和OpenGL的关系,从一种角度上讲,OpenGL(4.3及后续版本)是GLES(ES3.0)的超级,在ES中有的东西在OpenGL中都有,当然,GLES主要用于移动设备的渲染(android、ios、MeeGo等),而OpenGL 主要用于PC设备的渲染( Linux、OSX、Windows等)。2,OpenGL iOS环境搭建需要条件
需要XCode、OSX系统、iOS设备(用模拟器也可以,如果要进行大规模渲染,最好使用iOS硬件设备)、开发者AppID(如果使用iOS设备调度程序的话需要开发者AppID,这个需要付钱的)。3,环境搭建过程
1,安装XCode,我使用的公司的Mac Mini,使用Mac Pro、Mac Air、Mac等其它OSX设备均可安装XCode,使用 AppID在AppStore里面下载即可,安装后配置好AppId(需要开发者权限的AppId)。2,打开XCode,创建一个iOS single view工程(File -> New -> Project),如下图所求



5,将AppDelegate.m和ViewController.m改名为AppDeledate.mm和ViewController.mm(在左边的文件列表里面选中文件,然后右边可以改名,如下图所求)

6,将ViewController.h改成如下内容
//
// ViewController.h
// GLBug
//
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
@interface ViewController : GLKViewController
@end
7,将ViewController.mm改成如下内容
//
// ViewController.mm
// GLBug
//
#import "ViewController.h"
id g_GLKViewContext;
id g_GLKViewSharedContext;
ViewController* gViewController = nil;
static bool gInited = false;
@interface ViewController ()
@property (strong, nonatomic) EAGLContext *context;
@property (strong, nonatomic) EAGLContext *sharedContext;
- (void)InitGLES;
- (void)Render;
- (void)Tick;
- (void)PreRender;
- (void)PostRender;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context)
NSLog(@"Failed to create ES context");
self.sharedContext = [[EAGLContext alloc] initWithAPI:[self.context API] sharegroup:[self.context sharegroup]];
if (!self.sharedContext)
NSLog(@"Failed to create shared ES context");
g_GLKViewContext = self.context;
g_GLKViewSharedContext = self.sharedContext;
GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
gViewController = self;
[self InitGLES];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (void)InitGLES
[EAGLContext setCurrentContext:self.context];
gInited = true;
- (void)Render
- (void)Tick
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
if(!gInited)
NSLog(@"Not inited...");
else
[self PreRender];
[self Tick];
[self Render];
[self PostRender];
- (void)PreRender
- (void)PostRender
@end
8,修改Main.storyboard,如下图所求(将View改为GLKView)


以上是关于OpenGL学习笔记:iOS下GLES环境配置的主要内容,如果未能解决你的问题,请参考以下文章
OpenGL ES 学习教程(十七) Unity GPU Instance 原理及 GLES 实现
openGL之API学习(一七七)opengl gles glsl glsl es版本对应关系