Project

General

Profile

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

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

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.effect.EffectName;
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.PostprocessEffectBlur;
32
import org.distorted.library.main.Distorted;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedNode;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.main.EffectQuality;
38
import org.distorted.library.main.MeshCubes;
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 DistortedNode[] mNode;
71
    private Static3D[]  mMoveVector;
72
    private Static1D  mBlurVector;
73
    private DistortedScreen mScreen;
74
    private PostprocessEffectBlur mBlur;
75
    private int mDistance;
76
    private boolean[] mBlurStatus;
77
    private Static3D mMove, mScale, mCenter;
78

    
79
    Static4D mQuat1, mQuat2;
80
    int mScreenMin;
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
    MultiblurRenderer(GLSurfaceView v)
85
      {
86
      mView = v;
87
      mDistance = -1;
88

    
89
      mBlurStatus = new boolean[NUM_OBJECTS];
90
      mMoveVector = new Static3D[NUM_OBJECTS];
91
      mNode       = new DistortedNode[NUM_OBJECTS];
92

    
93
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
94

    
95
      for(int i=0; i<NUM_OBJECTS; i++)
96
        {
97
        mMoveVector[i] = new Static3D(0,0,0);
98
        effects[i]     = new DistortedEffects();
99
        mBlurStatus[i] = false;
100
        }
101

    
102
      mBlurVector = new Static1D(10);
103

    
104
      MeshCubes mesh = new MeshCubes(1,1,1);
105

    
106
      mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
107
      mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
108

    
109
      mQuat1 = new Static4D(0,0,0,1);  // unity
110
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
111

    
112
      mScreen = new DistortedScreen();
113
      mScreen.setDebug(DistortedScreen.DEBUG_FPS);
114

    
115
      for(int i=0; i<NUM_OBJECTS; i++)
116
        {
117
        mNode[i] = new DistortedNode(i < NUM_OBJECTS / 2 ? mTex1 : mTex2, effects[i], mesh);
118
        mScreen.attach(mNode[i]);
119
        }
120

    
121
      mBlur = new PostprocessEffectBlur(mBlurVector);
122
      mBlurStatus[0] = true;
123
      effects[0].apply(mBlur);
124

    
125
      mMove   = new Static3D(0,0,0);
126
      mScale  = new Static3D(1,1,1);
127
      mCenter = new Static3D(0,0,0);
128

    
129
      MatrixEffectMove moveEffect = new MatrixEffectMove(mMove);
130
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
131
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, mCenter);
132
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, mCenter);
133

    
134
      for(int i=0; i<NUM_OBJECTS; i++)
135
        {
136
        effects[i].apply(moveEffect);
137
        effects[i].apply(scaleEffect);
138
        effects[i].apply(quatEffect1);
139
        effects[i].apply(quatEffect2);
140
        effects[i].apply(new MatrixEffectMove(mMoveVector[i]));
141
        }
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
    void setQuality(EffectQuality quality)
147
      {
148
      DistortedEffects effects;
149

    
150
      for(int i=0; i<NUM_OBJECTS; i++)
151
        {
152
        effects = mNode[i].getEffects();
153
        effects.setQuality(quality);
154
        }
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
    public void onDrawFrame(GL10 glUnused) 
160
      {
161
      mScreen.render(System.currentTimeMillis());
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
    
166
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
167
      {
168
      mScreenMin = width<height ? width:height;
169

    
170
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
171
    	mScale.set(factor,factor,factor);
172
      mCenter.set((float)OBJ_SIZE/2, (float)OBJ_SIZE/2, -(float)OBJ_SIZE/2 );
173
      mMove.set( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
174
      computeMoveVectors();
175
      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
        catch(IOException e) { }
199
        }  
200

    
201
      mTex1.setTexture(bitmap1);
202
      mTex2.setTexture(bitmap2);
203

    
204
      DistortedEffects.enableEffect(EffectName.BLUR);
205

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

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
    private void computeMoveVectors()
219
      {
220
      float size= 0.026f*OBJ_SIZE*mDistance;
221

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

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
    void setDistance(int distance)
231
      {
232
      mDistance = distance;
233
      computeMoveVectors();
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    void setRange(int range)
239
      {
240
      mBlurVector.set(range/2);
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

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

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