范例工程:
- (void)setupTexture {
// 2 x 2 Image, 3 bytes per pixel(R, G, B)
GLubyte pixels[4 *3] =
{
255, 0, 0, // Red
0, 255, 0, // Green
0, 0, 255, // Blue
255, 255, 0 // Yellow
};
// Use tightly packed data
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
// Generate a texture object
glGenTextures(1, &_textureId);
// Bind the texture object
glBindTexture(GL_TEXTURE_2D,_textureId);
// Load the texture
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB,2, 2,0, GL_RGB,GL_UNSIGNED_BYTE, pixels);
// Set the filtering mode
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
- (void)render {
// 改变背景
glClearColor(0,104.0/255.0, 55.0/255.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
// 指定渲染的范围
glViewport(0,0, self.frame.size.width,self.frame.size.height);
// 关联顶点数据
glVertexAttribPointer(0,3, GL_FLOAT,GL_FALSE, 0,Vertices);
// 启用属性
glEnableVertexAttribArray(0);
//启用纹理
glVertexAttribPointer(1,2, GL_FLOAT,GL_FALSE, 0,texCoords);
glEnableVertexAttribArray(1);
// 绘制图形
// glDrawArrays(GL_POINTS, 0, 3);
// glDrawArrays(GL_LINE_LOOP, 0, 3);
glDrawArrays(GL_TRIANGLES,0, 3);
//缓存中的内容输出至屏幕
[_contextpresentRenderbuffer:GL_RENDERBUFFER];
}