Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingglow / MovingGlowRenderer.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.movingglow;
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.FragmentEffectChroma;
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectRotate;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.PostprocessEffectGlow;
32
import org.distorted.library.main.DistortedLibrary;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedNode;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshBase;
38
import org.distorted.library.mesh.MeshQuad;
39
import org.distorted.library.mesh.MeshRectangles;
40
import org.distorted.library.message.EffectListener;
41
import org.distorted.library.type.Dynamic1D;
42
import org.distorted.library.type.Dynamic4D;
43
import org.distorted.library.type.Static1D;
44
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46

    
47
import java.io.IOException;
48
import java.io.InputStream;
49

    
50
import javax.microedition.khronos.egl.EGLConfig;
51
import javax.microedition.khronos.opengles.GL10;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
class MovingGlowRenderer implements GLSurfaceView.Renderer,EffectListener
56
{
57
   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
58
   private static final int FLASH_TIME = 2000;
59
   private static final int LEAF_SIZE = 100;
60
   private static final int NUM_LEAVES= colors.length/3;
61
   
62
   private GLSurfaceView mView;
63
   private DistortedTexture mLeaf;
64
   private DistortedScreen mScreen;
65
   private DistortedEffects[] mLeafEffects = new DistortedEffects[NUM_LEAVES];
66
   private PostprocessEffectGlow[] mGlow = new PostprocessEffectGlow[NUM_LEAVES];
67
   private int mRootW, mRootH;
68
   private int mGlowing;
69
   private Static3D mMove, mScale;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
   MovingGlowRenderer(GLSurfaceView v)
74
      {     
75
      mView = v;
76

    
77
      mRootW = 5*LEAF_SIZE;
78
      mRootH = 5*LEAF_SIZE;
79

    
80
      mLeaf = new DistortedTexture();
81
      DistortedTexture surface = new DistortedTexture();
82

    
83
      MeshQuad rootMesh = new MeshQuad();
84
      rootMesh.setStretch(mRootW,mRootH,0);
85
      MeshQuad leafMesh = new MeshQuad();
86
      leafMesh.setStretch(LEAF_SIZE,LEAF_SIZE,0);
87

    
88
      DistortedEffects rootEffects = new DistortedEffects();
89
      DistortedNode root = new DistortedNode(surface, rootEffects, rootMesh);
90

    
91
      Static3D moveVector = new Static3D(0.55f*LEAF_SIZE, (mRootH-LEAF_SIZE)/2, 0);
92
      Static1D chromaLevel= new Static1D(0.5f);
93
      Static3D center     = new Static3D(mRootW/2, mRootH/2, 0);
94
      Static3D axis       = new Static3D(0,0,1);
95

    
96
      MatrixEffectMove leafMove = new MatrixEffectMove(moveVector);
97

    
98
      for(int j=0; j<NUM_LEAVES; j++)
99
        {
100
        mLeafEffects[j] = new DistortedEffects();
101
        mLeafEffects[j].apply(leafMove);
102
        mLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
103
        mLeafEffects[j].apply( new FragmentEffectChroma(chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2])) );
104
        DistortedNode node = new DistortedNode( mLeaf, mLeafEffects[j], leafMesh);
105
        root.attach(node);
106
        }
107

    
108
      mMove = new Static3D(0,0,0);
109
      mScale= new Static3D(1,1,1);
110

    
111
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
112
      rot.setMode(Dynamic1D.MODE_JUMP);
113
      rot.add(new Static1D(  0));
114
      rot.add(new Static1D(360));
115

    
116
      DistortedEffects effects = root.getEffects();
117
      effects.apply( new MatrixEffectRotate(rot, axis, center) );
118
      effects.apply(new MatrixEffectScale(mScale));
119
      effects.apply(new MatrixEffectMove(mMove));
120

    
121
      Dynamic1D radiusDyn = new Dynamic1D(FLASH_TIME,1.0f);
122
      radiusDyn.add(new Static1D( 0));
123
      radiusDyn.add(new Static1D(50));
124

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

    
133
        mGlow[leaf] = new PostprocessEffectGlow(radiusDyn,color);
134
        }
135

    
136
      makeGlow(0);
137

    
138
      mScreen = new DistortedScreen();
139
      mScreen.attach(root);
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
   private void makeGlow(int leaf)
145
     {
146
     mGlowing = leaf;
147
     mLeafEffects[leaf].apply(mGlow[leaf]);
148
     mGlow[leaf].notifyWhenFinished(this);
149
     }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
// Glow finished. Make the next Leaf glow.
153

    
154
   public void effectFinished(final long effectID)
155
     {
156
     mLeafEffects[mGlowing].abortById(effectID);
157

    
158
     int glowing = mGlowing+1;
159
     if( glowing>=NUM_LEAVES ) glowing = 0;
160
     makeGlow(glowing);
161
     }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
   public void onDrawFrame(GL10 glUnused)
166
     {
167
     mScreen.render(System.currentTimeMillis());
168
     }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
    
172
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
173
     {
174
     float qw = (float)width /mRootW;
175
     float qh = (float)height/mRootH;
176
     float factor = 0.9f* (qw<qh ? qw:qh);
177
     int w = (int)(factor*mRootW);
178
     int h = (int)(factor*mRootH);
179

    
180
     mMove.set((width-w)/2 ,(height-h)/2, 0);
181
     mScale.set( factor,factor,factor );
182
     mScreen.resize(width, height);
183
     }
184

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

    
207
     FragmentEffectChroma.enable();
208
     PostprocessEffectGlow.enable();
209

    
210
     try
211
       {
212
       DistortedLibrary.onCreate(mView.getContext());
213
       }
214
     catch(Exception ex)
215
       {
216
       android.util.Log.e("MovingGlow", ex.getMessage() );
217
       }
218
     }
219
}
(2-2/3)