How to make white transparent

I play with OGL mix. I have a white background. I draw a quad bike on this background. The square is tied to a white and black texture. An array of colors is filled with color and alpha values:

for (i = 0; i < IMAGE_WIDTH; i++) {
    for (j = 0; j < MAGE_HEIGHT; j++) {
        c = ((((i&0x8)==0)^((j&0x8))==0))*255;
        Image[i][j][0] = (GLubyte) c;
        Image[i][j][1] = (GLubyte) c;
        Image[i][j][2] = (GLubyte) c;
        Image[i][j][3] = (GLubyte) 255;
    };

Textured square display:

glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE);    
glDisable (GL_DEPTH_TEST);                                              

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texCheck);        

glLoadIdentity();                                                                      
glTranslatef (0.0f, 0.0f, -9.0f);

glBegin (GL_QUADS);    
    glColor3f (1.0f, 1.0f, 1.0f);
    glTexCoord2f(0.0, 0.0); glVertex3f(-5.0f, -5.0f, 0.0f);
    glTexCoord2f(0.0, 1.0); glVertex3f( 5.0f, -5.0f, 0.0f);
    glTexCoord2f(1.0, 1.0); glVertex3f( 5.0f, 5.0f, 0.0f);
    glTexCoord2f(1.0, 0.0); glVertex3f(-5.0f, 5.0f, 0.0f);
glEnd();

glDisable(GL_TEXTURE_2D);
glEnable (GL_DEPTH_TEST);                                              
glDisable (GL_BLEND);  

The black color of the tethered texture on the ATV is invisible and works great. What should I do so that the white color of the connected texture is transparent and the black color of the connected text is not transparent.

+4
source share
2 answers

You need to solve 2 questions:

  • differse texels -, -. , , , , - 255, - 0. , - 0 255 , .

  • blebding glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)


( ), ( ).

glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA), glBlendEquation GL_FUNC_ADD ( ), :

C_dest_new = C_src * A_src + C_dest * (1-A_src)

- 0.0:

C_dest_new = C_src * 0 + C_dest * (1-0) = C_dest

- 1.0 (255):

C_dest_new = C_src * 1 + C_dest * (1-1) = C_src    

- 0, , , :

for (i = 0; i < IMAGE_WIDTH; i++) {
    for (j = 0; j < MAGE_HEIGHT; j++) {
        c = ((((i&0x8)==0)^((j&0x8))==0))*255;
        Image[i][j][0] = (GLubyte) c;
        Image[i][j][1] = (GLubyte) c;
        Image[i][j][2] = (GLubyte) c;
        Image[i][j][3] = (GLubyte) (255 - c);
    }
}
+1

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

GL_ONE , -, .

+2

Source: https://habr.com/ru/post/1683728/


All Articles