iDevGames Forums
Discrad black parts in texture - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Discrad black parts in texture (/thread-9469.html)



Discrad black parts in texture - bonanza - Oct 26, 2011 01:51 AM

Hi,
i like to discard (without shaders) any black parts in a texture. Blending don't work, here is what i have so far:
Code:
glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  
    GLfloat boxBackground[]={0,0,320, 0,0, 480,320, 480,};
    GLfloat uvBackground[]={0,0,1,0,0,1,1,1};
    
    glBindTexture(GL_TEXTURE_2D, textureBackground);
    glVertexPointer(2, GL_FLOAT, 0, boxBackground);
    glTexCoordPointer(2, GL_FLOAT, 0, uvBackground);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
What i want to do is, to delete some parts (on the fly) of the foreground texture out (draw black lines for example), so that the background is visible trough the "deleted" parts of the foreground texture.
In principle, masking on the fly.

Any suggestions ?


RE: Discrad black parts in texture - backslash - Oct 26, 2011 06:16 AM

I'm not an OpenGL genius, but I think if you're not going to use shaders then you will need to generate an alpha channel for the texture. The logic to read through pixel by pixel and set the alpha to 0 for the black pixels is fairly trivial.


RE: Discrad black parts in texture - bonanza - Oct 26, 2011 07:09 AM

I think, read the pixel at realtime is a performance problem.


RE: Discrad black parts in texture - ThemsAllTook - Oct 26, 2011 07:27 AM

You don't need to read back the pixels from the texture itself. You can just keep the pixel data in memory after uploading it to GL, modify it there, and call glTexSubImage2D to reupload the modified parts. Also, there's no way to know for sure if it's a performance problem until you try it out.

If you describe the effect you're trying to create in more detail, we might be able to suggest more alternatives.


RE: Discrad black parts in texture - bonanza - Oct 26, 2011 07:54 AM

What i like to do is a simple rubber (like in a painting program). I move my finger over the top most texture that portion of this texture should become transparent and the background texture are visible at this parts.


RE: Discrad black parts in texture - ThemsAllTook - Oct 26, 2011 08:24 AM

Fair enough. Sounds like modifying your pixels in memory and calling glTexSubImage2D is exactly what you'd want for that case.


RE: Discrad black parts in texture - bonanza - Oct 26, 2011 08:37 AM

Mhh, i don't no, how to do that.

I have found this: http://stackoverflow.com/questions/3439616/eraser-in-opengl-es-iphone/4238716#4238716

seems to be the same, what i want.


Code:
// RTT
glBindFramebuffer(GL_FRAMEBUFFER, [(EAGLView *)self.view fbo]);
    glViewport(0, 0, 512, 512);
    
//  Bind the foreground texture and render this
    glBindTexture(GL_TEXTURE_2D, textureForeground);
    glVertexPointer(2, GL_FLOAT, 0, boxBackground);
    glTexCoordPointer(2, GL_FLOAT, 0, uvBackground);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
// the erase part
    if(vertexBuffer!=NULL)
    {
            glEnable(GL_BLEND);
            glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
            //glBlendFunc(1.0,0.0);

            glEnable(GL_POINT_SPRITE_OES);
            glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
            glPointSize(64.0 / kBrushScale);
            
            glBindTexture(GL_TEXTURE_2D, textureSprite);
            glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
            
  // only draw in the alpha-component        
glColorMask(0,0,0,1);
            glDrawArrays(GL_POINTS, 0, vCount);

            glDisable(GL_POINT_SPRITE_OES);
            glDisable(GL_BLEND);
    }
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    glColorMask(1,1,1,0);

    
    
    
// switch back to the normal frame buffer  
    [(EAGLView *)self.view setFramebuffer];    
    [(EAGLView *)self.view setViewPort];
    glClear(GL_COLOR_BUFFER_BIT);
    

// bind the fbo-texture and blend it over the background-texture    
    GLuint tmpTexture=[(EAGLView *)self.view img];
    glBindTexture(GL_TEXTURE_2D, tmpTexture);

    glVertexPointer(2, GL_FLOAT, 0, boxBackground);
    glTexCoordPointer(2, GL_FLOAT, 0, uvBackground);

   // glEnable(GL_ALPHA_TEST);
    //glAlphaFunc(GL_GREATER, 0.0);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glDisable(GL_ALPHA_TEST);
    glDisable(GL_BLEND);
    
    [(EAGLView *)self.view presentFramebuffer];



RE: Discrad black parts in texture - bonanza - Oct 27, 2011 01:05 AM

This should work:
http://www.zeuscmd.com/tutorials/opengles/23-Masking.php

Here my code
Code:
glBindTexture(GL_TEXTURE_2D, textureBackground);
    glVertexPointer(2, GL_FLOAT, 0, boxBackground);
    glTexCoordPointer(2, GL_FLOAT, 0, uvBackground);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    glEnable(GL_BLEND);

    glDisable(GL_DEPTH_TEST);    
    glBlendFunc(GL_DST_COLOR, GL_ZERO);
    
    // black lines from the RTT
    GLuint tmpTexture=[(EAGLView *)self.view img];
    glBindTexture(GL_TEXTURE_2D, tmpTexture);
    glVertexPointer(2, GL_FLOAT, 0, boxBackground);
    glTexCoordPointer(2, GL_FLOAT, 0, uvBackground);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
  
    glBlendFunc(GL_ONE, GL_ONE);
    glBindTexture(GL_TEXTURE_2D, textureForeground);
    glVertexPointer(2, GL_FLOAT, 0, boxBackground);
    glTexCoordPointer(2, GL_FLOAT, 0, uvBackground);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    
    glDisable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);

The result is masked but it's somehow overbright-ed. Any ideas?