Project

General

Profile

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

examples / src / main / java / org / distorted / examples / postprocesstree / PostprocessTreeRenderer.java @ 698ad0a8

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.mesh.MeshRectangles;
39
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42

    
43
import java.io.IOException;
44
import java.io.InputStream;
45

    
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class PostprocessTreeRenderer implements GLSurfaceView.Renderer
52
{
53
   private static final int LEAF_SIZE = 100;
54
   private static final int NUM_LEAVES  = 8;
55
   
56
   private GLSurfaceView mView;
57
   private DistortedTexture mLeaf;
58
   private DistortedScreen mScreen;
59
   private int mScreenW, mScreenH;
60
   private Static3D mMove, mScale;
61
   private Static1D mRadius;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
69
      final int OUTER = 9;
70
      final int INNER = 4;
71

    
72
      mScreenW = OUTER*LEAF_SIZE;
73
      mScreenH = OUTER*LEAF_SIZE;
74

    
75
      mRadius = new Static1D(5);
76
      Dynamic1D radiusDyn = new Dynamic1D();
77
      radiusDyn.add(mRadius);
78
      PostprocessEffectBlur blurEffect = new PostprocessEffectBlur(radiusDyn);
79

    
80
      mLeaf = new DistortedTexture();
81

    
82
      MeshQuad rootMesh  = new MeshQuad();
83
      rootMesh.setStretch(mScreenW,mScreenH,0);
84
      MeshQuad innerMesh = new MeshQuad();
85
      innerMesh.setStretch(INNER*LEAF_SIZE,INNER*LEAF_SIZE,0);
86
      MeshQuad leafMesh  = new MeshQuad();
87
      leafMesh.setStretch(LEAF_SIZE,LEAF_SIZE,0);
88

    
89
      mMove = new Static3D(0,0,0);
90
      mScale= new Static3D(1,1,1);
91

    
92
      DistortedEffects rootEffects  = new DistortedEffects();
93
      DistortedEffects innerEffects = new DistortedEffects();
94
      DistortedEffects[] innerLeafEffects= new DistortedEffects[NUM_LEAVES];
95
      DistortedEffects[] outerLeafEffects= new DistortedEffects[NUM_LEAVES];
96

    
97
      DistortedNode root = new DistortedNode(new DistortedTexture(), rootEffects, rootMesh);
98

    
99
      rootEffects.apply(new MatrixEffectScale(mScale));
100
      rootEffects.apply(new MatrixEffectMove(mMove));
101
      rootEffects.apply(blurEffect);
102

    
103
      Dynamic1D rotate = new Dynamic1D(5000,0.0f);
104
      rotate.setMode(Dynamic1D.MODE_JUMP);
105
      rotate.add(new Static1D(  0));
106
      rotate.add(new Static1D(360));
107

    
108
      Static3D outerCenter = new Static3D(OUTER*LEAF_SIZE/2, OUTER*LEAF_SIZE/2, 0);
109
      Static3D innerCenter = new Static3D(INNER*LEAF_SIZE/2, INNER*LEAF_SIZE/2, 0);
110

    
111
      Static3D axis   = new Static3D(0,0,1);
112
      Static3D innerMoveVector = new Static3D(            0, (INNER-1)*LEAF_SIZE/2, 0);
113
      Static3D outerMoveVector = new Static3D(3*LEAF_SIZE/2, (OUTER-1)*LEAF_SIZE/2, 0);
114

    
115
      for(int j=0; j<NUM_LEAVES; j++)
116
        {
117
        outerLeafEffects[j] = new DistortedEffects();
118
        outerLeafEffects[j].apply(new MatrixEffectMove(outerMoveVector));
119
        outerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, outerCenter) );
120

    
121
        root.attach(mLeaf, outerLeafEffects[j], leafMesh);
122
        }
123

    
124
      innerEffects.apply( new MatrixEffectRotate(rotate, axis, innerCenter) );
125
      innerEffects.apply( new MatrixEffectMove(new Static3D( (OUTER-INNER)*LEAF_SIZE/2,(OUTER-INNER)*LEAF_SIZE/2, 0)) );
126
      innerEffects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0) ) );
127
      innerEffects.apply(blurEffect);
128

    
129
      DistortedNode innerNode = new DistortedNode( new DistortedTexture(), innerEffects, innerMesh);
130
      root.attach(innerNode);
131

    
132
      for(int j=0; j<NUM_LEAVES; j++)
133
        {
134
        innerLeafEffects[j] = new DistortedEffects();
135
        innerLeafEffects[j].apply(new MatrixEffectMove(innerMoveVector));
136
        innerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, innerCenter) );
137

    
138
        innerNode.attach( mLeaf, innerLeafEffects[j], leafMesh );
139
        }
140

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

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

    
147
   int setBlur(int blur)
148
     {
149
     int radius = blur/2;
150
     mRadius.set(radius);
151
     return radius;
152
     }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
   
156
   public void onDrawFrame(GL10 glUnused)
157
     {
158
     mScreen.render(System.currentTimeMillis());
159
     }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
    
163
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
164
     {
165
     float factor;
166

    
167
     if( (float)mScreenH/mScreenW > (float)height/width )
168
       {
169
       int w = (height*mScreenW)/mScreenH;
170
       factor = (float)height/mScreenH;
171
       mMove.set((width-w)/2 ,0, 0);
172
       }
173
     else
174
       {
175
       int h = (width*mScreenH)/mScreenW;
176
       factor = (float)width/mScreenW;
177
       mMove.set(0,(height-h)/2,0);
178
       }
179

    
180
     mScale.set(factor,factor,factor);
181

    
182
     mScreen.resize(width, height);
183
     }
184

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

    
207
     FragmentEffectChroma.enable();
208
     PostprocessEffectBlur.enable();
209

    
210
     try
211
       {
212
       DistortedLibrary.onCreate(mView.getContext());
213
       }
214
     catch(Exception ex)
215
       {
216
       android.util.Log.e("PostprocessTree", ex.getMessage() );
217
       }
218
     }
219
}
(2-2/3)