Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL-ES 2.0 blend 2 textures and save the result into a original texture
    text
    copied!<p>I am a newbie to OpenGL ES 2.0, I have read this thread about how to blend 2 textures into a final framebuffer result. <a href="https://stackoverflow.com/questions/12242443/how-do-i-blend-two-textures-with-different-co-ordinates-in-opengl-es-2-0-on-ipho">How do I blend two textures with different co-ordinates in OpenGL ES 2.0 on iPhone?</a></p> <p>My current requirement is a little different.</p> <p>I want to blend texture called inputTextureTop(Varying) and texture inputTextureBot(constant) and save the result into texture inputTextureTop.</p> <p>It should be simple enough. How do I modify the sample code in the thread to try?</p> <p>Objective C code...</p> <pre><code> - (void) display { [EAGLContext setCurrentContext:context]; glBindFramebuffer(GL_FRAMEBUFFER, targetFBO); glUseProgram(program); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textureTop); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, textureBot); glUniform1i(inputTextureTop, 2); glUniform1i(inputTextureBot, 3); glUniform1f(alphaTop, alpha); glEnable (GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glVertexAttribPointer(position, 2, GL_FLOAT, 0, 0, imageVertices); glVertexAttribPointer(inputTextureCoordinate, 2, GL_FLOAT, 0, 0, textureCoordinates); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindRenderbuffer(GL_RENDERBUFFER, viewRenderbuffer); [context presentRenderbuffer:GL_RENDERBUFFER]; } </code></pre> <p>Vertex Shader code:</p> <pre><code> attribute vec4 position; attribute vec4 inputTextureCoordinate; varying vec2 textureCoordinate; void main() { gl_Position = position; textureCoordinate = inputTextureCoordinate.xy; } </code></pre> <p>Fragment shader...</p> <pre><code> varying highp vec2 textureCoordinate; uniform sampler2D inputTextureTop; uniform sampler2D inputTextureBot; uniform highp float alphaTop; void main() { lowp vec4 pixelTop = texture2D(inputTextureTop, textureCoordinate); lowp vec4 pixelBot = texture2D(inputTextureBot, textureCoordinate); gl_FragColor = someBlendOperation(pixelTop, pixelBot); } </code></pre> <p>Thanks so much!</p> <p>Regards,</p> <p>Howard</p> <p>======================== Updated on August 22nd.</p> <p>After some investigation, I found it's a dead path. There is no way to composite texture (top) + texture (bottom) and change to texture (top). </p> <p>However, there is a way to composite texture (top) + texture (bottom) and save it to texture (target). </p> <p>The way to do it is to bind a target texture to the same framebuffer that texture top and texture bottom uses. </p> <p>Add the code below will do the job: </p> <pre><code> glActiveTexture(GL_TEXTURE0); bindTexture(textureTarget); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureTarget, 0); </code></pre> <p>The result will be stored in textureTarget.</p> <p>Bingo.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload