Project

General

Profile

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

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

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