Project

General

Profile

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

examples / src / main / java / org / distorted / examples / triblur / TriblurRenderer.java @ 107e4b72

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.mesh.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 NUM = 6; // 6 ints (x,y,z,R,G,B) each describe 1 object below
54

    
55
    private static final int[] OBJECTS =
56
        {
57
        -150, 0, 0,   255,   0,  0,  // x,y,z, R,G,B
58
           0, 0, 0,   255, 255,  0,  //
59
        +150, 0, 0,     0, 255,  0,  //
60
        };
61

    
62
    private static final int NUM_OBJECTS = OBJECTS.length/NUM;
63
    private static final int OBJ_SIZE    = 100;
64

    
65
    private GLSurfaceView mView;
66
    private DistortedTexture mTex;
67
    private DistortedNode[] mNode;
68
    private Static1D[]  mBlurVector;
69
    private DistortedScreen mScreen;
70
    private PostprocessEffectBlur[] mBlur;
71
    private boolean[] mBlurStatus;
72
    private Static3D mMove1, mMove2, mScale1, mScale2, mCenter;
73

    
74
    Static4D mQuat1, mQuat2;
75
    int mScreenMin;
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
    TriblurRenderer(GLSurfaceView v)
80
      {
81
      mView = v;
82

    
83
      MeshCubes mesh = new MeshCubes(1,1,1);
84

    
85
      mTex = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
86

    
87
      mQuat1 = new Static4D(0,0,0,1);  // unity
88
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
89

    
90
      mScreen = new DistortedScreen();
91
      mScreen.showFPS();
92

    
93
      mBlurStatus   = new boolean[NUM_OBJECTS];
94
      mBlurVector   = new Static1D[NUM_OBJECTS];
95
      mNode         = new DistortedNode[NUM_OBJECTS];
96
      mBlur         = new PostprocessEffectBlur[NUM_OBJECTS];
97

    
98
      FragmentEffectChroma[] chroma= new FragmentEffectChroma[NUM_OBJECTS];
99
      Static3D[] chromaVector      = new Static3D[NUM_OBJECTS];
100
      Static3D[] moveVector        = new Static3D[NUM_OBJECTS];
101

    
102
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
103

    
104
      mMove1  = new Static3D(0,0,0);
105
      mMove2  = new Static3D(0,0,0);
106
      mScale1 = new Static3D(1,1,1);
107
      mScale2 = new Static3D(1.5f,1.5f,1.5f);
108
      mCenter = new Static3D(0,0,0);
109

    
110
      MatrixEffectMove moveEffect1 = new MatrixEffectMove(mMove1);
111
      MatrixEffectMove moveEffect2 = new MatrixEffectMove(mMove2);
112
      MatrixEffectScale scaleEffect1 = new MatrixEffectScale(mScale1);
113
      MatrixEffectScale scaleEffect2 = new MatrixEffectScale(mScale2);
114
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, mCenter);
115
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, mCenter);
116

    
117
      for(int i=0; i<NUM_OBJECTS; i++)
118
        {
119
        moveVector[i]    = new Static3D(OBJECTS[NUM*i  ], OBJECTS[NUM*i+1], OBJECTS[NUM*i+2]);
120
        chromaVector[i]  = new Static3D(OBJECTS[NUM*i+3], OBJECTS[NUM*i+4], OBJECTS[NUM*i+5]);
121
        mBlurVector[i]   = new Static1D(10);
122
        mBlur[i]         = new PostprocessEffectBlur(mBlurVector[i]);
123
        chroma[i]        = new FragmentEffectChroma( new Static1D(0.3f), chromaVector[i]);
124
        effects[i]       = new DistortedEffects();
125

    
126
        effects[i].apply(mBlur[i]);
127
        effects[i].apply(chroma[i]);
128
        effects[i].apply( (i==0||i==NUM_OBJECTS-1) ?  moveEffect1 :  moveEffect2 );
129
        effects[i].apply( (i==0||i==NUM_OBJECTS-1) ? scaleEffect1 : scaleEffect2 );
130
        effects[i].apply(quatEffect1);
131
        effects[i].apply(quatEffect2);
132
        effects[i].apply(new MatrixEffectMove(moveVector[i]));
133

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

    
139
      setQuality(EffectQuality.HIGH);
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

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

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
    void setRenderModeToOIT(boolean oit)
155
      {
156
      mScreen.setOrderIndependentTransparency(oit);
157
      }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
    void setBackground(float r, float g, float b, float a)
162
      {
163
      mScreen.glClearColor(r,g,b,a);
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
    public void onDrawFrame(GL10 glUnused) 
169
      {
170
      mScreen.render(System.currentTimeMillis());
171
      }
172

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

    
193
      mTex.setTexture(bitmap);
194

    
195
      PostprocessEffectBlur.enable();
196
      FragmentEffectChroma.enable();
197

    
198
      try
199
        {
200
        Distorted.onCreate(mView.getContext());
201
        }
202
      catch(Exception ex)
203
        {
204
        android.util.Log.e("Triblur", ex.getMessage() );
205
        }
206
      }
207

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

    
210
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
211
      {
212
      mScreenMin = width<height ? width:height;
213

    
214
      float factor1 = 0.22f*mScreenMin/OBJ_SIZE;
215
      float factor2 = 0.80f*factor1;
216
      mScale1.set(factor1,factor1,factor1);
217
      mScale2.set(factor2,factor2,factor2);
218
      mCenter.set((float)OBJ_SIZE/2, (float)OBJ_SIZE/2, -(float)OBJ_SIZE/2 );
219
      mMove1.set( (width -factor1*OBJ_SIZE)/2 ,(height-factor1*OBJ_SIZE)/2 ,0);
220
      mMove2.set( (width -factor2*OBJ_SIZE)/2 ,(height-factor2*OBJ_SIZE)/2 ,0);
221
      mScreen.resize(width, height);
222
      }
223

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

    
226
    void setRange(int object, int range)
227
      {
228
      if( object>=0 && object<NUM_OBJECTS )
229
        {
230
        mBlurVector[object].set(range / 2);
231
        }
232
      }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
    boolean[] getChecked()
237
     {
238
     return mBlurStatus;
239
     }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

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

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