Project

General

Profile

« Previous | Next » 

Revision 3c70973c

Added by Leszek Koltunski almost 7 years ago

Fix 'Bitmap Tree' APP. Now 28 out of 31 APPs work.

View differences:

src/main/AndroidManifest.xml
30 30
        <activity android:name=".differentbitmaps.DifferentBitmapsActivity" />    
31 31
        <activity android:name=".effectqueue.EffectQueueActivity" />
32 32
        <activity android:name=".check.CheckActivity" />    
33
        <activity android:name=".fbo.FBOActivity" />    
33
        <activity android:name=".bitmaptree.BitmapTreeActivity" />
34 34
        <activity android:name=".starwars.StarWarsActivity" />      
35 35
        <activity android:name=".olimpic.OlimpicActivity" />  
36 36
        <activity android:name=".cubes.CubesActivity" />       
src/main/java/org/distorted/examples/TableOfContents.java
50 50
import org.distorted.examples.effectqueue.EffectQueueActivity;
51 51
import org.distorted.examples.check.CheckActivity;
52 52
import org.distorted.examples.bean.BeanActivity;
53
import org.distorted.examples.fbo.FBOActivity;
53
import org.distorted.examples.bitmaptree.BitmapTreeActivity;
54 54
import org.distorted.examples.starwars.StarWarsActivity;
55 55
import org.distorted.examples.cubes.CubesActivity;
56 56
import org.distorted.examples.quaternion.QuaternionActivity;
......
216 216
   {
217 217
      final Map<String, Object> item = new HashMap<>();
218 218
      item.put(ITEM_IMAGE, R.drawable.icon_example_fbo);
219
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_fbo));
220
      item.put(ITEM_SUBTITLE, getText(R.string.example_fbo_subtitle));
219
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_bitmaptree));
220
      item.put(ITEM_SUBTITLE, getText(R.string.example_bitmaptree_subtitle));
221 221
      data.add(item);
222
      activityMapping.put(i++, FBOActivity.class);
222
      activityMapping.put(i++, BitmapTreeActivity.class);
223 223
   }
224 224
      
