GLKit 加載正方體圖形

首先在 xcode 中新建一個工程,并導(dǎo)入一張圖片。然后在 ViewController.m 寫入以下代碼:

#import "ViewController.h"
#import <GLKit/GLKit.h>

typedef struct {
    
    GLKVector3 vertexCoordinate; //頂點(diǎn)坐標(biāo)
    GLKVector2 textureCoordinate; //紋理坐標(biāo)

}VertexData;

//頂點(diǎn)數(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.設(shè)置背景顏色
    self.view.backgroundColor = [UIColor blackColor];
    
    //2.設(shè)置 OpenGL ES 相關(guān)初始化配置
    [self setUpConfig];
    
    //3.圖形相關(guān)的頂點(diǎn)/紋理坐標(biāo)數(shù)據(jù)
    [self setUpVertexData];
    
    //4.添加CADisplayLink
    [self addCADisplayLink];
}


-(void)setUpConfig
{
    //1.創(chuàng)建context并設(shè)置當(dāng)前context
    EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
    [EAGLContext setCurrentContext:context];
    
    //2.創(chuàng)建GLKView并設(shè)置代理
    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.設(shè)置使用的深度緩沖區(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è)置紋理參數(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.開辟頂點(diǎn)數(shù)據(jù)空間
    self.vertices = malloc(sizeof(VertexData) * kVertexCount);
    
    //2.設(shè)置頂點(diǎn)/紋理坐標(biāo)數(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è)置頂點(diǎn)數(shù)據(jù)讀取方式
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), NULL + offsetof(VertexData, vertexCoordinate));
    
    //5.設(shè)置紋理坐標(biāo)讀取方式
    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.設(shè)置EAGLContext為nil
    if ([EAGLContext currentContext] == self.glkView.context)
    {
        [EAGLContext setCurrentContext:nil];
    }
    
    //2.釋放頂點(diǎn)數(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.準(zhǔn)備繪制
    [self.baseEffect prepareToDraw];
    
    //4.開始繪制
    glDrawArrays(GL_TRIANGLES, 0, kVertexCount);
}

@end

運(yùn)行程序效果如下:
旋轉(zhuǎn)立方體.png

以下是程序的思維導(dǎo)圖:
思維導(dǎo)圖.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容