Project

General

Profile

Download (6.64 KB) Statistics
| Branch: | Revision:

library / src / main / res / raw / main_fragment_shader.glsl @ 2fce34f4

1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
3
//                                                                                          //
4
// This file is part of Distorted.                                                          //
5
//                                                                                          //
6
// Distorted is free software: you can redistribute it and/or modify                        //
7
// it under the terms of the GNU General Public License as published by                     //
8
// the Free Software Foundation, either version 2 of the License, or                        //
9
// (at your option) any later version.                                                      //
10
//                                                                                          //
11
// Distorted is distributed in the hope that it will be useful,                             //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                           //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            //
14
// GNU General Public License for more details.                                             //
15
//                                                                                          //
16
// You should have received a copy of the GNU General Public License                        //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18
//////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
precision highp float;
21
  
22
uniform sampler2D u_Texture;            // The input texture.
23
    
24
varying vec3 v_Position;                // Interpolated position for this fragment.
25
varying vec4 v_Color;                   // This is the color from the vertex shader interpolated across the triangle per fragment.
26
varying vec3 v_Normal;                  // Interpolated normal for this fragment.
27
varying vec2 v_TexCoordinate;           // Interpolated texture coordinate per fragment.
28

    
29
uniform int fNumEffects;                // total number of fragment effects
30

    
31
#if NUM_FRAGMENT>0
32
uniform int fType[NUM_FRAGMENT];        // their types.
33
uniform vec4 fUniforms[2*NUM_FRAGMENT]; // i-th effect is 2 consecutive vec4's: [2*i], [2*i+1]. First vec4 is the Interpolated values,
34
                                        // next describes the Region, i.e. area over which the effect is active.
35

    
36
const vec3 LUMI = vec3( 0.2125, 0.7154, 0.0721 );                                        
37
 
38
//////////////////////////////////////////////////////////////////////////////////////////////
39
// macroblocks
40

    
41
void macroblock(float degree, int effect, inout vec2 tex)
42
  {
43
  vec2 one = vec2(1.0,1.0);  
44
  vec2 a = degree*(fUniforms[effect].yz-one)+one;
45
  tex = ( max((1.0-degree)*tex,floor(tex*a)) + degree*vec2(0.5,0.5) ) / a;
46
  }
47

    
48
//////////////////////////////////////////////////////////////////////////////////////////////
49
// change the RGB values
50

    
51
void chroma(float degree, int effect, inout vec4 color)
52
  {
53
  color.rgb = mix(color.rgb, fUniforms[effect].yzw, degree*fUniforms[effect].x);
54
  }
55

    
56
//////////////////////////////////////////////////////////////////////////////////////////////
57
// change the transparency level
58

    
59
void alpha(float degree, int effect, inout vec4 color)
60
  {
61
  color.a *= (degree*(fUniforms[effect].x-1.0)+1.0); 
62
  }
63

    
64
//////////////////////////////////////////////////////////////////////////////////////////////
65
// change the brightness level
66

    
67
void brightness(float degree, int effect, inout vec4 color)
68
  {
69
  color.rgb = mix(vec3(0.0,0.0,0.0), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
70
  }
71
  
72
//////////////////////////////////////////////////////////////////////////////////////////////
73
// change the contrast level
74

    
75
void contrast(float degree, int effect, inout vec4 color)
76
  {
77
  color.rgb = mix(vec3(0.5,0.5,0.5), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
78
  }
79

    
80
//////////////////////////////////////////////////////////////////////////////////////////////
81
// change the saturation level
82

    
83
void saturation(float degree, int effect, inout vec4 color)
84
  {
85
  float luminance = dot(LUMI,color.rgb);
86
  color.rgb = mix(vec3(luminance,luminance,luminance), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
87
  }
88

    
89
#endif
90

    
91
//////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
void main()                    		
94
  {  
95
  vec2 tex = v_TexCoordinate;
96
  vec4 col = v_Color;
97
  vec2 diff;
98
  float pointDegree;
99
  
100
#if NUM_FRAGMENT>0  
101
  for(int i=0; i<fNumEffects; i++)
102
    {
103
    diff = (v_Position.xy - fUniforms[2*i+1].xy)/fUniforms[2*i+1].zw;
104
    pointDegree = max(0.0,1.0-dot(diff,diff));
105
  
106
    //switch(fType[i])  // future version of GLSL
107
    //  {
108
    //  case MACROBLOCK        : macroblock(sign(pointDegree),2*i,tex); break;
109
    //  case CHROMA            : chroma    (sign(pointDegree),2*i,col); break;
110
    //  case SMOOTH_CHROMA     : chroma    (     pointDegree ,2*i,col); break;
111
    //  case ALPHA             : alpha     (sign(pointDegree),2*i,col); break;
112
    //  case SMOOTH_ALPHA      : alpha     (     pointDegree ,2*i,col); break;
113
    //  case BRIGHTNESS        : brightness(sign(pointDegree),2*i,col); break;
114
    //  case SMOOTH_BRIGHTNESS : brightness(     pointDegree ,2*i,col); break;
115
    //  case CONTRAST          : contrast  (sign(pointDegree),2*i,col); break;
116
    //  case SMOOTH_CONTRAST   : contrast  (     pointDegree ,2*i,col); break;
117
    //  case SATURATION        : saturation(sign(pointDegree),2*i,col); break;
118
    //  case SMOOTH_SATURATION : saturation(     pointDegree ,2*i,col); break;
119
    //  }
120
    
121
         if( fType[i]==MACROBLOCK        ) macroblock(sign(pointDegree),2*i,tex);
122
    else if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),2*i,col);
123
    else if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,2*i,col);
124
    else if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),2*i,col);
125
    else if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,2*i,col);
126
    else if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),2*i,col);
127
    else if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,2*i,col);
128
    else if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),2*i,col);
129
    else if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,2*i,col);
130
    else if( fType[i]==SATURATION        ) saturation(sign(pointDegree),2*i,col);
131
    else if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,2*i,col);
132
    }
133
#endif
134
 
135
  gl_FragColor = (col * 0.5 * (v_Normal.z+1.0) * texture2D(u_Texture, tex));
136
  }
(1-1/2)