Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingglow / MovingGlowRenderer.java @ dc10a48d

1 08602667 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 08602667 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 08602667 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 08602667 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 08602667 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 11298b49 Leszek Koltunski
package org.distorted.examples.movingglow;
21 08602667 Leszek Koltunski
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 08602667 Leszek Koltunski
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29
30
import org.distorted.examples.R;
31 2890c5df Leszek Koltunski
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 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
37 01782e85 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 3a91bfe1 Leszek Koltunski
import org.distorted.library.message.EffectListener;
43 08602667 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
44 678c391d Leszek Koltunski
import org.distorted.library.type.Dynamic2D;
45 3a91bfe1 Leszek Koltunski
import org.distorted.library.type.Dynamic4D;
46 08602667 Leszek Koltunski
import org.distorted.library.type.Static1D;
47 678c391d Leszek Koltunski
import org.distorted.library.type.Static2D;
48 08602667 Leszek Koltunski
import org.distorted.library.type.Static3D;
49 3a91bfe1 Leszek Koltunski
import org.distorted.library.type.Static4D;
50 08602667 Leszek Koltunski
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 dc10a48d Leszek Koltunski
class MovingGlowRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.LibraryUser
60 08602667 Leszek Koltunski
{
61 2a8ee748 Leszek Koltunski
   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 08602667 Leszek Koltunski
   private static final int LEAF_SIZE = 100;
64
   private static final int NUM_LEAVES= colors.length/3;
65
   
66 dc10a48d Leszek Koltunski
   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 3a91bfe1 Leszek Koltunski
   private int mGlowing;
77 08602667 Leszek Koltunski
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80 11298b49 Leszek Koltunski
   MovingGlowRenderer(GLSurfaceView v)
81 08602667 Leszek Koltunski
      {     
82
      mView = v;
83 dc10a48d Leszek Koltunski
      mResources = v.getResources();
84 08602667 Leszek Koltunski
85 5a683295 Leszek Koltunski
      MeshQuad mesh = new MeshQuad();
86 57a3f798 Leszek Koltunski
      mesh.setComponentCenter(0,0,0,-0.1f);
87
88 5a683295 Leszek Koltunski
      mScale= new Static3D(1,1,1);
89 687263cc Leszek Koltunski
      mLeaf = new DistortedTexture();
90
      DistortedTexture surface = new DistortedTexture();
91 08602667 Leszek Koltunski
92 0ba5de00 Leszek Koltunski
      Static3D moveVector = new Static3D(-1.45f*LEAF_SIZE, 0, 0);
93 08602667 Leszek Koltunski
      Static1D chromaLevel= new Static1D(0.5f);
94 16b22aab Leszek Koltunski
      Static3D center     = new Static3D(0,0,0);
95 08602667 Leszek Koltunski
      Static3D axis       = new Static3D(0,0,1);
96
97 e50e7ba7 Leszek Koltunski
      Dynamic1D rot = new Dynamic1D(50000,0.0f);
98 5a683295 Leszek Koltunski
      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 2890c5df Leszek Koltunski
      MatrixEffectMove leafMove = new MatrixEffectMove(moveVector);
110
111 08602667 Leszek Koltunski
      for(int j=0; j<NUM_LEAVES; j++)
112
        {
113 698ad0a8 Leszek Koltunski
        mLeafEffects[j] = new DistortedEffects();
114 5a683295 Leszek Koltunski
        mLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
115 ed89e05e Leszek Koltunski
        mLeafEffects[j].apply(leafMove);
116 0ba5de00 Leszek Koltunski
        mLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
117 ed89e05e Leszek Koltunski
        mLeafEffects[j].apply( new FragmentEffectChroma(chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2])) );
118 5a683295 Leszek Koltunski
        DistortedNode node = new DistortedNode( mLeaf, mLeafEffects[j], mesh);
119 2890c5df Leszek Koltunski
        root.attach(node);
120 08602667 Leszek Koltunski
        }
121
122 52e8e76a Leszek Koltunski
      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 2890c5df Leszek Koltunski
128
      for(int leaf=0; leaf<NUM_LEAVES; leaf++)
129
        {
130 52e8e76a Leszek Koltunski
        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 2890c5df Leszek Koltunski
136 52e8e76a Leszek Koltunski
        mGlow[leaf] = new PostprocessEffectGlow(mHaloAndRadiusDyn,mColorDyn[leaf]);
137 2890c5df Leszek Koltunski
        }
138 ebb06a48 leszek
139
      makeGlow(0);
140
141 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
142 ebb06a48 leszek
      mScreen.attach(root);
143 08602667 Leszek Koltunski
      }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146 3a91bfe1 Leszek Koltunski
147
   private void makeGlow(int leaf)
148
     {
149
     mGlowing = leaf;
150 ed89e05e Leszek Koltunski
     mLeafEffects[leaf].apply(mGlow[leaf]);
151 52e8e76a Leszek Koltunski
     mHaloAndRadiusDyn.resetToBeginning();
152
     mColorDyn[leaf].resetToBeginning();
153 8d5a8e06 Leszek Koltunski
     mGlow[leaf].notifyWhenFinished(this);
154 3a91bfe1 Leszek Koltunski
     }
155 08602667 Leszek Koltunski
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157 3a91bfe1 Leszek Koltunski
// Glow finished. Make the next Leaf glow.
158
159 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
160 3a91bfe1 Leszek Koltunski
     {
161 8d5a8e06 Leszek Koltunski
     mLeafEffects[mGlowing].abortById(effectID);
162
163
     int glowing = mGlowing+1;
164
     if( glowing>=NUM_LEAVES ) glowing = 0;
165
     makeGlow(glowing);
166 3a91bfe1 Leszek Koltunski
     }
167 08602667 Leszek Koltunski
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170 3a91bfe1 Leszek Koltunski
   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 5a683295 Leszek Koltunski
     float qw = (float)width ;
180
     float qh = (float)height;
181 678c391d Leszek Koltunski
     float factor = 0.9f* (Math.min(qw, qh));
182 3a91bfe1 Leszek Koltunski
183 2890c5df Leszek Koltunski
     mScale.set( factor,factor,factor );
184 3a91bfe1 Leszek Koltunski
     mScreen.resize(width, height);
185
     }
186 08602667 Leszek Koltunski
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
    
189 3a91bfe1 Leszek Koltunski
   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 dc10a48d Leszek Koltunski
       catch(IOException ignored) { }
205 3a91bfe1 Leszek Koltunski
       }
206
      
207
     mLeaf.setTexture(leaf);
208
209 885b9cca leszek
     FragmentEffectChroma.enable();
210
     PostprocessEffectGlow.enable();
211 3a91bfe1 Leszek Koltunski
212 dc10a48d Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(this);
213 3a91bfe1 Leszek Koltunski
     }
214 061449ed Leszek Koltunski
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
    public void distortedException(Exception ex)
218
      {
219
      android.util.Log.e("MovingGlow", ex.getMessage() );
220
      }
221 dc10a48d Leszek Koltunski
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 08602667 Leszek Koltunski
}