Revision 94f6d472
Added by Leszek Koltunski over 7 years ago
src/main/java/org/distorted/library/Distorted.java | ||
---|---|---|
29 | 29 |
*/ |
30 | 30 |
public class Distorted |
31 | 31 |
{ |
32 |
static final String glslVersion="#version 300 es\n"; |
|
32 |
static int GLSL; |
|
33 |
static String GLSL_VERSION; |
|
34 |
|
|
33 | 35 |
/** |
34 | 36 |
* When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's |
35 | 37 |
* backing up our DistortedTexture. |
... | ... | |
104 | 106 |
public static void onCreate(final Context context) |
105 | 107 |
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException |
106 | 108 |
{ |
109 |
GLSL = 100; |
|
110 |
GLSL_VERSION= (GLSL==100 ? "#version 100\n" : "#version 300 es\n"); |
|
111 |
|
|
107 | 112 |
final Resources resources = context.getResources(); |
108 | 113 |
DistortedEffects.createProgram(resources); |
109 | 114 |
EffectQueuePostprocess.createProgram(resources); |
src/main/java/org/distorted/library/DistortedEffects.java | ||
---|---|---|
110 | 110 |
final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader); |
111 | 111 |
final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader); |
112 | 112 |
|
113 |
String mainVertHeader= Distorted.glslVersion;
|
|
114 |
String mainFragHeader= Distorted.glslVersion;
|
|
113 |
String mainVertHeader= Distorted.GLSL_VERSION;
|
|
114 |
String mainFragHeader= Distorted.GLSL_VERSION;
|
|
115 | 115 |
|
116 | 116 |
EffectNames name; |
117 | 117 |
EffectTypes type; |
... | ... | |
144 | 144 |
//android.util.Log.e("Effects", "vertHeader= "+mainVertHeader); |
145 | 145 |
//android.util.Log.e("Effects", "fragHeader= "+mainFragHeader); |
146 | 146 |
|
147 |
mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader); |
|
147 |
mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL);
|
|
148 | 148 |
|
149 | 149 |
int mainProgramH = mMainProgram.getProgramHandle(); |
150 | 150 |
EffectQueueFragment.getUniforms(mainProgramH); |
... | ... | |
156 | 156 |
final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader); |
157 | 157 |
final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader); |
158 | 158 |
|
159 |
String blitVertHeader= (Distorted.glslVersion + "#define NUM_VERTEX 0\n" );
|
|
160 |
String blitFragHeader= (Distorted.glslVersion + "#define NUM_FRAGMENT 0\n");
|
|
159 |
String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n" );
|
|
160 |
String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
|
|
161 | 161 |
|
162 | 162 |
try |
163 | 163 |
{ |
164 |
mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader); |
|
164 |
mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
|
|
165 | 165 |
} |
166 | 166 |
catch(Exception e) |
167 | 167 |
{ |
... | ... | |
179 | 179 |
|
180 | 180 |
try |
181 | 181 |
{ |
182 |
mDebugProgram = new DistortedProgram(debugVertexStream,debugFragmentStream, Distorted.glslVersion, Distorted.glslVersion);
|
|
182 |
mDebugProgram = new DistortedProgram(debugVertexStream,debugFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
|
|
183 | 183 |
} |
184 | 184 |
catch(Exception e) |
185 | 185 |
{ |
src/main/java/org/distorted/library/EffectQueuePostprocess.java | ||
---|---|---|
123 | 123 |
try |
124 | 124 |
{ |
125 | 125 |
mBlur1Program = new DistortedProgram(blur1VertexStream,blur1FragmentStream, |
126 |
Distorted.glslVersion,
|
|
127 |
Distorted.glslVersion+"#define MAX_BLUR "+MAX_BLUR);
|
|
126 |
Distorted.GLSL_VERSION,
|
|
127 |
Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_BLUR, Distorted.GLSL);
|
|
128 | 128 |
} |
129 | 129 |
catch(Exception e) |
130 | 130 |
{ |
... | ... | |
145 | 145 |
try |
146 | 146 |
{ |
147 | 147 |
mBlur2Program = new DistortedProgram(blur2VertexStream,blur2FragmentStream, |
148 |
Distorted.glslVersion,
|
|
149 |
Distorted.glslVersion + "#define MAX_BLUR "+MAX_BLUR);
|
|
148 |
Distorted.GLSL_VERSION,
|
|
149 |
Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_BLUR, Distorted.GLSL);
|
|
150 | 150 |
} |
151 | 151 |
catch(Exception e) |
152 | 152 |
{ |
153 | 153 |
android.util.Log.e("EFFECTS", "exception trying to compile BLUR2 program: "+e.getMessage()); |
154 |
throw new RuntimeException(e.getMessage()); |
|
154 |
// run anyway as compiling Blur2 WILL fail on OpenGL 2.0 contexts |
|
155 |
mBlur2Program = mBlur1Program; |
|
155 | 156 |
} |
156 | 157 |
|
157 | 158 |
int blur2ProgramH = mBlur2Program.getProgramHandle(); |
src/main/java/org/distorted/library/program/DistortedProgram.java | ||
---|---|---|
35 | 35 |
*/ |
36 | 36 |
public class DistortedProgram |
37 | 37 |
{ |
38 |
private int mProgramHandle; |
|
38 |
private String mAttributeStr; |
|
39 |
private int mAttributeLen; |
|
39 | 40 |
|
41 |
private int mProgramHandle; |
|
40 | 42 |
private int mNumAttributes; |
41 | 43 |
private String[] mAttributeName; |
42 | 44 |
|
... | ... | |
105 | 107 |
if( currChar==';') break; |
106 | 108 |
} |
107 | 109 |
|
108 |
if( semicolon<len && semicolon-whiteSpace>=4 ) // "in a;" --> 4
|
|
110 |
if( semicolon<len && semicolon-whiteSpace>=mAttributeLen+1 )
|
|
109 | 111 |
{ |
110 | 112 |
String subline = line.substring(whiteSpace,semicolon); |
111 | 113 |
int subLen = semicolon-whiteSpace; |
112 | 114 |
|
113 |
if( subline.startsWith("in "))
|
|
115 |
if( subline.startsWith(mAttributeStr))
|
|
114 | 116 |
{ |
115 | 117 |
//android.util.Log.e("program", "GOOD LINE: " +subline+" subLen="+subLen); |
116 | 118 |
|
117 |
for(nameBegin=subLen-1; nameBegin>1; nameBegin--)
|
|
119 |
for(nameBegin=subLen-1; nameBegin>mAttributeLen-2; nameBegin--)
|
|
118 | 120 |
{ |
119 | 121 |
currChar=subline.charAt(nameBegin); |
120 | 122 |
|
... | ... | |
260 | 262 |
* @throws LinkingException |
261 | 263 |
*/ |
262 | 264 |
|
263 |
public DistortedProgram(final InputStream vertex, final InputStream fragment, final String vertexHeader, final String fragmentHeader) |
|
265 |
public DistortedProgram(final InputStream vertex, final InputStream fragment, final String vertexHeader, final String fragmentHeader, int glslVersion)
|
|
264 | 266 |
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException |
265 | 267 |
{ |
268 |
mAttributeStr = (glslVersion == 100 ? "attribute " : "in "); |
|
269 |
mAttributeLen = mAttributeStr.length(); |
|
270 |
|
|
266 | 271 |
mNumAttributes = 0; |
267 | 272 |
|
268 | 273 |
final String vertexShader = readTextFileFromRawResource(vertex , true ); |
src/main/res/raw/blit_fragment_shader.glsl | ||
---|---|---|
19 | 19 |
|
20 | 20 |
precision lowp float; |
21 | 21 |
|
22 |
in vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment. |
|
22 |
#if __VERSION__ != 100 |
|
23 |
#define TEXTURE texture |
|
23 | 24 |
out vec4 fragColor; // The output color |
25 |
in vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment. |
|
26 |
#else |
|
27 |
#define TEXTURE texture2D |
|
28 |
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment. |
|
29 |
#endif |
|
30 |
|
|
24 | 31 |
uniform sampler2D u_Texture; // The input texture. |
25 | 32 |
|
26 | 33 |
////////////////////////////////////////////////////////////////////////////////////////////// |
27 | 34 |
|
28 | 35 |
void main() |
29 |
{ |
|
30 |
fragColor = texture(u_Texture,v_TexCoordinate); |
|
36 |
{ |
|
37 |
#if __VERSION__ != 100 |
|
38 |
fragColor = |
|
39 |
#else |
|
40 |
gl_FragColor = |
|
41 |
#endif |
|
42 |
|
|
43 |
TEXTURE(u_Texture,v_TexCoordinate); |
|
31 | 44 |
} |
src/main/res/raw/blit_vertex_shader.glsl | ||
---|---|---|
19 | 19 |
|
20 | 20 |
precision lowp float; |
21 | 21 |
|
22 |
uniform float u_Depth; // distance from the near plane to render plane, in clip coords |
|
23 |
in vec2 a_Position; // Per-vertex position. |
|
24 |
out vec2 v_TexCoordinate; // |
|
22 |
#if __VERSION__ != 100 |
|
23 |
in vec2 a_Position; // Per-vertex position. |
|
24 |
out vec2 v_TexCoordinate; // |
|
25 |
#else |
|
26 |
attribute vec2 a_Position; // Per-vertex position. |
|
27 |
varying vec2 v_TexCoordinate; // |
|
28 |
#endif |
|
29 |
|
|
30 |
uniform float u_Depth; // distance from the near plane to render plane, in clip coords |
|
25 | 31 |
|
26 | 32 |
////////////////////////////////////////////////////////////////////////////////////////////// |
27 | 33 |
|
src/main/res/raw/blur1_fragment_shader.glsl | ||
---|---|---|
19 | 19 |
|
20 | 20 |
precision lowp float; |
21 | 21 |
|
22 |
#if __VERSION__ != 100 |
|
23 |
#define TEXTURE texture |
|
22 | 24 |
in vec2 v_TexCoordinate; |
23 | 25 |
out vec4 fragColor; |
26 |
#else |
|
27 |
#define TEXTURE texture2D |
|
28 |
varying vec2 v_TexCoordinate; |
|
29 |
#endif |
|
30 |
|
|
24 | 31 |
uniform sampler2D u_ColorTexture; |
25 | 32 |
uniform float u_Offsets[MAX_BLUR]; |
26 | 33 |
uniform float u_Weights[MAX_BLUR]; |
... | ... | |
30 | 37 |
|
31 | 38 |
void main() |
32 | 39 |
{ |
33 |
vec4 pixel= texture(u_ColorTexture,v_TexCoordinate) * u_Weights[0];
|
|
40 |
vec4 pixel= TEXTURE(u_ColorTexture,v_TexCoordinate) * u_Weights[0];
|
|
34 | 41 |
|
35 | 42 |
for (int i=1; i<=u_Radius; i+=1) |
36 | 43 |
{ |
37 |
pixel += ( texture(u_ColorTexture,vec2(v_TexCoordinate.x+u_Offsets[i],v_TexCoordinate.y)) +
|
|
38 |
texture(u_ColorTexture,vec2(v_TexCoordinate.x-u_Offsets[i],v_TexCoordinate.y)) ) * u_Weights[i];
|
|
44 |
pixel += ( TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x+u_Offsets[i],v_TexCoordinate.y)) +
|
|
45 |
TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x-u_Offsets[i],v_TexCoordinate.y)) ) * u_Weights[i];
|
|
39 | 46 |
} |
40 | 47 |
|
41 |
fragColor = pixel; |
|
48 |
#if __VERSION__ != 100 |
|
49 |
fragColor = |
|
50 |
#else |
|
51 |
gl_FragColor = |
|
52 |
#endif |
|
53 |
|
|
54 |
pixel; |
|
42 | 55 |
} |
src/main/res/raw/blur2_fragment_shader.glsl | ||
---|---|---|
19 | 19 |
|
20 | 20 |
precision lowp float; |
21 | 21 |
|
22 |
#if __VERSION__ != 100 |
|
23 |
#define TEXTURE texture |
|
22 | 24 |
in vec2 v_TexCoordinate; |
23 | 25 |
out vec4 fragColor; |
26 |
#else |
|
27 |
#define TEXTURE texture2D |
|
28 |
varying vec2 v_TexCoordinate; |
|
29 |
#endif |
|
30 |
|
|
24 | 31 |
uniform sampler2D u_ColorTexture; |
25 | 32 |
uniform sampler2D u_DepthTexture; |
26 | 33 |
uniform float u_Offsets[MAX_BLUR]; |
... | ... | |
31 | 38 |
|
32 | 39 |
void main() |
33 | 40 |
{ |
34 |
gl_FragDepth = 0.0;//texture(u_DepthTexture,v_TexCoordinate);
|
|
41 |
gl_FragDepth = TEXTURE(u_DepthTexture,v_TexCoordinate).r;
|
|
35 | 42 |
|
36 |
vec4 pixel= texture(u_ColorTexture,v_TexCoordinate) * u_Weights[0];
|
|
43 |
vec4 pixel= TEXTURE(u_ColorTexture,v_TexCoordinate) * u_Weights[0];
|
|
37 | 44 |
|
38 | 45 |
for (int i=1; i<=u_Radius; i+=1) |
39 | 46 |
{ |
40 |
pixel += ( texture(u_ColorTexture,vec2(v_TexCoordinate.x+u_Offsets[i],v_TexCoordinate.y)) +
|
|
41 |
texture(u_ColorTexture,vec2(v_TexCoordinate.x-u_Offsets[i],v_TexCoordinate.y)) ) * u_Weights[i];
|
|
47 |
pixel += ( TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x+u_Offsets[i],v_TexCoordinate.y)) +
|
|
48 |
TEXTURE(u_ColorTexture,vec2(v_TexCoordinate.x-u_Offsets[i],v_TexCoordinate.y)) ) * u_Weights[i];
|
|
42 | 49 |
} |
43 | 50 |
|
44 |
fragColor = pixel; |
|
51 |
#if __VERSION__ != 100 |
|
52 |
fragColor = |
|
53 |
#else |
|
54 |
gl_FragColor = |
|
55 |
#endif |
|
56 |
|
|
57 |
pixel; |
|
45 | 58 |
} |
src/main/res/raw/blur_vertex_shader.glsl | ||
---|---|---|
19 | 19 |
|
20 | 20 |
precision lowp float; |
21 | 21 |
|
22 |
uniform float u_Depth; // distance from the near plane to render plane, in clip coords
|
|
22 |
#if __VERSION__ != 100
|
|
23 | 23 |
in vec2 a_Position; // Per-vertex position. |
24 | 24 |
in vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in. |
25 | 25 |
out vec2 v_TexCoordinate; // |
26 |
#else |
|
27 |
attribute vec2 a_Position; // Per-vertex position. |
|
28 |
attribute vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in. |
|
29 |
varying vec2 v_TexCoordinate; // |
|
30 |
#endif |
|
31 |
|
|
32 |
uniform float u_Depth; // distance from the near plane to render plane, in clip coords |
|
26 | 33 |
|
27 | 34 |
////////////////////////////////////////////////////////////////////////////////////////////// |
28 | 35 |
|
src/main/res/raw/main_fragment_shader.glsl | ||
---|---|---|
18 | 18 |
////////////////////////////////////////////////////////////////////////////////////////////// |
19 | 19 |
|
20 | 20 |
precision lowp float; |
21 |
|
|
22 |
uniform sampler2D u_Texture; // The input texture.
|
|
23 |
|
|
21 |
|
|
22 |
#if __VERSION__ != 100
|
|
23 |
#define TEXTURE texture |
|
24 | 24 |
in vec3 v_Position; // Interpolated position for this fragment. |
25 | 25 |
in vec3 v_Normal; // Interpolated normal for this fragment. |
26 | 26 |
in vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment. |
27 | 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. |
|
28 | 36 |
|
29 | 37 |
#if NUM_FRAGMENT>0 |
30 | 38 |
uniform int fNumEffects; // total number of fragment effects |
... | ... | |
93 | 101 |
|
94 | 102 |
void main() |
95 | 103 |
{ |
96 |
vec4 pixel = texture(u_Texture,v_TexCoordinate);
|
|
104 |
vec4 pixel = TEXTURE(u_Texture,v_TexCoordinate);
|
|
97 | 105 |
|
98 | 106 |
#if NUM_FRAGMENT>0 |
99 | 107 |
vec2 diff; |
... | ... | |
141 | 149 |
} |
142 | 150 |
#endif |
143 | 151 |
|
144 |
fragColor = vec4(pixel.rgb * (1.0 + 7.0*v_Normal.z) * 0.125, pixel.a); |
|
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); |
|
145 | 159 |
} |
src/main/res/raw/main_vertex_shader.glsl | ||
---|---|---|
19 | 19 |
|
20 | 20 |
precision lowp float; |
21 | 21 |
|
22 |
#if __VERSION__ != 100 |
|
23 |
in vec3 a_Position; // Per-vertex position. |
|
24 |
in vec3 a_Normal; // Per-vertex normal vector. |
|
25 |
in vec2 a_TexCoordinate; // Per-vertex texture coordinate. |
|
26 |
out vec3 v_Position; // |
|
27 |
out vec3 v_Normal; // |
|
28 |
out vec2 v_TexCoordinate; // |
|
29 |
#else |
|
30 |
attribute vec3 a_Position; // Per-vertex position. |
|
31 |
attribute vec3 a_Normal; // Per-vertex normal vector. |
|
32 |
attribute vec2 a_TexCoordinate; // Per-vertex texture coordinate. |
|
33 |
varying vec3 v_Position; // |
|
34 |
varying vec3 v_Normal; // |
|
35 |
varying vec2 v_TexCoordinate; // |
|
36 |
#endif |
|
37 |
|
|
22 | 38 |
uniform vec3 u_objD; // half of object width x half of object height X half the depth; |
23 | 39 |
// point (0,0,0) is the center of the object |
24 | 40 |
|
... | ... | |
26 | 42 |
// I read OpenGL ES has a built-in uniform variable gl_DepthRange.near = n, |
27 | 43 |
// .far = f, .diff = f-n so maybe u_Depth is redundant |
28 | 44 |
// Update: this struct is only available in fragment shaders |
29 |
|
|
45 |
|
|
30 | 46 |
uniform mat4 u_MVPMatrix; // the combined model/view/projection matrix. |
31 | 47 |
uniform mat4 u_MVMatrix; // the combined model/view matrix. |
32 |
|
|
33 |
in vec3 a_Position; // Per-vertex position. |
|
34 |
in vec3 a_Normal; // Per-vertex normal vector. |
|
35 |
in vec2 a_TexCoordinate; // Per-vertex texture coordinate. |
|
36 |
|
|
37 |
out vec3 v_Position; // |
|
38 |
out vec3 v_Normal; // |
|
39 |
out vec2 v_TexCoordinate; // |
|
40 | 48 |
|
41 | 49 |
#if NUM_VERTEX>0 |
42 | 50 |
uniform int vNumEffects; // total number of vertex effects |
src/main/res/raw/test_fragment_shader.glsl | ||
---|---|---|
19 | 19 |
|
20 | 20 |
precision lowp float; |
21 | 21 |
|
22 |
#if __VERSION__ != 100 |
|
22 | 23 |
out vec4 fragColor; |
24 |
#endif |
|
23 | 25 |
|
24 | 26 |
////////////////////////////////////////////////////////////////////////////////////////////// |
25 | 27 |
|
26 | 28 |
void main() |
27 | 29 |
{ |
28 |
fragColor = vec4(1.0,0.0,0.0,0.2); |
|
30 |
#if __VERSION__ != 100 |
|
31 |
fragColor = |
|
32 |
#else |
|
33 |
gl_FragColor = |
|
34 |
#endif |
|
35 |
|
|
36 |
vec4(1.0,0.0,0.0,0.2); |
|
29 | 37 |
} |
src/main/res/raw/test_vertex_shader.glsl | ||
---|---|---|
19 | 19 |
|
20 | 20 |
precision lowp float; |
21 | 21 |
|
22 |
#if __VERSION__ != 100 |
|
23 |
in vec2 a_Position; // Per-vertex position information we will pass in. |
|
24 |
#else |
|
25 |
attribute vec2 a_Position; // Per-vertex position information we will pass in. |
|
26 |
#endif |
|
27 |
|
|
22 | 28 |
uniform vec2 u_objD; // object width X object height. |
23 | 29 |
uniform mat4 u_MVPMatrix; // the combined model/view/projection matrix. |
24 |
in vec2 a_Position; // Per-vertex position information we will pass in. |
|
25 | 30 |
|
26 | 31 |
////////////////////////////////////////////////////////////////////////////////////////////// |
27 | 32 |
|
Also available in: Unified diff
Make it more flexible; now it can run almost all apps on OpenGL 2.0 contexts; OpenGL 3.0 ( with GLSL 3.00) required for POSTPROCESSING.