只有一個單色的三角形,總歸是不好看猎唁,下面就往上面粘貼一個圖片效拭。紋理貼圖會引入映射、UV坐標等概念。
#import "GameViewController.h"
#import <OpenGLES/ES2/glext.h>
//將頂點坐標和UV紋理坐標放入同一個數(shù)據(jù)中
typedef struct {
GLKVector3 positionCoords;
GLKVector2 textureCoords;
} VertexType;
//三角形頂點數(shù)據(jù)
static const VertexType vertices[] = {
{{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f}},
{{0.5f, -0.5f, 0.0f}, {1.0f, 0.0f}},
{{-0.5f, 0.5f, 0.0f}, {0.0f, 1.0f}}
};
@interface GameViewController () {
GLuint vertexBufferID;
}
//用于設置通用的OpenGL ES環(huán)境
@property (strong, nonatomic) GLKBaseEffect *baseEffect;
//OpenGL ES上下文
@property (strong, nonatomic) EAGLContext *context;
@end
@implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//使用支持OpenGL ES2的上下文
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context) {
NSLog(@"Failed to create ES context");
}
GLKView *view = (GLKView *)self.view;
view.context = self.context;
view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
[EAGLContext setCurrentContext:self.context];
self.baseEffect = [[GLKBaseEffect alloc] init];
//啟用元素的默認顏色
// self.baseEffect.useConstantColor = GL_TRUE;
//設置默認顏色
// self.baseEffect.constantColor = GLKVector4Make(0.0f, 1.0f, 1.0f, 1.0f);
//生成紋理缎患,由于UIKit的坐標系Y軸與OpenGL ES的Y軸剛好上下顛倒慕的,因此如果圖片不加任何處理的話,在貼圖后將是顛倒顯示
CGImageRef imageRef = [UIImage imageNamed:@"1.jpg"].CGImage;
GLKTextureInfo *textureInfo = [GLKTextureLoader textureWithCGImage:imageRef options:NULL error:nil];
self.baseEffect.texture2d0.name = textureInfo.name;
self.baseEffect.texture2d0.target = textureInfo.target;
//設置背景色
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
//1. 生成緩存ID
glGenBuffers(1, &vertexBufferID);
//2.
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
//3.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
#pragma mark - GLKView and GLKViewController delegate methods
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
[self.baseEffect prepareToDraw];
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//4. 頂點坐標(x, y, z)
glEnableVertexAttribArray(GLKVertexAttribPosition);
//5. 類型挤渔,成員個數(shù)肮街,類型,歸一化判导,間隔嫉父,偏移
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexType), NULL + offsetof(VertexType, positionCoords));
//4.1 紋理坐標(u, v)
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
//5.1
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(VertexType), NULL + offsetof(VertexType, textureCoords));
//6. 繪制
glDrawArrays(GL_TRIANGLES, 0, 3);
}
- (void)dealloc
{
if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
glDeleteBuffers(1, &vertexBufferID);
vertexBufferID = 0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if ([self isViewLoaded] && ([[self view] window] == nil)) {
self.view = nil;
if ([EAGLContext currentContext] == self.context) {
[EAGLContext setCurrentContext:nil];
}
self.context = nil;
glDeleteBuffers(1, &vertexBufferID);
vertexBufferID = 0;
}
}
@end
UIImage
獲取的CGImageRef
Y坐標方向與OpenGL ES的Y坐標相反,因此看到的圖片是反的眼刃。
難點是glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexType), NULL + offsetof(VertexType, positionCoords));
的參數(shù)含義:
- 頂點屬性類型
- 該類型每個頂點所含數(shù)據(jù)個數(shù)
- 數(shù)據(jù)類型
- 是否歸一化
- 每個頂點數(shù)組間的間隔(即每個頂點的數(shù)據(jù)所占空間大腥葡健)
- 該類型的數(shù)據(jù)起始位置相對于起點的偏移量
offsetof
用來獲取結構體的成員positionCoords
在整個結構體VertexType
中的偏移量。