Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 895d2f4f

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.EffectTypes;
35
import org.distorted.library.MeshCubes;
36
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

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

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

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

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

    
82
    Static4D mQuat1, mQuat2;
83
    int mScreenMin;
84
    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

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

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

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

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

    
114
      MeshCubes mesh = new MeshCubes(1,1,false);
115

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

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

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

    
128
      mScreen = new DistortedScreen();
129

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

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
   
142
    public void onDrawFrame(GL10 glUnused) 
143
      {
144
      mScreen.render( System.currentTimeMillis() );
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
    
149
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
150
      {
151
      mScreenMin = width<height ? width:height;
152

    
153
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
154
      Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, 0.0f );
155
      Static3D moveVec= new Static3D( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
156

    
157
      for(int i=0; i<NUM_OBJECTS; i++)
158
        {
159
        mEffects[i].abortEffects(EffectTypes.MATRIX);
160

    
161
        mEffects[i].move(moveVec);
162
        mEffects[i].scale(factor);
163
        mEffects[i].quaternion(mQuatInt1, center);
164
        mEffects[i].quaternion(mQuatInt2, center);
165
        mEffects[i].move(mMoveDynamic[i]);
166
        }
167

    
168
      computeMoveVectors();
169

    
170
      mScreen.resize(width, height);
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
    
175
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
176
      {
177
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
178
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
179
      Bitmap bitmap1,bitmap2;
180
        
181
      try 
182
        {
183
        bitmap1 = BitmapFactory.decodeStream(is1);
184
        bitmap2 = BitmapFactory.decodeStream(is2);
185
        }
186
      finally 
187
        {
188
        try 
189
          {
190
          is1.close();
191
          is2.close();
192
          }
193
        catch(IOException e) { }
194
        }  
195

    
196
      mTex1.setTexture(bitmap1);
197
      mTex2.setTexture(bitmap2);
198

    
199
      DistortedEffects.enableEffect(EffectNames.BLUR);
200

    
201
      try
202
        {
203
        Distorted.onCreate(mView.getContext());
204
        }
205
      catch(Exception ex)
206
        {
207
        android.util.Log.e("Multiblur", ex.getMessage() );
208
        }
209
      }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
    private void computeMoveVectors()
214
      {
215
      float size= 0.026f*OBJ_SIZE*mDistance;
216

    
217
      for(int i=0; i<NUM_OBJECTS; i++)
218
        {
219
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
220
        }
221
      }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
    void setDistance(int distance)
226
      {
227
      mDistance = distance;
228
      computeMoveVectors();
229

    
230
      //android.util.Log.d("renderer", "distance: "+distance);
231
      }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
    void setRange(int range)
236
      {
237
      mBlurVector.set(range);
238

    
239
      //android.util.Log.d("renderer", "range: "+range);
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
   boolean[] getChecked()
245
     {
246
     return mBlurStatus;
247
     }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

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

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