Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 01782e85

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
package org.distorted.examples.multiblur;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.main.Distorted;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedEffectsPostprocess;
30
import org.distorted.library.main.DistortedNode;
31
import org.distorted.library.main.DistortedScreen;
32
import org.distorted.library.main.DistortedTexture;
33
import org.distorted.library.EffectNames;
34
import org.distorted.library.main.EffectQuality;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.main.MeshCubes;
37
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Dynamic3D;
39
import org.distorted.library.type.DynamicQuat;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43

    
44
import java.io.IOException;
45
import java.io.InputStream;
46

    
47
import javax.microedition.khronos.egl.EGLConfig;
48
import javax.microedition.khronos.opengles.GL10;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class MultiblurRenderer implements GLSurfaceView.Renderer
53
{
54
    private static final int[] MOVE_VEC =
55
        {
56
        +1,+1,+1,
57
        +1,-1,+1,
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
        };
65

    
66
    private static final int NUM_OBJECTS = MOVE_VEC.length/3;
67
    private static final int OBJ_SIZE    = 100;
68

    
69
    private GLSurfaceView mView;
70
    private DistortedTexture mTex1, mTex2;
71
    private DistortedEffects[] mEffects;
72
    private DistortedNode[] mNode;
73
    private DistortedEffectsPostprocess mPostEffects;
74
    private Static3D[]  mMoveVector;
75
    private Dynamic3D[] mMoveDynamic;
76
    private Static1D  mBlurVector;
77
    private Dynamic1D  mBlurDynamic;
78
    private DistortedScreen mScreen;
79
    private DynamicQuat mQuatInt1, mQuatInt2;
80
    private int mDistance;
81
    private boolean[] mBlurStatus;
82

    
83
    Static4D mQuat1, mQuat2;
84
    int mScreenMin;
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
    MultiblurRenderer(GLSurfaceView v)
89
      {
90
      mView = v;
91
      mDistance = -1;
92

    
93
      mBlurStatus = new boolean[NUM_OBJECTS];
94
      mMoveDynamic= new Dynamic3D[NUM_OBJECTS];
95
      mMoveVector = new Static3D[NUM_OBJECTS];
96
      mNode       = new DistortedNode[NUM_OBJECTS];
97
      mEffects    = new DistortedEffects[NUM_OBJECTS];
98
      mPostEffects= new DistortedEffectsPostprocess();
99

    
100
      for(int i=0; i<NUM_OBJECTS; i++)
101
        {
102
        mMoveVector[i]  = new Static3D(0,0,0);
103
        mEffects[i]     = new DistortedEffects();
104
        mMoveDynamic[i] = new Dynamic3D();
105

    
106
        mMoveDynamic[i].add(mMoveVector[i]);
107
        mBlurStatus[i] = false;
108
        }
109

    
110
      mBlurDynamic= new Dynamic1D();
111
      mBlurVector = new Static1D(10);
112
      mBlurDynamic.add(mBlurVector);
113
      mPostEffects.blur(mBlurDynamic);
114

    
115
      MeshCubes mesh = new MeshCubes(1,1,1);
116

    
117
      mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
118
      mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
119

    
120
      mQuat1 = new Static4D(0,0,0,1);  // unity
121
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
122
      
123
      mQuatInt1 = new DynamicQuat(0,0.5f);
124
      mQuatInt2 = new DynamicQuat(0,0.5f);
125

    
126
      mQuatInt1.add(mQuat1);
127
      mQuatInt2.add(mQuat2);
128

    
129
      mScreen = new DistortedScreen(mView);
130
      mScreen.setDebug(DistortedScreen.DEBUG_FPS);
131

    
132
      for(int i=0; i<NUM_OBJECTS; i++)
133
        {
134
        mNode[i] = new DistortedNode(i < NUM_OBJECTS / 2 ? mTex1 : mTex2, mEffects[i], mesh);
135
        mScreen.attach(mNode[i]);
136
        }
137

    
138
      mBlurStatus[0] = true;
139
      mNode[0].setPostprocessEffects(mPostEffects);
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
    void setQuality(EffectQuality quality)
145
      {
146
      mPostEffects.setQuality(quality);
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
    public void onDrawFrame(GL10 glUnused) 
152
      {
153
      mScreen.render(System.currentTimeMillis());
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
    
158
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
159
      {
160
      mScreenMin = width<height ? width:height;
161

    
162
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
163
      Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, -(float)OBJ_SIZE/2 );
164
      Static3D moveVec= new Static3D( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
165

    
166
      for(int i=0; i<NUM_OBJECTS; i++)
167
        {
168
        mEffects[i].abortEffects(EffectTypes.MATRIX);
169

    
170
        mEffects[i].move(moveVec);
171
        mEffects[i].scale(factor);
172
        mEffects[i].quaternion(mQuatInt1, center);
173
        mEffects[i].quaternion(mQuatInt2, center);
174
        mEffects[i].move(mMoveDynamic[i]);
175
        }
176

    
177
      computeMoveVectors();
178
      mScreen.resize(width, height);
179
      }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
    
183
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
184
      {
185
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
186
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
187
      Bitmap bitmap1,bitmap2;
188
        
189
      try 
190
        {
191
        bitmap1 = BitmapFactory.decodeStream(is1);
192
        bitmap2 = BitmapFactory.decodeStream(is2);
193
        }
194
      finally 
195
        {
196
        try 
197
          {
198
          is1.close();
199
          is2.close();
200
          }
201
        catch(IOException e) { }
202
        }  
203

    
204
      mTex1.setTexture(bitmap1);
205
      mTex2.setTexture(bitmap2);
206

    
207
      DistortedEffects.enableEffect(EffectNames.BLUR);
208

    
209
      try
210
        {
211
        Distorted.onCreate(mView.getContext());
212
        }
213
      catch(Exception ex)
214
        {
215
        android.util.Log.e("Multiblur", ex.getMessage() );
216
        }
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    private void computeMoveVectors()
222
      {
223
      float size= 0.026f*OBJ_SIZE*mDistance;
224

    
225
      for(int i=0; i<NUM_OBJECTS; i++)
226
        {
227
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
228
        }
229
      }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
    void setDistance(int distance)
234
      {
235
      mDistance = distance;
236
      computeMoveVectors();
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
    void setRange(int range)
242
      {
243
      mBlurVector.set(range/2);
244
      }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
   boolean[] getChecked()
249
     {
250
     return mBlurStatus;
251
     }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
   void setChecked(int number, boolean checked)
256
     {
257
     if( number>=0 && number<=7 )
258
       {
259
       if( checked && !mBlurStatus[number] )
260
         {
261
         mBlurStatus[number] = true;
262
         mNode[number].setPostprocessEffects(mPostEffects);
263
         }
264
       if( !checked && mBlurStatus[number] )
265
         {
266
         mBlurStatus[number] = false;
267
         mNode[number].setPostprocessEffects(null);
268
         }
269
       }
270
     else
271
       {
272
       android.util.Log.e("renderer", "Error, number: "+number+" checked: "+checked );
273
       }
274

    
275
     //android.util.Log.d("renderer", "setting box "+number+" BLUR state to "+checked);
276
     }
277
}
(2-2/3)