Project

General

Profile

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

examples / src / main / java / org / distorted / examples / postprocesstree / PostprocessTreeRenderer.java @ dc10a48d

1 efc280af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 efc280af Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 efc280af 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 efc280af 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 efc280af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.postprocesstree;
21
22 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 efc280af Leszek Koltunski
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.FragmentEffectChroma;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectRotate;
34
import org.distorted.library.effect.MatrixEffectScale;
35
import org.distorted.library.effect.PostprocessEffectBlur;
36 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
37 efc280af 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 698ad0a8 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
42 efc280af Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
43 678c391d Leszek Koltunski
import org.distorted.library.type.Dynamic2D;
44 efc280af Leszek Koltunski
import org.distorted.library.type.Static1D;
45 678c391d Leszek Koltunski
import org.distorted.library.type.Static2D;
46 efc280af Leszek Koltunski
import org.distorted.library.type.Static3D;
47
48
import java.io.IOException;
49
import java.io.InputStream;
50
51
import javax.microedition.khronos.egl.EGLConfig;
52
import javax.microedition.khronos.opengles.GL10;
53
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 dc10a48d Leszek Koltunski
class PostprocessTreeRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
57 efc280af Leszek Koltunski
{
58
   private static final int LEAF_SIZE = 100;
59
   private static final int NUM_LEAVES  = 8;
60
   
61 dc10a48d Leszek Koltunski
   private final GLSurfaceView mView;
62
   private final Resources mResources;
63
   private final DistortedTexture mLeaf;
64
   private final DistortedScreen mScreen;
65
   private final Static3D mScale;
66
   private final Static2D mHaloRadius;
67 efc280af Leszek Koltunski
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70
   PostprocessTreeRenderer(GLSurfaceView v)
71
      {     
72
      mView = v;
73 dc10a48d Leszek Koltunski
      mResources = v.getResources();
74 efc280af Leszek Koltunski
75 0a098d35 Leszek Koltunski
      final int OUTER = 9;
76
      final int INNER = 4;
77 efc280af Leszek Koltunski
78 678c391d Leszek Koltunski
      mHaloRadius = new Static2D(30,5);
79
      Dynamic2D haloRadiusDyn = new Dynamic2D();
80
      haloRadiusDyn.add(mHaloRadius);
81
      PostprocessEffectBlur blurEffect = new PostprocessEffectBlur(haloRadiusDyn);
82 efc280af Leszek Koltunski
83 687263cc Leszek Koltunski
      mLeaf = new DistortedTexture();
84 0a098d35 Leszek Koltunski
      mScale= new Static3D(1,1,1);
85 5a683295 Leszek Koltunski
      MeshQuad mesh = new MeshQuad();
86 efc280af Leszek Koltunski
87 698ad0a8 Leszek Koltunski
      DistortedEffects rootEffects  = new DistortedEffects();
88
      DistortedEffects innerEffects = new DistortedEffects();
89 efc280af Leszek Koltunski
      DistortedEffects[] innerLeafEffects= new DistortedEffects[NUM_LEAVES];
90
      DistortedEffects[] outerLeafEffects= new DistortedEffects[NUM_LEAVES];
91
92 5a683295 Leszek Koltunski
      DistortedNode root = new DistortedNode(new DistortedTexture(), rootEffects, mesh);
93
      root.resizeFBO(OUTER*LEAF_SIZE,OUTER*LEAF_SIZE);
94 efc280af Leszek Koltunski
95 0a098d35 Leszek Koltunski
      rootEffects.apply(new MatrixEffectScale(mScale));
96 fba88037 Leszek Koltunski
      rootEffects.apply(blurEffect);
97 0a098d35 Leszek Koltunski
98
      Dynamic1D rotate = new Dynamic1D(5000,0.0f);
99
      rotate.setMode(Dynamic1D.MODE_JUMP);
100
      rotate.add(new Static1D(  0));
101
      rotate.add(new Static1D(360));
102 efc280af Leszek Koltunski
103 16b22aab Leszek Koltunski
      Static3D center = new Static3D(0,0,0);
104 efc280af Leszek Koltunski
      Static3D axis   = new Static3D(0,0,1);
105 678c391d Leszek Koltunski
      Static3D innerMoveVector = new Static3D( (1-INNER)*LEAF_SIZE*0.5f, 0, 0);
106
      Static3D outerMoveVector = new Static3D( (4-OUTER)*LEAF_SIZE*0.5f, 0, 0);
107 efc280af Leszek Koltunski
108
      for(int j=0; j<NUM_LEAVES; j++)
109
        {
110 698ad0a8 Leszek Koltunski
        outerLeafEffects[j] = new DistortedEffects();
111 5a683295 Leszek Koltunski
        outerLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
112
        outerLeafEffects[j].apply( new MatrixEffectMove(outerMoveVector));
113 0ba5de00 Leszek Koltunski
        outerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
114 efc280af Leszek Koltunski
115 5a683295 Leszek Koltunski
        root.attach(mLeaf, outerLeafEffects[j], mesh);
116 efc280af Leszek Koltunski
        }
117
118 5a683295 Leszek Koltunski
      innerEffects.apply( new MatrixEffectScale(INNER*LEAF_SIZE) );
119 16b22aab Leszek Koltunski
      innerEffects.apply( new MatrixEffectRotate(rotate, axis, center) );
120 efc280af Leszek Koltunski
      innerEffects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0) ) );
