Discrad black parts in texture

Apprentice
Posts: 9
Joined: 2010.02
Post: #1
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 ?
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #2
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.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2010.02
Post: #3
I think, read the pixel at realtime is a performance problem.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
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.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2010.02
Post: #5
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.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
Fair enough. Sounds like modifying your pixels in memory and calling glTexSubImage2D is exactly what you'd want for that case.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2010.02
Post: #7
Mhh, i don't no, how to do that.

I have found this: http://stackoverflow.com/questions/34396...16#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];
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2010.02
Post: #8
This should work:
http://www.zeuscmd.com/tutorials/opengle...asking.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?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Erasing parts of an image uripo 0 1,897 Jan 13, 2010 06:13 PM
Last Post: uripo
  Fade Particle to black bonanza 8 5,298 Apr 3, 2008 05:24 AM
Last Post: bonanza
  Black Bumpmaps Rassilon 2 2,970 Apr 12, 2005 04:43 AM
Last Post: isgoed
  Blending -> Black Lines Through Textures (Wireframe like) hangt5 2 3,225 Apr 5, 2005 05:06 AM
Last Post: ThemsAllTook
  glOrtho and a black screen Taxxodium 4 6,156 Sep 27, 2004 01:30 PM
Last Post: Vertizor