Mayoneez
Mayoneez

Reputation: 441

How to clear portion of a texture with alpha 0 using OpenGL ES?

I have a texture onto which I render 16 drawings. The texture is 1024x1024 in size and it's divided into 4x4 "slots", each 256x256 pixels.

Before I render a new drawing into a slot, I want to clear it so that the old drawing is erased and the slot is totally transparent (alpha=0).

Is there a way to do it with OpenGL or need I just access the texture pixels directly in memory and clear them with memset oslt?

Upvotes: 1

Views: 1458

Answers (2)

Tim
Tim

Reputation: 35923

Is your texture actively bound as a frame buffer target? (I'm assuming yes because you say you're rendering to it.)

If so, you can set a glScissor test, followed by a glClear to just clear a specific region of the framebuffer.

Upvotes: 1

luke
luke

Reputation: 37433

I imagine you'd just update the current texture normally:

std::vector<unsigned char> emptyPixels(1024*1024*4, 0); // Assuming RGBA / GL_UNSIGNED_BYTE
glBindTexture(GL_TEXTURE_2D, yourTextureId);
glTexSubImage2D(GL_TEXTURE_2D,
                0,
                0,
                0,
                1024,
                1024,
                GL_RGBA,
                GL_UNSIGNED_BYTE,
                emptyPixels.data());  // Or &emptyPixels[0] if you're stuck with C++03

Even though you're replacing every pixel, glTexSubImage2D is faster than recreating a new texture.

Upvotes: 1

Related Questions