Revision 1149be8f
Added by Leszek Koltunski over 8 years ago
| src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java | ||
|---|---|---|
| 30 | 30 |
|
| 31 | 31 |
public class PostprocessEffectBlur extends PostprocessEffect |
| 32 | 32 |
{
|
| 33 |
private static final int MAX_HALO = 50; // Support effects creating up to MAX_HALO pixels wide 'halo' around the object.
|
|
| 33 |
private static final int MAX_HALO = 50; |
|
| 34 | 34 |
|
| 35 | 35 |
private Data1D mBlurRadius; |
| 36 | 36 |
|
| ... | ... | |
| 59 | 59 |
private static float[] mWeights = new float[MAX_HALO]; |
| 60 | 60 |
private static float[] mOffsets = new float[MAX_HALO]; |
| 61 | 61 |
|
| 62 |
private static DistortedProgram mBlur1Program, mBlur2Program;
|
|
| 63 |
private static int mBlur1Index, mBlur2Index;
|
|
| 62 |
private static DistortedProgram mProgram1, mProgram2;
|
|
| 63 |
private static int mIndex1, mIndex2;
|
|
| 64 | 64 |
|
| 65 | 65 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 66 | 66 |
/** |
| ... | ... | |
| 86 | 86 |
|
| 87 | 87 |
public int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer) |
| 88 | 88 |
{
|
| 89 |
if( mBlur1Program==null)
|
|
| 89 |
if( mProgram1 ==null)
|
|
| 90 | 90 |
{
|
| 91 |
mBlur1Program = mPrograms.get(mBlur1Index);
|
|
| 92 |
mBlur2Program = mPrograms.get(mBlur2Index);
|
|
| 91 |
mProgram1 = mPrograms.get(mIndex1);
|
|
| 92 |
mProgram2 = mPrograms.get(mIndex2);
|
|
| 93 | 93 |
} |
| 94 | 94 |
|
| 95 | 95 |
buffer.setAsOutput(); |
| ... | ... | |
| 104 | 104 |
|
| 105 | 105 |
int offset = radius + radius*radius/4; |
| 106 | 106 |
radius = (radius+1)/2; |
| 107 |
GLES30.glViewport(0, 0, (int)w1, (int)h1); |
|
| 107 | 108 |
|
| 108 | 109 |
// horizontal blur |
| 109 |
GLES30.glViewport(0, 0, (int)w1, (int)h1); |
|
| 110 |
mBlur1Program.useProgram(); |
|
| 110 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h1; |
|
| 111 |
|
|
| 112 |
mProgram1.useProgram(); |
|
| 111 | 113 |
buffer.bindForOutput(1); |
| 112 | 114 |
buffer.setAsInput(0); |
| 113 | 115 |
|
| 114 |
GLES30.glUniform1fv( mBlur1Program.mUniform[3], radius+1, weightsCache,offset);
|
|
| 115 |
GLES30.glUniform1i( mBlur1Program.mUniform[4], radius);
|
|
| 116 |
GLES30.glUniform1f( mBlur1Program.mUniform[0] , near);
|
|
| 117 |
GLES30.glUniform1i( mBlur1Program.mUniform[1] , 0 );
|
|
| 118 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h1;
|
|
| 119 |
GLES30.glUniform1fv( mBlur1Program.mUniform[2] ,radius+1, mOffsets,0); |
|
| 120 |
GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
|
| 121 |
GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
|
|
| 116 |
GLES30.glUniform1f ( mProgram1.mUniform[0] , near);
|
|
| 117 |
GLES30.glUniform1i ( mProgram1.mUniform[1] , 0 );
|
|
| 118 |
GLES30.glUniform1fv( mProgram1.mUniform[2] , radius+1, mOffsets,0);
|
|
| 119 |
GLES30.glUniform1fv( mProgram1.mUniform[3] , radius+1, weightsCache,offset);
|
|
| 120 |
GLES30.glUniform1i ( mProgram1.mUniform[4] , radius);
|
|
| 121 |
|
|
| 122 |
GLES30.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
|
| 123 |
GLES30.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
|
|
| 122 | 124 |
|
| 123 | 125 |
DistortedRenderState.useStencilMark(); |
| 124 | 126 |
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4); |
| ... | ... | |
| 127 | 129 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0); |
| 128 | 130 |
|
| 129 | 131 |
// vertical blur |
| 130 |
mBlur2Program.useProgram(); |
|
| 132 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1; |
|
| 133 |
|
|
| 134 |
mProgram2.useProgram(); |
|
| 131 | 135 |
buffer.bindForOutput(0); |
| 132 | 136 |
buffer.setAsInput(1); |
| 133 | 137 |
|
| ... | ... | |
| 135 | 139 |
GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f); |
| 136 | 140 |
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT); |
| 137 | 141 |
|
| 138 |
GLES30.glUniform1fv( mBlur2Program.mUniform[3], radius+1, weightsCache,offset);
|
|
| 139 |
GLES30.glUniform1i( mBlur2Program.mUniform[4], radius);
|
|
| 140 |
GLES30.glUniform1f( mBlur2Program.mUniform[0] , near);
|
|
| 141 |
GLES30.glUniform1i( mBlur2Program.mUniform[1] , 0 );
|
|
| 142 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1;
|
|
| 143 |
GLES30.glUniform1fv( mBlur2Program.mUniform[2] ,radius+1, mOffsets,0); |
|
| 144 |
GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
|
| 145 |
GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
|
|
| 142 |
GLES30.glUniform1f ( mProgram2.mUniform[0] , near);
|
|
| 143 |
GLES30.glUniform1i ( mProgram2.mUniform[1] , 0 );
|
|
| 144 |
GLES30.glUniform1fv( mProgram2.mUniform[2] , radius+1, mOffsets,0);
|
|
| 145 |
GLES30.glUniform1fv( mProgram2.mUniform[3] , radius+1, weightsCache,offset);
|
|
| 146 |
GLES30.glUniform1i ( mProgram2.mUniform[4] , radius);
|
|
| 147 |
|
|
| 148 |
GLES30.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
|
| 149 |
GLES30.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
|
|
| 146 | 150 |
|
| 147 | 151 |
DistortedRenderState.useStencilMark(); |
| 148 | 152 |
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4); |
| ... | ... | |
| 218 | 222 |
" fragColor = pixel; \n"+ |
| 219 | 223 |
" }"; |
| 220 | 224 |
|
| 221 |
mBlur1Index = PostprocessEffect.register("BLUR1", blurVertex,blurFragment1);
|
|
| 222 |
mBlur2Index = PostprocessEffect.register("BLUR2", blurVertex,blurFragment2);
|
|
| 225 |
mIndex1 = PostprocessEffect.register("BLUR1", blurVertex,blurFragment1);
|
|
| 226 |
mIndex2 = PostprocessEffect.register("BLUR2", blurVertex,blurFragment2);
|
|
| 223 | 227 |
} |
| 224 | 228 |
|
| 225 | 229 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java | ||
|---|---|---|
| 19 | 19 |
|
| 20 | 20 |
package org.distorted.library.effect; |
| 21 | 21 |
|
| 22 |
import android.opengl.GLES30; |
|
| 23 |
|
|
| 22 | 24 |
import org.distorted.library.main.DistortedFramebuffer; |
| 25 |
import org.distorted.library.main.DistortedRenderState; |
|
| 26 |
import org.distorted.library.program.DistortedProgram; |
|
| 23 | 27 |
import org.distorted.library.type.Data1D; |
| 24 | 28 |
import org.distorted.library.type.Data4D; |
| 25 | 29 |
|
| ... | ... | |
| 27 | 31 |
|
| 28 | 32 |
public class PostprocessEffectGlow extends PostprocessEffect |
| 29 | 33 |
{
|
| 34 |
private static final int MAX_HALO = 50; |
|
| 35 |
|
|
| 30 | 36 |
private Data1D mGlowRadius; |
| 31 | 37 |
private Data4D mColor; |
| 32 | 38 |
|
| 33 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 39 |
private static final float GAUSSIAN[] = // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0 |
|
| 40 |
{ // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
|
|
| 41 |
0.398948f, 0.398769f, 0.398231f, 0.397336f, 0.396086f, 0.394485f, 0.392537f, 0.390247f, 0.387622f, 0.384668f, |
|
| 42 |
0.381393f, 0.377806f, 0.373916f, 0.369733f, 0.365268f, 0.360532f, 0.355538f, 0.350297f, 0.344823f, 0.339129f, |
|
| 43 |
0.333229f, 0.327138f, 0.320868f, 0.314436f, 0.307856f, 0.301142f, 0.294309f, 0.287373f, 0.280348f, 0.273248f, |
|
| 44 |
0.266089f, 0.258884f, 0.251648f, 0.244394f, 0.237135f, 0.229886f, 0.222657f, 0.215461f, 0.208311f, 0.201217f, |
|
| 45 |
0.194189f, 0.187238f, 0.180374f, 0.173605f, 0.166940f, 0.160386f, 0.153951f, 0.147641f, 0.141462f, 0.135420f, |
|
| 46 |
0.129520f, 0.123765f, 0.118159f, 0.112706f, 0.107408f, 0.102266f, 0.097284f, 0.092461f, 0.087797f, 0.083294f, |
|
| 47 |
0.078951f, 0.074767f, 0.070741f, 0.066872f, 0.063158f, 0.059596f, 0.056184f, 0.052920f, 0.049801f, 0.046823f, |
|
| 48 |
0.043984f, 0.041280f, 0.038707f, 0.036262f, 0.033941f, 0.031740f, 0.029655f, 0.027682f, 0.025817f, 0.024056f, |
|
| 49 |
0.022395f, 0.020830f, 0.019357f, 0.017971f, 0.016670f, 0.015450f, 0.014305f, 0.013234f, 0.012232f, 0.011295f, |
|
| 50 |
0.010421f, 0.009606f, 0.008847f, 0.008140f, 0.007483f, 0.006873f, 0.006307f, 0.005782f, 0.005296f, 0.004847f, |
|
| 51 |
0.004432f, 0.000000f |
|
| 52 |
}; |
|
| 53 |
private static final int NUM_GAUSSIAN = GAUSSIAN.length-2; |
|
| 34 | 54 |
|
| 35 |
public static void enable() |
|
| 36 |
{
|
|
| 55 |
// The (fixed-function-sampled) Gaussian Blur kernels are of the size k0=1, k1=2, k2=2, k3=3, k4=3, k5=4, k6=4,... |
|
| 56 |
// i.e. k(i)=floor((i+3)/2). (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels |
|
| 57 |
// in all 4 directions) |
|
| 58 |
// We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4) |
|
| 59 |
private static float[] weightsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4]; |
|
| 60 |
private static float[] offsetsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4]; |
|
| 61 |
private static float[] mWeights = new float[MAX_HALO]; |
|
| 62 |
private static float[] mOffsets = new float[MAX_HALO]; |
|
| 37 | 63 |
|
| 38 |
} |
|
| 64 |
private static DistortedProgram mProgram1, mProgram2; |
|
| 65 |
private static int mIndex1, mIndex2; |
|
| 39 | 66 |
|
| 40 | 67 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 68 |
|
|
| 41 | 69 |
/** |
| 42 | 70 |
* Make the object glow with a specific color and a halo of specific radius. |
| 43 | 71 |
* |
| ... | ... | |
| 64 | 92 |
|
| 65 | 93 |
public int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer) |
| 66 | 94 |
{
|
| 67 |
return 0; |
|
| 95 |
if( mProgram1 ==null) |
|
| 96 |
{
|
|
| 97 |
mProgram1 = mPrograms.get(mIndex1); |
|
| 98 |
mProgram2 = mPrograms.get(mIndex2); |
|
| 99 |
} |
|
| 100 |
|
|
| 101 |
buffer.setAsOutput(); |
|
| 102 |
|
|
| 103 |
float w1 = buffer.getWidth(); |
|
| 104 |
float h1 = buffer.getHeight(); |
|
| 105 |
float near= 1.0f - buffer.getNear(); |
|
| 106 |
|
|
| 107 |
int radius = (int)(uniforms[index]*qualityScale); |
|
| 108 |
if( radius>=MAX_HALO ) radius = MAX_HALO-1; |
|
| 109 |
computeGaussianKernel(radius); |
|
| 110 |
|
|
| 111 |
int offset = radius + radius*radius/4; |
|
| 112 |
radius = (radius+1)/2; |
|
| 113 |
GLES30.glViewport(0, 0, (int)w1, (int)h1); |
|
| 114 |
|
|
| 115 |
// horizontal blur |
|
| 116 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h1; |
|
| 117 |
|
|
| 118 |
mProgram1.useProgram(); |
|
| 119 |
buffer.bindForOutput(1); |
|
| 120 |
buffer.setAsInput(0); |
|
| 121 |
|
|
| 122 |
GLES30.glUniform1f ( mProgram1.mUniform[0] , near); |
|
| 123 |
GLES30.glUniform1i ( mProgram1.mUniform[1] , 0 ); |
|
| 124 |
GLES30.glUniform1fv( mProgram1.mUniform[2] , radius+1, mOffsets,0); |
|
| 125 |
GLES30.glUniform1fv( mProgram1.mUniform[3] , radius+1, weightsCache,offset); |
|
| 126 |
GLES30.glUniform1i ( mProgram1.mUniform[4] , radius); |
|
| 127 |
|
|
| 128 |
GLES30.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions); |
|
| 129 |
GLES30.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture); |
|
| 130 |
|
|
| 131 |
DistortedRenderState.useStencilMark(); |
|
| 132 |
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4); |
|
| 133 |
DistortedRenderState.unuseStencilMark(); |
|
| 134 |
GLES30.glActiveTexture(GLES30.GL_TEXTURE0); |
|
| 135 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0); |
|
| 136 |
|
|
| 137 |
// vertical blur |
|
| 138 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1; |
|
| 139 |
|
|
| 140 |
mProgram2.useProgram(); |
|
| 141 |
buffer.bindForOutput(0); |
|
| 142 |
buffer.setAsInput(1); |
|
| 143 |
|
|
| 144 |
GLES30.glColorMask(true,true,true,true); |
|
| 145 |
GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f); |
|
| 146 |
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT); |
|
| 147 |
|
|
| 148 |
GLES30.glUniform1f ( mProgram2.mUniform[0] , near); |
|
| 149 |
GLES30.glUniform1i ( mProgram2.mUniform[1] , 0 ); |
|
| 150 |
GLES30.glUniform1fv( mProgram2.mUniform[2] , radius+1, mOffsets,0); |
|
| 151 |
GLES30.glUniform1fv( mProgram2.mUniform[3] , radius+1, weightsCache,offset); |
|
| 152 |
GLES30.glUniform1i ( mProgram2.mUniform[4] , radius); |
|
| 153 |
|
|
| 154 |
GLES30.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions); |
|
| 155 |
GLES30.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture); |
|
| 156 |
|
|
| 157 |
DistortedRenderState.useStencilMark(); |
|
| 158 |
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4); |
|
| 159 |
DistortedRenderState.unuseStencilMark(); |
|
| 160 |
GLES30.glActiveTexture(GLES30.GL_TEXTURE0); |
|
| 161 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0); |
|
| 162 |
|
|
| 163 |
return 2; |
|
| 164 |
} |
|
| 165 |
|
|
| 166 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 167 |
|
|
| 168 |
public static void enable() |
|
| 169 |
{
|
|
| 170 |
final String glowVertex = |
|
| 171 |
|
|
| 172 |
"#version 300 es \n"+ |
|
| 173 |
"precision lowp float; \n"+ |
|
| 174 |
"in vec2 a_Position; \n"+ |
|
| 175 |
"in vec2 a_TexCoord; \n"+ |
|
| 176 |
"out vec2 v_TexCoord; \n"+ |
|
| 177 |
"uniform float u_Depth; \n"+ |
|
| 178 |
|
|
| 179 |
"void main() \n"+ |
|
| 180 |
" { \n"+
|
|
| 181 |
" v_TexCoord = a_TexCoord; \n"+ |
|
| 182 |
" gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+ |
|
| 183 |
" }"; |
|
| 184 |
|
|
| 185 |
final String glowFragment1 = |
|
| 186 |
|
|
| 187 |
"#version 300 es \n"+ |
|
| 188 |
"#define MAX_BLUR "+MAX_HALO+ "\n"+ |
|
| 189 |
"precision lowp float; \n"+ |
|
| 190 |
"in vec2 v_TexCoord; \n"+ |
|
| 191 |
"out vec4 fragColor; \n"+ |
|
| 192 |
"uniform sampler2D u_ColorTexture; \n"+ |
|
| 193 |
"uniform float u_Offsets[MAX_BLUR]; \n"+ |
|
| 194 |
"uniform float u_Weights[MAX_BLUR]; \n"+ |
|
| 195 |
"uniform int u_Radius; \n"+ |
|
| 196 |
|
|
| 197 |
"void main() \n"+ |
|
| 198 |
" { \n"+
|
|
| 199 |
" vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0]; \n"+ |
|
| 200 |
" for (int i=1; i<=u_Radius; i+=1) \n"+ |
|
| 201 |
" { \n"+
|
|
| 202 |
" pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) + \n"+ |
|
| 203 |
" texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+ |
|
| 204 |
" } \n"+ |
|
| 205 |
" fragColor = pixel; \n"+ |
|
| 206 |
" }"; |
|
| 207 |
|
|
| 208 |
final String glowFragment2 = |
|
| 209 |
|
|
| 210 |
"#version 300 es \n"+ |
|
| 211 |
"#define MAX_BLUR "+MAX_HALO+ "\n"+ |
|
| 212 |
"precision lowp float; \n"+ |
|
| 213 |
"in vec2 v_TexCoord; \n"+ |
|
| 214 |
"out vec4 fragColor; \n"+ |
|
| 215 |
"uniform sampler2D u_ColorTexture; \n"+ |
|
| 216 |
"uniform float u_Offsets[MAX_BLUR]; \n"+ |
|
| 217 |
"uniform float u_Weights[MAX_BLUR]; \n"+ |
|
| 218 |
"uniform int u_Radius; \n"+ |
|
| 219 |
|
|
| 220 |
"void main() \n"+ |
|
| 221 |
" { \n"+
|
|
| 222 |
" vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0]; \n"+ |
|
| 223 |
" for (int i=1; i<=u_Radius; i+=1) \n"+ |
|
| 224 |
" { \n"+
|
|
| 225 |
" pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) + \n"+ |
|
| 226 |
" texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+ |
|
| 227 |
" } \n"+ |
|
| 228 |
" fragColor = pixel; \n"+ |
|
| 229 |
" }"; |
|
| 230 |
|
|
| 231 |
mIndex1 = PostprocessEffect.register("BLUR1", glowVertex,glowFragment1);
|
|
| 232 |
mIndex2 = PostprocessEffect.register("BLUR2", glowVertex,glowFragment2);
|
|
| 233 |
} |
|
| 234 |
|
|
| 235 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
| 236 |
// This implements the 'Better separable implementation using GPU fixed function sampling' from |
|
| 237 |
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms |
|
| 238 |
|
|
| 239 |
private void computeGaussianKernel(int radius) |
|
| 240 |
{
|
|
| 241 |
int offset = radius + radius*radius/4; |
|
| 242 |
|
|
| 243 |
if( weightsCache[offset]==0.0f ) |
|
| 244 |
{
|
|
| 245 |
float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3); |
|
| 246 |
mWeights[0] = GAUSSIAN[0]; |
|
| 247 |
float sum = GAUSSIAN[0]; |
|
| 248 |
int j; |
|
| 249 |
|
|
| 250 |
for(int i=1; i<=radius; i++) |
|
| 251 |
{
|
|
| 252 |
x += P; |
|
| 253 |
j = (int)x; |
|
| 254 |
z = x-j; |
|
| 255 |
|
|
| 256 |
mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1]; |
|
| 257 |
sum += 2*mWeights[i]; |
|
| 258 |
} |
|
| 259 |
|
|
| 260 |
for(int i=0; i<=radius; i++) mWeights[i] /= sum; |
|
| 261 |
|
|
| 262 |
int numloops = radius/2; |
|
| 263 |
weightsCache[offset] = mWeights[0]; |
|
| 264 |
offsetsCache[offset] = 0.0f; |
|
| 265 |
|
|
| 266 |
for(int i=0; i<numloops; i++) |
|
| 267 |
{
|
|
| 268 |
offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2); |
|
| 269 |
weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2]; |
|
| 270 |
offsetsCache[offset+i+1]/= weightsCache[offset+i+1]; |
|
| 271 |
} |
|
| 272 |
|
|
| 273 |
if( radius%2 == 1 ) |
|
| 274 |
{
|
|
| 275 |
int index = offset + radius/2 +1; |
|
| 276 |
offsetsCache[index]=radius; |
|
| 277 |
weightsCache[index]=mWeights[radius]; |
|
| 278 |
} |
|
| 279 |
} |
|
| 68 | 280 |
} |
| 69 | 281 |
} |
| src/main/java/org/distorted/library/main/DistortedEffects.java | ||
|---|---|---|
| 247 | 247 |
|
| 248 | 248 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 249 | 249 |
|
| 250 |
int postprocess(long time, DistortedOutputSurface surface)
|
|
| 250 |
int postprocess(DistortedOutputSurface surface) |
|
| 251 | 251 |
{
|
| 252 |
return mP.postprocess(time,surface);
|
|
| 252 |
return mP.postprocess(surface); |
|
| 253 | 253 |
} |
| 254 | 254 |
|
| 255 | 255 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| ... | ... | |
| 341 | 341 |
mM.compute(currTime); |
| 342 | 342 |
mV.compute(currTime,halfW,halfH,halfZ); |
| 343 | 343 |
mF.compute(currTime,halfW,halfH); |
| 344 |
mP.compute(currTime); |
|
| 344 | 345 |
|
| 345 | 346 |
GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
| 346 | 347 |
|
| src/main/java/org/distorted/library/main/DistortedOutputSurface.java | ||
|---|---|---|
| 278 | 278 |
numRenders += child2.markStencilAndDepth(time,mBuffer[quality],lastEffects); |
| 279 | 279 |
} |
| 280 | 280 |
|
| 281 |
numRenders += lastEffects.postprocess(time, this);
|
|
| 281 |
numRenders += lastEffects.postprocess(this); |
|
| 282 | 282 |
numRenders += blitWithDepth(time, mBuffer[quality]); |
| 283 | 283 |
} |
| 284 | 284 |
|
| ... | ... | |
| 296 | 296 |
numRenders += child2.markStencilAndDepth(time,mBuffer[quality],currEffects); |
| 297 | 297 |
} |
| 298 | 298 |
|
| 299 |
numRenders += currEffects.postprocess(time,this);
|
|
| 299 |
numRenders += currEffects.postprocess(this); |
|
| 300 | 300 |
numRenders += blitWithDepth(time,mBuffer[quality]); |
| 301 | 301 |
} |
| 302 | 302 |
} |
| src/main/java/org/distorted/library/main/EffectQueue.java | ||
|---|---|---|
| 103 | 103 |
|
| 104 | 104 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 105 | 105 |
|
| 106 |
private void regenerateID()
|
|
| 106 |
void regenerateIDandSort()
|
|
| 107 | 107 |
{
|
| 108 | 108 |
if( mNumEffects>0 ) |
| 109 | 109 |
{
|
| ... | ... | |
| 125 | 125 |
{
|
| 126 | 126 |
mID = 0; |
| 127 | 127 |
} |
| 128 |
|
|
| 129 |
int numNodes = (mNodes==null ? 0: mNodes.size()); |
|
| 130 |
for(int i=0; i<numNodes; i++) mNodes.get(i).sort(); |
|
| 131 |
|
|
| 128 | 132 |
/* |
| 129 | 133 |
if( mIndex == EffectType.MATRIX.ordinal() ) |
| 130 | 134 |
android.util.Log.d("queue", "queueM id="+mID);
|
| ... | ... | |
| 386 | 390 |
} |
| 387 | 391 |
} |
| 388 | 392 |
|
| 389 |
if( num>0 && mIndex==EffectType.POSTPROCESS.ordinal() ) |
|
| 390 |
{
|
|
| 391 |
regenerateID(); |
|
| 392 |
int numNodes = (mNodes==null ? 0: mNodes.size()); |
|
| 393 |
for(int i=0; i<numNodes; i++) mNodes.get(i).sort(); |
|
| 394 |
} |
|
| 393 |
if( num>0 && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateIDandSort(); |
|
| 395 | 394 |
} |
| 396 | 395 |
} |
| src/main/java/org/distorted/library/main/EffectQueuePostprocess.java | ||
|---|---|---|
| 46 | 46 |
|
| 47 | 47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 48 | 48 |
|
| 49 |
private void compute(long currTime)
|
|
| 49 |
void compute(long currTime) |
|
| 50 | 50 |
{
|
| 51 | 51 |
if( currTime==mTime ) return; |
| 52 | 52 |
if( mTime==0 ) mTime = currTime; |
| ... | ... | |
| 68 | 68 |
{
|
| 69 | 69 |
remove(i--); |
| 70 | 70 |
mNumEffectsToBe--; |
| 71 |
regenerateIDandSort(); |
|
| 71 | 72 |
continue; |
| 72 | 73 |
} |
| 73 | 74 |
} |
| ... | ... | |
| 88 | 89 |
|
| 89 | 90 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 90 | 91 |
|
| 91 |
int postprocess(long time, DistortedOutputSurface surface)
|
|
| 92 |
int postprocess(DistortedOutputSurface surface) |
|
| 92 | 93 |
{
|
| 93 | 94 |
int numRenders = 0; |
| 94 | 95 |
|
| 95 |
if( mNumEffects>0 )
|
|
| 96 |
for(int i=0; i<mNumEffects; i++)
|
|
| 96 | 97 |
{
|
| 97 |
compute(time); |
|
| 98 |
|
|
| 99 |
for(int i=0; i<mNumEffects; i++) |
|
| 100 |
{
|
|
| 101 |
numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i,mQualityScale,surface.mBuffer[mQualityLevel]); |
|
| 102 |
} |
|
| 98 |
numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i,mQualityScale,surface.mBuffer[mQualityLevel]); |
|
| 103 | 99 |
} |
| 104 | 100 |
|
| 105 | 101 |
return numRenders; |
Also available in: Unified diff
Fixes for dynamic postprocessing.