carbontwelve / roguelikedev2019

My attempt at learning roguelike gamedev in 2019
MIT License
2 stars 0 forks source link

Implement alpha compositing for font tileset #3

Closed carbontwelve closed 5 years ago

carbontwelve commented 5 years ago

The way that libtcod implements font image files means in order to support the TCOD format the code needs to be able to set the alpha channel from the solid background colour.

libtcod does this by looking at the top left pixel and identifying its colour, then using that to set the alpha for the file.

In order to complete this issue the code needs to modify the image and set alpha correctly before converting to a Texture2D and passing to the TileSheet struct.

As an aside this looks to be an excellent tool https://extended-ascii-viewer.herokuapp.com/

carbontwelve commented 5 years ago

libtcod loads font files via TCOD_console_set_custom_font: here

It has something to do with setKeyColor with the key colour being the alpha.

carbontwelve commented 5 years ago

Something along these lines? https://developer.apple.com/documentation/coreimage/applying_a_chroma_key_effect

carbontwelve commented 5 years ago

Look at https://community.khronos.org/t/help-with-luminance-alpha-masking/54088

void main(void)
{   
    vec4 originalColor = texture2DRect(mask, gl_TexCoord[0].xy); //the mask
    vec4 maskedImage = texture2DRect(masked, gl_TexCoord[0].xy);
    vec4 mixed = vec4(0,0,0,0);

    vec4 LUMINENCE_FACTOR  = vec4(0.27, 0.67, 0.06, 0.0);

    // take the dot product of the luminance factor
    float lum = dot(originalColor, LUMINENCE_FACTOR);

    lum = 1.0 - lum;

    mixed = vec4(maskedImage.rgb, lum);

    gl_FragColor = mixed;   
}

This uses vec4 which is supported in Golang via third party libs. However our input image has no alpha (or rather the alpha is zeroed) so we should get away with using the built in Vector that comes with the Golang standard lib.

carbontwelve commented 5 years ago

Also look at https://stackoverflow.com/a/42518487/1225977

carbontwelve commented 5 years ago

Looks like this may be one approach that libtcod uses: https://github.com/libtcod/libtcod/blob/197b6c34a5f3f08d3a64008a13276971552ddefd/src/libtcod/sys_sdl_c.cpp#L366-L379

pixel = static_cast<uint8_t*>(charmap->pixels)
        + keyy * charmap->pitch + keyx * charmap->format->BytesPerPixel;
        fontKeyCol.r=*((pixel)+charmap->format->Rshift/8);
        fontKeyCol.g=*((pixel)+charmap->format->Gshift/8);
        fontKeyCol.b=*((pixel)+charmap->format->Bshift/8);
        TCOD_LOG(("key color : %d %d %d\n",fontKeyCol.r,fontKeyCol.g,fontKeyCol.b));
        if ( ! TCOD_ctx.font_greyscale && charmap->format->BytesPerPixel == 4 ) {
            /* 32 bits font but alpha layer not used. convert to 24 bits (faster) */
            SDL_Surface *temp;
            TCOD_LOG(("32bits font with no alpha => converting to faster 24 bits\n"));
            temp = TCOD_sys_get_surface(charmap->w, charmap->h, false);
            SDL_BlitSurface(charmap,NULL,temp,NULL);
            SDL_FreeSurface(charmap);
            charmap=temp;
        }
carbontwelve commented 5 years ago

Closed by #11