十、GLKit 紋理立方體旋轉(zhuǎn)

前面的文章——GLkit加載紋理圖片介紹了如何利用GLKit加載紋理圖片,本文就在其基礎(chǔ)上,實(shí)現(xiàn)一些進(jìn)階內(nèi)容,繪制一個(gè)立方體,使立方體每個(gè)面都附著一張紋理圖片,并讓其以某一軸旋轉(zhuǎn);

  • 實(shí)現(xiàn)的步驟


    紋理立方體旋轉(zhuǎn)繪制
  • 核心代碼

//
//  ViewController.m
//  OpenGL_ES紋理立方體
//
//  Created by TL on 2020/7/29.
//  Copyright ? 2020 tl. All rights reserved.
//

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

// 頂點(diǎn)數(shù)據(jù)結(jié)構(gòu)體
typedef struct {
    GLKVector3 positionCoord; // 頂點(diǎn)坐標(biāo)
    GLKVector2 textureCoord; // 紋理坐標(biāo)
    GLKVector3 normal; // 法線向量
    
} MyVertex;

static NSInteger coordCount = 36;

@interface ViewController ()<GLKViewDelegate>


@property (nonatomic,strong) GLKView * glkView;

@property (nonatomic,strong) GLKBaseEffect * myEffect;

@property (nonatomic,assign) MyVertex * myVertex;
/// 頂點(diǎn)緩存區(qū)
@property (nonatomic,assign) GLuint vertexBuffer;

/// 定時(shí)器
@property (nonatomic,strong) CADisplayLink * displayLink;

/// 旋轉(zhuǎn)角度
@property (nonatomic,assign) NSInteger angle;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化OpenGL ES
    [self initialize];
    // 加載紋理數(shù)據(jù)
    [self loadTextureData];
    // 初始化頂點(diǎn)數(shù)據(jù)以及將數(shù)據(jù)copy至顯存
    [self initVertexData];
    // 創(chuàng)建定時(shí)器
    [self startCADisplayLink];
}
- (void)initialize
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 1.創(chuàng)建context,并設(shè)置為當(dāng)前上下文
    EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
    [EAGLContext setCurrentContext:context];
    // 2.創(chuàng)建glkView
    CGRect glkRect = CGRectMake(20, 50, self.view.frame.size.width - 40, self.view.frame.size.height - 100);
    self.glkView = [[GLKView alloc] initWithFrame:glkRect context:context];
    self.glkView.backgroundColor = [UIColor lightGrayColor];
    // 設(shè)置代理
    self.glkView.delegate = self;
    
    // 3.設(shè)置顏色和深度緩沖區(qū)
    self.glkView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
    self.glkView.drawableDepthFormat = GLKViewDrawableDepthFormat16;
    
    [self.view addSubview:self.glkView];
    
    
}



/// 獲取紋理數(shù)據(jù)
- (void)loadTextureData
{
    // 1.用本地圖片
    NSString *imagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"miao.jpg"];
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
   
    // 2.轉(zhuǎn)換圖形數(shù)據(jù)以適配OpenGL左下角原點(diǎn)格式
    NSDictionary * option = [NSDictionary dictionaryWithObjectsAndKeys:@(YES),GLKTextureLoaderOriginBottomLeft, nil];
    // 3.設(shè)置紋理參數(shù)

     GLKTextureInfo * textureInfo = [GLKTextureLoader textureWithCGImage:[image CGImage] options:option error:nil];
    
    // 初始化effect
    self.myEffect = [[GLKBaseEffect alloc] init];
    _myEffect.texture2d0.name = textureInfo.name;
    _myEffect.texture2d0.target = textureInfo.target;
    
    // 開(kāi)啟光照
    _myEffect.light1.enabled = YES;
    // 設(shè)置漫反射顏色值
    _myEffect.light1.diffuseColor = GLKVector4Make(1, 1, 1, 1);
    // 設(shè)置光源位置,GLKVector4Make(float x, float y, float z, float w) w一般都填 1
    _myEffect.light1.position = GLKVector4Make(1, 1, -4, 1);
    
    
}

