Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 3e673c74

1 58059374 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.GLES30;
25
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedEffects;
30 d218d64e leszek
import org.distorted.library.DistortedScreen;
31 58059374 Leszek Koltunski
import org.distorted.library.DistortedTexture;
32 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
33 58059374 Leszek Koltunski
import org.distorted.library.EffectTypes;
34
import org.distorted.library.MeshCubes;
35 3e673c74 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
36 58059374 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
37 9bea2f59 Leszek Koltunski
import org.distorted.library.type.Static1D;
38 58059374 Leszek Koltunski
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 e11b37f4 Leszek Koltunski
    private static final int OBJ_SIZE    = 100;
65 58059374 Leszek Koltunski
66
    private GLSurfaceView mView;
67
    private DistortedTexture mTex1, mTex2;
68
    private DistortedEffects[] mEffects;
69 3e673c74 Leszek Koltunski
    private Static3D[]  mMoveVector;
70
    private Dynamic3D[] mMoveDynamic;
71 d218d64e leszek
    private DistortedScreen mScreen;
72 58059374 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
73 3e673c74 Leszek Koltunski
    private int mDistance;
74 58059374 Leszek Koltunski
75
    Static4D mQuat1, mQuat2;
76
    int mScreenMin;
77
    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80
    MultiblurRenderer(GLSurfaceView v)
81
      {
82
      mView = v;
83 3e673c74 Leszek Koltunski
      mDistance = -1;
84 58059374 Leszek Koltunski
85 3e673c74 Leszek Koltunski
      mMoveDynamic= new Dynamic3D[NUM_OBJECTS];
86 58059374 Leszek Koltunski
      mMoveVector = new Static3D[NUM_OBJECTS];
87 3e673c74 Leszek Koltunski
      mEffects    = new DistortedEffects[NUM_OBJECTS];
88 58059374 Leszek Koltunski
89 3e673c74 Leszek Koltunski
      for(int i=0; i<NUM_OBJECTS; i++)
90
        {
91
        mMoveVector[i]  = new Static3D(0,0,0);
92
        mEffects[i]     = new DistortedEffects();
93
        mMoveDynamic[i] = new Dynamic3D();
94
95
        mMoveDynamic[i].add(mMoveVector[i]);
96
        }
97 58059374 Leszek Koltunski
98 9bea2f59 Leszek Koltunski
      mEffects[0].blur(new Static1D(10));
99
100 fe59d375 Leszek Koltunski
      MeshCubes mesh = new MeshCubes(1,1,false);
101 58059374 Leszek Koltunski
102 e11b37f4 Leszek Koltunski
      mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
103
      mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
104 58059374 Leszek Koltunski
105
      mQuat1 = new Static4D(0,0,0,1);  // unity
106
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
107
      
108
      mQuatInt1 = new DynamicQuat(0,0.5f);
109
      mQuatInt2 = new DynamicQuat(0,0.5f);
110
111
      mQuatInt1.add(mQuat1);
112
      mQuatInt2.add(mQuat2);
113
114 d218d64e leszek
      mScreen = new DistortedScreen();
115 fe59d375 Leszek Koltunski
116
      for(int i=0; i<NUM_OBJECTS; i++)
117
        mScreen.attach( i<NUM_OBJECTS/2 ? mTex1:mTex2, mEffects[i], mesh);
118 58059374 Leszek Koltunski
      }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
   
122
    public void onDrawFrame(GL10 glUnused) 
123
      {
124 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
125 58059374 Leszek Koltunski
      }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
130
      {
131
      mScreenMin = width<height ? width:height;
132 3e673c74 Leszek Koltunski
133
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
134 e11b37f4 Leszek Koltunski
      Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, 0.0f );
135 3e673c74 Leszek Koltunski
      Static3D moveVec= new Static3D( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
136 58059374 Leszek Koltunski
137
      for(int i=0; i<NUM_OBJECTS; i++)
138
        {
139
        mEffects[i].abortEffects(EffectTypes.MATRIX);
140
141 3e673c74 Leszek Koltunski
        mEffects[i].move(moveVec);
142 58059374 Leszek Koltunski
        mEffects[i].scale(factor);
143
        mEffects[i].quaternion(mQuatInt1, center);
144
        mEffects[i].quaternion(mQuatInt2, center);
145 3e673c74 Leszek Koltunski
        mEffects[i].move(mMoveDynamic[i]);
146 58059374 Leszek Koltunski
        }
147
148 3e673c74 Leszek Koltunski
      computeMoveVectors();
149
150 58059374 Leszek Koltunski
      mScreen.resize(width, height);
151
      }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
156
      {
157
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
158
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
159
      Bitmap bitmap1,bitmap2;
160
        
161
      try 
162
        {
163
        bitmap1 = BitmapFactory.decodeStream(is1);
164
        bitmap2 = BitmapFactory.decodeStream(is2);
165
        }
166
      finally 
167
        {
168
        try 
169
          {
170
          is1.close();
171
          is2.close();
172
          }
173
        catch(IOException e) { }
174
        }  
175
176
      mTex1.setTexture(bitmap1);
177
      mTex2.setTexture(bitmap2);
178
179 6637d0f2 Leszek Koltunski
      DistortedEffects.enableEffect(EffectNames.BLUR);
180
181 58059374 Leszek Koltunski
      try
182
        {
183
        Distorted.onCreate(mView.getContext());
184
        }
185
      catch(Exception ex)
186
        {
187
        android.util.Log.e("Multiblur", ex.getMessage() );
188
        }
189
      }
190 3e673c74 Leszek Koltunski
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193
    private void computeMoveVectors()
194
      {
195
      float size= 1.3f*OBJ_SIZE*(mDistance/50.0f);
196
197
      for(int i=0; i<NUM_OBJECTS; i++)
198
        {
199
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
200
        }
201
      }
202
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205
    void setDistance(int distance)
206
      {
207
      mDistance = distance;
208
      computeMoveVectors();
209
      android.util.Log.e("renderer", "distance: "+distance);
210
      }
211 58059374 Leszek Koltunski
}