Project

General

Profile

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

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

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
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25
26
import org.distorted.examples.R;
27 2890c5df Leszek Koltunski
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 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 01782e85 Leszek Koltunski
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 698ad0a8 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
38 3a91bfe1 Leszek Koltunski
import org.distorted.library.message.EffectListener;
39 08602667 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
40 678c391d Leszek Koltunski
import org.distorted.library.type.Dynamic2D;
41 3a91bfe1 Leszek Koltunski
import org.distorted.library.type.Dynamic4D;
42 08602667 Leszek Koltunski
import org.distorted.library.type.Static1D;
43 678c391d Leszek Koltunski
import org.distorted.library.type.Static2D;
44 08602667 Leszek Koltunski
import org.distorted.library.type.Static3D;
45 3a91bfe1 Leszek Koltunski
import org.distorted.library.type.Static4D;
46 08602667 Leszek Koltunski
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 061449ed Leszek Koltunski
class MovingGlowRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.ExceptionListener
56 08602667 Leszek Koltunski
{
57 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
58
   private static final int FLASH_TIME = 2000;
59 08602667 Leszek Koltunski
   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 ed89e05e Leszek Koltunski
   private DistortedEffects[] mLeafEffects = new DistortedEffects[NUM_LEAVES];
66 2890c5df Leszek Koltunski
   private PostprocessEffectGlow[] mGlow = new PostprocessEffectGlow[NUM_LEAVES];
67 3a91bfe1 Leszek Koltunski
   private int mGlowing;
68 16b22aab Leszek Koltunski
   private Static3D mScale;
69 52e8e76a Leszek Koltunski
   private Dynamic2D mHaloAndRadiusDyn;
70
   private Dynamic4D[] mColorDyn;
71 08602667 Leszek Koltunski
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 11298b49 Leszek Koltunski
   MovingGlowRenderer(GLSurfaceView v)
75 08602667 Leszek Koltunski
      {     
76
      mView = v;
77
78 5a683295 Leszek Koltunski
      MeshQuad mesh = new MeshQuad();
79
      mScale= new Static3D(1,1,1);
80 687263cc Leszek Koltunski
      mLeaf = new DistortedTexture();
81
      DistortedTexture surface = new DistortedTexture();
82 08602667 Leszek Koltunski
83 0ba5de00 Leszek Koltunski
      Static3D moveVector = new Static3D(-1.45f*LEAF_SIZE, 0, 0);
84 08602667 Leszek Koltunski
      Static1D chromaLevel= new Static1D(0.5f);
85 16b22aab Leszek Koltunski
      Static3D center     = new Static3D(0,0,0);
86 08602667 Leszek Koltunski
      Static3D axis       = new Static3D(0,0,1);
87
88 e50e7ba7 Leszek Koltunski
      Dynamic1D rot = new Dynamic1D(50000,0.0f);
89 5a683295 Leszek Koltunski
      rot.setMode(Dynamic1D.MODE_JUMP);
90
      rot.add(new Static1D(  0));
91
      rot.add(new Static1D(360));
92
93
      DistortedEffects rootEffects = new DistortedEffects();
94
      rootEffects.apply(new MatrixEffectRotate(rot, axis, center) );
95
      rootEffects.apply(new MatrixEffectScale(mScale));
96
97
      DistortedNode root = new DistortedNode(surface, rootEffects, mesh);
98
      root.resizeFBO(5*LEAF_SIZE,5*LEAF_SIZE);
99
100 2890c5df Leszek Koltunski
      MatrixEffectMove leafMove = new MatrixEffectMove(moveVector);
101
102 08602667 Leszek Koltunski
      for(int j=0; j<NUM_LEAVES; j++)
103
        {
104 698ad0a8 Leszek Koltunski
        mLeafEffects[j] = new DistortedEffects();
105 5a683295 Leszek Koltunski
        mLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
106 ed89e05e Leszek Koltunski
        mLeafEffects[j].apply(leafMove);
107 0ba5de00 Leszek Koltunski
        mLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
108 ed89e05e Leszek Koltunski
        mLeafEffects[j].apply( new FragmentEffectChroma(chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2])) );
109 5a683295 Leszek Koltunski
        DistortedNode node = new DistortedNode( mLeaf, mLeafEffects[j], mesh);
110 2890c5df Leszek Koltunski
        root.attach(node);
111 08602667 Leszek Koltunski
        }
