Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingglow / MovingGlowRenderer.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.movingglow;
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.PostprocessEffectGlow;
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.message.EffectListener;
43
import org.distorted.library.type.Dynamic1D;
44
import org.distorted.library.type.Dynamic2D;
45
import org.distorted.library.type.Dynamic4D;
46
import org.distorted.library.type.Static1D;
47
import org.distorted.library.type.Static2D;
48
import org.distorted.library.type.Static3D;
49
import org.distorted.library.type.Static4D;
50

    
51
import java.io.IOException;
52
import java.io.InputStream;
53

    
54
import javax.microedition.khronos.egl.EGLConfig;
55
import javax.microedition.khronos.opengles.GL10;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
class MovingGlowRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.LibraryUser
60
{
61
   private static final int[] colors  = new int[] {0,0,1,  1,0,1,  1,0,0,  1,1,0,  0,1,0,  1,1,1}; // blue, pink, red, yellow, green, white
62
   private static final int FLASH_TIME = 2000;
63
   private static final int LEAF_SIZE = 100;
64
   private static final int NUM_LEAVES= colors.length/3;
65
   
66
   private final GLSurfaceView mView;
67
   private final Resources mResources;
68
   private final DistortedTexture mLeaf;
69
   private final DistortedScreen mScreen;
70
   private final DistortedEffects[] mLeafEffects = new DistortedEffects[NUM_LEAVES];
71
   private final PostprocessEffectGlow[] mGlow = new PostprocessEffectGlow[NUM_LEAVES];
72
   private final Static3D mScale;
73
   private final Dynamic2D mHaloAndRadiusDyn;
74
   private final Dynamic4D[] mColorDyn;
75

    
76
   private int mGlowing;
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
   MovingGlowRenderer(GLSurfaceView v)
81
      {     
82
      mView = v;
83
      mResources = v.getResources();
84

    
85
      MeshQuad mesh = new MeshQuad();
86
      mesh.setComponentCenter(0,0,0,-0.1f);
87

    
88
      mScale= new Static3D(1,1,1);
89
      mLeaf = new DistortedTexture();
90
      DistortedTexture surface = new DistortedTexture();
91

    
92
      Static3D moveVector = new Static3D(-1.45f*LEAF_SIZE, 0, 0);
93
      Static1D chromaLevel= new Static1D(0.5f);
94
      Static3D center     = new Static3D(0,0,0);
95
      Static3D axis       = new Static3D(0,0,1);
96

    
97
      Dynamic1D rot = new Dynamic1D(50000,0.0f);
98
      rot.setMode(Dynamic1D.MODE_JUMP);
99
      rot.add(new Static1D(  0));
100
      rot.add(new Static1D(360));
101

    
102
      DistortedEffects rootEffects = new DistortedEffects();
103
      rootEffects.apply(new MatrixEffectRotate(rot, axis, center) );
104
      rootEffects.apply(new MatrixEffectScale(mScale));
105

    
106
      DistortedNode root = new DistortedNode(surface, rootEffects, mesh);
107
      root.resizeFBO(5*LEAF_SIZE,5*LEAF_SIZE);
108

    
109
      MatrixEffectMove leafMove = new MatrixEffectMove(moveVector);
110

    
111
      for(int j=0; j<NUM_LEAVES; j++)
112
        {
113
        mLeafEffects[j] = new DistortedEffects();
114
        mLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
115
        mLeafEffects[j].apply(leafMove);
116
        mLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
117
        mLeafEffects[j].apply( new FragmentEffectChroma(chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2])) );
118
        DistortedNode node = new DistortedNode( mLeaf, mLeafEffects[j], mesh);
119
        root.attach(node);
120
        }
121

    
122
      mHaloAndRadiusDyn = new Dynamic2D(FLASH_TIME,1.0f);
123
      mHaloAndRadiusDyn.add(new Static2D( 0, 0));
124
      mHaloAndRadiusDyn.add(new Static2D(70,50));
125

    
126
      mColorDyn = new Dynamic4D[NUM_LEAVES];
127

    
128
      for(int leaf=0; leaf<NUM_LEAVES; leaf++)
129
        {
130
        mColorDyn[leaf] = new Dynamic4D(FLASH_TIME,1.0f);
131
        Static4D P1     = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 0.0f);
132
        Static4D P2     = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 1.0f);
133
        mColorDyn[leaf].add(P1);
134
        mColorDyn[leaf].add(P2);
135

    
136
        mGlow[leaf] = new PostprocessEffectGlow(mHaloAndRadiusDyn,mColorDyn[leaf]);
137
        }
138

    
139
      makeGlow(0);
140

    
141
      mScreen = new DistortedScreen();
142
      mScreen.attach(root);
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
   private void makeGlow(int leaf)
148
     {
149
     mGlowing = leaf;
150
     mLeafEffects[leaf].apply(mGlow[leaf]);
151
     mHaloAndRadiusDyn.resetToBeginning();
152
     mColorDyn[leaf].resetToBeginning();
153
     mGlow[leaf].notifyWhenFinished(this);
154
     }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
// Glow finished. Make the next Leaf glow.
158

    
159
   public void effectFinished(final long effectID)
160
     {
161
     mLeafEffects[mGlowing].abortById(effectID);
162

    
163
     int glowing = mGlowing+1;
164
     if( glowing>=NUM_LEAVES ) glowing = 0;
165
     makeGlow(glowing);
166
     }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
   public void onDrawFrame(GL10 glUnused)
171
     {
172
     mScreen.render(System.currentTimeMillis());
173
     }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
    
177
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
178
     {
179
     float qw = (float)width ;
180
     float qh = (float)height;
181
     float factor = 0.9f* (Math.min(qw, qh));
182

    
183
     mScale.set( factor,factor,factor );
184
     mScreen.resize(width, height);
185
     }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
    
189
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
190
     {
191
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
192
     Bitmap leaf;
193
      
194
     try
195
       {
196
       leaf = BitmapFactory.decodeStream(is);
197
       }
198
     finally
199
       {
200
       try
201
         {
202
         is.close();
203
         }
204
       catch(IOException ignored) { }
205
       }
206
      
207
     mLeaf.setTexture(leaf);
208

    
209
     FragmentEffectChroma.enable();
210
     PostprocessEffectGlow.enable();
211

    
212
     DistortedLibrary.onSurfaceCreated(this);
213
     }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
    public void distortedException(Exception ex)
218
      {
219
      android.util.Log.e("MovingGlow", ex.getMessage() );
220
      }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
    public int openGlVersion()
225
      {
226
      Context context = mView.getContext();
227
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
228
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
229
      int glESversion = configurationInfo.reqGlEsVersion;
230
      int major = glESversion >> 16;
231
      int minor = glESversion & 0xff;
232

    
233
      return 100*major + 10*minor;
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public InputStream localFile(int fileID)
239
      {
240
      return mResources.openRawResource(fileID);
241
      }
242

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

    
245
    public void logMessage(String message)
246
      {
247
      android.util.Log.e("MovingGlow", message );
248
      }
249
}
(2-2/3)