Project

General

Profile

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

examples / src / main / java / org / distorted / examples / triblur / TriblurRenderer.java @ 625c67de

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.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29

    
30
import org.distorted.examples.R;
31
import org.distorted.library.effect.Effect;
32
import org.distorted.library.effect.EffectQuality;
33
import org.distorted.library.effect.EffectType;
34
import org.distorted.library.effect.FragmentEffectChroma;
35
import org.distorted.library.effect.MatrixEffectMove;
36
import org.distorted.library.effect.MatrixEffectQuaternion;
37
import org.distorted.library.effect.MatrixEffectScale;
38
import org.distorted.library.effect.PostprocessEffectBlur;
39
import org.distorted.library.effect.PostprocessEffectBorder;
40
import org.distorted.library.effect.PostprocessEffectGlow;
41
import org.distorted.library.effect.VertexEffectScale;
42
import org.distorted.library.main.DistortedLibrary;
43
import org.distorted.library.main.DistortedEffects;
44
import org.distorted.library.main.DistortedNode;
45
import org.distorted.library.main.DistortedScreen;
46
import org.distorted.library.main.DistortedTexture;
47
import org.distorted.library.mesh.MeshCubes;
48
import org.distorted.library.type.Static1D;
49
import org.distorted.library.type.Static2D;
50
import org.distorted.library.type.Static3D;
51
import org.distorted.library.type.Static4D;
52

    
53
import java.io.IOException;
54
import java.io.InputStream;
55

    
56
import javax.microedition.khronos.egl.EGLConfig;
57
import javax.microedition.khronos.opengles.GL10;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
class TriblurRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
62
{
63
    private static final int[][] OBJECTS =
64
        {
65
            {-130, 0, 0,   255,   0,  0},  // x,y,z, R,G,B
66
            {   0, 0, 0,   255, 255,  0},  //
67
            {+130, 0, 0,     0, 255,  0},  //
68
        };
69

    
70
    private static final int NUM_OBJECTS = OBJECTS.length;
71
    private static final int OBJ_SIZE    = 100;
72

    
73
    private final GLSurfaceView mView;
74
    private final Resources mResources;
75
    private final DistortedTexture mTex;
76
    private final DistortedNode[] mNode;
77
    private final Static2D[] mEffectHaloRadius;
78
    private final Static1D[] mEffectHalo;
79
    private final DistortedScreen mScreen;
80
    private final PostprocessEffectBlur[] mBlur;
81
    private final PostprocessEffectGlow[] mGlow;
82
    private final PostprocessEffectBorder[] mBord;
83
    private final int[] mEffectStatus;
84
    private final Static3D mScale1, mScale2;
85

    
86
    Static4D mQuat1, mQuat2;
87
    int mScreenMin;
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
// MeshBase.setUseCenters() and mesh.setComponentCenter(0,x,y,z) with a vector (x,y,z) that points
91
// directly behind the cube to see BORDER work.
92

    
93
    TriblurRenderer(GLSurfaceView v)
94
      {
95
      mView = v;
96
      mResources = v.getResources();
97

    
98
      MeshCubes mesh = new MeshCubes(1,1,1);
99

    
100
      mTex = new DistortedTexture();
101

    
102
      mQuat1 = new Static4D(0,0,0,1);  // unity
103
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
104

    
105
      mScreen = new DistortedScreen();
106
      mScreen.showFPS();
107

    
108
      mEffectStatus    = new int[NUM_OBJECTS];
109
      mEffectHaloRadius= new Static2D[NUM_OBJECTS];
110
      mEffectHalo      = new Static1D[NUM_OBJECTS];
111
      mNode            = new DistortedNode[NUM_OBJECTS];
112
      mBlur            = new PostprocessEffectBlur[NUM_OBJECTS];
113
      mGlow            = new PostprocessEffectGlow[NUM_OBJECTS];
114
      mBord            = new PostprocessEffectBorder[NUM_OBJECTS];
115

    
116
      FragmentEffectChroma[] chroma= new FragmentEffectChroma[NUM_OBJECTS];
117
      Static3D[] chromaVector      = new Static3D[NUM_OBJECTS];
118
      Static3D[] moveVector        = new Static3D[NUM_OBJECTS];
119

    
120
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
121

    
122
      mScale1 = new Static3D(1,1,1);
123
      mScale2 = new Static3D(1.5f,1.5f,1.5f);
124
      Static3D center = new Static3D(0,0,0);
125

    
126
      VertexEffectScale scaleEffectV = new VertexEffectScale(OBJ_SIZE);
127
      MatrixEffectScale scaleEffect1 = new MatrixEffectScale(mScale1);
128
      MatrixEffectScale scaleEffect2 = new MatrixEffectScale(mScale2);
129
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
130
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
131

    
132
      for(int i=0; i<NUM_OBJECTS; i++)
133
        {
134
        moveVector[i]       = new Static3D(OBJECTS[i][0], OBJECTS[i][1], OBJECTS[i][2]);
135
        chromaVector[i]     = new Static3D(OBJECTS[i][3], OBJECTS[i][4], OBJECTS[i][5]);
136
        mEffectHaloRadius[i]= new Static2D(10,10);
137
        mEffectHalo[i]      = new Static1D(10);
138
        mBlur[i]            = new PostprocessEffectBlur(mEffectHaloRadius[i] );
139
        mGlow[i]            = new PostprocessEffectGlow(mEffectHaloRadius[i], new Static4D(1.0f,1.0f,1.0f,0.5f) );
140
        mBord[i]            = new PostprocessEffectBorder(mEffectHalo[i], new Static4D(0.0f,0.0f,0.0f,0.8f) );
141
        chroma[i]           = new FragmentEffectChroma( new Static1D(0.3f), chromaVector[i]);
142
        effects[i]          = new DistortedEffects();
143
        effects[i].apply(chroma[i]);
144
        effects[i].apply(scaleEffectV);
145
        effects[i].apply(new MatrixEffectMove(moveVector[i]));
146
        effects[i].apply(quatEffect2);
147
        effects[i].apply(quatEffect1);
148
        effects[i].apply( (i==0||i==NUM_OBJECTS-1) ? scaleEffect1 : scaleEffect2 );
149

    
150
        mEffectStatus[i] = 0;
151
        mNode[i] = new DistortedNode(mTex, effects[i], mesh );
152
        mScreen.attach(mNode[i]);
153
        }
154

    
155
      setQuality(EffectQuality.HIGH);
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
    void setQuality(EffectQuality quality)
161
      {
162
      for(int i=0; i<NUM_OBJECTS; i++)
163
        {
164
        mBlur[i].setQuality(quality);
165
        mGlow[i].setQuality(quality);
166
        mBord[i].setQuality(quality);
167
        }
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
    void setRenderModeToOIT(boolean oit)
173
      {
174
      mScreen.setOrderIndependentTransparency(oit);
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
    void setBackground(float r, float g, float b, float a)
180
      {
181
      mScreen.glClearColor(r,g,b,a);
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
    public void onDrawFrame(GL10 glUnused) 
187
      {
188
      mScreen.render(System.currentTimeMillis());
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
194
      {
195
      mScreenMin = Math.min(width, height);
196

    
197
      float factor1 = 0.20f*mScreenMin/OBJ_SIZE;
198
      float factor2 = 0.75f*factor1;
199
      mScale1.set(factor1,factor1,factor1);
200
      mScale2.set(factor2,factor2,factor2);
201
      mScreen.resize(width, height);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
    
206
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
207
      {
208
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
209
      Bitmap bitmap;
210
        
211
      try 
212
        {
213
        bitmap = BitmapFactory.decodeStream(is);
214
        }
215
      finally 
216
        {
217
        try 
218
          {
219
          is.close();
220
          }
221
        catch(IOException e) { }
222
        }  
223

    
224
      mTex.setTexture(bitmap);
225

    
226
      VertexEffectScale.enable();
227
      FragmentEffectChroma.enable();
228
      Effect.enableEffects(EffectType.POSTPROCESS);
229

    
230
      DistortedLibrary.onSurfaceCreated(this);
231
      }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
    void setRange(int object, int range)
236
      {
237
      if( object>=0 && object<NUM_OBJECTS )
238
        {
239
        mEffectHaloRadius[object].set(range*0.5f,range*0.5f);
240
        mEffectHalo[object].set(range*0.5f);
241
        }
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    int[] getEffects()
247
     {
248
     return mEffectStatus;
249
     }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
    void setEffects(int object, int effect)
254
      {
255
      if( object>=0 && object<NUM_OBJECTS )
256
        {
257
        if( mEffectStatus[object] != effect )
258
          {
259
          mEffectStatus[object] = effect;
260
          mNode[object].getEffects().abortByType(EffectType.POSTPROCESS);
261

    
262
          if( effect==1 ) mNode[object].getEffects().apply(mBlur[object]);
263
          if( effect==2 ) mNode[object].getEffects().apply(mGlow[object]);
264
          if( effect==3 ) mNode[object].getEffects().apply(mBord[object]);
265
          }
266
        }
267
      else
268
        {
269
        android.util.Log.e("renderer", "Error, number: "+object+" effect: "+effect );
270
        }
271
      }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
    public void distortedException(Exception ex)
276
      {
277
      android.util.Log.e("Triblur", ex.getMessage() );
278
      }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
    public InputStream localFile(int fileID)
283
      {
284
      return mResources.openRawResource(fileID);
285
      }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
    public void logMessage(String message)
290
      {
291
      android.util.Log.e("Triblur", message );
292
      }
293
}
(2-2/3)