Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 16b22aab

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.Distorted;
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.Static1D;
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
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
    private static final int OBJ_SIZE    = 100;
66

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

    
78
    Static4D mQuat1, mQuat2;
79
    int mScreenMin;
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

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

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

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

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

    
101
      mBlurVector = new Static1D(10);
102

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

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

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

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

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

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

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

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

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

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

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

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

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

    
198
      PostprocessEffectBlur.enable();
199

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

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

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

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

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

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

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
    void setRange(int range)
233
      {
234
      mBlurVector.set(range/2);
235
      }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    void setChecked(int number, boolean checked)
247
      {
248
      if( number>=0 && number<NUM_OBJECTS )
249
        {
250
        if( checked && !mBlurStatus[number] )
251
          {
252
          mBlurStatus[number] = true;
253
          mNode[number].getEffects().apply(mBlur);
254
          }
255
        if( !checked && mBlurStatus[number] )
256
          {
257
          mBlurStatus[number] = false;
258
          mNode[number].getEffects().abortEffect(mBlur);
259
          }
260
        }
261
      else
262
        {
263
        android.util.Log.e("renderer", "Error, number: "+number+" checked: "+checked );
264
        }
265
      }
266
}
(2-2/3)