OpenGL ES 纹理

匿名 (未验证) 提交于 2019-12-03 00:18:01

//

//ViewController.m

//OpenGL_Ch3_1

//

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

//

//


#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,

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



文章来源: OpenGL ES 纹理
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!