Project

General

Profile

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

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

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.Distorted;
28
import org.distorted.library.DistortedEffects;
29
import org.distorted.library.DistortedScreen;
30
import org.distorted.library.DistortedTexture;
31
import org.distorted.library.EffectNames;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.MeshCubes;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic3D;
36
import org.distorted.library.type.DynamicQuat;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

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

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

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

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

    
66
    private GLSurfaceView mView;
67
    private DistortedTexture mTex1, mTex2;
68
    private DistortedEffects[] mEffects;
69
    private Static3D[]  mMoveVector;
70
    private Dynamic3D[] mMoveDynamic;
71
    private Static1D  mBlurVector;
72
    private DistortedScreen mScreen;
73
    private DynamicQuat mQuatInt1, mQuatInt2;
74
    private int mDistance;
75

    
76
    Static4D mQuat1, mQuat2;
77
    int mScreenMin;
78
    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

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

    
86
      mMoveDynamic= new Dynamic3D[NUM_OBJECTS];
87
      mMoveVector = new Static3D[NUM_OBJECTS];
88
      mEffects    = new DistortedEffects[NUM_OBJECTS];
89

    
90
      for(int i=0; i<NUM_OBJECTS; i++)
91
        {
92
        mMoveVector[i]  = new Static3D(0,0,0);
93
        mEffects[i]     = new DistortedEffects();
94
        mMoveDynamic[i] = new Dynamic3D();
95

    
96
        mMoveDynamic[i].add(mMoveVector[i]);
97
        }
98

    
99
      Dynamic1D blurDynamic= new Dynamic1D();
100
      mBlurVector = new Static1D(10);
101
      blurDynamic.add(mBlurVector);
102

    
103
      mEffects[0].blur(blurDynamic);
104

    
105
      MeshCubes mesh = new MeshCubes(1,1,false);
106

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

    
110
      mQuat1 = new Static4D(0,0,0,1);  // unity
111
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
112
      
113
      mQuatInt1 = new DynamicQuat(0,0.5f);
114
      mQuatInt2 = new DynamicQuat(0,0.5f);
115

    
116
      mQuatInt1.add(mQuat1);
117
      mQuatInt2.add(mQuat2);
118

    
119
      mScreen = new DistortedScreen();
120

    
121
      for(int i=0; i<NUM_OBJECTS; i++)
122
        mScreen.attach( i<NUM_OBJECTS/2 ? mTex1:mTex2, mEffects[i], mesh);
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
   
127
    public void onDrawFrame(GL10 glUnused) 
128
      {
129
      mScreen.render( System.currentTimeMillis() );
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
    
134
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
135
      {
136
      mScreenMin = width<height ? width:height;
137

    
138
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
139
      Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, 0.0f );
140
      Static3D moveVec= new Static3D( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
141

    
142
      for(int i=0; i<NUM_OBJECTS; i++)
143
        {
144
        mEffects[i].abortEffects(EffectTypes.MATRIX);
145

    
146
        mEffects[i].move(moveVec);
147
        mEffects[i].scale(factor);
148
        mEffects[i].quaternion(mQuatInt1, center);
149
        mEffects[i].quaternion(mQuatInt2, center);
150
        mEffects[i].move(mMoveDynamic[i]);
151
        }
152

    
153
      computeMoveVectors();
154

    
155
      mScreen.resize(width, height);
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
    
160
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
161
      {
162
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
163
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
164
      Bitmap bitmap1,bitmap2;
165
        
166
      try 
167
        {
168
        bitmap1 = BitmapFactory.decodeStream(is1);
169
        bitmap2 = BitmapFactory.decodeStream(is2);
170
        }
171
      finally 
172
        {
173
        try 
174
          {
175
          is1.close();
176
          is2.close();
177
          }
178
        catch(IOException e) { }
179
        }  
180

    
181
      mTex1.setTexture(bitmap1);
182
      mTex2.setTexture(bitmap2);
183

    
184
      DistortedEffects.enableEffect(EffectNames.BLUR);
185

    
186
      try
187
        {
188
        Distorted.onCreate(mView.getContext());
189
        }
190
      catch(Exception ex)
191
        {
192
        android.util.Log.e("Multiblur", ex.getMessage() );
193
        }
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    private void computeMoveVectors()
199
      {
200
      float size= 1.3f*OBJ_SIZE*(mDistance/50.0f);
201

    
202
      for(int i=0; i<NUM_OBJECTS; i++)
203
        {
204
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
205
        }
206
      }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
    void setDistance(int distance)
211
      {
212
      mDistance = distance;
213
      computeMoveVectors();
214
      android.util.Log.e("renderer", "distance: "+distance);
215
      }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    void setRange(int range)
220
      {
221
      mBlurVector.set(range);
222
      android.util.Log.e("renderer", "range: "+range);
223
      }
224
}
(2-2/3)