225 225
   {
src/main/java/org/distorted/examples/bitmaptree/BitmapTreeActivity.java
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.bitmaptree;
21

  
22
import org.distorted.examples.R;
23
import org.distorted.library.main.Distorted;
24

  
25
import android.app.Activity;
26
import android.os.Bundle;
27
import android.view.View;
28

  
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

  
31
public class BitmapTreeActivity extends Activity
32
{
33
    private boolean mDepth;
34

  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
    
37
    @Override
38
    protected void onCreate(Bundle savedState)
39
      {
40
      super.onCreate(savedState);
41
      setContentView(R.layout.bitmaptreelayout);
42

  
43
      if( savedState==null )
44
        {
45
        mDepth = true;
46
        }
47
      }
48

  
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
    
51
    @Override
52
    protected void onPause() 
53
      {
54
      BitmapTreeSurfaceView view = (BitmapTreeSurfaceView) this.findViewById(R.id.bitmaptreeSurfaceView);
55

  
56
      view.onPause();
57
      Distorted.onPause();
58
      super.onPause();
59
      }
60

  
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
    
63
    @Override
64
    protected void onResume() 
65
      {
66
      BitmapTreeSurfaceView view = (BitmapTreeSurfaceView) this.findViewById(R.id.bitmaptreeSurfaceView);
67

  
68
      super.onResume();
69
      view.onResume();
70
      }
71
    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74
    @Override
75
    protected void onDestroy() 
76
      {
77
      Distorted.onDestroy();  
78
      super.onDestroy();
79
      }
80

  
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

  
83
    public void DepthYes(View v)
84
      {
85
      BitmapTreeSurfaceView view = (BitmapTreeSurfaceView) this.findViewById(R.id.bitmaptreeSurfaceView);
86
      BitmapTreeRenderer renderer = view.getRenderer();
87

  
88
      renderer.setDepth(true);
89
      mDepth = true;
90
      }
91

  
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

  
94
    public void DepthNo(View v)
95
      {
96
      BitmapTreeSurfaceView view = (BitmapTreeSurfaceView) this.findViewById(R.id.bitmaptreeSurfaceView);
97
      BitmapTreeRenderer renderer = view.getRenderer();
98

  
99
      renderer.setDepth(false);
100
      mDepth = false;
101
      }
102

  
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

  
105
    @Override
106
    public void onSaveInstanceState(Bundle savedInstanceState)
107
      {
108
      super.onSaveInstanceState(savedInstanceState);
109
      savedInstanceState.putBoolean("depth", mDepth);
110
      }
111

  
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

  
114
    @Override
115
    public void onRestoreInstanceState(Bundle savedInstanceState)
116
      {
117
      super.onRestoreInstanceState(savedInstanceState);
118

  
119
      mDepth = savedInstanceState.getBoolean("depth");
120

  
121
      if(mDepth) DepthYes(null);
122
      else       DepthNo(null);
123
      }
124

  
125
}
src/main/java/org/distorted/examples/bitmaptree/BitmapTreeRenderer.java
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.bitmaptree;
21

  
22
import java.io.IOException;
23
import java.io.InputStream;
24

  
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27

  
28
import org.distorted.examples.R;
29

  
30
import org.distorted.library.effect.EffectName;
31
import org.distorted.library.effect.FragmentEffectChroma;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectRotate;
34
import org.distorted.library.effect.MatrixEffectScale;
35
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedFramebuffer;
38
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedScreen;
40
import org.distorted.library.main.Distorted;
41
import org.distorted.library.main.MeshCubes;
42
import org.distorted.library.main.MeshFlat;
43
import org.distorted.library.main.DistortedTexture;
44
import org.distorted.library.type.Dynamic;
45
import org.distorted.library.type.Dynamic1D;
46
import org.distorted.library.type.Static1D;
47
import org.distorted.library.type.Static3D;
48

  
49
import android.graphics.Bitmap;
50
import android.graphics.BitmapFactory;
51
import android.opengl.GLES30;
52
import android.opengl.GLSurfaceView;
53

  
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

  
56
class BitmapTreeRenderer implements GLSurfaceView.Renderer
57
{
58
   private GLSurfaceView mView;
59
   private DistortedEffects mEffects;
60
   private DistortedTexture mLisaTexture, mGridTexture;
61
   private DistortedScreen mScreen;
62
   private DistortedNode mRoot;
63
   private MeshFlat mMeshFlat;
64
   private MeshCubes mMeshCubes;
65
   private int lisaHeight, lisaWidth;
66
   private boolean mDepth;
67
   private Static3D mScale, mMove;
68

  
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

  
71
   BitmapTreeRenderer(GLSurfaceView v)
72
      {
73
      mView   = v;
74
      mDepth  = true;
75
      mScale  = new Static3D(1,1,1);
76
      mMove   = new Static3D(0,0,0);
77
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
78
      chromaDyn.add(new Static1D(0.0f));
79
      chromaDyn.add(new Static1D(0.8f));
80

  
81
      mEffects= new DistortedEffects();
82
      mEffects.apply(new MatrixEffectMove(mMove));
83
      mEffects.apply(new MatrixEffectScale(mScale));
84
      mEffects.apply(new FragmentEffectChroma(chromaDyn, new Static3D(0,0,1)));
85

  
86
      mScreen = new DistortedScreen(mView);
87
      }
88

  
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

  
91
   private void setDepthPriv()
92
     {
93
     if( mDepth ) mRoot.glEnable (GLES30.GL_DEPTH_TEST);
94
     else         mRoot.glDisable(GLES30.GL_DEPTH_TEST);
95

  
96
     mRoot.glDepthMask(mDepth);
97

  
98
     // we can also, to save memory, delete/recreate
99
     // the depth buffer each time. This is optional.
100
     mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
101
     }
102

  
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

  
105
   void setDepth(boolean depth)
106
      {
107
      mDepth = depth;
108
      if( mRoot!=null ) setDepthPriv();
109
      }
110

  
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
   
113
   public void onDrawFrame(GL10 glUnused)
114
      {
115
      mScreen.render(System.currentTimeMillis());
116
      }
117

  
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
121
      { 
122
      if( (float)lisaHeight/lisaWidth > (float)height/width )
123
        {
124
        int w = (height*lisaWidth)/lisaHeight;
125
        float factor = (float)height/lisaHeight;
126

  
127
        mMove.set((width-w)/2,0,0);
128
        mScale.set(factor,factor,factor);
129
        }
130
      else
131
        {
132
        int h = (width*lisaHeight)/lisaWidth;
133
        float factor = (float)width/lisaWidth;
134

  
135
        mMove.set(0,(height-h)/2,0);
136
        mScale.set(factor,factor,factor);
137
        }
138
      
139
      mScreen.resize(width, height);
140
      }
141

  
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
    
144
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
145
      {
146
      // TODO
147
      // This appears to be needed in case we create the app anew with NO DEPTH
148
      // (i.e. we set 'no depth' and then rotate screen). Investigate.
149
      GLES30.glEnable(GLES30.GL_CULL_FACE);
150
      GLES30.glCullFace(GLES30.GL_BACK);
151
      GLES30.glFrontFace(GLES30.GL_CW);
152

  
153
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
154
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
155
      
156
      Bitmap bitmap1, bitmap2;
157
       
158
      try 
159
        {
160
        bitmap1 = BitmapFactory.decodeStream(is1);
161
        bitmap2 = BitmapFactory.decodeStream(is2);
162
        } 
163
      finally 
164
        {
165
        try 
166
          {
167
          is1.close();
168
          is2.close();
169
          } 
170
        catch(IOException e) { }
171
        }  
172
      
173
      lisaWidth     = bitmap1.getWidth();
174
      lisaHeight    = bitmap1.getHeight();
175
      int gridWidth = bitmap2.getWidth();
176
      int gridHeight= bitmap2.getHeight();
177

  
178
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture(lisaWidth,lisaHeight);
179
      if( mGridTexture==null ) mGridTexture = new DistortedTexture(gridWidth,gridHeight);
180
      mLisaTexture.setTexture(bitmap1);
181
      mGridTexture.setTexture(bitmap2);
182
      DistortedEffects gridEffects = new DistortedEffects();
183

  
184
      final int GRID = 10;
185

  
186
      if( mMeshFlat==null ) mMeshFlat = new MeshFlat(1,1);
187
      if( mMeshCubes==null) mMeshCubes= new MeshCubes(GRID,GRID,1);
188

  
189
      mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshFlat);
190
      mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
191

  
192
      setDepthPriv();
193

  
194
      mScreen.detachAll();
195
      mScreen.attach(mRoot);
196

  
197
      float factor = lisaWidth/(2.0f*gridWidth);
198
      MatrixEffectMove move = new MatrixEffectMove( new Static3D((lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2, gridWidth/(2.0f*GRID)));
199
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
200
      gridEffects.apply(move);
201
      gridEffects.apply(scale);
202

  
203
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
204
      rotDyn.add(new Static1D(  0));
205
      rotDyn.add(new Static1D(360));
206
      rotDyn.setMode(Dynamic.MODE_JUMP);
207
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridWidth/(2*GRID)));
208
      gridEffects.apply(rotate);
209

  
210
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
211
      sinkDyn.add(new Static1D(1.0f));
212
      sinkDyn.add(new Static1D(0.3f));
213
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
214
      gridEffects.apply(sink);
215

  
216
      DistortedEffects.enableEffect(EffectName.SINK);
217
      DistortedEffects.enableEffect(EffectName.CHROMA);
218

  
219
      try
220
        {
221
        Distorted.onCreate(mView.getContext());
222
        }
223
      catch(Exception ex)
224
        {
225
        android.util.Log.e("BitmapTree", ex.getMessage() );
226
        }
227
      }
