Files
cpp-imanox-chroma-matting/opengl-test-two/refinement.fragmentshader
2016-09-23 13:27:34 +02:00

137 lines
3.4 KiB
Plaintext

#version 330 core
// Interpolated values from the vertex shaders
in vec2 UV;
// Ouput data
out vec3 color;
// Values that stay constant for the whole mesh.
uniform sampler2D trimap;
uniform sampler2D foreground;
uniform sampler2D background;
// Relative width/height of a pixel
uniform float pixWidth;
uniform float pixHeight;
int IS_BACKGROUND = 0;
int IS_UNDEFINED = 1;
int IS_FOREGROUND = 2;
int UNSET = -1;
float M_PI= 3.14159;
int suchbreich=20; //die anzahl der Pixel, die maximal in eine Richtung gegangen wird um einen Sicheren Pixel zu finden
vec4 SicheresPixel[20]; //Speichert die das nachste Sichere Pixel, Array grosse=anzahl der Richtungen
int anzahl_richtungen=20; //Speichert anzahl der Richtungen muss gleich der Array grosse von SicheresPixel sein
vec3 getTriColor(vec2 uvCoord) {
return texture(trimap, uvCoord).rgb;
}
vec3 getForeColor(vec2 uvCoord) {
return texture(foreground, uvCoord).rgb;
}
vec3 getBackColor(vec2 uvCoord) {
return texture(background, uvCoord).rgb;
}
int getState(vec3 c) {
if (all(lessThanEqual(c, vec3(0.2f)))) {
return IS_BACKGROUND;
} else if (all(greaterThanEqual(c, vec3(0.8f)))) {
return IS_FOREGROUND;
} else {
return IS_UNDEFINED;
}
}
vec4 suche_Pixel_nach_winkel (vec2 uv, float winkel){
vec2 pix;
float i=1;
vec3 color_sichers_Pixel;
while(i<suchbreich){
pix.x=uv.x+ sin(winkel)*pixWidth*i;
pix.y=uv.y+ cos(winkel)*pixWidth*i;
color_sichers_Pixel = getTriColor(pix);
int status = getState(color_sichers_Pixel);
if (status==IS_FOREGROUND) {
return vec4(getForeColor(pix), i);
} else if (status==IS_BACKGROUND) {
return vec4(getBackColor(pix), i);
}else{
i++;
}
}
return vec4(0.0f, 0.0f, 0.0f, -1.0f);
}
float Farbabstand(int i){
float d=sqrt(pow(color.x-SicheresPixel[i].x,2)+pow(color.y-SicheresPixel[i].y,2)+pow(color.z-SicheresPixel[i].z, 2));//sqr((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)
return d;
}
vec3 Pruefe_Pixel_in_Umgebung (vec2 start){
float winkel=2*M_PI/anzahl_richtungen;
int i=0;
float gefundene_pixel=0.0f;
float maximal=0;
float gesamt_abstand=0.0f;
while(i<anzahl_richtungen){
SicheresPixel[i]=suche_Pixel_nach_winkel(start, winkel*i);
if(SicheresPixel[i].w!=-1){
gesamt_abstand+=SicheresPixel[i].w;
gefundene_pixel++;
if(maximal<SicheresPixel[i].w)
maximal=SicheresPixel[i].w;
}
i=i+1;
}
maximal++;
//float d = Farbabstand(1);//eigene Funktion
/*float d1=distance(SicheresPixel[1].xyz, color);//mitgelieferte Funktion
if (d1==d ){
return SicheresPixel[1];
}else{
return vec3(0.5f);
}*/
i=0;
vec3 kombiniert=vec3(0.0f);
float nenner=gefundene_pixel*maximal-gesamt_abstand;
while(i<anzahl_richtungen){
if(SicheresPixel[i].w!=-1)
kombiniert+=SicheresPixel[i].xyz*(maximal-SicheresPixel[i].w)/nenner;
i++;
}
return kombiniert;
}
void main(){
color = getTriColor(UV);
int status = getState(color);
if (status==IS_FOREGROUND) {
color = getForeColor(UV).rgb;
} else if (status==IS_BACKGROUND) {
color = getBackColor(UV).rgb;
} else {
vec3 h=Pruefe_Pixel_in_Umgebung(UV);
//h=vec3(0.5f);
color = h;
}
}