Project

General

Profile

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

examples / src / main / java / org / distorted / examples / objecttree / ObjectTreeRenderer.java @ 698ad0a8

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19 427ab7bf Leszek Koltunski
20 5ceea148 Leszek Koltunski
package org.distorted.examples.objecttree;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 6646c476 leszek
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 01782e85 Leszek Koltunski
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 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
40 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
41 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
42 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
43 c0486401 Leszek Koltunski
import org.distorted.library.type.Dynamic;
44 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
45
import org.distorted.library.type.Static1D;
46 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
47 5b4a2d76 Leszek Koltunski
import org.distorted.library.type.Static4D;
48 427ab7bf Leszek Koltunski
49
import android.graphics.Bitmap;
50
import android.graphics.BitmapFactory;
51 41a85bb7 Leszek Koltunski
import android.opengl.GLES31;
52 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
53
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 5ceea148 Leszek Koltunski
class ObjectTreeRenderer implements GLSurfaceView.Renderer
57 427ab7bf Leszek Koltunski
{
58
   private GLSurfaceView mView;
59 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
60 59540ba2 Leszek Koltunski
   private DistortedTexture mLisaTexture, mGridTexture;
61 d218d64e leszek
   private DistortedScreen mScreen;
62 8744aa41 Leszek Koltunski
   private DistortedNode mRoot;
63 f3ca895f Leszek Koltunski
   private MeshRectangles mMeshRectangles;
64 5f2a53b6 leszek
   private MeshCubes mMeshCubes;
65 392e16fd Leszek Koltunski
   private int lisaHeight, lisaWidth;
66 b5636dbf leszek
   private boolean mDepth;
67 6646c476 leszek
   private Static3D mScale, mMove;
68 392e16fd Leszek Koltunski
69 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 5ceea148 Leszek Koltunski
   ObjectTreeRenderer(GLSurfaceView v)
72 427ab7bf Leszek Koltunski
      {
73 392e16fd Leszek Koltunski
      mView   = v;
74 b5636dbf leszek
      mDepth  = true;
75 6646c476 leszek
      mScale  = new Static3D(1,1,1);
76
      mMove   = new Static3D(0,0,0);
77 3c70973c Leszek Koltunski
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
78
      chromaDyn.add(new Static1D(0.0f));
79
      chromaDyn.add(new Static1D(0.8f));
80
81 698ad0a8 Leszek Koltunski
      mEffects= new DistortedEffects();
82 6646c476 leszek
      mEffects.apply(new MatrixEffectScale(mScale));
83 dae661e9 Leszek Koltunski
      mEffects.apply(new MatrixEffectMove(mMove));
84 3c70973c Leszek Koltunski
      mEffects.apply(new FragmentEffectChroma(chromaDyn, new Static3D(0,0,1)));
85
86 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
87 427ab7bf Leszek Koltunski
      }
88
89 1746198f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91 b5636dbf leszek
   private void setDepthPriv()
92
     {
93 41a85bb7 Leszek Koltunski
     if( mDepth ) mRoot.glEnable (GLES31.GL_DEPTH_TEST);
94
     else         mRoot.glDisable(GLES31.GL_DEPTH_TEST);
95 fc695380 leszek
96 b5636dbf leszek
     mRoot.glDepthMask(mDepth);
97 fc695380 leszek
98 b5636dbf leszek
     // we can also, to save memory, delete/recreate
99
     // the depth buffer each time. This is optional.
100 559da65d Leszek Koltunski
     mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
101 b5636dbf leszek
     }
102
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
105
   void setDepth(boolean depth)
106
      {
107
      mDepth = depth;
108
      if( mRoot!=null ) setDepthPriv();
109 1746198f Leszek Koltunski
      }
110
111 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
112
   
113 1746198f Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
114 427ab7bf Leszek Koltunski
      {
115 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
116 427ab7bf Leszek Koltunski
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120 1746198f Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
121 427ab7bf Leszek Koltunski
      { 
122 630703d1 leszek
      if( (float)lisaHeight/lisaWidth > (float)height/width )
123
        {
124
        int w = (height*lisaWidth)/lisaHeight;
125
        float factor = (float)height/lisaHeight;
126
127 6646c476 leszek
        mMove.set((width-w)/2,0,0);
128
        mScale.set(factor,factor,factor);
129 630703d1 leszek
        }
130
      else
131
        {
132
        int h = (width*lisaHeight)/lisaWidth;
133
        float factor = (float)width/lisaWidth;
134
135 6646c476 leszek
        mMove.set(0,(height-h)/2,0);
136
        mScale.set(factor,factor,factor);
137 630703d1 leszek
        }
138 427ab7bf Leszek Koltunski
      
139 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
140 427ab7bf Leszek Koltunski
      }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
    
144 1746198f Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
145 427ab7bf Leszek Koltunski
      {
146 3c70973c Leszek Koltunski
      // 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 41a85bb7 Leszek Koltunski
      GLES31.glEnable(GLES31.GL_CULL_FACE);
150
      GLES31.glCullFace(GLES31.GL_BACK);
151
      GLES31.glFrontFace(GLES31.GL_CW);
152 e7a4ef16 Leszek Koltunski
153 427ab7bf Leszek Koltunski
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
154 c0486401 Leszek Koltunski
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
155 427ab7bf Leszek Koltunski
      
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 687263cc Leszek Koltunski
173
      final int GRID = 20;
174
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f     ,1.0f     );
175
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRID,1.0f     );
176
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f     ,1.0f/GRID);
177
178 f6d884d5 Leszek Koltunski
      lisaWidth     = bitmap1.getWidth();