228
}
src/main/java/org/distorted/examples/bitmaptree/BitmapTreeSurfaceView.java
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.bitmaptree;
21

  
22
import android.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.util.AttributeSet;
25

  
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

  
28
class BitmapTreeSurfaceView extends GLSurfaceView
29
{
30
    private BitmapTreeRenderer mRenderer;
31

  
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
   
34
    public BitmapTreeSurfaceView(Context context, AttributeSet attrs)
35
      {
36
      super(context, attrs);
37

  
38
      if(!isInEditMode())
39
        {
40
        mRenderer = new BitmapTreeRenderer(this);
41
        setRenderer(mRenderer);
42
        }
43
      }
44

  
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

  
47
    public BitmapTreeRenderer getRenderer()
48
      {
49
      return mRenderer;
50
      }
51
}
52

  
src/main/java/org/distorted/examples/fbo/FBOActivity.java
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.fbo;
21

  
22
import org.distorted.examples.R;
23
import org.distorted.library.main.Distorted;
24

  
25
import android.app.Activity;
26
import android.os.Bundle;
27
import android.view.View;
28

  
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

  
31
public class FBOActivity extends Activity 
32
{
33
    private boolean mDepth;
34

  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
    
37
    @Override
38
    protected void onCreate(Bundle savedState)
39
      {
40
      super.onCreate(savedState);
41
      setContentView(R.layout.fbolayout);
42

  
43
      if( savedState==null )
44
        {
45
        mDepth = true;
46
        }
47
      }
48

  
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
    
51
    @Override
52
    protected void onPause() 
53
      {
54
      FBOSurfaceView view = (FBOSurfaceView) this.findViewById(R.id.fboSurfaceView);
55

  
56
      view.onPause();
57
      Distorted.onPause();
58
      super.onPause();
59
      }
60

  
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
    
63
    @Override
64
    protected void onResume() 
65
      {
66
      FBOSurfaceView view = (FBOSurfaceView) this.findViewById(R.id.fboSurfaceView);
67

  
68
      super.onResume();
69
      view.onResume();
70
      }
71
    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74
    @Override
75
    protected void onDestroy() 
76
      {
77
      Distorted.onDestroy();  
78
      super.onDestroy();
79
      }
80

  
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

  
83
    public void DepthYes(View v)
84
      {
85
      FBOSurfaceView view = (FBOSurfaceView) this.findViewById(R.id.fboSurfaceView);
86
      FBORenderer renderer = view.getRenderer();
87

  
88
      renderer.setDepth(true);
89
      mDepth = true;
90
      }
91

  
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

  
94
    public void DepthNo(View v)
95
      {
96
      FBOSurfaceView view = (FBOSurfaceView) this.findViewById(R.id.fboSurfaceView);
97
      FBORenderer renderer = view.getRenderer();
98

  
99
      renderer.setDepth(false);
100
      mDepth = false;
101
      }
102

  
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

  
105
    @Override
106
    public void onSaveInstanceState(Bundle savedInstanceState)
107
      {
108
      super.onSaveInstanceState(savedInstanceState);
109
      savedInstanceState.putBoolean("depth", mDepth);
110
      }
111

  
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

  
114
    @Override
115
    public void onRestoreInstanceState(Bundle savedInstanceState)
116
      {
117
      super.onRestoreInstanceState(savedInstanceState);
118

  
119
      mDepth = savedInstanceState.getBoolean("depth");
120

  
121
      if(mDepth) DepthYes(null);
122
      else       DepthNo(null);
123
      }
124

  
125
}
src/main/java/org/distorted/examples/fbo/FBORenderer.java
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.fbo;
21

  
22
import java.io.IOException;
23
import java.io.InputStream;
24

  
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27

  
28
import org.distorted.examples.R;
29

  
30
import org.distorted.library.effect.EffectName;
31
import org.distorted.library.effect.FragmentEffectChroma;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectRotate;
34
import org.distorted.library.effect.MatrixEffectScale;
35
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedFramebuffer;
38
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedScreen;
40
import org.distorted.library.main.Distorted;
41
import org.distorted.library.main.MeshCubes;
42
import org.distorted.library.main.MeshFlat;
43
import org.distorted.library.main.DistortedTexture;
44
import org.distorted.library.type.Dynamic;
45
import org.distorted.library.type.Dynamic1D;
46
import org.distorted.library.type.Static1D;
47
import org.distorted.library.type.Static3D;
48

  
49
import android.graphics.Bitmap;
50
import android.graphics.BitmapFactory;
51
import android.opengl.GLES30;
52
import android.opengl.GLSurfaceView;
53

  
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

  
56
class FBORenderer implements GLSurfaceView.Renderer 
57
{
58
   private GLSurfaceView mView;
59
   private DistortedEffects mEffects;
60
   private DistortedTexture mLisaTexture, mGridTexture;
61
   private DistortedScreen mScreen;
62
   private DistortedNode mRoot;
63
   private MeshFlat mMeshFlat;
64
   private MeshCubes mMeshCubes;
65
   private int lisaHeight, lisaWidth;
66
   private boolean mDepth;
67
   private Static3D mScale, mMove;
68

  
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

  
71
   FBORenderer(GLSurfaceView v)
72
      {
73
      mView   = v;
74
      mDepth  = true;
75
      mEffects= new DistortedEffects();
76
      mScale  = new Static3D(1,1,1);
77
      mMove   = new Static3D(0,0,0);
78
      mEffects.apply(new MatrixEffectMove(mMove));
79
      mEffects.apply(new MatrixEffectScale(mScale));
80
      mScreen = new DistortedScreen(mView);
81
      }
82

  
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

  
85
   private void setDepthPriv()
86
     {
87
     if( mDepth ) mRoot.glEnable (GLES30.GL_DEPTH_TEST);
88
     else         mRoot.glDisable(GLES30.GL_DEPTH_TEST);
89

  
90
     mRoot.glDepthMask(mDepth);
91

  
92
     // we can also, to save memory, delete/recreate
93
     // the depth buffer each time. This is optional.
94
     mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
95
     }
96

  
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

  
99
   void setDepth(boolean depth)
100
      {
101
      mDepth = depth;
102
      if( mRoot!=null ) setDepthPriv();
103
      }
104

  
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107
   public void onDrawFrame(GL10 glUnused)
108
      {
109
      mScreen.render(System.currentTimeMillis());
110
      }
111

  
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
115
      { 
116
      if( (float)lisaHeight/lisaWidth > (float)height/width )
117
        {
118
        int w = (height*lisaWidth)/lisaHeight;
119
        float factor = (float)height/lisaHeight;
120

  
121
        mMove.set((width-w)/2,0,0);
122
        mScale.set(factor,factor,factor);
123
        }
124
      else
125
        {
126
        int h = (width*lisaHeight)/lisaWidth;
127
        float factor = (float)width/lisaWidth;
128

  
129
        mMove.set(0,(height-h)/2,0);
130
        mScale.set(factor,factor,factor);
131
        }
132
      
133
      mScreen.resize(width, height);
134
      }
135

  
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
139
      {
140
      GLES30.glEnable(GLES30.GL_CULL_FACE);
141
      GLES30.glCullFace(GLES30.GL_BACK);
142
      GLES30.glFrontFace(GLES30.GL_CW);
143

  
144
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
145
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
146
      
147
      Bitmap bitmap1, bitmap2;
148
       
149
      try 
150
        {
151
        bitmap1 = BitmapFactory.decodeStream(is1);
152
        bitmap2 = BitmapFactory.decodeStream(is2);
153
        } 
154
      finally 
155
        {
156
        try 
157
          {
158
          is1.close();
159
          is2.close();
160
          } 
161
        catch(IOException e) { }
162
        }  
163
      
164
      lisaWidth     = bitmap1.getWidth();
165
      lisaHeight    = bitmap1.getHeight();
166
      int gridWidth = bitmap2.getWidth();
167
      int gridHeight= bitmap2.getHeight();
168

  
169
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture(lisaWidth,lisaHeight);
170
      if( mGridTexture==null ) mGridTexture = new DistortedTexture(gridWidth,gridHeight);
171
      mLisaTexture.setTexture(bitmap1);
172
      mGridTexture.setTexture(bitmap2);
173
      DistortedEffects gridEffects = new DistortedEffects();
174

  
175
      mEffects.abortAllEffects();
176

  
177
      final int GRID = 10;
178

  
179
      if( mMeshFlat==null ) mMeshFlat = new MeshFlat(1,1);
180
      if( mMeshCubes==null) mMeshCubes= new MeshCubes(GRID,GRID,1);
181

  
182
      mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshFlat);
183
      mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
184

  
185
      setDepthPriv();
186

  
187
      mScreen.detachAll();
188
      mScreen.attach(mRoot);
189

  
190
      float factor = lisaWidth/(2.0f*gridWidth);
191
      MatrixEffectMove move = new MatrixEffectMove( new Static3D((lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2, gridWidth/(2.0f*GRID)));
192
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
193
      gridEffects.apply(move);
194
      gridEffects.apply(scale);
195

  
196
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
197
      rotDyn.add(new Static1D(  0));
198
      rotDyn.add(new Static1D(360));
199
      rotDyn.setMode(Dynamic.MODE_JUMP);
200
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridWidth/(2*GRID)));
201
      gridEffects.apply(rotate);
