Thread: OpenGL Masks

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    15

    OpenGL Masks

    I have been trying to implement a sort of mask with OpenGL for the last few days and have been unsuccessful so far.

    I found this solution - c++ - OpenGL - mask with multiple textures - Stack Overflow which I tried but was only partly successful in that the mask worked but then my foreground had no alpha blending. This was in an application for PS3. I haven't recently written a short test program on Windows to try and get a ful working solution but seem to have gone backwards.

    My current code:
    Code:
    gluOrtho2D(0.0f, 1.0f, 1.0f, 0.0f);
    
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ZERO);
    renderBackground(); // Draws multiple primitives
    
    glEnableClientState(GL_VERTEX_ARRAY);
    // Next, we want a blendfunc that doesn't change the color of any pixels,
    // but rather replaces the framebuffer alpha values with values based
    // on the mask. In other words, if a pixel is alpha value 1 in the mask,
    // then the corresponding framebuffer pixel's alpha will be set to 1.
    glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_ALPHA, GL_ZERO);
    
    // Now "draw" the mask (again, this doesn't produce a visible result, it just
    // changes the alpha values in the framebuffer)
    GLfloat mask[16] = {0,0, 1,0, 0,0.1, 1,0.1, 0,0.9, 1,0.9, 0,1, 1,1 };
    GLfloat colour[32] = {	0,0,0,0,
                                    0,0,0,0,
                                    0,0,0,1,
                                    0,0,0,1,
                                    0,0,0,1,
                                    0,0,0,1,
                                    0,0,0,0,
                                    0,0,0,0 };
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, mask);
    glColorPointer(4, GL_FLOAT, 0, colour);
    glDrawArrays(GL_QUAD_STRIP, 0, 8);
    glDisableClientState(GL_COLOR_ARRAY);
    
    // Finally, we want a blendfunc that makes the foreground visible only in
    // areas with high alpha.
    glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
    renderForeground(); //Draws multiple primitives which also include alpha values
    
    glFlush(); // Flush the OpenGL buffers to the window
    On the PS3 the mask was replacing all the alpha values of the foreground rather than merging the alpha values from mask and foreground, but on Windows its not even doing that and all I get is the foreground without any transparency. Hope I've been concise enough.
    Thanks in advance.
    Last edited by will of fortune; 08-20-2012 at 07:46 AM. Reason: strange spacing in code tag

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf masks Doubt(s)
    By RLeePlusPlus in forum C Programming
    Replies: 23
    Last Post: 11-27-2011, 02:21 AM
  2. Replies: 15
    Last Post: 10-06-2009, 11:20 PM
  3. LAN IP masks?
    By whackaxe in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-12-2004, 04:30 AM
  4. Input Masks - PASSWORDS
    By destiny_believe in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2004, 02:03 PM
  5. Map masks?
    By Kavity in forum Game Programming
    Replies: 8
    Last Post: 11-11-2001, 03:00 AM

Tags for this Thread