Project

General

Profile

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

examples / src / main / java / org / distorted / examples / objecttree / ObjectTreeRenderer.java @ 625c67de

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.MatrixEffectRotate;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectScale;
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.DistortedLibrary;
40
import org.distorted.library.mesh.MeshCubes;
41
import org.distorted.library.mesh.MeshQuad;
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
import org.distorted.library.type.Static4D;
48

    
49
import android.app.ActivityManager;
50
import android.content.Context;
51
import android.content.pm.ConfigurationInfo;
52
import android.content.res.Resources;
53
import android.graphics.Bitmap;
54
import android.graphics.BitmapFactory;
55
import android.opengl.GLES31;
56
import android.opengl.GLSurfaceView;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
class ObjectTreeRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
61
{
62
   private static final int NODE_FBO_SIZE = 300;
63
   private static final int GRID = 10;
64

    
65
   private final GLSurfaceView mView;
66
   private final Resources mResources;
67
   private final DistortedEffects mEffectsNode,mEffectsGrid;
68
   private final DistortedScreen mScreen;
69
   private final Static3D mScale;
70

    
71
   private DistortedTexture mLisaTexture, mGridTexture;
72
   private DistortedNode mRoot;
73
   private MeshQuad mMeshQuad;
74
   private MeshCubes mMeshCubes;
75
   private boolean mDepth;
76
   private float mLisaAspectRatio;
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
   ObjectTreeRenderer(GLSurfaceView v)
81
      {
82
      mView = v;
83
      mResources = v.getResources();
84

    
85
      mDepth  = true;
86
      mScale  = new Static3D(1,1,1);
87
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
88
      chromaDyn.add(new Static1D(0.0f));
89
      chromaDyn.add(new Static1D(0.8f));
90

    
91
      mEffectsNode= new DistortedEffects();
92
      mEffectsNode.apply(new MatrixEffectScale(mScale));
93
      mEffectsNode.apply(new FragmentEffectChroma(chromaDyn, new Static3D(0,0,1)));
94

    
95
      mEffectsGrid = new DistortedEffects();
96

    
97
      float factor = NODE_FBO_SIZE/(2.0f*GRID);
98
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
99
      mEffectsGrid.apply(scale);
100

    
101
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
102
      rotDyn.add(new Static1D(  0));
103
      rotDyn.add(new Static1D(360));
104
      rotDyn.setMode(Dynamic.MODE_JUMP);
105
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(0,0,0));
106
      mEffectsGrid.apply(rotate);
107

    
108
      VertexEffectScale vScale = new VertexEffectScale( new Static3D(GRID,GRID,1) );
109
      mEffectsGrid.apply(vScale);
110

    
111
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
112
      sinkDyn.add(new Static1D(1.0f));
113
      sinkDyn.add(new Static1D(0.3f));
114
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(0,0,0), new Static4D(0,0,0,GRID*0.5f) );
115
      mEffectsGrid.apply(sink);
116

    
117
      mScreen = new DistortedScreen();
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
   private void setDepthPriv()
123
     {
124
     if( mDepth ) mRoot.glEnable (GLES31.GL_DEPTH_TEST);
125
     else         mRoot.glDisable(GLES31.GL_DEPTH_TEST);
126

    
127
     mRoot.glDepthMask(mDepth);
128

    
129
     // we can also, to save memory, delete/recreate
130
     // the depth buffer each time. This is optional.
131
     mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
132
     }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
   void setDepth(boolean depth)
137
      {
138
      mDepth = depth;
139
      if( mRoot!=null ) setDepthPriv();
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
   
144
   public void onDrawFrame(GL10 glUnused)
145
      {
146
      mScreen.render(System.currentTimeMillis());
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
    
151
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
152
      {
153
      float factor= Math.min(width/mLisaAspectRatio, height);
154
      mScale.set( factor,factor,factor );
155
      mScreen.resize(width, height);
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
    
160
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
161
      {
162
      Resources res = mView.getContext().getResources();
163

    
164
      InputStream isLisa = res.openRawResource(R.raw.monalisa);
165
      InputStream isGrid = res.openRawResource(R.raw.grid);
166
      
167
      Bitmap bitmapLisa, bitmapGrid;
168
       
169
      try 
170
        {
171
        bitmapLisa = BitmapFactory.decodeStream(isLisa);
172
        bitmapGrid = BitmapFactory.decodeStream(isGrid);
173
        } 
174
      finally 
175
        {
176
        try 
177
          {
178
          isLisa.close();
179
          isGrid.close();
180
          } 
181
        catch(IOException ignored) { }
182
        }  
183

    
184
      mLisaAspectRatio = ((float)bitmapLisa.getWidth())/bitmapLisa.getHeight();
185

    
186
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture();
187
      if( mGridTexture==null ) mGridTexture = new DistortedTexture();
188
      mLisaTexture.setTexture(bitmapLisa);
189
      mGridTexture.setTexture(bitmapGrid);
190

    
191
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f     ,1.0f     );
192
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRID,1.0f     );
193
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f     ,1.0f/GRID);
194

    
195
      if( mMeshQuad ==null ) mMeshQuad = new MeshQuad();
196
      if( mMeshCubes==null ) mMeshCubes= new MeshCubes(GRID,GRID,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
197

    
198
      mRoot = new DistortedNode(mLisaTexture, mEffectsNode, mMeshQuad);
199
      mRoot.resizeFBO(NODE_FBO_SIZE,NODE_FBO_SIZE);
200
      mRoot.attach(mGridTexture, mEffectsGrid, mMeshCubes);
201

    
202
      setDepthPriv();
203

    
204
      mScreen.detachAll();
205
      mScreen.attach(mRoot);
206

    
207
      VertexEffectScale.enable();
208
      VertexEffectSink.enable();
209
      FragmentEffectChroma.enable();
210

    
211
      DistortedLibrary.onSurfaceCreated(this);
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    public void distortedException(Exception ex)
217
      {
218
      android.util.Log.e("ObjectTree", ex.getMessage() );
219
      }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
    public InputStream localFile(int fileID)
224
      {
225
      return mResources.openRawResource(fileID);
226
      }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
    public void logMessage(String message)
231
      {
232
      android.util.Log.e("ObjectTree", message );
233
      }
234
}
(2-2/3)