Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ ebb06a48

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.effect.EffectName;
28
import org.distorted.library.effect.FragmentEffectChroma;
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectRotate;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.effect.PostprocessEffectGlow;
33
import org.distorted.library.main.Distorted;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedEffectsPostprocess;
36
import org.distorted.library.main.DistortedNode;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.main.MeshFlat;
40
import org.distorted.library.main.MeshObject;
41
import org.distorted.library.message.EffectListener;
42
import org.distorted.library.message.EffectMessage;
43
import org.distorted.library.type.Dynamic1D;
44
import org.distorted.library.type.Dynamic4D;
45
import org.distorted.library.type.Static1D;
46
import org.distorted.library.type.Static3D;
47
import org.distorted.library.type.Static4D;
48

    
49
import java.io.IOException;
50
import java.io.InputStream;
51

    
52
import javax.microedition.khronos.egl.EGLConfig;
53
import javax.microedition.khronos.opengles.GL10;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

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

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

    
78
      mRootW = 3*LEAF_SIZE;
79
      mRootH = 3*LEAF_SIZE;
80

    
81
      mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
82
      DistortedTexture surface = new DistortedTexture(mRootW,mRootH);
83
      MeshObject mesh = new MeshFlat(1,1);
84
      DistortedEffects[] leafEffects = new DistortedEffects[NUM_LEAVES];
85

    
86
      DistortedNode root = new DistortedNode(surface, new DistortedEffects(), mesh);
87
     
88
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
89
      Static1D chromaLevel= new Static1D(0.5f);
90
      Static3D center     = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
91
      Static3D axis       = new Static3D(0,0,1);
92

    
93
      MatrixEffectMove leafMove = new MatrixEffectMove(moveVector);
94

    
95
      for(int j=0; j<NUM_LEAVES; j++)
96
        {
97
        mLeafGlow[j] = new DistortedEffectsPostprocess();
98
        mLeafGlow[j].registerForMessages(this);
99

    
100
        leafEffects[j] = new DistortedEffects();
101
        leafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
102
        leafEffects[j].apply(leafMove);
103
        leafEffects[j].apply( new FragmentEffectChroma(chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2])) );
104
        DistortedNode node = new DistortedNode( mLeaf, leafEffects[j], mesh);
105
        node.setPostprocessEffects(mLeafGlow[j]);
106
        root.attach(node);
107
        }
108

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

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

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

    
122
      Dynamic1D radius = new Dynamic1D(5000,1.0f);
123
      Static1D startR  = new Static1D( 0);
124
      Static1D endR    = new Static1D(50);
125
      radius.add(startR);
126
      radius.add(endR);
127

    
128
      for(int leaf=0; leaf<NUM_LEAVES; leaf++)
129
        {
130
        Dynamic4D color  = new Dynamic4D(5000,1.0f);
131
        Static4D startC  = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 0);
132
        Static4D endC    = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 1);
133
        color.add(startC);
134
        color.add(endC);
135

    
136
        mGlow[leaf] = new PostprocessEffectGlow(radius,color);
137
        }
138

    
139
      makeGlow(0);
140

    
141
      mScreen = new DistortedScreen(mView);
142
      mScreen.attach(root);
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
   private void makeGlow(int leaf)
148
     {
149
     //android.util.Log.e("glow", "glowing: "+leaf);
150

    
151
     mGlowing = leaf;
152
     mLeafGlow[leaf].apply(mGlow[leaf]);
153
     }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// Glow finished. Make the next Leaf glow.
157

    
158
   public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
159
     {
160
     switch(em)
161
       {
162
       case EFFECT_FINISHED: //android.util.Log.e("glow", "effectMessage FINISHED");
163
                             int glowing = mGlowing+1;
164
                             if( glowing>=NUM_LEAVES ) glowing = 0;
165
                             makeGlow(glowing);
166
                             break;
167
       default:              //android.util.Log.e("glow", "effectMessage REMOVED");
168
                             break;
169
       }
170
     }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
   public void onDrawFrame(GL10 glUnused)
175
     {
176
     mScreen.render(System.currentTimeMillis());
177
     }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
    
181
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
182
     {
183
     float qw = (float)width /mRootW;
184
     float qh = (float)height/mRootH;
185
     float factor = 0.6f* (qw<qh ? qw:qh);
186
     int w = (int)(factor*mRootW);
187
     int h = (int)(factor*mRootH);
188

    
189
     mMove.set((width-w)/2 ,(height-h)/2, 0);
190
     mScale.set( factor,factor,factor );
191
     mScreen.resize(width, height);
192
     }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
    
196
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
197
     {
198
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
199
     Bitmap leaf;
200
      
201
     try
202
       {
203
       leaf = BitmapFactory.decodeStream(is);
204
       }
205
     finally
206
       {
207
       try
208
         {
209
         is.close();
210
         }
211
       catch(IOException e) { }
212
       }
213
      
214
     mLeaf.setTexture(leaf);
215

    
216
     DistortedEffects.enableEffect(EffectName.CHROMA);
217
     DistortedEffects.enableEffect(EffectName.GLOW);
218

    
219
     try
220
       {
221
       Distorted.onCreate(mView.getContext());
222
       }
223
     catch(Exception ex)
224
       {
225
       android.util.Log.e("Glow", ex.getMessage() );
226
       }
227
     }
228
}
(2-2/3)