1.頂點坐標(-1 ~ 1)
static GLfloat vertex[8] = {
1, 1, //右上V1
-1, 1,//左上 V0
-1,-1,//左下 V2
-1,-1,//右下 V3
}
逆時針為正
將頂點坐標傳到GPU中
//1.在GPU中 申請一個內存標識
glGenBuffers(1, &_verTextureBuffer);
//2.讓這個標識去綁定一個內存區域,但此時,這個內存沒有大小
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
//3.根據頂點數組的大小,開辟內存空間,并將數據加載到內存中
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), &vertex, GL_STATUS_DRAW);
//4.啟用這塊內存
glEnableVertexAttribArray(GLKVertexArrtibPosition);
//5. 告訴GPU,定點數據在內存中的格式是怎樣的,應該如何去用
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 8 , NULL);
2.紋理坐標(取值范圍0~1)
static GLfloat textureCoords[8] = {
1, 1,//對V1
0, 1,//對V0
0, 0,//對V2
1, 0,//對V3
}
將紋理坐標傳到GPU中
1.在GPU中申請一個內存
glGenBuffers(1, &_textureCoordBuffer);
2.讓這個標示去綁定一個內存區域,但是此時,內存大小為0
glBindBuffer(GL_ARRAY_BUFFER, _textureCoordBuffer);
3.根據紋理坐標數組的大小,開辟內存空間,并將數據加載到內存中
glBufferData(GL_ARRAY_BUFFER, sizeof(textureCoords), textureCoords, GL_STATUS_DRAW);
4.啟用這塊內存,標記為位置
glEnableVertexAttribArray(GLKVertexAttribTextureCoord0);
5.告訴GPU,定點數據在內存中國年的格式是怎樣的,應該如何去使用
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 8, NULL);
3.加載紋理圖片像素數據
- (void *)getImageData:(UIImage *)image{
CGImageRef imageRef = [image CGImage];
size_t imageWidth = CGImageGetWidth(imageRef);
size_t imageHeight = CGImageGetHeight(imageRef);
GLubyte * imageData = (GLubyte *)malloc(imageWidth * imageHeight * 4);
memset(imageData, 0, imageWidth * imageHeight * 4);
CGContextRef imageContextRef = CGBitmapContextCreate(imageData, imageWidth, imageHeight, 8, imageWidth * 4, CGImageGetColorSpace(imageRef), kCGImageAlphaPremultipiled);
CGContextTranslateCTM(imageContextRef, 0, imageHeight);
CGContextScaleCTM(imageContxetRef, 0, imageHeight);
CGContextDrawImage(imageContextRef, CGRectMake(0.0, 0.0, (CGFloat)imageWidth, (CGFLoat)imageHeight), imageRef);
CGContextRelease(imageContextRef);
return imageData;
}
4.加載像素數據
- (void)loadTexture{
1.將著色器中的紋理采樣器和紋理區域0進行關聯
glUniform1i(_textureBufferf, 0);
GLuint text1;
2.激活紋理單元1
glActiveTexture(GL_TEXTURE0);
3.申請內存
glGenTextures(1, &text1);
4.將內存和激活的紋理綁定
glBindTexture(GL_TEXTURE_2D, text1);
UIImage *image = [UIImage imageName:@"2.png"];
GLubyte * imageData = [self getImageData:image];
5.將圖片像素數據,加載到紋理區域0 中去
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA , image.size.width, image.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
6.設置圖片在渲染時的一些配置
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
5.繪制
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
1.設置清除顏色
glClearColor(1, 1, 1, 1);
2.清除顏色緩沖區
glClear(GL_COLOR_BUFFER_BIT);
3.使用著色去源程序
[self.shaderManager useProgram];
4.繪制
glDrawArrays(GL_TRANGLE_FAN, 0, 4);
}
6.頂點著色器
attribute vec4 position;//頂點位置
attribute vec2 texCoord0;//紋理坐標
varying vec2 texCoordVarying;//片段著色器的輸入變量
void main()
{
gl_Position = position;
texCoordVarying = texCoord0;
}
7.片段著色器
precision medium float ;
varying vec2 texCoordVarying;
uniform sample2D sam2DR;
void main ()
{
low vec4 reba = vec4(0, 0, 0, 1);
reba = texture@d(sam2DR, texCoordVarying);
gl_FragColor = rgba;
}