Project

General

Profile

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

examples / src / main / java / org / distorted / examples / postprocesstree / PostprocessTreeRenderer.java @ 5a683295

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.DistortedLibrary;
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.mesh.MeshQuad;
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 Static3D mScale;
59
   private Static1D mRadius;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
67
      final int OUTER = 9;
68
      final int INNER = 4;
69

    
70
      mRadius = new Static1D(5);
71
      Dynamic1D radiusDyn = new Dynamic1D();
72
      radiusDyn.add(mRadius);
73
      PostprocessEffectBlur blurEffect = new PostprocessEffectBlur(radiusDyn);
74

    
75
      mLeaf = new DistortedTexture();
76
      mScale= new Static3D(1,1,1);
77
      MeshQuad mesh = new MeshQuad();
78

    
79
      DistortedEffects rootEffects  = new DistortedEffects();
80
      DistortedEffects innerEffects = new DistortedEffects();
81
      DistortedEffects[] innerLeafEffects= new DistortedEffects[NUM_LEAVES];
82
      DistortedEffects[] outerLeafEffects= new DistortedEffects[NUM_LEAVES];
83

    
84
      DistortedNode root = new DistortedNode(new DistortedTexture(), rootEffects, mesh);
85
      root.resizeFBO(OUTER*LEAF_SIZE,OUTER*LEAF_SIZE);
86

    
87
      rootEffects.apply(new MatrixEffectScale(mScale));
88
      rootEffects.apply(blurEffect);
89

    
90
      Dynamic1D rotate = new Dynamic1D(5000,0.0f);
91
      rotate.setMode(Dynamic1D.MODE_JUMP);
92
      rotate.add(new Static1D(  0));
93
      rotate.add(new Static1D(360));
94

    
95
      Static3D center = new Static3D(0,0,0);
96
      Static3D axis   = new Static3D(0,0,1);
97
      Static3D innerMoveVector = new Static3D( (1-INNER)*LEAF_SIZE/2, 0, 0);
98
      Static3D outerMoveVector = new Static3D( (4-OUTER)*LEAF_SIZE/2, 0, 0);
99

    
100
      for(int j=0; j<NUM_LEAVES; j++)
101
        {
102
        outerLeafEffects[j] = new DistortedEffects();
103
        outerLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
104
        outerLeafEffects[j].apply( new MatrixEffectMove(outerMoveVector));
105
        outerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
106

    
107
        root.attach(mLeaf, outerLeafEffects[j], mesh);
108
        }
109

    
110
      innerEffects.apply( new MatrixEffectScale(INNER*LEAF_SIZE) );
111
      innerEffects.apply( new MatrixEffectRotate(rotate, axis, center) );
112
      innerEffects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0) ) );
113
      innerEffects.apply(blurEffect);
114

    
115
      DistortedNode innerNode = new DistortedNode( new DistortedTexture(), innerEffects, mesh);
116
      innerNode.resizeFBO(INNER*LEAF_SIZE,INNER*LEAF_SIZE);
117
      root.attach(innerNode);
118

    
119
      for(int j=0; j<NUM_LEAVES; j++)
120
        {
121
        innerLeafEffects[j] = new DistortedEffects();
122
        innerLeafEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
123
        innerLeafEffects[j].apply(new MatrixEffectMove(innerMoveVector));
124
        innerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
125

    
126
        innerNode.attach( mLeaf, innerLeafEffects[j], mesh );
127
        }
128

    
129
      mScreen = new DistortedScreen();
130
      mScreen.attach(root);
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
   int setBlur(int blur)
136
     {
137
     int radius = blur/2;
138
     mRadius.set(radius);
139
     return radius;
140
     }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
   
144
   public void onDrawFrame(GL10 glUnused)
145
     {
146
     mScreen.render(System.currentTimeMillis());
147
     }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
    
151
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
152
     {
153
     float horiRatio = (float)width ;
154
     float vertRatio = (float)height;
155
     float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
156

    
157
     mScale.set(factor,factor,factor);
158
     mScreen.resize(width, height);
159
     }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
    
163
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
164
     {
165
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
166
     Bitmap leaf;
167
      
168
     try
169
       {
170
       leaf = BitmapFactory.decodeStream(is);
171
       }
172
     finally
173
       {
174
       try
175
         {
176
         is.close();
177
         }
178
       catch(IOException e) { }
179
       }
180
      
181
     mLeaf.setTexture(leaf);
182

    
183
     FragmentEffectChroma.enable();
184
     PostprocessEffectBlur.enable();
185

    
186
     try
187
       {
188
       DistortedLibrary.onCreate(mView.getContext());
189
       }
190
     catch(Exception ex)
191
       {
192
       android.util.Log.e("PostprocessTree", ex.getMessage() );
193
       }
194
     }
195
}
(2-2/3)