Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 2890c5df

1 08602667 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 2890c5df Leszek Koltunski
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 01782e85 Leszek Koltunski
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 3a91bfe1 Leszek Koltunski
import org.distorted.library.message.EffectListener;
42
import org.distorted.library.message.EffectMessage;
43 08602667 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
44 3a91bfe1 Leszek Koltunski
import org.distorted.library.type.Dynamic4D;
45 08602667 Leszek Koltunski
import org.distorted.library.type.Static1D;
46
import org.distorted.library.type.Static3D;
47 3a91bfe1 Leszek Koltunski
import org.distorted.library.type.Static4D;
48 08602667 Leszek Koltunski
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 3a91bfe1 Leszek Koltunski
class GlowRenderer implements GLSurfaceView.Renderer,EffectListener
58 08602667 Leszek Koltunski
{
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 3a91bfe1 Leszek Koltunski
   private DistortedEffectsPostprocess[] mLeafGlow = new DistortedEffectsPostprocess[NUM_LEAVES];
67 2890c5df Leszek Koltunski
   private PostprocessEffectGlow[] mGlow = new PostprocessEffectGlow[NUM_LEAVES];
68 08602667 Leszek Koltunski
   private int mRootW, mRootH;
69 3a91bfe1 Leszek Koltunski
   private int mGlowing;
70 2890c5df Leszek Koltunski
   private Static3D mMove, mScale;
71 08602667 Leszek Koltunski
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 2890c5df Leszek Koltunski
      DistortedNode root = new DistortedNode(surface, new DistortedEffects(), mesh);
87 08602667 Leszek Koltunski
     
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 2890c5df Leszek Koltunski
      MatrixEffectMove leafMove = new MatrixEffectMove(moveVector);
94
95 08602667 Leszek Koltunski
      for(int j=0; j<NUM_LEAVES; j++)
96
        {
97 3a91bfe1 Leszek Koltunski
        mLeafGlow[j] = new DistortedEffectsPostprocess();
98
        mLeafGlow[j].registerForMessages(this);
99
100 08602667 Leszek Koltunski
        leafEffects[j] = new DistortedEffects();
101 2890c5df Leszek Koltunski
        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 3a91bfe1 Leszek Koltunski
        DistortedNode node = new DistortedNode( mLeaf, leafEffects[j], mesh);
105
        node.setPostprocessEffects(mLeafGlow[j]);
106 2890c5df Leszek Koltunski
        root.attach(node);
107 08602667 Leszek Koltunski
        }
108
109 3a91bfe1 Leszek Koltunski
      makeGlow(0);
110
111 08602667 Leszek Koltunski
      mScreen = new DistortedScreen(mView);
112 2890c5df Leszek Koltunski
      mScreen.attach(root);
113
114
      mMove = new Static3D(0,0,0);
115
      mScale= new Static3D(1,1,1);
116
117
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
118
      rot.setMode(Dynamic1D.MODE_JUMP);
119
      rot.add(new Static1D(  0));
120
      rot.add(new Static1D(360));
121
122
      DistortedEffects effects = root.getEffects();
123
      effects.apply(new MatrixEffectMove(mMove));
124
      effects.apply(new MatrixEffectScale(mScale));
125
      effects.apply( new MatrixEffectRotate(rot, axis, center) );
126
127
      Dynamic1D radius = new Dynamic1D(5000,1.0f);
128
      Static1D startR  = new Static1D( 0);
129
      Static1D endR    = new Static1D(50);
130
      radius.add(startR);
131
      radius.add(endR);
132
133
      for(int leaf=0; leaf<NUM_LEAVES; leaf++)
134
        {
135
        Dynamic4D color  = new Dynamic4D(5000,1.0f);
136
        Static4D startC  = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 0);
137
        Static4D endC    = new Static4D(colors[3*leaf],colors[3*leaf+1], colors[3*leaf+2], 1);
138
        color.add(startC);
139
        color.add(endC);
140
141
        mGlow[leaf] = new PostprocessEffectGlow(radius,color);
142
        }
143 08602667 Leszek Koltunski
      }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146 3a91bfe1 Leszek Koltunski
147
   private void makeGlow(int leaf)
148
     {
149
     //android.util.Log.e("glow", "glowing: "+leaf);
150
151
     mGlowing = leaf;
152 2890c5df Leszek Koltunski
     mLeafGlow[leaf].apply(mGlow[leaf]);
153 3a91bfe1 Leszek Koltunski
     }
154 08602667 Leszek Koltunski
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156 3a91bfe1 Leszek Koltunski
// Glow finished. Make the next Leaf glow.
157
158 2890c5df Leszek Koltunski
   public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
159 3a91bfe1 Leszek Koltunski
     {
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 08602667 Leszek Koltunski
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174 3a91bfe1 Leszek Koltunski
   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 2890c5df Leszek Koltunski
     mMove.set((width-w)/2 ,(height-h)/2, 0);
190
     mScale.set( factor,factor,factor );
191 3a91bfe1 Leszek Koltunski
     mScreen.resize(width, height);
192
     }
193 08602667 Leszek Koltunski
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
    
196 3a91bfe1 Leszek Koltunski
   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 2890c5df Leszek Koltunski
     DistortedEffects.enableEffect(EffectName.CHROMA);
217
     DistortedEffects.enableEffect(EffectName.GLOW);
218 3a91bfe1 Leszek Koltunski
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 08602667 Leszek Koltunski
}