Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 77e66c58

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.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29

    
30
import org.distorted.examples.R;
31
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
import org.distorted.library.main.DistortedLibrary;
36
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
import org.distorted.library.effect.EffectQuality;
41
import org.distorted.library.mesh.MeshCubes;
42
import org.distorted.library.type.Static2D;
43
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
class MultiblurRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
55
{
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
    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
    private int mDistance;
82

    
83
    Static4D mQuat1, mQuat2;
84
    int mScreenMin;
85

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

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

    
94
      mBlurStatus = new boolean[NUM_OBJECTS];
95
      mMoveVector = new Static3D[NUM_OBJECTS];
96
      mNode       = new DistortedNode[NUM_OBJECTS];
97

    
98
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
99

    
100
      for(int i=0; i<NUM_OBJECTS; i++)
101
        {
102
        mMoveVector[i] = new Static3D(0,0,0);
103
        effects[i]     = new DistortedEffects();
104
        mBlurStatus[i] = false;
105
        }
106

    
107
      mBlurHaloRadius = new Static2D(10,2);
108

    
109
      MeshCubes mesh = new MeshCubes(1,1,1);
110

    
111
      mTex1 = new DistortedTexture();
112
      mTex2 = new DistortedTexture();
113

    
114
      mQuat1 = new Static4D(0,0,0,1);  // unity
115
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
116

    
117
      mScreen = new DistortedScreen();
118
      mScreen.showFPS();
119

    
120
      for(int i=0; i<NUM_OBJECTS; i++)
121
        {
122
        mNode[i] = new DistortedNode(i < NUM_OBJECTS / 2 ? mTex1 : mTex2, effects[i], mesh);
123
        mScreen.attach(mNode[i]);
124
        }
125

    
126
      mBlur = new PostprocessEffectBlur(mBlurHaloRadius);
127
      mBlurStatus[0] = true;
128
      effects[0].apply(mBlur);
129

    
130
      mScale  = new Static3D(1,1,1);
131
      Static3D center = new Static3D(0,0,0);
132

    
133
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
134
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
135
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
136

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
    void setQuality(EffectQuality quality)
149
      {
150
      mBlur.setQuality(quality);
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
    void setRenderModeToOIT(boolean oit)
156
      {
157
      mScreen.setOrderIndependentTransparency(oit);
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
    public void onDrawFrame(GL10 glUnused) 
163
      {
164
      mScreen.render(System.currentTimeMillis());
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
    
169
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
170
      {
171
      mScreenMin  = Math.min(width, height);
172
    	float factor= 0.15f*mScreenMin;
173
    	mScale.set(factor,factor,factor);
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 ignored) { }
199
        }  
200

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

    
204
      PostprocessEffectBlur.enable();
205

    
206
      DistortedLibrary.onSurfaceCreated(this);
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
    private void computeMoveVectors()
212
      {
213
      float size= 0.026f*mDistance;
214

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

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
    void setDistance(int distance)
224
      {
225
      mDistance = distance;
226
      computeMoveVectors();
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
    void setRange(int range)
232
      {
233
      mBlurHaloRadius.set(range*0.2f,range*0.5f);
234
      }
235

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

    
238
    boolean[] getChecked()
239
     {
240
     return mBlurStatus;
241
     }
242

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

    
245
    void setChecked(int number, boolean checked)
246
      {
247
      if( number>=0 && number<NUM_OBJECTS )
248
        {
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

    
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
}
(2-2/3)