我已经将CCNode子类化,并希望在每次绘制调用时闪现存储在成员GLubyte *缓冲区中的图像。这会导致绘制黑色方块,同时兄弟CCLabel节点也完全变黑。
所以有两个问题:
数据初始化:
exponent = e;
width = 1 << exponent;
height = 1 << exponent;
imageData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
memset(imageData, 0xff, 4 * width * height * sizeof(GLubyte));
绘制方法:
- (void) draw
{
float w = 100.0f;
GLfloat vertices[] = {
0.0f, 0.0f, 0.0f,
w, 0.0f, 0.0f,
0.0f, w, 0.0f,
w, w, 0.0f
};
const GLshort texture_coordinates[] = {
0, 0,
1, 0,
0, 1,
1, 1,
};
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, width, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_SHORT, 0, texture_coordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
答案 0 :(得分:0)
在填充数据之前,您需要生成纹理对象。也就是说,在初始化的某个地方,你需要调用:
int myTexture; // this is class member variable
glGenTextures(1, &myTexture); // generates an empty texture object
glBindTexture(GL_TEXTURE_2D, myTexture); // activate the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// makes sure there are no mipmaps (you only specify level 0, in case
// the texture is scaled during rendering, that could result in black square)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// this allocates the texture, but specifies no data
稍后,在绘制时(而不是你的glTexImage2D()调用),你需要激活纹理并使用glTexSubImage2D()指定它的数据(不为纹理分配新的存储空间)。
glBindTexture(GL_TEXTURE_2D, myTexture); // activate the texture
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
// this specifies texture data without allocating new storage
调用glTex(Sub)Image2D而不首先绑定纹理会a)导致OpenGL错误,或b)覆盖当前活动的其他纹理 - 可能是兄弟标签节点的纹理。
希望这会有所帮助:)