179
      lisaHeight    = bitmap1.getHeight();
180 371d99fa Leszek Koltunski
      int gridWidth = bitmap2.getWidth();
181
      int gridHeight= bitmap2.getHeight();
182 687263cc Leszek Koltunski
      int gridDepth = gridWidth/GRID;
183 f6d884d5 Leszek Koltunski
184 687263cc Leszek Koltunski
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture();
185
      if( mGridTexture==null ) mGridTexture = new DistortedTexture();
186 59540ba2 Leszek Koltunski
      mLisaTexture.setTexture(bitmap1);
187
      mGridTexture.setTexture(bitmap2);
188 e8b6aa95 Leszek Koltunski
189 698ad0a8 Leszek Koltunski
      DistortedEffects gridEffects = new DistortedEffects();
190
191
      if( mMeshRectangles ==null )
192
        {
193
        mMeshRectangles = new MeshRectangles(1,1);
194
        mMeshRectangles.setStretch(lisaWidth,lisaHeight,0);
195
        }
196 4b676b3f Leszek Koltunski
197 698ad0a8 Leszek Koltunski
      if( mMeshCubes==null)
198
        {
199
        mMeshCubes= new MeshCubes(GRID,GRID,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
200
        mMeshCubes.setStretch(gridWidth,gridHeight,gridDepth);
201
        }
202 5b4a2d76 Leszek Koltunski
203 f3ca895f Leszek Koltunski
      mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshRectangles);
204 5f2a53b6 leszek
      mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
205 7589635e Leszek Koltunski
206 b5636dbf leszek
      setDepthPriv();
207
208 fe59d375 Leszek Koltunski
      mScreen.detachAll();
209
      mScreen.attach(mRoot);
210
211 630703d1 leszek
      float factor = lisaWidth/(2.0f*gridWidth);
212 6646c476 leszek
      MatrixEffectMove move = new MatrixEffectMove( new Static3D((lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2, gridWidth/(2.0f*GRID)));
213
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
214
      gridEffects.apply(scale);
215 dae661e9 Leszek Koltunski
      gridEffects.apply(move);
216 7bf107f7 Leszek Koltunski
217 c0486401 Leszek Koltunski
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
218
      rotDyn.add(new Static1D(  0));
219
      rotDyn.add(new Static1D(360));
220
      rotDyn.setMode(Dynamic.MODE_JUMP);
221 6646c476 leszek
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridWidth/(2*GRID)));
222
      gridEffects.apply(rotate);
223 c0486401 Leszek Koltunski
224
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
225 7bf107f7 Leszek Koltunski
      sinkDyn.add(new Static1D(1.0f));
226 c0486401 Leszek Koltunski
      sinkDyn.add(new Static1D(0.3f));
227 5b4a2d76 Leszek Koltunski
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2.0f, gridHeight/2.0f, gridDepth/2.0f) );
228 6646c476 leszek
      gridEffects.apply(sink);
229 59759251 Leszek Koltunski
230 885b9cca leszek
      VertexEffectSink.enable();
231
      FragmentEffectChroma.enable();
232 6637d0f2 Leszek Koltunski
233 427ab7bf Leszek Koltunski
      try
234
        {
235 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
236 427ab7bf Leszek Koltunski
        }
237
      catch(Exception ex)
238
        {
239 5ceea148 Leszek Koltunski
        android.util.Log.e("ObjectTree", ex.getMessage() );
240 427ab7bf Leszek Koltunski
        }
241
      }
242
}