Project

General

Profile

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

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

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.content.res.Resources;
50
import android.graphics.Bitmap;
51
import android.graphics.BitmapFactory;
52
import android.opengl.GLES31;
53
import android.opengl.GLSurfaceView;
54

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

    
57
class ObjectTreeRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
58
{
59
   private static final int NODE_FBO_SIZE = 300;
60
   private static final int GRID = 10;
61

    
62
   private final GLSurfaceView mView;
63
   private final DistortedEffects mEffectsNode,mEffectsGrid;
64
   private final DistortedScreen mScreen;
65
   private final Static3D mScale;
66

    
67
   private DistortedTexture mLisaTexture, mGridTexture;
68
   private DistortedNode mRoot;
69
   private MeshQuad mMeshQuad;
70
   private MeshCubes mMeshCubes;
71
   private boolean mDepth;
72
   private float mLisaAspectRatio;
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
   ObjectTreeRenderer(GLSurfaceView v)
77
      {
78
      mView   = v;
79
      mDepth  = true;
80
      mScale  = new Static3D(1,1,1);
81
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
82
      chromaDyn.add(new Static1D(0.0f));
83
      chromaDyn.add(new Static1D(0.8f));
84

    
85
      mEffectsNode= new DistortedEffects();
86
      mEffectsNode.apply(new MatrixEffectScale(mScale));
87
      mEffectsNode.apply(new FragmentEffectChroma(chromaDyn, new Static3D(0,0,1)));
88

    
89
      mEffectsGrid = new DistortedEffects();
90

    
91
      float factor = NODE_FBO_SIZE/(2.0f*GRID);
92
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
93
      mEffectsGrid.apply(scale);
94

    
95
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
96
      rotDyn.add(new Static1D(  0));
97
      rotDyn.add(new Static1D(360));
98
      rotDyn.setMode(Dynamic.MODE_JUMP);
99
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(0,0,0));
100
      mEffectsGrid.apply(rotate);
101

    
102
      VertexEffectScale vScale = new VertexEffectScale( new Static3D(GRID,GRID,1) );
103
      mEffectsGrid.apply(vScale);
104

    
105
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
106
      sinkDyn.add(new Static1D(1.0f));
107
      sinkDyn.add(new Static1D(0.3f));
108
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(0,0,0), new Static4D(0,0,0,GRID*0.5f) );
109
      mEffectsGrid.apply(sink);
110

    
111
      mScreen = new DistortedScreen();
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
   private void setDepthPriv()
117
     {
118
     if( mDepth ) mRoot.glEnable (GLES31.GL_DEPTH_TEST);
119
     else         mRoot.glDisable(GLES31.GL_DEPTH_TEST);
120

    
121
     mRoot.glDepthMask(mDepth);
122

    
123
     // we can also, to save memory, delete/recreate
124
     // the depth buffer each time. This is optional.
125
     mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
126
     }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
   void setDepth(boolean depth)
131
      {
132
      mDepth = depth;
133
      if( mRoot!=null ) setDepthPriv();
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
   
138
   public void onDrawFrame(GL10 glUnused)
139
      {
140
      mScreen.render(System.currentTimeMillis());
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
    
145
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
146
      {
147
      float factor= Math.min(width/mLisaAspectRatio, height);
148
      mScale.set( factor,factor,factor );
149
      mScreen.resize(width, height);
150
      }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
    
154
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
155
      {
156
      Resources res = mView.getContext().getResources();
157

    
158
      InputStream isLisa = res.openRawResource(R.raw.monalisa);
159
      InputStream isGrid = res.openRawResource(R.raw.grid);
160
      
161
      Bitmap bitmapLisa, bitmapGrid;
162
       
163
      try 
164
        {
165
        bitmapLisa = BitmapFactory.decodeStream(isLisa);
166
        bitmapGrid = BitmapFactory.decodeStream(isGrid);
167
        } 
168
      finally 
169
        {
170
        try 
171
          {
172
          isLisa.close();
173
          isGrid.close();
174
          } 
175
        catch(IOException ignored) { }
176
        }  
177

    
178
      mLisaAspectRatio = ((float)bitmapLisa.getWidth())/bitmapLisa.getHeight();
179

    
180
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture();
181
      if( mGridTexture==null ) mGridTexture = new DistortedTexture();
182
      mLisaTexture.setTexture(bitmapLisa);
183
      mGridTexture.setTexture(bitmapGrid);
184

    
185
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f     ,1.0f     );
186
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRID,1.0f     );
187
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f     ,1.0f/GRID);
188

    
189
      if( mMeshQuad ==null ) mMeshQuad = new MeshQuad();
190
      if( mMeshCubes==null ) mMeshCubes= new MeshCubes(GRID,GRID,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
191

    
192
      mRoot = new DistortedNode(mLisaTexture, mEffectsNode, mMeshQuad);
193
      mRoot.resizeFBO(NODE_FBO_SIZE,NODE_FBO_SIZE);
194
      mRoot.attach(mGridTexture, mEffectsGrid, mMeshCubes);
195

    
196
      setDepthPriv();
197

    
198
      mScreen.detachAll();
199
      mScreen.attach(mRoot);
200

    
201
      VertexEffectScale.enable();
202
      VertexEffectSink.enable();
203
      FragmentEffectChroma.enable();
204

    
205
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
206
      }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
    public void distortedException(Exception ex)
211
      {
212
      android.util.Log.e("ObjectTree", ex.getMessage() );
213
      }
214
}
(2-2/3)