Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bitmaptree / BitmapTreeRenderer.java @ 885b9cca

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.VertexEffectDistort;
36
import org.distorted.library.effect.VertexEffectSink;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedFramebuffer;
39
import org.distorted.library.main.DistortedNode;
40
import org.distorted.library.main.DistortedScreen;
41
import org.distorted.library.main.Distorted;
42
import org.distorted.library.main.MeshCubes;
43
import org.distorted.library.main.MeshFlat;
44
import org.distorted.library.main.DistortedTexture;
45
import org.distorted.library.type.Dynamic;
46
import org.distorted.library.type.Dynamic1D;
47
import org.distorted.library.type.Static1D;
48
import org.distorted.library.type.Static3D;
49

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

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

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

    
87
      mScreen = new DistortedScreen();
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

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

    
97
     mRoot.glDepthMask(mDepth);
98

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

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

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

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

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

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

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

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

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

    
185
      final int GRID = 10;
186

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

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

    
193
      setDepthPriv();
194

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

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

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

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

    
217
      VertexEffectSink.enable();
218
      FragmentEffectChroma.enable();
219

    
220
      try
221
        {
222
        Distorted.onCreate(mView.getContext());
223
        }
224
      catch(Exception ex)
225
        {
226
        android.util.Log.e("BitmapTree", ex.getMessage() );
227
        }
228
      }
229
}
(2-2/3)