subclassing CCNode, blitzing image with glTexImage2D
I've subclassed CCNode and would like to blitz an image stored in a member GLubyte* buffer on each draw call. This results in a black square being drawn, and a sibling CCLabel node is now also completely black.
So there are two issues:
- Why isn't the square white? I'm memset-ing the buffer to 0xffffffff.
- There must be something wrong with my OpenGL calls in the draw m开发者_开发知识库ethod, since it affects the drawing of another node. Can anyone say what?
data initialization:
exponent = e;
width = 1 << exponent;
height = 1 << exponent;
imageData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
memset(imageData, 0xff, 4 * width * height * sizeof(GLubyte));
draw method:
- (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);
}
you need to generate a texture object before filling it with data. That is, somewhere in initialization, you need to call:
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
Later, when drawing (instead of your glTexImage2D() call), you need to activate your texture and specify it's data using glTexSubImage2D() (that doesn't allocate new storage for the texture).
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
Calling glTex(Sub)Image2D without first binding the texture would a) result in an OpenGL error, or b) overwrite some other texture that is active at the moment - might be texture of the sibling label node.
Hope this helps :)
精彩评论