Project

General

Profile

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

examples / src / main / java / org / distorted / examples / triblur / TriblurRenderer.java @ cef5d326

1 80f37d1b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 80f37d1b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 80f37d1b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 80f37d1b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 80f37d1b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 5b829159 Leszek Koltunski
import org.distorted.library.effect.Effect;
28 80f37d1b Leszek Koltunski
import org.distorted.library.effect.EffectQuality;
29 c40df162 Leszek Koltunski
import org.distorted.library.effect.EffectType;
30 80f37d1b Leszek Koltunski
import org.distorted.library.effect.FragmentEffectChroma;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.PostprocessEffectBlur;
35 1b80a6e2 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffectBorder;
36 c40df162 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffectGlow;
37 513b2e9c Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
38 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
39 80f37d1b Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
40
import org.distorted.library.main.DistortedNode;
41
import org.distorted.library.main.DistortedScreen;
42
import org.distorted.library.main.DistortedTexture;
43 cef5d326 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
44 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
45 80f37d1b Leszek Koltunski
import org.distorted.library.type.Static1D;
46 678c391d Leszek Koltunski
import org.distorted.library.type.Static2D;
47 80f37d1b Leszek Koltunski
import org.distorted.library.type.Static3D;
48
import org.distorted.library.type.Static4D;
49
50
import java.io.IOException;
51
import java.io.InputStream;
52
53
import javax.microedition.khronos.egl.EGLConfig;
54
import javax.microedition.khronos.opengles.GL10;
55
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 061449ed Leszek Koltunski
class TriblurRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
59 80f37d1b Leszek Koltunski
{
60 caf7ce0a Leszek Koltunski
    private static final int[][] OBJECTS =
61 80f37d1b Leszek Koltunski
        {
62 caf7ce0a Leszek Koltunski
            {-130, 0, 0,   255,   0,  0},  // x,y,z, R,G,B
63
            {   0, 0, 0,   255, 255,  0},  //
64
            {+130, 0, 0,     0, 255,  0},  //
65 80f37d1b Leszek Koltunski
        };
66
67 caf7ce0a Leszek Koltunski
    private static final int NUM_OBJECTS = OBJECTS.length;
68 80f37d1b Leszek Koltunski
    private static final int OBJ_SIZE    = 100;
69
70 1b80a6e2 Leszek Koltunski
    private final GLSurfaceView mView;
71
    private final DistortedTexture mTex;
72
    private final DistortedNode[] mNode;
73
    private final Static2D[] mEffectHaloRadius;
74
    private final Static1D[] mEffectHalo;
75
    private final DistortedScreen mScreen;
76
    private final PostprocessEffectBlur[] mBlur;
77
    private final PostprocessEffectGlow[] mGlow;
78
    private final PostprocessEffectBorder[] mBord;
79
    private final int[] mEffectStatus;
80
    private final Static3D mScale1, mScale2;
81 80f37d1b Leszek Koltunski
82
    Static4D mQuat1, mQuat2;
83
    int mScreenMin;
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86 cef5d326 Leszek Koltunski
// MeshBase.setUseCenters() and mesh.setComponentCenter(0,x,y,z) with a vector (x,y,z) that points
87
// directly behind the cube to see BORDER work.
88 80f37d1b Leszek Koltunski
89
    TriblurRenderer(GLSurfaceView v)
90
      {
91
      mView = v;
92
93
      MeshCubes mesh = new MeshCubes(1,1,1);
94
95 687263cc Leszek Koltunski
      mTex = new DistortedTexture();
96 80f37d1b Leszek Koltunski
97
      mQuat1 = new Static4D(0,0,0,1);  // unity
98
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
99
100
      mScreen = new DistortedScreen();
101 e72c53e5 Leszek Koltunski
      mScreen.showFPS();
102 80f37d1b Leszek Koltunski
103 678c391d Leszek Koltunski
      mEffectStatus    = new int[NUM_OBJECTS];
104
      mEffectHaloRadius= new Static2D[NUM_OBJECTS];
105 1b80a6e2 Leszek Koltunski
      mEffectHalo      = new Static1D[NUM_OBJECTS];
106 678c391d Leszek Koltunski
      mNode            = new DistortedNode[NUM_OBJECTS];
107
      mBlur            = new PostprocessEffectBlur[NUM_OBJECTS];
108
      mGlow            = new PostprocessEffectGlow[NUM_OBJECTS];
109 1b80a6e2 Leszek Koltunski
      mBord            = new PostprocessEffectBorder[NUM_OBJECTS];
110 d7413405 Leszek Koltunski
111
      FragmentEffectChroma[] chroma= new FragmentEffectChroma[NUM_OBJECTS];
112
      Static3D[] chromaVector      = new Static3D[NUM_OBJECTS];
113
      Static3D[] moveVector        = new Static3D[NUM_OBJECTS];
114 80f37d1b Leszek Koltunski
115
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
116
117 371c43ca Leszek Koltunski
      mScale1 = new Static3D(1,1,1);
118
      mScale2 = new Static3D(1.5f,1.5f,1.5f);
119 16b22aab Leszek Koltunski
      Static3D center = new Static3D(0,0,0);
120 80f37d1b Leszek Koltunski
121 f1f50ad1 Leszek Koltunski
      VertexEffectScale scaleEffectV = new VertexEffectScale(OBJ_SIZE);
122 371c43ca Leszek Koltunski
      MatrixEffectScale scaleEffect1 = new MatrixEffectScale(mScale1);
123
      MatrixEffectScale scaleEffect2 = new MatrixEffectScale(mScale2);
124 16b22aab Leszek Koltunski
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
125
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
126 80f37d1b Leszek Koltunski
127
      for(int i=0; i<NUM_OBJECTS; i++)
128
        {
129 caf7ce0a Leszek Koltunski
        moveVector[i]       = new Static3D(OBJECTS[i][0], OBJECTS[i][1], OBJECTS[i][2]);
130
        chromaVector[i]     = new Static3D(OBJECTS[i][3], OBJECTS[i][4], OBJECTS[i][5]);
131 678c391d Leszek Koltunski
        mEffectHaloRadius[i]= new Static2D(10,10);
132 1b80a6e2 Leszek Koltunski
        mEffectHalo[i]      = new Static1D(10);
133 678c391d Leszek Koltunski
        mBlur[i]            = new PostprocessEffectBlur(mEffectHaloRadius[i] );
134
        mGlow[i]            = new PostprocessEffectGlow(mEffectHaloRadius[i], new Static4D(1.0f,1.0f,1.0f,0.5f) );
135 caf7ce0a Leszek Koltunski
        mBord[i]            = new PostprocessEffectBorder(mEffectHalo[i], new Static4D(0.0f,0.0f,0.0f,0.8f) );
136 678c391d Leszek Koltunski
        chroma[i]           = new FragmentEffectChroma( new Static1D(0.3f), chromaVector[i]);
137
        effects[i]          = new DistortedEffects();
138 3f70dd29 Leszek Koltunski
        effects[i].apply(chroma[i]);
139 f1f50ad1 Leszek Koltunski
        effects[i].apply(scaleEffectV);
140 d7413405 Leszek Koltunski
        effects[i].apply(new MatrixEffectMove(moveVector[i]));
141 0ba5de00 Leszek Koltunski
        effects[i].apply(quatEffect2);
142
        effects[i].apply(quatEffect1);
143
        effects[i].apply( (i==0||i==NUM_OBJECTS-1) ? scaleEffect1 : scaleEffect2 );
144 80f37d1b Leszek Koltunski
145 1b80a6e2 Leszek Koltunski
        mEffectStatus[i] = 0;
146 371c43ca Leszek Koltunski
        mNode[i] = new DistortedNode(mTex, effects[i], mesh );
147 80f37d1b Leszek Koltunski
        mScreen.attach(mNode[i]);
148
        }
149
150
      setQuality(EffectQuality.HIGH);
151
      }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
155
    void setQuality(EffectQuality quality)
156
      {
157
      for(int i=0; i<NUM_OBJECTS; i++)
158
        {
159
        mBlur[i].setQuality(quality);
160 5b829159 Leszek Koltunski
        mGlow[i].setQuality(quality);
161
        mBord[i].setQuality(quality);
162 80f37d1b Leszek Koltunski
        }
163
      }
164
165 a935e9db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
    void setRenderModeToOIT(boolean oit)
168
      {
169
      mScreen.setOrderIndependentTransparency(oit);
170
      }
171
172 f1b8b412 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174
    void setBackground(float r, float g, float b, float a)
175
      {
176
      mScreen.glClearColor(r,g,b,a);
177
      }
178
179 80f37d1b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181
    public void onDrawFrame(GL10 glUnused) 
182
      {
183
      mScreen.render(System.currentTimeMillis());
184
      }
185
186 061449ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
189
      {
190
      mScreenMin = Math.min(width, height);
191
192
      float factor1 = 0.20f*mScreenMin/OBJ_SIZE;
193
      float factor2 = 0.75f*factor1;
194
      mScale1.set(factor1,factor1,factor1);
195
      mScale2.set(factor2,factor2,factor2);
196
      mScreen.resize(width, height);
197
      }
198
199 80f37d1b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
200
    
201
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
202
      {
203
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
204
      Bitmap bitmap;
205
        
206
      try 
207
        {
208
        bitmap = BitmapFactory.decodeStream(is);
209
        }
210
      finally 
211
        {
212
        try 
213
          {
214
          is.close();
215
          }
216
        catch(IOException e) { }
217
        }  
218
219
      mTex.setTexture(bitmap);
220
221 513b2e9c Leszek Koltunski
      VertexEffectScale.enable();
222 80f37d1b Leszek Koltunski
      FragmentEffectChroma.enable();
223 5b829159 Leszek Koltunski
      Effect.enableEffects(EffectType.POSTPROCESS);
224
225 7c72e798 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
226 80f37d1b Leszek Koltunski
      }
227
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
230 061449ed Leszek Koltunski
    public void distortedException(Exception ex)
231 80f37d1b Leszek Koltunski
      {
232 061449ed Leszek Koltunski
      android.util.Log.e("Triblur", ex.getMessage() );
233 80f37d1b Leszek Koltunski
      }
234
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237
    void setRange(int object, int range)
238
      {
239
      if( object>=0 && object<NUM_OBJECTS )
240
        {
241 678c391d Leszek Koltunski
        mEffectHaloRadius[object].set(range*0.5f,range*0.5f);
242 1b80a6e2 Leszek Koltunski
        mEffectHalo[object].set(range*0.5f);
243 80f37d1b Leszek Koltunski
        }
244
      }
245
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
248 c40df162 Leszek Koltunski
    int[] getEffects()
249 80f37d1b Leszek Koltunski
     {
250 c40df162 Leszek Koltunski
     return mEffectStatus;
251 80f37d1b Leszek Koltunski
     }
252
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254
255 c40df162 Leszek Koltunski
    void setEffects(int object, int effect)
256 80f37d1b Leszek Koltunski
      {
257
      if( object>=0 && object<NUM_OBJECTS )
258
        {
259 c40df162 Leszek Koltunski
        if( mEffectStatus[object] != effect )
260 80f37d1b Leszek Koltunski
          {
261 c40df162 Leszek Koltunski
          mEffectStatus[object] = effect;
262
          mNode[object].getEffects().abortByType(EffectType.POSTPROCESS);
263
264
          if( effect==1 ) mNode[object].getEffects().apply(mBlur[object]);
265
          if( effect==2 ) mNode[object].getEffects().apply(mGlow[object]);
266 1b80a6e2 Leszek Koltunski
          if( effect==3 ) mNode[object].getEffects().apply(mBord[object]);
267 80f37d1b Leszek Koltunski
          }
268
        }
269
      else
270
        {
271 c40df162 Leszek Koltunski
        android.util.Log.e("renderer", "Error, number: "+object+" effect: "+effect );
272 80f37d1b Leszek Koltunski
        }
273
      }
274
}