Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 061449ed

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.MatrixEffectMove;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.PostprocessEffectBlur;
31
import org.distorted.library.main.DistortedLibrary;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedNode;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.effect.EffectQuality;
37
import org.distorted.library.mesh.MeshCubes;
38
import org.distorted.library.type.Static2D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

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

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
64
    private static final int NUM_OBJECTS = MOVE_VEC.length/3;
65

    
66
    private GLSurfaceView mView;
67
    private DistortedTexture mTex1, mTex2;
68
    private DistortedNode[] mNode;
69
    private Static3D[]  mMoveVector;
70
    private Static2D mBlurHaloRadius;
71
    private DistortedScreen mScreen;
72
    private PostprocessEffectBlur mBlur;
73
    private int mDistance;
74
    private boolean[] mBlurStatus;
75
    private Static3D mScale;
76

    
77
    Static4D mQuat1, mQuat2;
78
    int mScreenMin;
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
    MultiblurRenderer(GLSurfaceView v)
83
      {
84
      mView = v;
85
      mDistance = -1;
86

    
87
      mBlurStatus = new boolean[NUM_OBJECTS];
88
      mMoveVector = new Static3D[NUM_OBJECTS];
89
      mNode       = new DistortedNode[NUM_OBJECTS];
90

    
91
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
92

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

    
100
      mBlurHaloRadius = new Static2D(10,2);
101

    
102
      MeshCubes mesh = new MeshCubes(1,1,1);
103

    
104
      mTex1 = new DistortedTexture();
105
      mTex2 = new DistortedTexture();
106

    
107
      mQuat1 = new Static4D(0,0,0,1);  // unity
108
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
109

    
110
      mScreen = new DistortedScreen();
111
      mScreen.showFPS();
112

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

    
119
      mBlur = new PostprocessEffectBlur(mBlurHaloRadius);
120
      mBlurStatus[0] = true;
121
      effects[0].apply(mBlur);
122

    
123
      mScale  = new Static3D(1,1,1);
124
      Static3D center = new Static3D(0,0,0);
125

    
126
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
127
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
128
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
129

    
130
      for(int i=0; i<NUM_OBJECTS; i++)
131
        {
132
        effects[i].apply(new MatrixEffectMove(mMoveVector[i]));
133
        effects[i].apply(quatEffect2);
134
        effects[i].apply(quatEffect1);
135
        effects[i].apply(scaleEffect);
136
        }
137
      }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
    void setQuality(EffectQuality quality)
142
      {
143
      mBlur.setQuality(quality);
144
      }
145

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

    
148
    void setRenderModeToOIT(boolean oit)
149
      {
150
      mScreen.setOrderIndependentTransparency(oit);
151
      }
152

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

    
155
    public void onDrawFrame(GL10 glUnused) 
156
      {
157
      mScreen.render(System.currentTimeMillis());
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
    
162
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
163
      {
164
      mScreenMin  = Math.min(width, height);
165
    	float factor= 0.15f*mScreenMin;
166
    	mScale.set(factor,factor,factor);
167
      computeMoveVectors();
168
      mScreen.resize(width, height);
169
      }
170

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

    
194
      mTex1.setTexture(bitmap1);
195
      mTex2.setTexture(bitmap2);
196

    
197
      PostprocessEffectBlur.enable();
198

    
199
      DistortedLibrary.onCreate(mView.getContext(), this);
200
      }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
    public void distortedException(Exception ex)
205
      {
206
      android.util.Log.e("Multiblur", ex.getMessage() );
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
}
(2-2/3)