Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 01782e85

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.glow;
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.main.Distorted;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedEffectsPostprocess;
30
import org.distorted.library.main.DistortedNode;
31
import org.distorted.library.main.DistortedScreen;
32
import org.distorted.library.main.DistortedTexture;
33
import org.distorted.library.EffectNames;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.main.MeshFlat;
36
import org.distorted.library.main.MeshObject;
37
import org.distorted.library.message.EffectListener;
38
import org.distorted.library.message.EffectMessage;
39
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Dynamic4D;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

    
45
import java.io.IOException;
46
import java.io.InputStream;
47

    
48
import javax.microedition.khronos.egl.EGLConfig;
49
import javax.microedition.khronos.opengles.GL10;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
class GlowRenderer implements GLSurfaceView.Renderer,EffectListener
54
{
55
   private static final int[] colors  = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0,  1,1,1}; // blue, black, red, yellow, green, white
56
   private static final int LEAF_SIZE = 100;
57
   private static final int NUM_LEAVES= colors.length/3;
58
   
59
   private GLSurfaceView mView;
60
   private DistortedNode mRoot;
61
   private DistortedTexture mLeaf;
62
   private DistortedScreen mScreen;
63
   private DistortedEffectsPostprocess[] mLeafGlow = new DistortedEffectsPostprocess[NUM_LEAVES];
64
   private int mRootW, mRootH;
65
   private int mGlowing;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
   GlowRenderer(GLSurfaceView v)
70
      {     
71
      mView = v;
72

    
73
      mRootW = 3*LEAF_SIZE;
74
      mRootH = 3*LEAF_SIZE;
75

    
76
      mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
77
      DistortedTexture surface = new DistortedTexture(mRootW,mRootH);
78
      MeshObject mesh = new MeshFlat(1,1);
79
      DistortedEffects[] leafEffects = new DistortedEffects[NUM_LEAVES];
80

    
81
      mRoot = new DistortedNode(surface, new DistortedEffects(), mesh);
82
     
83
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
84
      Static1D chromaLevel= new Static1D(0.5f);
85
      Static3D center     = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
86
      Static3D axis       = new Static3D(0,0,1);
87

    
88
      for(int j=0; j<NUM_LEAVES; j++)
89
        {
90
        mLeafGlow[j] = new DistortedEffectsPostprocess();
91
        mLeafGlow[j].registerForMessages(this);
92

    
93
        leafEffects[j] = new DistortedEffects();
94
        leafEffects[j].rotate( new Static1D(j*(360/NUM_LEAVES)), axis, center );
95
        leafEffects[j].move(moveVector);
96
        leafEffects[j].chroma( chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2]) );
97
        DistortedNode node = new DistortedNode( mLeaf, leafEffects[j], mesh);
98
        node.setPostprocessEffects(mLeafGlow[j]);
99
        mRoot.attach(node);
100
        }
101

    
102
      makeGlow(0);
103

    
104
      mScreen = new DistortedScreen(mView);
105
      mScreen.attach(mRoot);
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
   private void makeGlow(int leaf)
111
     {
112
     //android.util.Log.e("glow", "glowing: "+leaf);
113

    
114
     mGlowing = leaf;
115

    
116
     Dynamic1D radius = new Dynamic1D(5000,1.0f);
117
     Static1D startR  = new Static1D( 0);
118
     Static1D endR    = new Static1D(50);
119
     radius.add(startR);
120
     radius.add(endR);
121

    
122
     Dynamic4D color  = new Dynamic4D(5000,1.0f);
123
     Static4D startC  = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 0);
124
     Static4D endC    = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 1);
125
     color.add(startC);
126
     color.add(endC);
127

    
128
     mLeafGlow[leaf].glow( radius, color );
129
     }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// Glow finished. Make the next Leaf glow.
133

    
134
   public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID)
135
     {
136
     switch(em)
137
       {
138
       case EFFECT_FINISHED: //android.util.Log.e("glow", "effectMessage FINISHED");
139
                             int glowing = mGlowing+1;
140
                             if( glowing>=NUM_LEAVES ) glowing = 0;
141
                             makeGlow(glowing);
142
                             break;
143
       default:              //android.util.Log.e("glow", "effectMessage REMOVED");
144
                             break;
145
       }
146
     }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
   public void onDrawFrame(GL10 glUnused)
151
     {
152
     mScreen.render(System.currentTimeMillis());
153
     }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
    
157
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
158
     {
159
     float qw = (float)width /mRootW;
160
     float qh = (float)height/mRootH;
161
     float factor = 0.6f* (qw<qh ? qw:qh);
162
     int w = (int)(factor*mRootW);
163
     int h = (int)(factor*mRootH);
164

    
165
     Dynamic1D rot = new Dynamic1D(5000,0.0f);
166
     rot.setMode(Dynamic1D.MODE_JUMP);
167
     rot.add(new Static1D(  0));
168
     rot.add(new Static1D(360));
169

    
170
     Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
171
     Static3D axis   = new Static3D(0,0,1);
172

    
173
     DistortedEffects effects = mRoot.getEffects();
174
     effects.abortEffects(EffectTypes.MATRIX);
175
     effects.move( new Static3D((width-w)/2 ,(height-h)/2, 0) );
176
     effects.scale( factor );
177
     effects.rotate( rot, axis, center );
178

    
179
     mScreen.resize(width, height);
180
     }
181

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

    
204
     DistortedEffects.enableEffect(EffectNames.CHROMA);
205
     DistortedEffects.enableEffect(EffectNames.GLOW);
206

    
207
     try
208
       {
209
       Distorted.onCreate(mView.getContext());
210
       }
211
     catch(Exception ex)
212
       {
213
       android.util.Log.e("Glow", ex.getMessage() );
214
       }
215
     }
216
}
(2-2/3)