- (void)initVertexData
{
    
    /*
     如果不復(fù)用頂點(diǎn),使用每 3 個(gè)點(diǎn)畫(huà)一個(gè)三角形的方式,需要 12 個(gè)三角形,則需要 36 個(gè)頂點(diǎn)
     以下的數(shù)據(jù)用來(lái)繪制以(0,0,0)為中心,邊長(zhǎng)為 1 的立方體
     */
    
    // 開(kāi)辟頂點(diǎn)數(shù)據(jù)空間(數(shù)據(jù)結(jié)構(gòu)SenceVertex 大小 * 頂點(diǎn)個(gè)數(shù)kCoordCount)
    self.myVertex = malloc(sizeof(MyVertex) * coordCount);
    
    // 前面
    self.myVertex[0] = (MyVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 0, 1}};
    self.myVertex[1] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
    self.myVertex[2] = (MyVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
    self.myVertex[3] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 0}, {0, 0, 1}};
    self.myVertex[4] = (MyVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 0, 1}};
    self.myVertex[5] = (MyVertex){{0.5, -0.5, 0.5}, {1, 0}, {0, 0, 1}};
    
    // 上面
    self.myVertex[6] = (MyVertex){{0.5, 0.5, 0.5}, {1, 1}, {0, 1, 0}};
    self.myVertex[7] = (MyVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
    self.myVertex[8] = (MyVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
    self.myVertex[9] = (MyVertex){{-0.5, 0.5, 0.5}, {0, 1}, {0, 1, 0}};
    self.myVertex[10] = (MyVertex){{0.5, 0.5, -0.5}, {1, 0}, {0, 1, 0}};
    self.myVertex[11] = (MyVertex){{-0.5, 0.5, -0.5}, {0, 0}, {0, 1, 0}};
    
    // 下面
    self.myVertex[12] = (MyVertex){{0.5, -0.5, 0.5}, {1, 1}, {0, -1, 0}};
    self.myVertex[13] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
    self.myVertex[14] = (MyVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
    self.myVertex[15] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 1}, {0, -1, 0}};
    self.myVertex[16] = (MyVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, -1, 0}};
    self.myVertex[17] = (MyVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, -1, 0}};
    
    // 左面
    self.myVertex[18] = (MyVertex){{-0.5, 0.5, 0.5}, {1, 1}, {-1, 0, 0}};
    self.myVertex[19] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
    self.myVertex[20] = (MyVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
    self.myVertex[21] = (MyVertex){{-0.5, -0.5, 0.5}, {0, 1}, {-1, 0, 0}};
    self.myVertex[22] = (MyVertex){{-0.5, 0.5, -0.5}, {1, 0}, {-1, 0, 0}};
    self.myVertex[23] = (MyVertex){{-0.5, -0.5, -0.5}, {0, 0}, {-1, 0, 0}};
    
    // 右面
    self.myVertex[24] = (MyVertex){{0.5, 0.5, 0.5}, {1, 1}, {1, 0, 0}};
    self.myVertex[25] = (MyVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
    self.myVertex[26] = (MyVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
    self.myVertex[27] = (MyVertex){{0.5, -0.5, 0.5}, {0, 1}, {1, 0, 0}};
    self.myVertex[28] = (MyVertex){{0.5, 0.5, -0.5}, {1, 0}, {1, 0, 0}};
    self.myVertex[29] = (MyVertex){{0.5, -0.5, -0.5}, {0, 0}, {1, 0, 0}};
    
    // 后面
    self.myVertex[30] = (MyVertex){{-0.5, 0.5, -0.5}, {0, 1}, {0, 0, -1}};
    self.myVertex[31] = (MyVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
    self.myVertex[32] = (MyVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
    self.myVertex[33] = (MyVertex){{-0.5, -0.5, -0.5}, {0, 0}, {0, 0, -1}};
    self.myVertex[34] = (MyVertex){{0.5, 0.5, -0.5}, {1, 1}, {0, 0, -1}};
    self.myVertex[35] = (MyVertex){{0.5, -0.5, -0.5}, {1, 0}, {0, 0, -1}};
    
    // 1.開(kāi)辟頂點(diǎn)緩存區(qū)
    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, self.vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(MyVertex) * coordCount, self.myVertex, GL_STATIC_DRAW);
    
    // copy頂點(diǎn)數(shù)據(jù)
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), NULL + offsetof(MyVertex, positionCoord));
    
    // 紋理數(shù)據(jù)
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(MyVertex), NULL + offsetof(MyVertex, textureCoord));
    
    // 法線數(shù)據(jù)
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), NULL + offsetof(MyVertex, normal));
    
    
}



/// 開(kāi)啟定時(shí)器
- (void)startCADisplayLink
{
    self.angle = 0;
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
    [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)update
{
    // 計(jì)算旋轉(zhuǎn)度數(shù)
    self.angle = (self.angle + 8) % 360;
    // 旋轉(zhuǎn)
    self.myEffect.transform.modelviewMatrix = GLKMatrix4MakeRotation(GLKMathDegreesToRadians(self.angle), -0.5, 0.9, 1);
    // 重新渲染
    [self.glkView display];
}

#pragma mark - GLKViewDelegate
-(void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    // 清除顏色、深度緩沖區(qū)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // 開(kāi)啟深度測(cè)試n
    glEnable(GL_DEPTH_TEST);

    // 準(zhǔn)備繪制
    [self.myEffect prepareToDraw];
    
    glDrawArrays(GL_TRIANGLES, 0, coordCount);
    
}

- (void)dealloc {
    
    if ([EAGLContext currentContext] == self.glkView.context) {
        [EAGLContext setCurrentContext:nil];
    }
    if (_myVertex) {
        free(_myVertex);
        _myVertex = nil;
    }
    
    if (_vertexBuffer) {
        glDeleteBuffers(1, &_vertexBuffer);
        _vertexBuffer = 0;
    }
    
    //displayLink 失效
    [self.displayLink invalidate];
}



@end

  • 最終效果


    效果

github Demo地址

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

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