Project

General

Profile

« Previous | Next » 

Revision 5ceea148

Added by Leszek Koltunski over 5 years ago

- new icon for the GLOW app
- rename the BitmapTree app to ObjectTree

View differences:

src/main/AndroidManifest.xml
29 29
        <activity android:name=".differentbitmaps.DifferentBitmapsActivity" />    
30 30
        <activity android:name=".effectqueue.EffectQueueActivity" />
31 31
        <activity android:name=".check.CheckActivity" />    
32
        <activity android:name=".bitmaptree.BitmapTreeActivity" />
32
        <activity android:name=".objecttree.ObjectTreeActivity" />
33 33
        <activity android:name=".starwars.StarWarsActivity" />      
34 34
        <activity android:name=".olimpic.OlimpicActivity" />  
35 35
        <activity android:name=".cubes.CubesActivity" />       
src/main/java/org/distorted/examples/TableOfContents.java
49 49
import org.distorted.examples.effectqueue.EffectQueueActivity;
50 50
import org.distorted.examples.check.CheckActivity;
51 51
import org.distorted.examples.bean.BeanActivity;
52
import org.distorted.examples.bitmaptree.BitmapTreeActivity;
52
import org.distorted.examples.objecttree.ObjectTreeActivity;
53 53
import org.distorted.examples.starwars.StarWarsActivity;
54 54
import org.distorted.examples.cubes.CubesActivity;
55 55
import org.distorted.examples.quaternion.QuaternionActivity;
......
220 220
   {
221 221
      final Map<String, Object> item = new HashMap<>();
222 222
      item.put(ITEM_IMAGE, R.drawable.icon_example_fbo);
223
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_bitmaptree));
224
      item.put(ITEM_SUBTITLE, getText(R.string.example_bitmaptree_subtitle));
223
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_objecttree));
224
      item.put(ITEM_SUBTITLE, getText(R.string.example_objecttree_subtitle));
225 225
      data.add(item);
226
      activityMapping.put(i++, BitmapTreeActivity.class);
226
      activityMapping.put(i++, ObjectTreeActivity.class);
227 227
   }
228 228
      
229 229
   {
......
381 381

  
382 382
   {
383 383
      final Map<String, Object> item = new HashMap<>();
384
      item.put(ITEM_IMAGE, R.drawable.icon_example_wip);
384
      item.put(ITEM_IMAGE, R.drawable.icon_example_glow);
385 385
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_glow));
386 386
      item.put(ITEM_SUBTITLE, getText(R.string.example_glow_subtitle));
387 387
      data.add(item);
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.FragmentEffectChroma;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectRotate;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectSink;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedFramebuffer;
37
import org.distorted.library.main.DistortedNode;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.Distorted;
40
import org.distorted.library.mesh.MeshCubes;
41
import org.distorted.library.mesh.MeshFlat;
42
import org.distorted.library.main.DistortedTexture;
43
import org.distorted.library.type.Dynamic;
44
import org.distorted.library.type.Dynamic1D;
45
import org.distorted.library.type.Static1D;
46
import org.distorted.library.type.Static3D;
47

  
48
import android.graphics.Bitmap;
49
import android.graphics.BitmapFactory;
50
import android.opengl.GLES31;
51
import android.opengl.GLSurfaceView;
52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

  
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

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

  
85
      mScreen = new DistortedScreen();
86
      }
87

  
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

  
95
     mRoot.glDepthMask(mDepth);
96

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

  
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

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

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

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

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

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

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

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

  
183
      final int GRID = 10;
184

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

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

  
191
      setDepthPriv();
192

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

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

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

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

  
215
      VertexEffectSink.enable();
216
      FragmentEffectChroma.enable();
217

  
218
      try
219
        {
220
        Distorted.onCreate(mView.getContext());
221
        }
222
      catch(Exception ex)
223
        {
224
        android.util.Log.e("BitmapTree", ex.getMessage() );
225
        }
226
      }
