Project

General

Profile

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

examples / src / main / java / org / distorted / examples / postprocesstree / PostprocessTreeRenderer.java @ efc280af

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.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
import org.distorted.library.main.MeshFlat;
38
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
   private Static3D mMove, mInnerScale, mOuterScale;
60
   private Static1D mRadiusSta;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
   PostprocessTreeRenderer(GLSurfaceView v)
65
      {     
66
      mView = v;
67

    
68
      mScreenW = 9*LEAF_SIZE;
69
      mScreenH = 9*LEAF_SIZE;
70

    
71
      mRadiusSta = new Static1D(5);
72
      Dynamic1D radiusDyn = new Dynamic1D();
73
      radiusDyn.add(mRadiusSta);
74

    
75
      mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
76

    
77
      MeshFlat mesh = new MeshFlat(1,1);
78
      mMove = new Static3D(0,0,0);
79

    
80
      mInnerScale= new Static3D(1,1,1);
81
      mOuterScale= new Static3D(1,1,1);
82

    
83
      DistortedEffects rootEffects  = new DistortedEffects();
84
      DistortedEffects innerEffects = new DistortedEffects();
85
      DistortedEffects[] innerLeafEffects= new DistortedEffects[NUM_LEAVES];
86
      DistortedEffects[] outerLeafEffects= new DistortedEffects[NUM_LEAVES];
87

    
88
      DistortedNode root = new DistortedNode(new DistortedTexture(mScreenW,mScreenH), rootEffects, mesh);
89

    
90
      DistortedNode[] innerLeafNodes = new DistortedNode[NUM_LEAVES];
91
      DistortedNode[] outerLeafNodes = new DistortedNode[NUM_LEAVES];
92

    
93
      rootEffects.apply(new MatrixEffectMove(mMove));
94
      rootEffects.apply(new MatrixEffectScale(mOuterScale));
95

    
96
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
97
      rot.setMode(Dynamic1D.MODE_JUMP);
98
      rot.add(new Static1D(  0));
99
      rot.add(new Static1D(360));
100

    
101
      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
102
      Static3D axis   = new Static3D(0,0,1);
103
      Static3D innerMoveVector = new Static3D(0,  LEAF_SIZE,0);
104
      Static3D outerMoveVector = new Static3D(0,3*LEAF_SIZE,0);
105

    
106
      for(int j=0; j<NUM_LEAVES; j++)
107
        {
108
        outerLeafEffects[j] = new DistortedEffects();
109
        outerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
110
        outerLeafEffects[j].apply(new MatrixEffectMove(outerMoveVector));
111
        outerLeafNodes[j] = new DistortedNode( mLeaf, outerLeafEffects[j], mesh);
112

    
113
        root.attach(outerLeafNodes[j]);
114
        }
115

    
116
      innerEffects.apply( new MatrixEffectMove(new Static3D( mScreenW/2, mScreenH/2, 0)) );
117
      innerEffects.apply( new MatrixEffectRotate(rot, axis, center) );
118
      rootEffects.apply(new MatrixEffectScale(mInnerScale));
119
      innerEffects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0) ) );
120

    
121
      DistortedNode innerNode = new DistortedNode( new DistortedTexture(3*LEAF_SIZE,3*LEAF_SIZE), innerEffects, mesh);
122
      root.attach(innerNode);
123

    
124
      for(int j=0; j<NUM_LEAVES; j++)
125
        {
126
        innerLeafEffects[j] = new DistortedEffects();
127
        innerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
128
        innerLeafEffects[j].apply(new MatrixEffectMove(innerMoveVector));
129
        innerLeafNodes[j] = new DistortedNode( mLeaf, innerLeafEffects[j], mesh);
130

    
131
        innerNode.attach(innerLeafNodes[j]);
132
        }
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
     mRadiusSta.set(radius);
144
     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
     float iFactor, oFactor;
159

    
160
     if( (float)mScreenH/mScreenW > (float)height/width )
161
       {
162
       int w = (height*mScreenW)/mScreenH;
163
       oFactor = (float)height/mScreenH;
164
       iFactor = 0.3f*oFactor;
165
       mMove.set((width-w)/2 ,0, 0);
166
       }
167
     else
168
       {
169
       int h = (width*mScreenH)/mScreenW;
170
       oFactor = (float)width/mScreenW;
171
       iFactor = 0.3f*oFactor;
172
       mMove.set(0,(height-h)/2,0);
173
       }
174

    
175
     mOuterScale.set(oFactor,oFactor,oFactor);
176
     mInnerScale.set(iFactor,iFactor,iFactor);
177

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

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

    
203
     FragmentEffectChroma.enable();
204
     PostprocessEffectBlur.enable();
205

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