Project

General

Profile

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

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

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.Effect;
28
import org.distorted.library.effect.EffectQuality;
29
import org.distorted.library.effect.EffectType;
30
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
import org.distorted.library.effect.PostprocessEffectBorder;
36
import org.distorted.library.effect.PostprocessEffectGlow;
37
import org.distorted.library.effect.VertexEffectScale;
38
import org.distorted.library.main.DistortedLibrary;
39
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
import org.distorted.library.mesh.MeshCubes;
44
import org.distorted.library.type.Static1D;
45
import org.distorted.library.type.Static2D;
46
import org.distorted.library.type.Static3D;
47
import org.distorted.library.type.Static4D;
48

    
49
import java.io.IOException;
50
import java.io.InputStream;
51

    
52
import javax.microedition.khronos.egl.EGLConfig;
53
import javax.microedition.khronos.opengles.GL10;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
class TriblurRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
58
{
59
    private static final int[][] OBJECTS =
60
        {
61
            {-130, 0, 0,   255,   0,  0},  // x,y,z, R,G,B
62
            {   0, 0, 0,   255, 255,  0},  //
63
            {+130, 0, 0,     0, 255,  0},  //
64
        };
65

    
66
    private static final int NUM_OBJECTS = OBJECTS.length;
67
    private static final int OBJ_SIZE    = 100;
68

    
69
    private final GLSurfaceView mView;
70
    private final DistortedTexture mTex;
71
    private final DistortedNode[] mNode;
72
    private final Static2D[] mEffectHaloRadius;
73
    private final Static1D[] mEffectHalo;
74
    private final DistortedScreen mScreen;
75
    private final PostprocessEffectBlur[] mBlur;
76
    private final PostprocessEffectGlow[] mGlow;
77
    private final PostprocessEffectBorder[] mBord;
78
    private final int[] mEffectStatus;
79
    private final Static3D mScale1, mScale2;
80

    
81
    Static4D mQuat1, mQuat2;
82
    int mScreenMin;
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
    TriblurRenderer(GLSurfaceView v)
87
      {
88
      mView = v;
89

    
90
      MeshCubes mesh = new MeshCubes(1,1,1);
91

    
92
      mTex = new DistortedTexture();
93

    
94
      mQuat1 = new Static4D(0,0,0,1);  // unity
95
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
96

    
97
      mScreen = new DistortedScreen();
98
      mScreen.showFPS();
99

    
100
      mEffectStatus    = new int[NUM_OBJECTS];
101
      mEffectHaloRadius= new Static2D[NUM_OBJECTS];
102
      mEffectHalo      = new Static1D[NUM_OBJECTS];
103
      mNode            = new DistortedNode[NUM_OBJECTS];
104
      mBlur            = new PostprocessEffectBlur[NUM_OBJECTS];
105
      mGlow            = new PostprocessEffectGlow[NUM_OBJECTS];
106
      mBord            = new PostprocessEffectBorder[NUM_OBJECTS];
107

    
108
      FragmentEffectChroma[] chroma= new FragmentEffectChroma[NUM_OBJECTS];
109
      Static3D[] chromaVector      = new Static3D[NUM_OBJECTS];
110
      Static3D[] moveVector        = new Static3D[NUM_OBJECTS];
111

    
112
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
113

    
114
      mScale1 = new Static3D(1,1,1);
115
      mScale2 = new Static3D(1.5f,1.5f,1.5f);
116
      Static3D center = new Static3D(0,0,0);
117

    
118
      VertexEffectScale scaleEffectV = new VertexEffectScale(OBJ_SIZE);
119
      MatrixEffectScale scaleEffect1 = new MatrixEffectScale(mScale1);
120
      MatrixEffectScale scaleEffect2 = new MatrixEffectScale(mScale2);
121
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
122
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
123

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

    
142
        mEffectStatus[i] = 0;
143
        mNode[i] = new DistortedNode(mTex, effects[i], mesh );
144
        mScreen.attach(mNode[i]);
145
        }
146

    
147
      setQuality(EffectQuality.HIGH);
148
      }
149

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

    
152
    void setQuality(EffectQuality quality)
153
      {
154
      for(int i=0; i<NUM_OBJECTS; i++)
155
        {
156
        mBlur[i].setQuality(quality);
157
        mGlow[i].setQuality(quality);
158
        mBord[i].setQuality(quality);
159
        }
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
    void setRenderModeToOIT(boolean oit)
165
      {
166
      mScreen.setOrderIndependentTransparency(oit);
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
    void setBackground(float r, float g, float b, float a)
172
      {
173
      mScreen.glClearColor(r,g,b,a);
174
      }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
    public void onDrawFrame(GL10 glUnused) 
179
      {
180
      mScreen.render(System.currentTimeMillis());
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
186
      {
187
      mScreenMin = Math.min(width, height);
188

    
189
      float factor1 = 0.20f*mScreenMin/OBJ_SIZE;
190
      float factor2 = 0.75f*factor1;
191
      mScale1.set(factor1,factor1,factor1);
192
      mScale2.set(factor2,factor2,factor2);
193
      mScreen.resize(width, height);
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
    
198
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
199
      {
200
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
201
      Bitmap bitmap;
202
        
203
      try 
204
        {
205
        bitmap = BitmapFactory.decodeStream(is);
206
        }
207
      finally 
208
        {
209
        try 
210
          {
211
          is.close();
212
          }
213
        catch(IOException e) { }
214
        }  
215

    
216
      mTex.setTexture(bitmap);
217

    
218
      VertexEffectScale.enable();
219
      FragmentEffectChroma.enable();
220
      Effect.enableEffects(EffectType.POSTPROCESS);
221

    
222
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
223
      }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
    public void distortedException(Exception ex)
228
      {
229
      android.util.Log.e("Triblur", ex.getMessage() );
230
      }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

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

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

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

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