Project

General

Profile

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

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

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.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
import org.distorted.library.DistortedScreen;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.EffectNames;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.MeshCubes;
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 DistortedScreen mScreen;
72
    private DynamicQuat mQuatInt1, mQuatInt2;
73
    private int mDistance;
74

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

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

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

    
89
      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

    
98
      mEffects[0].blur(new Static1D(10));
99

    
100
      MeshCubes mesh = new MeshCubes(1,1,false);
101

    
102
      mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
103
      mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
104

    
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
      mScreen = new DistortedScreen();
115

    
116
      for(int i=0; i<NUM_OBJECTS; i++)
117
        mScreen.attach( i<NUM_OBJECTS/2 ? mTex1:mTex2, mEffects[i], mesh);
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
   
122
    public void onDrawFrame(GL10 glUnused) 
123
      {
124
      mScreen.render( System.currentTimeMillis() );
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
130
      {
131
      mScreenMin = width<height ? width:height;
132

    
133
    	float factor    = 0.15f*mScreenMin/OBJ_SIZE;
134
      Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, 0.0f );
135
      Static3D moveVec= new Static3D( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
136

    
137
      for(int i=0; i<NUM_OBJECTS; i++)
138
        {
139
        mEffects[i].abortEffects(EffectTypes.MATRIX);
140

    
141
        mEffects[i].move(moveVec);
142
        mEffects[i].scale(factor);
143
        mEffects[i].quaternion(mQuatInt1, center);
144
        mEffects[i].quaternion(mQuatInt2, center);
145
        mEffects[i].move(mMoveDynamic[i]);
146
        }
147

    
148
      computeMoveVectors();
149

    
150
      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
      DistortedEffects.enableEffect(EffectNames.BLUR);
180

    
181
      try
182
        {
183
        Distorted.onCreate(mView.getContext());
184
        }
185
      catch(Exception ex)
186
        {
187
        android.util.Log.e("Multiblur", ex.getMessage() );
188
        }
189
      }
190

    
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
}
(2-2/3)