Project

General

Profile

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

examples / src / main / java / org / distorted / examples / objecttree / ObjectTreeRenderer.java @ 0ba5de00

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.MatrixEffectRotate;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectSink;
34 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedFramebuffer;
36
import org.distorted.library.main.DistortedNode;
37
import org.distorted.library.main.DistortedScreen;
38 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
39 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
40 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
41 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
42 c0486401 Leszek Koltunski
import org.distorted.library.type.Dynamic;
43 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
44
import org.distorted.library.type.Static1D;
45 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
46 5b4a2d76 Leszek Koltunski
import org.distorted.library.type.Static4D;
47 427ab7bf Leszek Koltunski
48
import android.graphics.Bitmap;
49
import android.graphics.BitmapFactory;
50 41a85bb7 Leszek Koltunski
import android.opengl.GLES31;
51 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
52
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 5ceea148 Leszek Koltunski
class ObjectTreeRenderer implements GLSurfaceView.Renderer
56 427ab7bf Leszek Koltunski
{
57
   private GLSurfaceView mView;
58 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
59 59540ba2 Leszek Koltunski
   private DistortedTexture mLisaTexture, mGridTexture;
60 d218d64e leszek
   private DistortedScreen mScreen;
61 8744aa41 Leszek Koltunski
   private DistortedNode mRoot;
62 f3ca895f Leszek Koltunski
   private MeshRectangles mMeshRectangles;
63 5f2a53b6 leszek
   private MeshCubes mMeshCubes;
64 b5636dbf leszek
   private boolean mDepth;
65 16b22aab Leszek Koltunski
   private Static3D mScale;
66 392e16fd Leszek Koltunski
67 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 5ceea148 Leszek Koltunski
   ObjectTreeRenderer(GLSurfaceView v)
70 427ab7bf Leszek Koltunski
      {
71 392e16fd Leszek Koltunski
      mView   = v;
72 b5636dbf leszek
      mDepth  = true;
73 6646c476 leszek
      mScale  = new Static3D(1,1,1);
74 3c70973c Leszek Koltunski
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
75
      chromaDyn.add(new Static1D(0.0f));
76
      chromaDyn.add(new Static1D(0.8f));
77
78 698ad0a8 Leszek Koltunski
      mEffects= new DistortedEffects();
79 6646c476 leszek
      mEffects.apply(new MatrixEffectScale(mScale));
80 3c70973c Leszek Koltunski
      mEffects.apply(new FragmentEffectChroma(chromaDyn, new Static3D(0,0,1)));
81
82 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
83 427ab7bf Leszek Koltunski
      }
84
85 1746198f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87 b5636dbf leszek
   private void setDepthPriv()
88
     {
89 41a85bb7 Leszek Koltunski
     if( mDepth ) mRoot.glEnable (GLES31.GL_DEPTH_TEST);
90
     else         mRoot.glDisable(GLES31.GL_DEPTH_TEST);
91 fc695380 leszek
92 b5636dbf leszek
     mRoot.glDepthMask(mDepth);
93 fc695380 leszek
94 b5636dbf leszek
     // we can also, to save memory, delete/recreate
95
     // the depth buffer each time. This is optional.
96 559da65d Leszek Koltunski
     mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
97 b5636dbf leszek
     }
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101
   void setDepth(boolean depth)
102
      {
103
      mDepth = depth;
104
      if( mRoot!=null ) setDepthPriv();
105 1746198f Leszek Koltunski
      }
106
107 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
108
   
109 1746198f Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
110 427ab7bf Leszek Koltunski
      {
111 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
112 427ab7bf Leszek Koltunski
      }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116 1746198f Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
117 16b22aab Leszek Koltunski
      {
118
      float horiRatio = (float)width / mLisaTexture.getWidth();
119
      float vertRatio = (float)height/ mLisaTexture.getHeight();
120
      float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
121 630703d1 leszek
122 16b22aab Leszek Koltunski
      mScale.set( factor,factor,factor );
123 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
124 427ab7bf Leszek Koltunski
      }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128 1746198f Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
129 427ab7bf Leszek Koltunski
      {
130 3c70973c Leszek Koltunski
      // TODO
131
      // This appears to be needed in case we create the app anew with NO DEPTH
132
      // (i.e. we set 'no depth' and then rotate screen). Investigate.
133 41a85bb7 Leszek Koltunski
      GLES31.glEnable(GLES31.GL_CULL_FACE);
134
      GLES31.glCullFace(GLES31.GL_BACK);
135
      GLES31.glFrontFace(GLES31.GL_CW);
136 e7a4ef16 Leszek Koltunski
137 427ab7bf Leszek Koltunski
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
138 c0486401 Leszek Koltunski
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
139 427ab7bf Leszek Koltunski
      
140
      Bitmap bitmap1, bitmap2;
141
       
142
      try 
143
        {
144
        bitmap1 = BitmapFactory.decodeStream(is1);
145
        bitmap2 = BitmapFactory.decodeStream(is2);
146
        } 
147
      finally 
148
        {
149
        try 
150
          {
151
          is1.close();
152
          is2.close();
153
          } 
154
        catch(IOException e) { }
155
        }  
156 687263cc Leszek Koltunski
157
      final int GRID = 20;
158
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f     ,1.0f     );
159
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRID,1.0f     );
160
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f     ,1.0f/GRID);
161
162 386fd702 Leszek Koltunski
      int lisaWidth = bitmap1.getWidth();
