Project

General

Profile

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

library / src / main / res / raw / main_fragment_shader.glsl @ 94f6d472

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
#if __VERSION__ != 100
23
#define TEXTURE texture
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
#else
29
#define TEXTURE texture2D
30
varying vec3 v_Position;                // Interpolated position for this fragment.
31
varying vec3 v_Normal;                  // Interpolated normal for this fragment.
32
varying vec2 v_TexCoordinate;           // Interpolated texture coordinate per fragment.
33
#endif
34

    
35
uniform sampler2D u_Texture;            // The input texture.
36

    
37
#if NUM_FRAGMENT>0
38
uniform int fNumEffects;                // total number of fragment effects
39
uniform int fType[NUM_FRAGMENT];        // their types.
40
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,
41
                                        // next describes the Region, i.e. area over which the effect is active.
42

    
43
#if defined(SATURATION) || defined(SMOOTH_SATURATION)
44
const vec3 LUMI = vec3( 0.2125, 0.7154, 0.0721 );                                        
45
#endif
46

    
47
//////////////////////////////////////////////////////////////////////////////////////////////
48
// CHROMA EFFECT
49

    
50
#if defined(CHROMA) || defined(SMOOTH_CHROMA)
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
#endif
56

    
57
//////////////////////////////////////////////////////////////////////////////////////////////
58
// ALPHA EFFECT (change transparency level)
59

    
60
#if defined(ALPHA) || defined(SMOOTH_ALPHA)
61
void alpha(float degree, int effect, inout vec4 color)
62
  {
63
  color.a *= (degree*(fUniforms[effect].x-1.0)+1.0); 
64
  }
65
#endif
66

    
67
//////////////////////////////////////////////////////////////////////////////////////////////
68
// BRIGHTNESS EFFECT
69

    
70
#if defined(BRIGHTNESS) || defined(SMOOTH_BRIGHTNESS)
71
void brightness(float degree, int effect, inout vec4 color)
72
  {
73
  color.rgb = mix(vec3(0.0,0.0,0.0), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
74
  }
75
#endif
76

    
77
//////////////////////////////////////////////////////////////////////////////////////////////
78
// CONTRAST EFFECT
79

    
80
#if defined(CONTRAST) || defined(SMOOTH_CONTRAST)
81
void contrast(float degree, int effect, inout vec4 color)
82
  {
83
  color.rgb = mix(vec3(0.5,0.5,0.5), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
84
  }
85
#endif
86

    
87
//////////////////////////////////////////////////////////////////////////////////////////////
88
// SATURATION EFFECT
89

    
90
#if defined(SATURATION) || defined(SMOOTH_SATURATION)
91
void saturation(float degree, int effect, inout vec4 color)
92
  {
93
  float luminance = dot(LUMI,color.rgb);
94
  color.rgb = mix(vec3(luminance,luminance,luminance), color.rgb, degree*(fUniforms[effect].x-1.0)+1.0 ); 
95
  }
96
#endif
97

    
98
#endif    // NUM_FRAGMENT>0
99

    
100
//////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
void main()                    		
103
  {  
104
  vec4 pixel = TEXTURE(u_Texture,v_TexCoordinate);
105

    
106
#if NUM_FRAGMENT>0
107
  vec2 diff;
108
  float pointDegree;
109
  int j=0;
110

    
111
  for(int i=0; i<fNumEffects; i++)
112
    {
113
    diff = (v_Position.xy - fUniforms[j+1].xy)/fUniforms[j+1].zw;
114
    pointDegree = max(0.0,1.0-dot(diff,diff));
115

    
116
#ifdef CHROMA
117
    if( fType[i]==CHROMA            ) chroma    (sign(pointDegree),j,pixel); else
118
#endif
119
#ifdef SMOOTH_CHROMA
120
    if( fType[i]==SMOOTH_CHROMA     ) chroma    (     pointDegree ,j,pixel); else
121
#endif
122
#ifdef ALPHA
123
    if( fType[i]==ALPHA             ) alpha     (sign(pointDegree),j,pixel); else
124
#endif
125
#ifdef SMOOTH_ALPHA
126
    if( fType[i]==SMOOTH_ALPHA      ) alpha     (     pointDegree ,j,pixel); else
127
#endif
128
#ifdef BRIGHTNESS
129
    if( fType[i]==BRIGHTNESS        ) brightness(sign(pointDegree),j,pixel); else
130
#endif
131
#ifdef SMOOTH_BRIGHTNESS
132
    if( fType[i]==SMOOTH_BRIGHTNESS ) brightness(     pointDegree ,j,pixel); else
133
#endif
134
#ifdef CONTRAST
135
    if( fType[i]==CONTRAST          ) contrast  (sign(pointDegree),j,pixel); else
136
#endif
137
#ifdef SMOOTH_CONTRAST
138
    if( fType[i]==SMOOTH_CONTRAST   ) contrast  (     pointDegree ,j,pixel); else
139
#endif
140
#ifdef SATURATION
141
    if( fType[i]==SATURATION        ) saturation(sign(pointDegree),j,pixel); else
142
#endif
143
#ifdef SMOOTH_SATURATION
144
    if( fType[i]==SMOOTH_SATURATION ) saturation(     pointDegree ,j,pixel); else
145
#endif
146
    {}
147

    
148
    j+=2;
149
    }
150
#endif
151

    
152
#if __VERSION__ != 100
153
  fragColor    =
154
#else
155
  gl_FragColor =
156
#endif
157

    
158
  vec4(pixel.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, pixel.a);
159
  }
(6-6/9)