首先在 xcode 中新建一個工程县钥,并導入一張圖片良价。然后在 ViewController.m 寫入以下代碼:
#import "ViewController.h"
#import <GLKit/GLKit.h>
typedef struct {
GLKVector3 vertexCoordinate; //頂點坐標
GLKVector2 textureCoordinate; //紋理坐標
}VertexData;
//頂點數(shù)
static NSInteger const kVertexCount = 36;
@interface ViewController ()<GLKViewDelegate>
@property(nonatomic,strong)GLKView * glkView;
@property(nonatomic,strong)GLKBaseEffect * baseEffect;
@property(nonatomic,assign)VertexData * vertices;
@property(nonatomic,strong)CADisplayLink * displayLink;
@property(nonatomic,assign)NSInteger angle;
@property(nonatomic,assign)GLuint * vertexBuffer;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//1.設置背景顏色
self.view.backgroundColor = [UIColor blackColor];
//2.設置 OpenGL ES 相關初始化配置
[self setUpConfig];
//3.圖形相關的頂點/紋理坐標數(shù)據(jù)
[self setUpVertexData];
//4.添加CADisplayLink
[self addCADisplayLink];
}
-(void)setUpConfig
{
//1.創(chuàng)建context并設置當前context
EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
[EAGLContext setCurrentContext:context];
//2.創(chuàng)建GLKView并設置代理
self.glkView = [[GLKView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.width) context:context];
self.glkView.backgroundColor = [UIColor clearColor];
self.glkView.delegate = self;
//3.設置使用的深度緩沖區(qū)個數(shù)
self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat24;
//4.添加GLKView
[self.view addSubview:self.glkView];
//5.獲取紋理圖片
NSString * filePath = [[NSBundle mainBundle] pathForResource:@"mei" ofType:@"jpg"];
UIImage * image = [UIImage imageWithContentsOfFile:filePath];
//6.設置紋理參數(shù)
NSDictionary * options = @{GLKTextureLoaderOriginBottomLeft:@(YES)};
GLKTextureInfo * textureInfo = [GLKTextureLoader textureWithCGImage:image.CGImage options:options error:NULL];
//7.創(chuàng)建GLKBaseEffect
self.baseEffect = [[GLKBaseEffect alloc] init];
self.baseEffect.texture2d0.name = textureInfo.name;
self.baseEffect.texture2d0.target = textureInfo.target;
}
-(void)setUpVertexData
{
//1.開辟頂點數(shù)據(jù)空間
self.vertices = malloc(sizeof(VertexData) * kVertexCount);
//2.設置頂點/紋理坐標數(shù)據(jù)
// 前面
self.vertices[0] = (VertexData){{-0.5, 0.5, 0.5}, {0, 1}};
self.vertices[1] = (VertexData){{-0.5, -0.5, 0.5}, {0, 0}};
self.vertices[2] = (VertexData){{0.5, 0.5, 0.5}, {1, 1}};
self.vertices[3] = (VertexData){{-0.5, -0.5, 0.5}, {0, 0}};
self.vertices[4] = (VertexData){{0.5, 0.5, 0.5}, {1, 1}};
self.vertices[5] = (VertexData){{0.5, -0.5, 0.5}, {1, 0}};
// 上面
self.vertices[6] = (VertexData){{0.5, 0.5, 0.5}, {1, 1}};
self.vertices[7] = (VertexData){{-0.5, 0.5, 0.5}, {0, 1}};
self.vertices[8] = (VertexData){{0.5, 0.5, -0.5}, {1, 0}};
self.vertices[9] = (VertexData){{-0.5, 0.5, 0.5}, {0, 1}};
self.vertices[10] = (VertexData){{0.5, 0.5, -0.5}, {1, 0}};
self.vertices[11] = (VertexData){{-0.5, 0.5, -0.5}, {0, 0}};
// 下面
self.vertices[12] = (VertexData){{0.5, -0.5, 0.5}, {1, 1}};
self.vertices[13] = (VertexData){{-0.5, -0.5, 0.5}, {0, 1}};
self.vertices[14] = (VertexData){{0.5, -0.5, -0.5}, {1, 0}};
self.vertices[15] = (VertexData){{-0.5, -0.5, 0.5}, {0, 1}};
self.vertices[16] = (VertexData){{0.5, -0.5, -0.5}, {1, 0}};
self.vertices[17] = (VertexData){{-0.5, -0.5, -0.5}, {0, 0}};
// 左面
self.vertices[18] = (VertexData){{-0.5, 0.5, 0.5}, {1, 1}};
self.vertices[19] = (VertexData){{-0.5, -0.5, 0.5}, {0, 1}};
self.vertices[20] = (VertexData){{-0.5, 0.5, -0.5}, {1, 0}};
self.vertices[21] = (VertexData){{-0.5, -0.5, 0.5}, {0, 1}};
self.vertices[22] = (VertexData){{-0.5, 0.5, -0.5}, {1, 0}};
self.vertices[23] = (VertexData){{-0.5, -0.5, -0.5}, {0, 0}};
// 右面
self.vertices[24] = (VertexData){{0.5, 0.5, 0.5}, {1, 1}};
self.vertices[25] = (VertexData){{0.5, -0.5, 0.5}, {0, 1}};
self.vertices[26] = (VertexData){{0.5, 0.5, -0.5}, {1, 0}};
self.vertices[27] = (VertexData){{0.5, -0.5, 0.5}, {0, 1}};
self.vertices[28] = (VertexData){{0.5, 0.5, -0.5}, {1, 0}};
self.vertices[29] = (VertexData){{0.5, -0.5, -0.5}, {0, 0}};
// 后面
self.vertices[30] = (VertexData){{-0.5, 0.5, -0.5}, {0, 1}};
self.vertices[31] = (VertexData){{-0.5, -0.5, -0.5}, {0, 0}};
self.vertices[32] = (VertexData){{0.5, 0.5, -0.5}, {1, 1}};
self.vertices[33] = (VertexData){{-0.5, -0.5, -0.5}, {0, 0}};
self.vertices[34] = (VertexData){{0.5, 0.5, -0.5}, {1, 1}};
self.vertices[35] = (VertexData){{0.5, -0.5, -0.5}, {1, 0}};
//3.開辟數(shù)據(jù)緩存區(qū)
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
GLsizeiptr bufferSize = sizeof(VertexData) * kVertexCount;
glBufferData(GL_ARRAY_BUFFER, bufferSize, self.vertices, GL_STATIC_DRAW);
//4.設置頂點數(shù)據(jù)讀取方式
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), NULL + offsetof(VertexData, vertexCoordinate));
//5.設置紋理坐標讀取方式
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), NULL + offsetof(VertexData, textureCoordinate));
}
-(void)addCADisplayLink
{
self.angle = 0;
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
-(void)update
{
//1.計算旋轉(zhuǎn)的度數(shù)(度數(shù)->弧度)
self.angle = (self.angle + 5) % 360;
//2.修改GLKBaseEffect中模型視圖矩陣堆棧
self.baseEffect.transform.modelviewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(self.angle), 0.3f, 1.0f, -0.7f);
//3.重新渲染
[self.glkView display];
}
-(void)dealloc
{
//1.設置EAGLContext為nil
if ([EAGLContext currentContext] == self.glkView.context)
{
[EAGLContext setCurrentContext:nil];
}
//2.釋放頂點數(shù)據(jù)
if (_vertices)
{
free(_vertices);
_vertices = nil;
}
//3.刪除緩存區(qū)
if (_vertexBuffer)
{
glDeleteBuffers(1, &_vertexBuffer);
_vertexBuffer = 0;
}
//4.停止CADisplayLink
[self.displayLink invalidate];
}
#pragma mark - GLKViewDelegate
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
//1.開啟深度測試
glEnable(GL_DEPTH_TEST);
//2.清除顏色/深度緩存區(qū)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//3.準備繪制
[self.baseEffect prepareToDraw];
//4.開始繪制
glDrawArrays(GL_TRIANGLES, 0, kVertexCount);
}
@end
運行程序效果如下: