AVFoundation 拍片专用

使用OpenGL绘制简易三角形

2017-03-01  本文已影响166人  傻傻小萝卜

#import"ViewController.h"

@interface ViewController()

@property(nonatomic,strong)EAGLContext * mContext;// OpenGL的上下文

@property(nonatomic,strong)GLKBaseEffect * mEffect;// OpenGL的着色器

@property(nonatomic,assign)int mCount;

@end

创建OpenGL的上下文和着色器

(1)创建OpenGL上下文

self.mContext= [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];//2.0,还有1.0和3.0

GLKView* view = (GLKView*)self.view;//storyboard记得添加

view.context=self.mContext;

view.drawableColorFormat=GLKViewDrawableColorFormatRGBA8888;//颜色缓冲区格式

[EAGLContext setCurrentContext:self.mContext];

(2)创建顶点数据

//顶点数据,前三个是顶点坐标,后面两个是纹理坐标

GLfloat squareVertexData[] =

{

0.5, -0.5,0.0f,1.0f,0.0f,//右下

-0.5,0.5,0.0f,0.0f,1.0f,//左上

-0.5, -0.5,0.0f,0.0f,0.0f,//左下

//0.5, 0.5, -0.0f,1.0f, 1.0f, //右上

};

//顶点索引,数组中的每三个代表了顶点数组中的顶点比如0,1,2表示取得是0.5, -0.5,0.0f,1.0f,0.0f,//右下,-0.5,0.5,0.0f,0.0f,1.0f,//左上,-0.5, -0.5,0.0f,0.0f,0.0f,//左下这三个点

GLuint indices[] =

{

0,1,2,

//1, 3, 0

};

self.mCount=sizeof(indices) /sizeof(GLuint);

//顶点数据缓存

GLuint buffer;// 

glGenBuffers(1, &buffer);// OpenGL为图形处理器控制的缓存生成一个独一无二的标识符

glBindBuffer(GL_ARRAY_BUFFER, buffer);// 为接下来的运算使用一个缓存

glBufferData(GL_ARRAY_BUFFER,sizeof(squareVertexData), squareVertexData,GL_STATIC_DRAW);// 为当前的缓存分配并初始化足够的连续内存

// 顶点索引缓存

GLuint index;

glGenBuffers(1, &index);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);

glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices), indices,GL_STATIC_DRAW);

glEnableVertexAttribArray(GLKVertexAttribPosition);// 告诉OpenGL是否使用缓存中的数据

glVertexAttribPointer(GLKVertexAttribPosition,3,GL_FLOAT,GL_FALSE,sizeof(GLfloat) *5, (GLfloat*)NULL+0);// 告诉OpenGL在缓存中数据的类型和所需要访问数据的偏移量

//纹理贴图

NSString* filePath = [[NSBundlemainBundle]pathForResource:@"for_test"ofType:@"jpg"];

NSDictionary* options = [NSDictionarydictionaryWithObjectsAndKeys:@(1),GLKTextureLoaderOriginBottomLeft,nil];//GLKTextureLoaderOriginBottomLeft纹理坐标系是相反的

GLKTextureInfo* textureInfo = [GLKTextureLoadertextureWithContentsOfFile:filePathoptions:optionserror:nil];

//着色器

self.mEffect= [[GLKBaseEffectalloc]init];

self.mEffect.texture2d0.enabled=GL_TRUE;

self.mEffect.texture2d0.name= textureInfo.name;

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

glClearColor(0.3f,0.6f,0.5f,1.0f);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

//启动着色器

[self.mEffect prepareToDraw];

glDrawElements(GL_TRIANGLES,self.mCount,GL_UNSIGNED_INT,0);

}

上一篇下一篇

猜你喜欢

热点阅读