Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 24ab1cf8

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.Distorted;
28
import org.distorted.library.DistortedEffects;
29
import org.distorted.library.DistortedEffectsPostprocess;
30
import org.distorted.library.DistortedNode;
31
import org.distorted.library.DistortedScreen;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.EffectNames;
34
import org.distorted.library.EffectQuality;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.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

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

    
137
      mBlurStatus[0] = true;
138
      mNode[0].setPostprocessEffects(mPostEffects);
139

    
140
      Distorted.setDebug(Distorted.DEBUG_FPS);
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

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

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

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

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

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

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

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

    
208
      DistortedEffects.enableEffect(EffectNames.BLUR);
209

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

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

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

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

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

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

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

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

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

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

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

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