Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ b0083951

1 58059374 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 58059374 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 58059374 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 58059374 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 58059374 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.multiblur;
21
22 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 58059374 Leszek Koltunski
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29
30
import org.distorted.examples.R;
31 d38672b1 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.PostprocessEffectBlur;
35 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedNode;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40 8843b69b leszek
import org.distorted.library.effect.EffectQuality;
41 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
42 678c391d Leszek Koltunski
import org.distorted.library.type.Static2D;
43 58059374 Leszek Koltunski
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45
46
import java.io.IOException;
47
import java.io.InputStream;
48
49
import javax.microedition.khronos.egl.EGLConfig;
50
import javax.microedition.khronos.opengles.GL10;
51
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 dc10a48d Leszek Koltunski
class MultiblurRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
55 58059374 Leszek Koltunski
{
56
    private static final int[] MOVE_VEC =
57
        {
58
        +1,+1,+1,
59
        +1,-1,+1,
60
        +1,+1,-1,
61
        +1,-1,-1,
62
        -1,+1,+1,
63
        -1,-1,+1,
64
        -1,+1,-1,
65
        -1,-1,-1
66
        };
67
68
    private static final int NUM_OBJECTS = MOVE_VEC.length/3;
69
70 dc10a48d Leszek Koltunski
    private final GLSurfaceView mView;
71
    private final Resources mResources;
72
    private final DistortedTexture mTex1, mTex2;
73
    private final DistortedNode[] mNode;
74
    private final Static3D[]  mMoveVector;
75
    private final Static2D mBlurHaloRadius;
76
    private final DistortedScreen mScreen;
77
    private final PostprocessEffectBlur mBlur;
78
    private final boolean[] mBlurStatus;
79
    private final Static3D mScale;
80
81 3e673c74 Leszek Koltunski
    private int mDistance;
82 58059374 Leszek Koltunski
83
    Static4D mQuat1, mQuat2;
84
    int mScreenMin;
85 28fe91ae leszek
86 58059374 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88
    MultiblurRenderer(GLSurfaceView v)
89
      {
90
      mView = v;
91 dc10a48d Leszek Koltunski
      mResources = v.getResources();
92 3e673c74 Leszek Koltunski
      mDistance = -1;
93 58059374 Leszek Koltunski
94 46ab4363 Leszek Koltunski
      mBlurStatus = new boolean[NUM_OBJECTS];
95 58059374 Leszek Koltunski
      mMoveVector = new Static3D[NUM_OBJECTS];
96 895d2f4f leszek
      mNode       = new DistortedNode[NUM_OBJECTS];
97 58059374 Leszek Koltunski
98 d38672b1 Leszek Koltunski
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
99
100 3e673c74 Leszek Koltunski
      for(int i=0; i<NUM_OBJECTS; i++)
101
        {
102 d38672b1 Leszek Koltunski
        mMoveVector[i] = new Static3D(0,0,0);
103 698ad0a8 Leszek Koltunski
        effects[i]     = new DistortedEffects();
104 46ab4363 Leszek Koltunski
        mBlurStatus[i] = false;
105 3e673c74 Leszek Koltunski
        }
106 58059374 Leszek Koltunski
107 678c391d Leszek Koltunski
      mBlurHaloRadius = new Static2D(10,2);
108 9bea2f59 Leszek Koltunski
109 58e9e190 leszek
      MeshCubes mesh = new MeshCubes(1,1,1);
110 58059374 Leszek Koltunski
111 687263cc Leszek Koltunski
      mTex1 = new DistortedTexture();
112
      mTex2 = new DistortedTexture();
113 58059374 Leszek Koltunski
114
      mQuat1 = new Static4D(0,0,0,1);  // unity
115
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
116
117 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
118 e72c53e5 Leszek Koltunski
      mScreen.showFPS();
119 fe59d375 Leszek Koltunski
120
      for(int i=0; i<NUM_OBJECTS; i++)
121 895d2f4f leszek
        {
122 d38672b1 Leszek Koltunski
        mNode[i] = new DistortedNode(i < NUM_OBJECTS / 2 ? mTex1 : mTex2, effects[i], mesh);
123 895d2f4f leszek
        mScreen.attach(mNode[i]);
124
        }
125
126 678c391d Leszek Koltunski
      mBlur = new PostprocessEffectBlur(mBlurHaloRadius);
127 895d2f4f leszek
      mBlurStatus[0] = true;
128 66d31749 Leszek Koltunski
      effects[0].apply(mBlur);
129 d38672b1 Leszek Koltunski
130
      mScale  = new Static3D(1,1,1);
131 16b22aab Leszek Koltunski
      Static3D center = new Static3D(0,0,0);
132 d38672b1 Leszek Koltunski
133
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
134 16b22aab Leszek Koltunski
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
135
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
136 d38672b1 Leszek Koltunski
137
      for(int i=0; i<NUM_OBJECTS; i++)
138
        {
139
        effects[i].apply(new MatrixEffectMove(mMoveVector[i]));
140 0ba5de00 Leszek Koltunski
        effects[i].apply(quatEffect2);
141
        effects[i].apply(quatEffect1);
142
        effects[i].apply(scaleEffect);
143 d38672b1 Leszek Koltunski
        }
144 58059374 Leszek Koltunski
      }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147 bf0bc90f Leszek Koltunski
148
    void setQuality(EffectQuality quality)
149
      {
150 6155c308 Leszek Koltunski
      mBlur.setQuality(quality);
151 bf0bc90f Leszek Koltunski
      }
152
153 a935e9db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155
    void setRenderModeToOIT(boolean oit)
156
      {
157
      mScreen.setOrderIndependentTransparency(oit);
158
      }
159
160 bf0bc90f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
161
162 58059374 Leszek Koltunski
    public void onDrawFrame(GL10 glUnused) 
163
      {
164 24ab1cf8 leszek
      mScreen.render(System.currentTimeMillis());
165 58059374 Leszek Koltunski
      }
166
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
    
169
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
170
      {
171 678c391d Leszek Koltunski
      mScreenMin  = Math.min(width, height);
172 c50a38f9 Leszek Koltunski
    	float factor= 0.15f*mScreenMin;
173 d38672b1 Leszek Koltunski
    	mScale.set(factor,factor,factor);
174 3e673c74 Leszek Koltunski
      computeMoveVectors();
175 58059374 Leszek Koltunski
      mScreen.resize(width, height);
176
      }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
    
180
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
181
      {
182
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
183
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
184
      Bitmap bitmap1,bitmap2;
185
        
186
      try 
187
        {
188
        bitmap1 = BitmapFactory.decodeStream(is1);
189
        bitmap2 = BitmapFactory.decodeStream(is2);
190
        }
191
      finally 
192
        {
193
        try 
194
          {
195
          is1.close();
196
          is2.close();
197
          }
198 dc10a48d Leszek Koltunski
        catch(IOException ignored) { }
199 58059374 Leszek Koltunski
        }  
200
201
      mTex1.setTexture(bitmap1);
202
      mTex2.setTexture(bitmap2);
203
204 885b9cca leszek
      PostprocessEffectBlur.enable();
205 6637d0f2 Leszek Koltunski
206 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
207 58059374 Leszek Koltunski
      }
208 3e673c74 Leszek Koltunski
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
211
    private void computeMoveVectors()
212
      {
213 c50a38f9 Leszek Koltunski
      float size= 0.026f*mDistance;
214 3e673c74 Leszek Koltunski
215
      for(int i=0; i<NUM_OBJECTS; i++)
216
        {
217 8c1caf83 leszek
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
218 3e673c74 Leszek Koltunski
        }
219
      }
220
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
223
    void setDistance(int distance)
224
      {
225
      mDistance = distance;
226
      computeMoveVectors();
227 46ab4363 Leszek Koltunski
      }
228
229 fc6f1299 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
    void setRange(int range)
232
      {
233 678c391d Leszek Koltunski
      mBlurHaloRadius.set(range*0.2f,range*0.5f);
234 fc6f1299 Leszek Koltunski
      }
235 febb61ba Leszek Koltunski
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238 66d31749 Leszek Koltunski
    boolean[] getChecked()
239 46ab4363 Leszek Koltunski
     {
240
     return mBlurStatus;
241
     }
242
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245 66d31749 Leszek Koltunski
    void setChecked(int number, boolean checked)
246
      {
247 d4a46952 leszek
      if( number>=0 && number<NUM_OBJECTS )
248 66d31749 Leszek Koltunski
        {
249
        if( checked && !mBlurStatus[number] )
250
          {
251
          mBlurStatus[number] = true;
252
          mNode[number].getEffects().apply(mBlur);
253
          }
254
        if( !checked && mBlurStatus[number] )
255
          {
256
          mBlurStatus[number] = false;
257
          mNode[number].getEffects().abortEffect(mBlur);
258
          }
259
        }
260
      else
261
        {
262
        android.util.Log.e("renderer", "Error, number: "+number+" checked: "+checked );
263
        }
264
      }
265 dc10a48d Leszek Koltunski
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268
    public void distortedException(Exception ex)
269
      {
270
      android.util.Log.e("Multiblur", ex.getMessage() );
271
      }
272
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275
    public InputStream localFile(int fileID)
276
      {
277
      return mResources.openRawResource(fileID);
278
      }
279
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281
282
    public void logMessage(String message)
283
      {
284
      android.util.Log.e("Multiblur", message );
285
      }
286 58059374 Leszek Koltunski
}