121 d8f9a204 Leszek Koltunski
      innerEffects.apply(blurEffect);
122 efc280af Leszek Koltunski
123 5a683295 Leszek Koltunski
      DistortedNode innerNode = new DistortedNode( new DistortedTexture(), innerEffects, mesh);
124
      innerNode.resizeFBO(INNER*LEAF_SIZE,INNER*LEAF_SIZE);
125 efc280af Leszek Koltunski
      root.attach(innerNode);
126
127
      for(int j=0; j<NUM_LEAVES; j++)
128
        {
129 698ad0a8 Leszek Koltunski
        innerLeafEffects[j] = new DistortedEffects();
130 5a683295 Leszek Koltunski
        innerLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
131 efc280af Leszek Koltunski
        innerLeafEffects[j].apply(new MatrixEffectMove(innerMoveVector));
132 0ba5de00 Leszek Koltunski
        innerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
133 efc280af Leszek Koltunski
134 5a683295 Leszek Koltunski
        innerNode.attach( mLeaf, innerLeafEffects[j], mesh );
135 efc280af Leszek Koltunski
        }
136
137
      mScreen = new DistortedScreen();
138
      mScreen.attach(root);
139
      }
140
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143
   int setBlur(int blur)
144
     {
145
     int radius = blur/2;
146 678c391d Leszek Koltunski
     mHaloRadius.set1(radius);
147 efc280af Leszek Koltunski
     return radius;
148
     }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
   
152
   public void onDrawFrame(GL10 glUnused)
153
     {
154
     mScreen.render(System.currentTimeMillis());
155
     }
156
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
    
159
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
160
     {
161 5a683295 Leszek Koltunski
     float horiRatio = (float)width ;
162
     float vertRatio = (float)height;
163 678c391d Leszek Koltunski
     float factor    = Math.min(horiRatio, vertRatio);
164 efc280af Leszek Koltunski
165 0a098d35 Leszek Koltunski
     mScale.set(factor,factor,factor);
166 efc280af Leszek Koltunski
     mScreen.resize(width, height);
167
     }
168
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
    
171
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
172
     {
173
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
174
     Bitmap leaf;
175
      
176
     try
177
       {
178
       leaf = BitmapFactory.decodeStream(is);
179
       }
180
     finally
181
       {
182
       try
183
         {
184
         is.close();
185
         }
186 dc10a48d Leszek Koltunski
       catch(IOException ignored) { }
187 efc280af Leszek Koltunski
       }
188
      
189
     mLeaf.setTexture(leaf);
190
191
     FragmentEffectChroma.enable();
192
     PostprocessEffectBlur.enable();
193
194 dc10a48d Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(this);
195 061449ed Leszek Koltunski
     }
196
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
199
   public void distortedException(Exception ex)
200
     {
201
     android.util.Log.e("PostprocessTree", ex.getMessage() );
202 efc280af Leszek Koltunski
     }
203 dc10a48d Leszek Koltunski
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
206
   public int openGlVersion()
207
      {
208
      Context context = mView.getContext();
209
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
210
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
211
      int glESversion = configurationInfo.reqGlEsVersion;
212
      int major = glESversion >> 16;
213
      int minor = glESversion & 0xff;
214
215
      return 100*major + 10*minor;
216
      }
217
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
220
   public InputStream localFile(int fileID)
221
      {
222
      return mResources.openRawResource(fileID);
223
      }
224
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227
   public void logMessage(String message)
228
      {
229
      android.util.Log.e("PostprocessTree", message );
230
      }
231 efc280af Leszek Koltunski
}