Search code examples
javaopenglglsltextureslwjgl

Writing raw data to texture using GLSL, lwjgl


currently, I need the fragment shader to write to a texture(which it does), but rather than overwriting, it blends. Here is the fragment shader itself

#version 400 core

in vec2 pass_textureCoordinates;

out vec4 out_Color;
layout(location = 1) out vec4 out_location0;

uniform sampler2D modelTexture;
uniform sampler2D bumpTexture;

uniform sampler2D overlayTexture;
uniform sampler2D scratchLevels;

void main(void)
{
    vec2 txt = pass_textureCoordinates;
    vec4 base = texture(overlayTexture,txt);
    vec4 over = texture(modelTexture,txt);
    
    float baseA = base[3] * (1.0f - over[3]);
    float overA = over[3];
    float finalA = base[3] + (1.0f - base[3]) * overA;
    if(finalA == 0)
    {
        out_Color[0] = 0;
        out_Color[1] = 0;
        out_Color[2] = 0;
    }
    else
    {
        out_Color[0] = (base[0] * baseA + over[0] * overA) / finalA;
        out_Color[1] = (base[1] * baseA + over[1] * overA) / finalA;
        out_Color[2] = (base[2] * baseA + over[2] * overA) / finalA;
    }
    out_Color[3] = finalA; 
    out_location0 = out_Color;
}

How can I write to the texture with out blending?

Edit: I need to overwrite the alpha channel as well


Solution

  • Well you have several solutions to choose from:

    1. the easiest is just to Disable Blending before the render call glDisable(GL_BLEND) and enable it again afterwards
    2. You could just clear the data of the texture before rendering (if that is what you want) with opengl calls, e.g. glClear(GL_COLOR_BUFFER_BIT) and glClearColor(r,g,b)
    3. change the alpha in your fragment shader e.g. to if(out_Color.a > 0) out_Color = vec4(vec3(0,0,0)*(1.0-out_Color.a) + out_Color.rgb*out_Color.a, 1.0); this is just manually setting the background to black, but you could also discard it if out_Color.a == 0 (just discard;, that would - at least without a glClear call - cause the old data to stay visible at this pixel).

    I hope I was able to help you :)