Project

General

Profile

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

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

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.postprocesstree;
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.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
import org.distorted.library.main.DistortedLibrary;
37
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
import org.distorted.library.mesh.MeshQuad;
42
import org.distorted.library.type.Dynamic1D;
43
import org.distorted.library.type.Dynamic2D;
44
import org.distorted.library.type.Static1D;
45
import org.distorted.library.type.Static2D;
46
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
class PostprocessTreeRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
57
{
58
   private static final int LEAF_SIZE = 100;
59
   private static final int NUM_LEAVES  = 8;
60
   
61
   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

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
   PostprocessTreeRenderer(GLSurfaceView v)
71
      {     
72
      mView = v;
73
      mResources = v.getResources();
74

    
75
      final int OUTER = 9;
76
      final int INNER = 4;
77

    
78
      mHaloRadius = new Static2D(30,5);
79
      Dynamic2D haloRadiusDyn = new Dynamic2D();
80
      haloRadiusDyn.add(mHaloRadius);
81
      PostprocessEffectBlur blurEffect = new PostprocessEffectBlur(haloRadiusDyn);
82

    
83
      mLeaf = new DistortedTexture();
84
      mScale= new Static3D(1,1,1);
85
      MeshQuad mesh = new MeshQuad();
86

    
87
      DistortedEffects rootEffects  = new DistortedEffects();
88
      DistortedEffects innerEffects = new DistortedEffects();
89
      DistortedEffects[] innerLeafEffects= new DistortedEffects[NUM_LEAVES];
90
      DistortedEffects[] outerLeafEffects= new DistortedEffects[NUM_LEAVES];
91

    
92
      DistortedNode root = new DistortedNode(new DistortedTexture(), rootEffects, mesh);
93
      root.resizeFBO(OUTER*LEAF_SIZE,OUTER*LEAF_SIZE);
94

    
95
      rootEffects.apply(new MatrixEffectScale(mScale));
96
      rootEffects.apply(blurEffect);
97

    
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

    
103
      Static3D center = new Static3D(0,0,0);
104
      Static3D axis   = new Static3D(0,0,1);
105
      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

    
108
      for(int j=0; j<NUM_LEAVES; j++)
109
        {
110
        outerLeafEffects[j] = new DistortedEffects();
111
        outerLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
112
        outerLeafEffects[j].apply( new MatrixEffectMove(outerMoveVector));
113
        outerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
114

    
115
        root.attach(mLeaf, outerLeafEffects[j], mesh);
116
        }
117

    
118
      innerEffects.apply( new MatrixEffectScale(INNER*LEAF_SIZE) );
119
      innerEffects.apply( new MatrixEffectRotate(rotate, axis, center) );
120
      innerEffects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0) ) );
121
      innerEffects.apply(blurEffect);
122

    
123
      DistortedNode innerNode = new DistortedNode( new DistortedTexture(), innerEffects, mesh);
124
      innerNode.resizeFBO(INNER*LEAF_SIZE,INNER*LEAF_SIZE);
125
      root.attach(innerNode);
126

    
127
      for(int j=0; j<NUM_LEAVES; j++)
128
        {
129
        innerLeafEffects[j] = new DistortedEffects();
130
        innerLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
131
        innerLeafEffects[j].apply(new MatrixEffectMove(innerMoveVector));
132
        innerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
133

    
134
        innerNode.attach( mLeaf, innerLeafEffects[j], mesh );
135
        }
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
     mHaloRadius.set1(radius);
147
     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
     float horiRatio = (float)width ;
162
     float vertRatio = (float)height;
163
     float factor    = Math.min(horiRatio, vertRatio);
164

    
165
     mScale.set(factor,factor,factor);
166
     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
       catch(IOException ignored) { }
187
       }
188
      
189
     mLeaf.setTexture(leaf);
190

    
191
     FragmentEffectChroma.enable();
192
     PostprocessEffectBlur.enable();
193

    
194
     DistortedLibrary.onSurfaceCreated(this);
195
     }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
   public void distortedException(Exception ex)
200
     {
201
     android.util.Log.e("PostprocessTree", ex.getMessage() );
202
     }
203

    
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
}
(2-2/3)