Project

General

Profile

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

library / src / main / res / raw / main_fragment_shader.glsl @ f2367b75

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 lowp float;
21
  
22
uniform sampler2D u_Texture;            // The input texture.
23
    
24
in vec3 v_Position;                     // Interpolated position for this fragment.
25
in vec3 v_Normal;                       // Interpolated normal for this fragment.
26
in vec2 v_TexCoordinate;                // Interpolated texture coordinate per fragment.
27
out vec4 fragColor;                     // The output color
28

    
29
#if NUM_FRAGMENT>0
30
uniform int fNumEffects;                // total number of fragment effects
31
uniform int fType[NUM_FRAGMENT];        // their types.
32
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,
33
                                        // next describes the Region, i.e. area over which the effect is active.
34

    
35
#if defined(SATURATION) || defined(SMOOTH_SATURATION)
36
const vec3 LUMI = vec3( 0.2125, 0.7154, 0.0721 );                                        
37
#endif
38

    
39
//////////////////////////////////////////////////////////////////////////////////////////////
40
// CHROMA EFFECT
41

    
42
#if defined(CHROMA) || defined(SMOOTH_CHROMA)
43
void chroma(float degree, int effect, inout vec4 color)
44
  {
45
  color.rgb = mix(color.rgb, fUniforms[effect].yzw, degree*fUniforms[effect].x);
46
  }
47
#endif
48

    
49
//////////////////////////////////////////////////////////////////////////////////////////////
50
// ALPHA EFFECT (change transparency level)
51

    
52
#if defined(ALPHA) || defined(SMOOTH_ALPHA)
53
void alpha(float degree, int effect, inout vec4 color)
54
  {
55
  color.a *= (degree*(fUniforms[effect].x-1.0)+1.0); 
56
  }
57
#endif
58

    
59
//////////////////////////////////////////////////////////////////////////////////////////////
60
// BRIGHTNESS EFFECT
61

    
62
#if defined(BRIGHTNESS) || defined(SMOOTH_BRIGHTNESS)
63
void brightness(float degree, int effect, inout vec4 color)
64
  {
65
  color.rgb = mix(vec3(0.0,0.0,0.0), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
66
  }
67
#endif
68

    
69
//////////////////////////////////////////////////////////////////////////////////////////////
70
// CONTRAST EFFECT
71

    
72
#if defined(CONTRAST) || defined(SMOOTH_CONTRAST)
73
void contrast(float degree, int effect, inout vec4 color)
74
  {
75
  color.rgb = mix(vec3(0.5,0.5,0.5), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
76
  }
77
#endif
78

    
79
//////////////////////////////////////////////////////////////////////////////////////////////
80
// SATURATION EFFECT
81

    
82
#if defined(SATURATION) || defined(SMOOTH_SATURATION)
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
#endif
89

    
90
#endif    // NUM_FRAGMENT>0
91

    
92
//////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
void main()                    		
95
  {  
96
  vec4 pixel = texture(u_Texture,v_TexCoordinate);
97

    
98
#if NUM_FRAGMENT>0
99
  vec2 diff;
100
  float pointDegree;
101
  int j=0;
102

    
103
  for(int i=0; i<fNumEffects; i++)
104
    {
105
    diff = (v_Position.xy - fUniforms[j+1].xy)/fUniforms[j+1].zw;
106
    pointDegree = max(0.0,1.0-dot(diff,diff));
107

    
108
#ifdef CHROMA
109
    if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),j,pixel); else
110
#endif
111
#ifdef SMOOTH_CHROMA
112
    if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,j,pixel); else
113
#endif
114
#ifdef ALPHA
115
    if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),j,pixel); else
116
#endif
117
#ifdef SMOOTH_ALPHA
118
    if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,j,pixel); else
119
#endif
120
#ifdef BRIGHTNESS
121
    if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),j,pixel); else
122
#endif
123
#ifdef SMOOTH_BRIGHTNESS
124
    if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,j,pixel); else
125
#endif
126
#ifdef CONTRAST
127
    if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),j,pixel); else
128
#endif
129
#ifdef SMOOTH_CONTRAST
130
    if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,j,pixel); else
131
#endif
132
#ifdef SATURATION
133
    if( fType[i]==SATURATION        ) saturation(sign(pointDegree),j,pixel); else
134
#endif
135
#ifdef SMOOTH_SATURATION
136
    if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,j,pixel); else
137
#endif
138
    {}
139

    
140
    j+=2;
141
    }
142
#endif
143

    
144
  fragColor = vec4(pixel.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, pixel.a);
145
  }
(6-6/9)