What you want is to bind a texture to a shader and then render to that texture. I could spit through miles of documentation specification of OpenGL, but I'm going to give the feeling my gut gives me:
I don't think that's possible.
What I do know is possible, however, is that you create a Frame Buffer Object (FBO) and render to that. First, you must generate an FBO and attach a texture to it with the same size as the one you want to update.
GLuint fbo_handle, fbo_texture_handle;
GLuint texture_width = GetTextureWidth();
GLuint texture_height = GetTextureWidth();
// generate texture
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texture_width, texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
// generate framebuffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo_texture_handle, 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
LOG_ERROR("Could not validate framebuffer);
When you want to update your texture, you must take the followings steps.
First, you attach the FBO:
glViewport(0, 0, texture_width, texture_height);
Now you can render a fullscreen quad with a shader that outputs the updated color:
vec4 frag_texture = texture2D(TextureID, gl_TexCoord[TextureIndex].st);
vec4 frag_updated = frag_texture + vec4(0.0, 0.0, 0.0, AlphaPatch);
gl_FragData = frag_updated;
And then you have to copy the resulting framebuffer to the original texture.
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, texture_width, texture_height);
However, you should consider the following:
- How often do you update this texture?
- If you don't have to update it every frame, is this method worth the speed penalty?
- Can you generate multiple version of this texture and cycle between them?
- Is there another way to achieve the same effect?