我正在使用 raywenderlich.com http://www.raywenderlich.com/4404/opengl-es-2-0-for-iphone-tutorial-part-2-textures上的教程学习 OpenGL ES
当我开始调整示例项目以获取 UIView 的内容作为要渲染的纹理时,结果只是黑屏:
黑色视图是 OpenGL ES 视图。
我使用了 Tommy 在这篇文章中发布的代码:将 UIView 的内容渲染为 OpenGL 纹理,这是我的版本:
- (GLuint)createTexture:(UIView *)view
{
size_t width = CGRectGetWidth(view.layer.bounds) * [UIScreen mainScreen].scale;
size_t height = CGRectGetHeight(view.layer.bounds) * [UIScreen mainScreen].scale;
GLubyte * texturePixelBuffer = (GLubyte *) calloc(width * height * 4,
sizeof(GLubyte));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(texturePixelBuffer,
width, height, 8, width*4, colorSpace,
kCGImageAlphaPremultipliedLast |
kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
[view.layer renderInContext:context];
CGContextRelease(context);
GLuint texName;
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, texturePixelBuffer);
free(texturePixelBuffer);
return texName;
}