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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenGL ES中,纹理数组是一组纹理的集合,可以一次性加载多个纹理并进行处理。使用纹理数组可以提高渲染效率,减少内存占用。 纹理数组的创建和使用主要有以下几个步骤: 1. 创建纹理数组对象:使用glGenTextures函数创建纹理数组对象,并使用glBindTexture函数绑定到OpenGL ES上下文中。 2. 加载纹理数据:使用glTexImage2D函数将纹理数据加载到纹理数组中。 3. 设置纹理参数:使用glTexParameter函数设置纹理参数,如过滤模式、纹理环绕模式等。 4. 绑定纹理数组:使用glBindTexture函数将纹理数组绑定到OpenGL ES上下文中。 5. 使用纹理数组:在渲染时,使用glActiveTexture函数激活纹理单元,并使用glBindTexture函数将纹理数组绑定到激活的纹理单元上,然后在着色器中使用sampler2DArray类型的变量进行采样。 需要注意的是,在使用纹理数组时,每个纹理的大小和格式必须相同。 下面是一个使用纹理数组的示例代码: ``` GLuint texArray; glGenTextures(1, &texArray); glBindTexture(GL_TEXTURE_2D_ARRAY, texArray); glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, width, height, numTextures, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_ARRAY, texArray); glUniform1i(textureLoc, 0); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值