Project

General

Profile

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

examples / src / main / java / org / distorted / examples / postprocesstree / PostprocessTreeRenderer.java @ 107e4b72

1 efc280af 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.postprocesstree;
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.PostprocessEffectBlur;
32
import org.distorted.library.main.Distorted;
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 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshFlat;
38 efc280af Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41
42
import java.io.IOException;
43
import java.io.InputStream;
44
45
import javax.microedition.khronos.egl.EGLConfig;
46
import javax.microedition.khronos.opengles.GL10;
47
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50
class PostprocessTreeRenderer implements GLSurfaceView.Renderer
51
{
52
   private static final int LEAF_SIZE = 100;
53
   private static final int NUM_LEAVES  = 8;
54
   
55
   private GLSurfaceView mView;
56
   private DistortedTexture mLeaf;
57
   private DistortedScreen mScreen;
58
   private int mScreenW, mScreenH;
59 0a098d35 Leszek Koltunski
   private Static3D mMove, mScale;
60
   private Static1D mRadius;
61 efc280af Leszek Koltunski
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
   PostprocessTreeRenderer(GLSurfaceView v)
65
      {     
66
      mView = v;
67
68 0a098d35 Leszek Koltunski
      final int OUTER = 9;
69
      final int INNER = 4;
70 efc280af Leszek Koltunski
71 0a098d35 Leszek Koltunski
      mScreenW = OUTER*LEAF_SIZE;
72
      mScreenH = OUTER*LEAF_SIZE;
73
74
      mRadius = new Static1D(5);
75 efc280af Leszek Koltunski
      Dynamic1D radiusDyn = new Dynamic1D();
76 0a098d35 Leszek Koltunski
      radiusDyn.add(mRadius);
77
      PostprocessEffectBlur blurEffect = new PostprocessEffectBlur(radiusDyn);
78 efc280af Leszek Koltunski
79
      mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
80
81
      MeshFlat mesh = new MeshFlat(1,1);
82
      mMove = new Static3D(0,0,0);
83 0a098d35 Leszek Koltunski
      mScale= new Static3D(1,1,1);
84 efc280af Leszek Koltunski
85
      DistortedEffects rootEffects  = new DistortedEffects();
86
      DistortedEffects innerEffects = new DistortedEffects();
87
      DistortedEffects[] innerLeafEffects= new DistortedEffects[NUM_LEAVES];
88
      DistortedEffects[] outerLeafEffects= new DistortedEffects[NUM_LEAVES];
89
90
      DistortedNode root = new DistortedNode(new DistortedTexture(mScreenW,mScreenH), rootEffects, mesh);
91
92
      rootEffects.apply(new MatrixEffectMove(mMove));
93 0a098d35 Leszek Koltunski
      rootEffects.apply(new MatrixEffectScale(mScale));
94 fba88037 Leszek Koltunski
      rootEffects.apply(blurEffect);
95 0a098d35 Leszek Koltunski
96
      Dynamic1D rotate = new Dynamic1D(5000,0.0f);
97
      rotate.setMode(Dynamic1D.MODE_JUMP);
98
      rotate.add(new Static1D(  0));
99
      rotate.add(new Static1D(360));
100 efc280af Leszek Koltunski
101 0a098d35 Leszek Koltunski
      Static3D outerCenter = new Static3D(OUTER*LEAF_SIZE/2, OUTER*LEAF_SIZE/2, 0);
102
      Static3D innerCenter = new Static3D(INNER*LEAF_SIZE/2, INNER*LEAF_SIZE/2, 0);
103 efc280af Leszek Koltunski
104
      Static3D axis   = new Static3D(0,0,1);
105 0a098d35 Leszek Koltunski
      Static3D innerMoveVector = new Static3D(            0, (INNER-1)*LEAF_SIZE/2, 0);
106
      Static3D outerMoveVector = new Static3D(3*LEAF_SIZE/2, (OUTER-1)*LEAF_SIZE/2, 0);
107 efc280af Leszek Koltunski
108
      for(int j=0; j<NUM_LEAVES; j++)
109
        {
110
        outerLeafEffects[j] = new DistortedEffects();
111 0a098d35 Leszek Koltunski
        outerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, outerCenter) );
112 efc280af Leszek Koltunski
        outerLeafEffects[j].apply(new MatrixEffectMove(outerMoveVector));
113
114 0a098d35 Leszek Koltunski
        root.attach(mLeaf, outerLeafEffects[j], mesh);
115 efc280af Leszek Koltunski
        }
116
117 0a098d35 Leszek Koltunski
      innerEffects.apply( new MatrixEffectMove(new Static3D( (OUTER-INNER)*LEAF_SIZE/2,(OUTER-INNER)*LEAF_SIZE/2, 0)) );
118
      innerEffects.apply( new MatrixEffectRotate(rotate, axis, innerCenter) );
119 efc280af Leszek Koltunski
      innerEffects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0) ) );
120 d8f9a204 Leszek Koltunski
      innerEffects.apply(blurEffect);
121 efc280af Leszek Koltunski
122 0a098d35 Leszek Koltunski
      DistortedNode innerNode = new DistortedNode( new DistortedTexture(INNER*LEAF_SIZE,INNER*LEAF_SIZE), innerEffects, mesh);
123 efc280af Leszek Koltunski
      root.attach(innerNode);
124
125
      for(int j=0; j<NUM_LEAVES; j++)
126
        {
127
        innerLeafEffects[j] = new DistortedEffects();
128 0a098d35 Leszek Koltunski
        innerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, innerCenter) );
129 efc280af Leszek Koltunski
        innerLeafEffects[j].apply(new MatrixEffectMove(innerMoveVector));
130
131 0a098d35 Leszek Koltunski
        innerNode.attach( mLeaf, innerLeafEffects[j], mesh );
132 efc280af Leszek Koltunski
        }
133
134
      mScreen = new DistortedScreen();
135
      mScreen.attach(root);
136
      }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140
   int setBlur(int blur)
141
     {
142
     int radius = blur/2;
143 0a098d35 Leszek Koltunski
     mRadius.set(radius);
144 efc280af Leszek Koltunski
     return radius;
145
     }
146
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
   
149
   public void onDrawFrame(GL10 glUnused)
150
     {
151
     mScreen.render(System.currentTimeMillis());
152
     }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
157
     {
158 0a098d35 Leszek Koltunski
     float factor;
159 efc280af Leszek Koltunski
160
     if( (float)mScreenH/mScreenW > (float)height/width )
161
       {
162
       int w = (height*mScreenW)/mScreenH;
163 0a098d35 Leszek Koltunski
       factor = (float)height/mScreenH;
164 efc280af Leszek Koltunski
       mMove.set((width-w)/2 ,0, 0);
165
       }
166
     else
167
       {
168
       int h = (width*mScreenH)/mScreenW;
169 0a098d35 Leszek Koltunski
       factor = (float)width/mScreenW;
170 efc280af Leszek Koltunski
       mMove.set(0,(height-h)/2,0);
171
       }
172
173 0a098d35 Leszek Koltunski
     mScale.set(factor,factor,factor);
174 efc280af Leszek Koltunski
175
     mScreen.resize(width, height);
176
     }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
    
180
   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
     FragmentEffectChroma.enable();
201
     PostprocessEffectBlur.enable();
202
203
     try
204
       {
205
       Distorted.onCreate(mView.getContext());
206
       }
207
     catch(Exception ex)
208
       {
209
       android.util.Log.e("PostprocessTree", ex.getMessage() );
210
       }
211
     }
212
}