Project

General

Profile

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

examples / src / main / java / org / distorted / examples / triblur / TriblurRenderer.java @ 698ad0a8

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.EffectType;
29
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
import org.distorted.library.effect.PostprocessEffectGlow;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedNode;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.mesh.MeshCubes;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

    
45
import java.io.IOException;
46
import java.io.InputStream;
47

    
48
import javax.microedition.khronos.egl.EGLConfig;
49
import javax.microedition.khronos.opengles.GL10;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
class TriblurRenderer implements GLSurfaceView.Renderer
54
{
55
    private static final int NUM = 6; // 6 ints (x,y,z,R,G,B) each describe 1 object below
56

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

    
64
    private static final int NUM_OBJECTS = OBJECTS.length/NUM;
65
    private static final int OBJ_SIZE    = 100;
66

    
67
    private GLSurfaceView mView;
68
    private DistortedTexture mTex;
69
    private DistortedNode[] mNode;
70
    private Static1D[]  mEffectVector;
71
    private DistortedScreen mScreen;
72
    private PostprocessEffectBlur[] mBlur;
73
    private PostprocessEffectGlow[] mGlow;
74
    private int[] mEffectStatus;
75
    private Static3D mMove1, mMove2, mScale1, mScale2, mCenter;
76

    
77
    Static4D mQuat1, mQuat2;
78
    int mScreenMin;
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
    TriblurRenderer(GLSurfaceView v)
83
      {
84
      mView = v;
85

    
86
      MeshCubes mesh = new MeshCubes(1,1,1);
87
      mesh.setStretch(OBJ_SIZE,OBJ_SIZE,OBJ_SIZE);
88

    
89
      mTex = new DistortedTexture();
90

    
91
      mQuat1 = new Static4D(0,0,0,1);  // unity
92
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
93

    
94
      mScreen = new DistortedScreen();
95
      mScreen.showFPS();
96

    
97
      mEffectStatus = new int[NUM_OBJECTS];
98
      mEffectVector = new Static1D[NUM_OBJECTS];
99
      mNode         = new DistortedNode[NUM_OBJECTS];
100
      mBlur         = new PostprocessEffectBlur[NUM_OBJECTS];
101
      mGlow         = new PostprocessEffectGlow[NUM_OBJECTS];
102

    
103
      FragmentEffectChroma[] chroma= new FragmentEffectChroma[NUM_OBJECTS];
104
      Static3D[] chromaVector      = new Static3D[NUM_OBJECTS];
105
      Static3D[] moveVector        = new Static3D[NUM_OBJECTS];
106

    
107
      DistortedEffects[] effects= new DistortedEffects[NUM_OBJECTS];
108

    
109
      mMove1  = new Static3D(0,0,0);
110
      mMove2  = new Static3D(0,0,0);
111
      mScale1 = new Static3D(1,1,1);
112
      mScale2 = new Static3D(1.5f,1.5f,1.5f);
113
      mCenter = new Static3D(0,0,0);
114

    
115
      MatrixEffectMove moveEffect1 = new MatrixEffectMove(mMove1);
116
      MatrixEffectMove moveEffect2 = new MatrixEffectMove(mMove2);
117
      MatrixEffectScale scaleEffect1 = new MatrixEffectScale(mScale1);
118
      MatrixEffectScale scaleEffect2 = new MatrixEffectScale(mScale2);
119
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, mCenter);
120
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, mCenter);
121

    
122
      for(int i=0; i<NUM_OBJECTS; i++)
123
        {
124
        moveVector[i]    = new Static3D(OBJECTS[NUM*i  ], OBJECTS[NUM*i+1], OBJECTS[NUM*i+2]);
125
        chromaVector[i]  = new Static3D(OBJECTS[NUM*i+3], OBJECTS[NUM*i+4], OBJECTS[NUM*i+5]);
126
        mEffectVector[i] = new Static1D(10);
127
        mBlur[i]         = new PostprocessEffectBlur(mEffectVector[i]);
128
        mGlow[i]         = new PostprocessEffectGlow(mEffectVector[i], new Static4D(1.0f,1.0f,1.0f,0.5f) );
129
        chroma[i]        = new FragmentEffectChroma( new Static1D(0.3f), chromaVector[i]);
130
        effects[i]       = new DistortedEffects();
131
        effects[i].apply(mBlur[i]);
132
        effects[i].apply(chroma[i]);
133

    
134
        effects[i].apply(new MatrixEffectMove(moveVector[i]));
135
        effects[i].apply(quatEffect2);
136
        effects[i].apply(quatEffect1);
137
        effects[i].apply( (i==0||i==NUM_OBJECTS-1) ? scaleEffect1 : scaleEffect2 );
138
        effects[i].apply( (i==0||i==NUM_OBJECTS-1) ?  moveEffect1 :  moveEffect2 );
139

    
140
        mEffectStatus[i] = 1;
141
        mNode[i] = new DistortedNode(mTex, effects[i], mesh );
142
        mScreen.attach(mNode[i]);
143
        }
144

    
145
      setQuality(EffectQuality.HIGH);
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    void setQuality(EffectQuality quality)
151
      {
152
      for(int i=0; i<NUM_OBJECTS; i++)
153
        {
154
        mBlur[i].setQuality(quality);
155
        }
156
      }
157

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

    
160
    void setRenderModeToOIT(boolean oit)
161
      {
162
      mScreen.setOrderIndependentTransparency(oit);
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
    void setBackground(float r, float g, float b, float a)
168
      {
169
      mScreen.glClearColor(r,g,b,a);
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
    public void onDrawFrame(GL10 glUnused) 
175
      {
176
      mScreen.render(System.currentTimeMillis());
177
      }
178

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

    
199
      mTex.setTexture(bitmap);
200

    
201
      PostprocessEffectBlur.enable();
202
      FragmentEffectChroma.enable();
203

    
204
      try
205
        {
206
        DistortedLibrary.onCreate(mView.getContext());
207
        }
208
      catch(Exception ex)
209
        {
210
        android.util.Log.e("Triblur", ex.getMessage() );
211
        }
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
217
      {
218
      mScreenMin = width<height ? width:height;
219

    
220
      float factor1 = 0.22f*mScreenMin/OBJ_SIZE;
221
      float factor2 = 0.80f*factor1;
222
      mScale1.set(factor1,factor1,factor1);
223
      mScale2.set(factor2,factor2,factor2);
224
      mCenter.set((float)OBJ_SIZE/2, (float)OBJ_SIZE/2, (float)OBJ_SIZE/2 );
225
      mMove1.set( (width -factor1*OBJ_SIZE)/2 ,(height-factor1*OBJ_SIZE)/2 , -factor1*OBJ_SIZE);
226
      mMove2.set( (width -factor2*OBJ_SIZE)/2 ,(height-factor2*OBJ_SIZE)/2 , -factor2*OBJ_SIZE);
227
      mScreen.resize(width, height);
228
      }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
    void setRange(int object, int range)
233
      {
234
      if( object>=0 && object<NUM_OBJECTS )
235
        {
236
        mEffectVector[object].set(range / 2);
237
        }
238
      }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
    int[] getEffects()
243
     {
244
     return mEffectStatus;
245
     }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

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

    
258
          if( effect==1 ) mNode[object].getEffects().apply(mBlur[object]);
259
          if( effect==2 ) mNode[object].getEffects().apply(mGlow[object]);
260
          }
261
        }
262
      else
263
        {
264
        android.util.Log.e("renderer", "Error, number: "+object+" effect: "+effect );
265
        }
266

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