Project

General

Profile

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

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

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.DistortedEffectsPostprocess;
35
import org.distorted.library.main.DistortedNode;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.main.EffectQuality;
39
import org.distorted.library.main.MeshCubes;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43

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

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

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

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

    
69
    private GLSurfaceView mView;
70
    private DistortedTexture mTex1, mTex2;
71
    private DistortedNode[] mNode;
72
    private DistortedEffectsPostprocess mPostEffects;
73
    private Static3D[]  mMoveVector;
74
    private Static1D  mBlurVector;
75
    private DistortedScreen mScreen;
76
    private int mDistance;
77
    private boolean[] mBlurStatus;
78
    private Static3D mMove, mScale, mCenter;
79

    
80
    Static4D mQuat1, mQuat2;
81
    int mScreenMin;
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

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

    
90
      mBlurStatus = new boolean[NUM_OBJECTS];
91
      mMoveVector = new Static3D[NUM_OBJECTS];
92
      mNode       = new DistortedNode[NUM_OBJECTS];
93
      mPostEffects= new DistortedEffectsPostprocess();
94

    
95
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
96

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

    
104
      mBlurVector = new Static1D(10);
105
      mPostEffects.apply( new PostprocessEffectBlur(mBlurVector) );
106

    
107
      MeshCubes mesh = new MeshCubes(1,1,1);
108

    
109
      mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
110
      mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
111

    
112
      mQuat1 = new Static4D(0,0,0,1);  // unity
113
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
114

    
115
      mScreen = new DistortedScreen(mView);
116
      mScreen.setDebug(DistortedScreen.DEBUG_FPS);
117

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

    
124
      mBlurStatus[0] = true;
125
      mNode[0].setPostprocessEffects(mPostEffects);
126

    
127
      mMove   = new Static3D(0,0,0);
128
      mScale  = new Static3D(1,1,1);
129
      mCenter = new Static3D(0,0,0);
130

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

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

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

    
148
    void setQuality(EffectQuality quality)
149
      {
150
      mPostEffects.setQuality(quality);
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 = width<height ? width:height;
165

    
166
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
167
    	mScale.set(factor,factor,factor);
168
      mCenter.set((float)OBJ_SIZE/2, (float)OBJ_SIZE/2, -(float)OBJ_SIZE/2 );
169
      mMove.set( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
170
      computeMoveVectors();
171
      mScreen.resize(width, height);
172
      }
173

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

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

    
200
      DistortedEffects.enableEffect(EffectName.BLUR);
201

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

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

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

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

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

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

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

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

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
   boolean[] getChecked()
242
     {
243
     return mBlurStatus;
244
     }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

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

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