202

  
203
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
204
      sinkDyn.add(new Static1D(1.0f));
205
      sinkDyn.add(new Static1D(0.3f));
206
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
207
      gridEffects.apply(sink);
208

  
209
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
210
      chromaDyn.add(new Static1D(0.0f));
211
      chromaDyn.add(new Static1D(1.0f));
212
      FragmentEffectChroma chroma = new FragmentEffectChroma(chromaDyn, new Static3D(0,0,1));
213
      mEffects.apply(chroma);
214

  
215
      DistortedEffects.enableEffect(EffectName.SINK);
216
      DistortedEffects.enableEffect(EffectName.CHROMA);
217

  
218
      try
219
        {
220
        Distorted.onCreate(mView.getContext());
221
        }
222
      catch(Exception ex)
223
        {
224
        android.util.Log.e("FBO", ex.getMessage() );
225
        }
226
      }
227
}
src/main/java/org/distorted/examples/fbo/FBOSurfaceView.java
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.fbo;
21

  
22
import android.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.util.AttributeSet;
25

  
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

  
28
class FBOSurfaceView extends GLSurfaceView 
29
{
30
    private FBORenderer mRenderer;
31

  
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
   
34
    public FBOSurfaceView(Context context, AttributeSet attrs)
35
      {
36
      super(context, attrs);
37

  
38
      if(!isInEditMode())
39
        {
40
        mRenderer = new FBORenderer(this);
41
        setRenderer(mRenderer);
42
        }
43
      }
44

  
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

  
47
    public FBORenderer getRenderer()
48
      {
49
      return mRenderer;
50
      }
51
}
52

  
src/main/res/layout/bitmaptreelayout.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    android:layout_width="fill_parent"
4
    android:layout_height="fill_parent"
