Project

General

Profile

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

examples / src / main / java / org / distorted / examples / triblur / TriblurRenderer.java @ 80f37d1b

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.triblur;
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.EffectQuality;
28
import org.distorted.library.effect.FragmentEffectChroma;
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectQuaternion;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.effect.PostprocessEffectBlur;
33
import org.distorted.library.main.Distorted;
34
import org.distorted.library.main.DistortedEffects;
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.MeshCubes;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

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

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class TriblurRenderer implements GLSurfaceView.Renderer
52
{
53
    private static final int[] OBJECTS =
54
        {
55
        -1, 0, 0, 255,   0,  0,  // x,y,z, R,G,B
56
         0, 0, 0, 255, 255,  0,  //
57
        +1, 0, 0,   0, 255,  0,  //
58
        };
59

    
60
    private static final int NUM_OBJECTS = OBJECTS.length/6;
61
    private static final int OBJ_SIZE    = 100;
62

    
63
    private GLSurfaceView mView;
64
    private DistortedTexture mTex;
65
    private DistortedNode[] mNode;
66
    private Static3D[]  mMoveVector;
67
    private Static3D[]  mChromaVector;
68
    private Static1D[]  mBlurVector;
69
    private DistortedScreen mScreen;
70
    private PostprocessEffectBlur[] mBlur;
71
    private FragmentEffectChroma[] mChroma;
72
    private int mDistance;
73
    private boolean[] mBlurStatus;
74
    private Static3D mMove, mScale, mCenter;
75

    
76
    Static4D mQuat1, mQuat2;
77
    int mScreenMin;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
    TriblurRenderer(GLSurfaceView v)
82
      {
83
      mView = v;
84
      mDistance = OBJ_SIZE/2;
85

    
86
      MeshCubes mesh = new MeshCubes(1,1,1);
87

    
88
      mTex = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
89

    
90
      mQuat1 = new Static4D(0,0,0,1);  // unity
91
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
92

    
93
      mScreen = new DistortedScreen();
94
      mScreen.setDebug(DistortedScreen.DEBUG_FPS);
95

    
96
      mBlurStatus   = new boolean[NUM_OBJECTS];
97
      mMoveVector   = new Static3D[NUM_OBJECTS];
98
      mChromaVector = new Static3D[NUM_OBJECTS];
99
      mBlurVector   = new Static1D[NUM_OBJECTS];
100
      mNode         = new DistortedNode[NUM_OBJECTS];
101
      mBlur         = new PostprocessEffectBlur[NUM_OBJECTS];
102
      mChroma       = new FragmentEffectChroma[NUM_OBJECTS];
103

    
104
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
105

    
106
      mMove   = new Static3D(0,0,0);
107
      mScale  = new Static3D(1,1,1);
108
      mCenter = new Static3D(0,0,0);
109

    
110
      MatrixEffectMove moveEffect = new MatrixEffectMove(mMove);
111
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
112
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, mCenter);
113
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, mCenter);
114

    
115
      for(int i=0; i<NUM_OBJECTS; i++)
116
        {
117
        mMoveVector[i]   = new Static3D(0,0,0);
118
        mChromaVector[i] = new Static3D(OBJECTS[6*i+3],OBJECTS[6*i+4],OBJECTS[6*i+5]);
119
        mBlurVector[i]   = new Static1D(10);
120
        mBlur[i]         = new PostprocessEffectBlur(mBlurVector[i]);
121
        mChroma[i]       = new FragmentEffectChroma( new Static1D(0.3f), mChromaVector[i]);
122
        effects[i]       = new DistortedEffects();
123

    
124
        effects[i].apply(mBlur[i]);
125
        effects[i].apply(mChroma[i]);
126
        effects[i].apply(moveEffect);
127
        effects[i].apply(scaleEffect);
128
        effects[i].apply(quatEffect1);
129
        effects[i].apply(quatEffect2);
130
        effects[i].apply(new MatrixEffectMove(mMoveVector[i]));
131

    
132
        mBlurStatus[i] = true;
133
        mNode[i] = new DistortedNode(mTex, effects[i], mesh);
134
        mScreen.attach(mNode[i]);
135
        }
136

    
137
      setQuality(EffectQuality.HIGH);
138
      }
139

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

    
142
    void setQuality(EffectQuality quality)
143
      {
144
      for(int i=0; i<NUM_OBJECTS; i++)
145
        {
146
        mBlur[i].setQuality(quality);
147
        }
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
    public void onDrawFrame(GL10 glUnused) 
153
      {
154
      mScreen.render(System.currentTimeMillis());
155
      }
156

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

    
177
      mTex.setTexture(bitmap);
178

    
179
      PostprocessEffectBlur.enable();
180
      FragmentEffectChroma.enable();
181

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

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
195
      {
196
      mScreenMin = width<height ? width:height;
197

    
198
      float factor = 0.24f*mScreenMin/OBJ_SIZE;
199
      mScale.set(factor,factor,factor);
200
      mCenter.set((float)OBJ_SIZE/2, (float)OBJ_SIZE/2, -(float)OBJ_SIZE/2 );
201
      mMove.set( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
202
      computeMoveVectors();
203
      mScreen.resize(width, height);
204
      }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
    private void computeMoveVectors()
209
      {
210
      float size= 0.026f*OBJ_SIZE*mDistance;
211

    
212
      for(int i=0; i<NUM_OBJECTS; i++)
213
        {
214
        mMoveVector[i].set(size*OBJECTS[6*i], size*OBJECTS[6*i+1], size*OBJECTS[6*i+2]);
215
        }
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
    void setRange(int object, int range)
221
      {
222
      if( object>=0 && object<NUM_OBJECTS )
223
        {
224
        mBlurVector[object].set(range / 2);
225
        }
226
      }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
    boolean[] getChecked()
231
     {
232
     return mBlurStatus;
233
     }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
    void setChecked(int object, boolean checked)
238
      {
239
      if( object>=0 && object<NUM_OBJECTS )
240
        {
241
        if( checked && !mBlurStatus[object] )
242
          {
243
          mBlurStatus[object] = true;
244
          mNode[object].getEffects().apply(mBlur[object]);
245
          }
246
        if( !checked && mBlurStatus[object] )
247
          {
248
          mBlurStatus[object] = false;
249
          mNode[object].getEffects().abortEffect(mBlur[object]);
250
          }
251
        }
252
      else
253
        {
254
        android.util.Log.e("renderer", "Error, number: "+object+" checked: "+checked );
255
        }
256

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