1 year ago

#377233

test-img

Brotchu

Mixing colours in OpenGL GLSL

I am trying to create a translucent , stained glass effect in OpenGL, using C++.

I use a normal map generated from a perlin noise texture. Refraction is calculated with respect to manipulated normal vectors and it works. For stained glass, I am trying mixing the colour from skybox through refraction, with another colour. I tried this with a constant value, and that works too. When I try to get the second colour from a texture, I do not see the object on screen. I can load the texture if i ignore refraction and mixing though. here is my vertex shader :

#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoord;
layout (location = 3) in vec3 aTangent;
layout (location = 4) in vec3 aBitangent;

out vec3 FragPos;
out vec2 texCoord;
out vec3 tangentLightPos;
out vec3 tangentViewPos;
out vec3 tangentFragPos;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

//normal
uniform vec3 lightPos;
uniform vec3 viewPos;

void main()
{
    texCoord = aTexCoord;
    FragPos = vec3(view * model * vec4(aPos, 1.0));

    mat3 normalMatrix = transpose(inverse(mat3(model)));
    vec3 T = normalize(normalMatrix * aTangent);
    vec3 N = normalize(normalMatrix * aNormal);
    T = normalize(T - dot(T, N) * N);
    vec3 B = cross(N, T);
    mat3 TBN = transpose(mat3(T, B, N));  

    tangentLightPos = TBN * lightPos;
    tangentViewPos = TBN * viewPos;
    tangentFragPos = TBN * FragPos;

    gl_Position = projection * view * model * vec4(aPos, 1.0);
}

and my fragment shader :

#version 330 core
out vec4 FragColor;

in vec3 FragPos;
in vec2 texCoord;
in vec3 tangentLightPos;
in vec3 tangentViewPos;
in vec3 tangentFragPos;

uniform sampler2D texture1;
uniform sampler2D texture2;
uniform samplerCube skybox;

uniform vec3 cameraPos;

void main()
{
    //normal map - normal
    vec3 normalDir = texture(texture2, texCoord).rgb;
    normalDir = normalize(normalDir * 2.0 - 1.0);

    vec3 Incident = normalize(tangentFragPos - cameraPos);
    vec3 RefractedDir = refract(Incident, normalDir, 1.0/1.2);

    float DispersionIndex = 1.0 - dot(Incident, normalDir);
    vec3 RefractedFragColor = vec3(texture(skybox, RefractedDir).rgb);
 
    //FIXME:
    vec3 texColor = texture(texture1, texCoord).rgb;
    vec3 constTexColor = vec3(1.0, 1.0, 1.0);
 
    FragColor = mix( vec4(RefractedFragColor, 1.0), vec4(texColor.rgb, 0.5), 0.2); // doesnt show object on screen
    // FragColor = mix( vec4(RefractedFragColor, 1.0), vec4(constTexColor.rgb, 0.5), 0.2); --> works , shows a tint on glass
    // FragColor = vec4(texColor, 1.0); ---> shows only texture
}

with constant value for color in mix : enter image description here

with only texture: enter image description here

Why does the mix not work with color from texture?

c++

opengl

glsl

0 Answers

Your Answer

Accepted video resources