OpenGL ES;渲染从 CGBitmapContext 创建的纹理 [英] OpenGL ES; rendering texture created from CGBitmapContext
本文介绍了OpenGL ES;渲染从 CGBitmapContext 创建的纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在执行以下内容,这些内容来自几个不同的教程(只是一个渲染过程,未显示初始化代码,但适用于无纹理的基元):
I am executing the following, which I have derived from a few different tutorials (Just a single render pass, initialisation code not shown but works fine for untextured primitives):
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrthof(0, xSize, 0, ySize, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_ONE, GL_SRC_COLOR);
GLuint texture[1];
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
int width = 50;
int height = 50;
void* textureData = malloc(width * height * 4);
CGColorSpaceRef cSp = CGColorSpaceCreateDeviceRGB();
CGContextRef ct = CGBitmapContextCreate(textureData, width, height, 8, width*4, cSp, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextSetRGBFillColor(ct, 0, 1, 0, 1);
CGContextFillRect(ct, CGRectMake(0, 0, 50, 50));
CGContextRelease(ct);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
float verts[] = {
0.0f, 0.0f, 0.0f,
50.0f, 0.0f, 0.0f,
0.0f, 50.0f, 0.0f,
50.0f, 50.0f, 0.0f
};
float texCords[] = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f
};
glVertexPointer(3, GL_FLOAT, 0, verts);
glTexCoordPointer(2, GL_FLOAT, 0, texCords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_TEXTURE_2D);
结果是一个白色方块.不是预期的绿色.任何人都可以在我的代码中发现导致其无法呈现的错误吗?
The result is a white square. Not the green one as intended. Can anyone spot the error(s) in my code which result in its' failure to render?
我希望让它工作,然后将其移至文本渲染.
I hope to get this working then move it on to text rendering.
推荐答案
问题是 width
和 height
不是二的幂.有两种解决方案:
The problem is that width
and height
are not powers of two. There are two solutions:
- 使用纹理矩形扩展.将纹理目标设置为
GL_TEXTURE_RECTANGLE_ARB
而不是GL_TEXTURE_2D
.在使用它之前,您必须启用此扩展程序.请注意,矩形纹理不支持 mipmap. - 对纹理尺寸使用 2 的幂.
- Use the texture rectangle extension. Set the texture target to
GL_TEXTURE_RECTANGLE_ARB
instead ofGL_TEXTURE_2D
. You will have to enable this extension before using it. Note that rectangle textures do not support mipmaps. - Use powers of two for texture dimensions.
这篇关于OpenGL ES;渲染从 CGBitmapContext 创建的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文