163
      int lisaHeight= bitmap1.getHeight();
164 371d99fa Leszek Koltunski
      int gridWidth = bitmap2.getWidth();
165
      int gridHeight= bitmap2.getHeight();
166 687263cc Leszek Koltunski
      int gridDepth = gridWidth/GRID;
167 f6d884d5 Leszek Koltunski
168 687263cc Leszek Koltunski
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture();
169
      if( mGridTexture==null ) mGridTexture = new DistortedTexture();
170 59540ba2 Leszek Koltunski
      mLisaTexture.setTexture(bitmap1);
171
      mGridTexture.setTexture(bitmap2);
172 e8b6aa95 Leszek Koltunski
173 698ad0a8 Leszek Koltunski
      DistortedEffects gridEffects = new DistortedEffects();
174
175
      if( mMeshRectangles ==null )
176
        {
177
        mMeshRectangles = new MeshRectangles(1,1);
178
        mMeshRectangles.setStretch(lisaWidth,lisaHeight,0);
179
        }
180 4b676b3f Leszek Koltunski
181 698ad0a8 Leszek Koltunski
      if( mMeshCubes==null)
182
        {
183
        mMeshCubes= new MeshCubes(GRID,GRID,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
184
        mMeshCubes.setStretch(gridWidth,gridHeight,gridDepth);
185
        }
186 5b4a2d76 Leszek Koltunski
187 f3ca895f Leszek Koltunski
      mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshRectangles);
188 5f2a53b6 leszek
      mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
189 7589635e Leszek Koltunski
190 b5636dbf leszek
      setDepthPriv();
191
192 fe59d375 Leszek Koltunski
      mScreen.detachAll();
193
      mScreen.attach(mRoot);
194
195 630703d1 leszek
      float factor = lisaWidth/(2.0f*gridWidth);
196 6646c476 leszek
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
197
      gridEffects.apply(scale);
198 7bf107f7 Leszek Koltunski
199 c0486401 Leszek Koltunski
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
200
      rotDyn.add(new Static1D(  0));
201
      rotDyn.add(new Static1D(360));
202
      rotDyn.setMode(Dynamic.MODE_JUMP);
203 16b22aab Leszek Koltunski
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(0,0,0));
204 6646c476 leszek
      gridEffects.apply(rotate);
205 c0486401 Leszek Koltunski
206
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
207 7bf107f7 Leszek Koltunski
      sinkDyn.add(new Static1D(1.0f));
208 c0486401 Leszek Koltunski
      sinkDyn.add(new Static1D(0.3f));
209 5b4a2d76 Leszek Koltunski
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2.0f, gridHeight/2.0f, gridDepth/2.0f) );
210 6646c476 leszek
      gridEffects.apply(sink);
211 59759251 Leszek Koltunski
212 885b9cca leszek
      VertexEffectSink.enable();
213
      FragmentEffectChroma.enable();
214 6637d0f2 Leszek Koltunski
215 427ab7bf Leszek Koltunski
      try
216
        {
217 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
218 427ab7bf Leszek Koltunski
        }
219
      catch(Exception ex)
220
        {
221 5ceea148 Leszek Koltunski
        android.util.Log.e("ObjectTree", ex.getMessage() );
222 427ab7bf Leszek Koltunski
        }
223
      }
224
}