OpenGL ES 纹理

//

//  ViewController.m

//  OpenGL_Ch3_1

//

//  Created by Ansel on 2018/4/19.

//  Copyright © 2018年 Ansel. All rights reserved.

//

 

#import "ViewController.h"

 

// This data type is used to store information for each vertex

typedef struct {

    GLKVector3 positionCoords;

    GLKVector2 textureCoords;

} SceneVertex;

 

 

// Define vertex data for a triangle to use in example, relative screen center

 

static const SceneVertex vertices[] = {

    {{-0.5f, -0.5f, 0.0}, {0.0, 0.0}}, // lower left corner

    {{ 0.5f, -0.5f, 0.0}, {1.0, 0.0}}, // lower right corner

    {{-0.5f,  0.5f, 0.0}, {0.0, 1.0}}, // upper left corner

};

 

@interface ViewController ()

{

    GLuint _vertexBufferID;

}

 

@property(nonatomic, strong) GLKBaseEffect *baseEffect;

 

@end

 

@implementation ViewController

 

- (void)dealloc

{

    // Make the view's context current

    GLKView *view = (GLKView *)self.view;

    [EAGLContext setCurrentContext:view.context];

    

    // Delete buffers that aren't needed when view is unloaded

    if (0 != _vertexBufferID) {

        // Setp 7

        glDeleteBuffers(1, &_vertexBufferID);

        _vertexBufferID = 0;

    }

    

    [((GLKView *)self.view) setContext:nil];

    [EAGLContext setCurrentContext:nil];

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    GLKView *view = (GLKView *)self.view;

    NSAssert([view isKindOfClass:[GLKView class]], @"View controller's view is not a GLKView");

    

 

    view.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    

    [EAGLContext setCurrentContext:view.context];

    

    self.baseEffect = [[GLKBaseEffect alloc] init];

    self.baseEffect.useConstantColor = GL_TRUE;

    self.baseEffect.constantColor = GLKVector4Make(1.0, //red

                                                   1.0, //green

                                                   1.0, //blue

                                                   1.0 //alpha

                                                   );

    

    glClearColor(1.0, 1.0, 1.0, 1.0); //set self.view background color

    //Setp 1 为缓存生成一个唯一的标识符

    glGenBuffers(1, &_vertexBufferID);

    //Setp 2 把指定缓存的标识符绑定到当前缓存

    glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);

    //Setp 3 初始化缓存并复制数据到缓存中

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    

    CGImageRef image = [[UIImage imageNamed:@"leaves.gif"] CGImage];

    GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithCGImage:image options:nil error:nil];

    self.baseEffect.texture2d0.name = textureInfo.name;

    self.baseEffect.texture2d0.target = textureInfo.target;

}

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

#pragma mark - 

 

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {

    // Sync all effect changes for consistent state when drawing

    [self.baseEffect prepareToDraw];

    

    //清楚以前的缓存, 并设置 glClearColor中的color生效

    glClear(GL_COLOR_BUFFER_BIT);

    

    //设置位置step 4 5

    glEnableVertexAttribArray(GLKVertexAttribPosition);

    GLsizeiptr positionCoordsOffset = offsetof(SceneVertex, positionCoords);

    glVertexAttribPointer(GLKVertexAttribPosition,

                          3,  //number of coordinates for per vertex 

                          GL_FLOAT, // data is floating point

                          GL_FALSE, // no fixed point scaling

                          sizeof(SceneVertex), //stride

                          NULL + positionCoordsOffset //tells GPU to start at beginning of bound buffer

                          );

    

    //设置纹理 step 4 5

    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);

    GLsizeiptr textureCoordsOffset = offsetof(SceneVertex, textureCoords);

    glVertexAttribPointer(GLKVertexAttribTexCoord0,

                          2,  //number of coordinates for per vertex

                          GL_FLOAT, // data is floating point

                          GL_FALSE, // no fixed point scaling

                          sizeof(SceneVertex), //stride

                          NULL + textureCoordsOffset //tells GPU to start at beginning of bound buffer

                          );

    

    //Step 6.绘图

    GLuint count = sizeof(vertices) / sizeof(SceneVertex);

    glDrawArrays(GL_TRIANGLE_STRIP,

                 0,  // Start with first vertex in currently bound buffer

                 count

                 );

}

 

@end

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值