5
    android:orientation="vertical" >
6

  
7
    <org.distorted.examples.bitmaptree.BitmapTreeSurfaceView
8
        android:id="@+id/bitmaptreeSurfaceView"
9
        android:layout_width="fill_parent"
10
        android:layout_height="0dp"
11
        android:layout_weight="1" />
12

  
13
    <LinearLayout
14
        android:id="@+id/linearLayout1"
15
        android:layout_width="fill_parent"
16
        android:layout_height="wrap_content"
17
        android:gravity="center|fill_horizontal"
18
        android:orientation="vertical">
19

  
20
        <RadioGroup
21
            android:id="@+id/radioGroup1"
22
            android:layout_width="match_parent"
23
            android:layout_height="wrap_content"
24
            android:orientation="horizontal">
25

  
26
            <RadioButton
27
                android:id="@+id/depthYesButton"
28
                android:layout_width="wrap_content"
29
                android:layout_height="wrap_content"
30
                android:checked="true"
31
                android:onClick="DepthYes"
32
                android:text="@string/DepthYes"/>
33

  
34
            <RadioButton
35
                android:id="@+id/depthNoButton"
36
                android:layout_width="wrap_content"
37
                android:layout_height="wrap_content"
38
                android:onClick="DepthNo"
39
                android:text="@string/DepthNo"/>
