Project

General

Profile

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

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

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 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
      mScale= new Static3D(1,1,1);
90

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

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

    
98
      rootEffects.apply(new MatrixEffectScale(mScale));
99
      rootEffects.apply(blurEffect);
100

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

    
106
      Static3D center = new Static3D(0,0,0);
107
      Static3D axis   = new Static3D(0,0,1);
108
      Static3D innerMoveVector = new Static3D( (1-INNER)*LEAF_SIZE/2, 0, 0);
109
      Static3D outerMoveVector = new Static3D( (4-OUTER)*LEAF_SIZE/2, 0, 0);
110

    
111
      for(int j=0; j<NUM_LEAVES; j++)
112
        {
113
        outerLeafEffects[j] = new DistortedEffects();
114
        outerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
115
        outerLeafEffects[j].apply(new MatrixEffectMove(outerMoveVector));
116

    
117
        root.attach(mLeaf, outerLeafEffects[j], leafMesh);
118
        }
119

    
120
      innerEffects.apply( new MatrixEffectRotate(rotate, axis, center) );
121
      innerEffects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(1,0,0) ) );
122
      innerEffects.apply(blurEffect);
123

    
124
      DistortedNode innerNode = new DistortedNode( new DistortedTexture(), innerEffects, innerMesh);
125
      root.attach(innerNode);
126

    
127
      for(int j=0; j<NUM_LEAVES; j++)
128
        {
129
        innerLeafEffects[j] = new DistortedEffects();
130
        innerLeafEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
131
        innerLeafEffects[j].apply(new MatrixEffectMove(innerMoveVector));
132

    
133
        innerNode.attach( mLeaf, innerLeafEffects[j], leafMesh );
134
        }
135

    
136
      mScreen = new DistortedScreen();
137
      mScreen.attach(root);
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
   int setBlur(int blur)
143
     {
144
     int radius = blur/2;
145
     mRadius.set(radius);
146
     return radius;
147
     }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
   
151
   public void onDrawFrame(GL10 glUnused)
152
     {
153
     mScreen.render(System.currentTimeMillis());
154
     }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
    
158
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
159
     {
160
     float horiRatio = (float)width / mScreenW;
161
     float vertRatio = (float)height/ mScreenH;
162
     float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
163

    
164
     mScale.set(factor,factor,factor);
165
     mScreen.resize(width, height);
166
     }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
    
170
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
171
     {
172
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
173
     Bitmap leaf;
174
      
175
     try
176
       {
177
       leaf = BitmapFactory.decodeStream(is);
178
       }
179
     finally
180
       {
181
       try
182
         {
183
         is.close();
184
         }
185
       catch(IOException e) { }
186
       }
187
      
188
     mLeaf.setTexture(leaf);
189

    
190
     FragmentEffectChroma.enable();
191
     PostprocessEffectBlur.enable();
192

    
193
     try
194
       {
195
       DistortedLibrary.onCreate(mView.getContext());
196
       }
197
     catch(Exception ex)
198
       {
199
       android.util.Log.e("PostprocessTree", ex.getMessage() );
200
       }
201
     }
202
}
(2-2/3)