227
}
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.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLSurfaceView;
26
import android.util.AttributeSet;
27

  
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

  
30
class BitmapTreeSurfaceView extends GLSurfaceView
31
{
32
    private BitmapTreeRenderer mRenderer;
33

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

  
40
      if(!isInEditMode())
41
        {
42
        mRenderer = new BitmapTreeRenderer(this);
43
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
44
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
45
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
46
        setRenderer(mRenderer);
47
        }
48
      }
49

  
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

  
52
    public BitmapTreeRenderer getRenderer()
53
      {
54
      return mRenderer;
55
      }
56
}
57

  
src/main/java/org/distorted/examples/objecttree/ObjectTreeActivity.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.objecttree;
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 ObjectTreeActivity 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.objecttreelayout);
42

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

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

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

  
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
    
63
    @Override
64
    protected void onResume() 
65
      {
66
      ObjectTreeSurfaceView view = (ObjectTreeSurfaceView) this.findViewById(R.id.objecttreeSurfaceView);
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
      ObjectTreeSurfaceView view = (ObjectTreeSurfaceView) this.findViewById(R.id.objecttreeSurfaceView);
86
      ObjectTreeRenderer renderer = view.getRenderer();
87

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

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

  
94
    public void DepthNo(View v)
95
      {
96
      ObjectTreeSurfaceView view = (ObjectTreeSurfaceView) this.findViewById(R.id.objecttreeSurfaceView);
97
      ObjectTreeRenderer 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/objecttree/ObjectTreeRenderer.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.objecttree;
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.FragmentEffectChroma;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectRotate;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectSink;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedFramebuffer;
37
import org.distorted.library.main.DistortedNode;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.Distorted;
40
import org.distorted.library.mesh.MeshCubes;
41
import org.distorted.library.mesh.MeshFlat;
42
import org.distorted.library.main.DistortedTexture;
43
import org.distorted.library.type.Dynamic;
44
import org.distorted.library.type.Dynamic1D;
45
import org.distorted.library.type.Static1D;
46
import org.distorted.library.type.Static3D;
47

  
48
import android.graphics.Bitmap;
49
import android.graphics.BitmapFactory;
50
import android.opengl.GLES31;
51
import android.opengl.GLSurfaceView;
52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

  
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

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

  
85
      mScreen = new DistortedScreen();
86
      }
87

  
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

  
95
     mRoot.glDepthMask(mDepth);
96

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

  
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

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

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

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

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

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

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

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

  
183
      final int GRID = 10;
184

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

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

  
191
      setDepthPriv();
192

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

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

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

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

  
215
      VertexEffectSink.enable();
216
      FragmentEffectChroma.enable();
217

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

  
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLSurfaceView;
26
import android.util.AttributeSet;
27

  
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

  
30
class ObjectTreeSurfaceView extends GLSurfaceView
31
{
32
    private ObjectTreeRenderer mRenderer;
33

  
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
   
36
    public ObjectTreeSurfaceView(Context context, AttributeSet attrs)
37
      {
38
      super(context, attrs);
39

  
40
      if(!isInEditMode())
41
        {
42
        mRenderer = new ObjectTreeRenderer(this);
43
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
44
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
45
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
46
        setRenderer(mRenderer);
47
        }
48
      }
49

  
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

  
52
    public ObjectTreeRenderer getRenderer()
53
      {
54
      return mRenderer;
55
      }
56
}
57

  
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/objecttreelayout.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.objecttree.ObjectTreeSurfaceView
8
        android:id="@+id/objecttreeSurfaceView"
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
137 137
    <string name="example_effectqueue_subtitle">Add, remove and list all effects currently acting on a Bitmap.</string>
138 138
    <string name="example_check">Check</string>
139 139
    <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>
140
    <string name="example_bitmaptree">Bitmap Tree</string>
141
    <string name="example_bitmaptree_subtitle">Render one bitmap on top of another.</string>
140
    <string name="example_objecttree">Object Tree</string>
141
    <string name="example_objecttree_subtitle">Render one object on top of another.</string>
142 142
    <string name="example_starwars">Star Wars</string>
143 143
    <string name="example_starwars_subtitle">Shows how to create the opening crawl from Star Wars Episode IV.</string>
144 144
    <string name="example_olimpic">Olimpic Rings</string>

Also available in: Unified diff