Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingglow / MovingGlowRenderer.java @ 7c72e798

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.MeshQuad;
38
import org.distorted.library.message.EffectListener;
39
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Dynamic2D;
41
import org.distorted.library.type.Dynamic4D;
42
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static2D;
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, DistortedLibrary.ExceptionListener
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 mGlowing;
68
   private Static3D mScale;
69
   private Dynamic2D mHaloAndRadiusDyn;
70
   private Dynamic4D[] mColorDyn;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

    
78
      MeshQuad mesh = new MeshQuad();
79
      mesh.setComponentCenter(0,0,0,-0.1f);
80

    
81
      mScale= new Static3D(1,1,1);
82
      mLeaf = new DistortedTexture();
83
      DistortedTexture surface = new DistortedTexture();
84

    
85
      Static3D moveVector = new Static3D(-1.45f*LEAF_SIZE, 0, 0);
86
      Static1D chromaLevel= new Static1D(0.5f);
87
      Static3D center     = new Static3D(0,0,0);
88
      Static3D axis       = new Static3D(0,0,1);
89

    
90
      Dynamic1D rot = new Dynamic1D(50000,0.0f);
91
      rot.setMode(Dynamic1D.MODE_JUMP);
92
      rot.add(new Static1D(  0));
93
      rot.add(new Static1D(360));
94

    
95
      DistortedEffects rootEffects = new DistortedEffects();
96
      rootEffects.apply(new MatrixEffectRotate(rot, axis, center) );
97
      rootEffects.apply(new MatrixEffectScale(mScale));
98

    
99
      DistortedNode root = new DistortedNode(surface, rootEffects, mesh);
100
      root.resizeFBO(5*LEAF_SIZE,5*LEAF_SIZE);
101

    
102
      MatrixEffectMove leafMove = new MatrixEffectMove(moveVector);
103

    
104
      for(int j=0; j<NUM_LEAVES; j++)
105
        {
106
        mLeafEffects[j] = new DistortedEffects();
107
        mLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
108
        mLeafEffects[j].apply(leafMove);
109
        mLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
110
        mLeafEffects[j].apply( new FragmentEffectChroma(chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2])) );
111
        DistortedNode node = new DistortedNode( mLeaf, mLeafEffects[j], mesh);
112
        root.attach(node);
113
        }
114

    
115
      mHaloAndRadiusDyn = new Dynamic2D(FLASH_TIME,1.0f);
116
      mHaloAndRadiusDyn.add(new Static2D( 0, 0));
117
      mHaloAndRadiusDyn.add(new Static2D(70,50));
118

    
119
      mColorDyn = new Dynamic4D[NUM_LEAVES];
120

    
121
      for(int leaf=0; leaf<NUM_LEAVES; leaf++)
122
        {
123
        mColorDyn[leaf] = new Dynamic4D(FLASH_TIME,1.0f);
124
        Static4D P1     = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 0.0f);
125
        Static4D P2     = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 1.0f);
126
        mColorDyn[leaf].add(P1);
127
        mColorDyn[leaf].add(P2);
128

    
129
        mGlow[leaf] = new PostprocessEffectGlow(mHaloAndRadiusDyn,mColorDyn[leaf]);
130
        }
131

    
132
      makeGlow(0);
133

    
134
      mScreen = new DistortedScreen();
135
      mScreen.attach(root);
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
   private void makeGlow(int leaf)
141
     {
142
     mGlowing = leaf;
143
     mLeafEffects[leaf].apply(mGlow[leaf]);
144
     mHaloAndRadiusDyn.resetToBeginning();
145
     mColorDyn[leaf].resetToBeginning();
146
     mGlow[leaf].notifyWhenFinished(this);
147
     }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
// Glow finished. Make the next Leaf glow.
151

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

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

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
    
170
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
171
     {
172
     float qw = (float)width ;
173
     float qh = (float)height;
174
     float factor = 0.9f* (Math.min(qw, qh));
175

    
176
     mScale.set( factor,factor,factor );
177
     mScreen.resize(width, height);
178
     }
179

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

    
202
     FragmentEffectChroma.enable();
203
     PostprocessEffectGlow.enable();
204

    
205
     DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
206
     }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
    public void distortedException(Exception ex)
211
      {
212
      android.util.Log.e("MovingGlow", ex.getMessage() );
213
      }
214
}
(2-2/3)