Kaylee Cayton
Kaylee Cayton

Reputation: 27

Opengl use destination luminance to blend color to black or white

Aseprite is a pixel art drawing app. The cursor becomes a crosshair when using the pencil tool in Aseprite. The crosshair is white when the destination pixel luminance is between 0 and 127. The crosshair is black when the the destination pixel luminance is between 128 and 255. The images below show Aseprite's crosshair.

enter image description here enter image description here

I'm developing my own pixel art drawing app using ModernGL (A Python wrapper for Modern OpenGL). I've managed to invert the destination color as shown below, but I'd like for the destination color to be black or white depending on luminance. enter image description here

I've enabled blending.

gl_context.enable(moderngl.BLEND)

Here's my blending equation:

gl_context.blend_equation = moderngl.FUNC_ADD

Here's my blending function:

gl_context.blend_func = (moderngl.SRC_ALPHA, moderngl.ONE_MINUS_SRC_ALPHA)

Here's my vertex shader:

#version 330 core

uniform float aspect;

in vec2 vert;
in vec2 texcoord;
out vec2 uvs;
void main() {
    uvs = texcoord;
    gl_Position = vec4(
    vert.x / aspect, 
    vert.y, 0.0, 1.0
    );
}

Here's my fragment shader:

#version 330 core

uniform sampler2D tex;
uniform float red;

in vec2 uvs;
out vec4 f_color;

void main() {
    float grey_scale = (0.3333 * texture(tex, uvs).r) + (0.3333 * texture(tex, uvs).g) + (0.3333 * texture(tex, uvs).b);
    f_color = vec4(
        texture(tex, uvs).r,
        texture(tex, uvs).g,
        texture(tex, uvs).b,
        texture(tex, uvs).a
        );
    float factor = 1.0 * f_color.a;
    f_color.rgb *= factor;
    f_color.rgb *= grey_scale;
    f_color = f_color;
}

Upvotes: -1

Views: 34

Answers (0)

Related Questions