112
113 52e8e76a Leszek Koltunski
      mHaloAndRadiusDyn = new Dynamic2D(FLASH_TIME,1.0f);
114
      mHaloAndRadiusDyn.add(new Static2D( 0, 0));
115
      mHaloAndRadiusDyn.add(new Static2D(70,50));
116
117
      mColorDyn = new Dynamic4D[NUM_LEAVES];
118 2890c5df Leszek Koltunski
119
      for(int leaf=0; leaf<NUM_LEAVES; leaf++)
120
        {
121 52e8e76a Leszek Koltunski
        mColorDyn[leaf] = new Dynamic4D(FLASH_TIME,1.0f);
122
        Static4D P1     = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 0.0f);
123
        Static4D P2     = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 1.0f);
124
        mColorDyn[leaf].add(P1);
125
        mColorDyn[leaf].add(P2);
126 2890c5df Leszek Koltunski
127 52e8e76a Leszek Koltunski
        mGlow[leaf] = new PostprocessEffectGlow(mHaloAndRadiusDyn,mColorDyn[leaf]);
128 2890c5df Leszek Koltunski
        }
129 ebb06a48 leszek
130
      makeGlow(0);
131
132 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
133 ebb06a48 leszek
      mScreen.attach(root);
134 08602667 Leszek Koltunski
      }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137 3a91bfe1 Leszek Koltunski
138
   private void makeGlow(int leaf)
139
     {
140
     mGlowing = leaf;
141 ed89e05e Leszek Koltunski
     mLeafEffects[leaf].apply(mGlow[leaf]);
142 52e8e76a Leszek Koltunski
     mHaloAndRadiusDyn.resetToBeginning();
143
     mColorDyn[leaf].resetToBeginning();
144 8d5a8e06 Leszek Koltunski
     mGlow[leaf].notifyWhenFinished(this);
145 3a91bfe1 Leszek Koltunski
     }
146 08602667 Leszek Koltunski
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148 3a91bfe1 Leszek Koltunski
// Glow finished. Make the next Leaf glow.
149
150 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
151 3a91bfe1 Leszek Koltunski
     {
152 8d5a8e06 Leszek Koltunski
     mLeafEffects[mGlowing].abortById(effectID);
153
154
     int glowing = mGlowing+1;
155
     if( glowing>=NUM_LEAVES ) glowing = 0;
156
     makeGlow(glowing);
157 3a91bfe1 Leszek Koltunski
     }
158 08602667 Leszek Koltunski
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161 3a91bfe1 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
162
     {
163
     mScreen.render(System.currentTimeMillis());
164
     }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
    
168
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
169
     {
170 5a683295 Leszek Koltunski
     float qw = (float)width ;
171
     float qh = (float)height;
172 678c391d Leszek Koltunski
     float factor = 0.9f* (Math.min(qw, qh));
173 3a91bfe1 Leszek Koltunski
174 2890c5df Leszek Koltunski
     mScale.set( factor,factor,factor );
175 3a91bfe1 Leszek Koltunski
     mScreen.resize(width, height);
176
     }
177 08602667 Leszek Koltunski
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
    
180 3a91bfe1 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
181
     {
182
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
183
     Bitmap leaf;
184
      
185
     try
186
       {
187
       leaf = BitmapFactory.decodeStream(is);
188
       }
189
     finally
190
       {
191
       try
192
         {
193
         is.close();
194
         }
195
       catch(IOException e) { }
196
       }
197
      
198
     mLeaf.setTexture(leaf);
199
200 885b9cca leszek
     FragmentEffectChroma.enable();
201
     PostprocessEffectGlow.enable();
202 3a91bfe1 Leszek Koltunski
203 061449ed Leszek Koltunski
     DistortedLibrary.onCreate(mView.getContext(), this);
204 3a91bfe1 Leszek Koltunski
     }
205 061449ed Leszek Koltunski
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208
    public void distortedException(Exception ex)
209
      {
210
      android.util.Log.e("MovingGlow", ex.getMessage() );
211
      }
212 08602667 Leszek Koltunski
}