Project

General

Profile

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

library / src / main / res / raw / main_fragment_shader.glsl @ 1cbf4103

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
in vec3 v_Position;                     // Interpolated position for this fragment.
24
in vec3 v_Normal;                       // Interpolated normal for this fragment.
25
in vec2 v_TexCoordinate;                // Interpolated texture coordinate per fragment.
26
out vec4 fragColor;                     // The output color
27
#define TEXTURE texture
28
#define FRAG_COLOR fragColor
29
#else
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
#define TEXTURE texture2D
34
#define FRAG_COLOR gl_FragColor
35
#endif
36

    
37
uniform sampler2D u_Texture;            // The input texture.
38

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

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

    
49
//////////////////////////////////////////////////////////////////////////////////////////////
50
// CHROMA EFFECT
51

    
52
#if defined(CHROMA) || defined(SMOOTH_CHROMA)
53
void chroma(float degree, int effect, inout vec4 color)
54
  {
55
  color.rgb = mix(color.rgb, fUniforms[effect].yzw, degree*fUniforms[effect].x);
56
  }
57
#endif
58

    
59
//////////////////////////////////////////////////////////////////////////////////////////////
60
// ALPHA EFFECT (change transparency level)
61

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

    
69
//////////////////////////////////////////////////////////////////////////////////////////////
70
// BRIGHTNESS EFFECT
71

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

    
79
//////////////////////////////////////////////////////////////////////////////////////////////
80
// CONTRAST EFFECT
81

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

    
89
//////////////////////////////////////////////////////////////////////////////////////////////
90
// SATURATION EFFECT
91

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

    
100
#endif    // NUM_FRAGMENT>0
101

    
102
//////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
void main()                    		
105
  {  
106
  vec4 pixel = TEXTURE(u_Texture,v_TexCoordinate);
107

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

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

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

    
150
    j+=2;
151
    }
152
#endif
153

    
154
  FRAG_COLOR = vec4(pixel.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, pixel.a);
155
  }
(6-6/9)