40

  
41
        </RadioGroup>
42

  
43
    </LinearLayout>
44

  
45
</LinearLayout>
src/main/res/layout/fbolayout.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    android:layout_width="fill_parent"
4
    android:layout_height="fill_parent"
5
    android:orientation="vertical" >
6

  
7
    <org.distorted.examples.fbo.FBOSurfaceView
8
        android:id="@+id/fboSurfaceView"
9
        android:layout_width="fill_parent"
10
        android:layout_height="0dp"
11
        android:layout_weight="1" />
12

  
13
    <LinearLayout
14
        android:id="@+id/linearLayout1"
15
        android:layout_width="fill_parent"
16
        android:layout_height="wrap_content"
17
        android:gravity="center|fill_horizontal"
18
        android:orientation="vertical">
19

  
20
        <RadioGroup
21
            android:id="@+id/radioGroup1"
22
            android:layout_width="match_parent"
23
            android:layout_height="wrap_content"
24
            android:orientation="horizontal">
25

  
26
            <RadioButton
27
                android:id="@+id/depthYesButton"
28
                android:layout_width="wrap_content"
29
                android:layout_height="wrap_content"
30
                android:checked="true"
31
                android:onClick="DepthYes"
32
                android:text="@string/DepthYes"/>
33

  
34
            <RadioButton
35
                android:id="@+id/depthNoButton"
36
                android:layout_width="wrap_content"
37
                android:layout_height="wrap_content"
38
                android:onClick="DepthNo"
39
                android:text="@string/DepthNo"/>
40

  
41
        </RadioGroup>
42

  
43
    </LinearLayout>
44

  
45
</LinearLayout>
src/main/res/values/strings.xml
113 113
    <string name="example_effectqueue_subtitle">Add, remove and list all effects currently acting on a Bitmap.</string>
114 114
    <string name="example_check">Check</string>  
115 115
    <string name="example_check_subtitle">Check the maximum number of effects (separately for vertex and fragment shaders) we can apply to a single bitmap.</string>
116
    <string name="example_fbo">Bitmap Tree</string>  
117
    <string name="example_fbo_subtitle">Render one bitmap on top of another.</string>
116
    <string name="example_bitmaptree">Bitmap Tree</string>
117
    <string name="example_bitmaptree_subtitle">Render one bitmap on top of another.</string>
118 118
    <string name="example_starwars">Star Wars</string>  
119 119
    <string name="example_starwars_subtitle">Shows how to create the opening crawl from Star Wars Episode IV.</string>
120 120
    <string name="example_olimpic">Olimpic Rings</string>  

Also available in: Unified diff