Project

General

Profile

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

examples / src / main / java / org / distorted / examples / objecttree / ObjectTreeRenderer.java @ 513b2e9c

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.VertexEffectSink;
34
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
import org.distorted.library.main.DistortedLibrary;
39
import org.distorted.library.mesh.MeshCubes;
40
import org.distorted.library.mesh.MeshQuad;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.type.Dynamic;
43
import org.distorted.library.type.Dynamic1D;
44
import org.distorted.library.type.Static1D;
45
import org.distorted.library.type.Static3D;
46
import org.distorted.library.type.Static4D;
47

    
48
import android.content.res.Resources;
49
import android.graphics.Bitmap;
50
import android.graphics.BitmapFactory;
51
import android.opengl.GLES31;
52
import android.opengl.GLSurfaceView;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
class ObjectTreeRenderer implements GLSurfaceView.Renderer
57
{
58
   private static final int NODE_FBO_SIZE = 300;
59
   private static final int GRID = 20;
60

    
61
   private GLSurfaceView mView;
62
   private DistortedEffects mEffectsNode, mEffectsGrid;
63
   private DistortedTexture mLisaTexture, mGridTexture;
64
   private DistortedScreen mScreen;
65
   private DistortedNode mRoot;
66
   private MeshQuad mMeshQuad;
67
   private MeshCubes mMeshCubes;
68
   private boolean mDepth;
69
   private Static3D mScale;
70
   private float mLisaAspectRatio;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

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

    
87
      mEffectsGrid = new DistortedEffects();
88

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

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

    
100
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
101
      sinkDyn.add(new Static1D(1.0f));
102
      sinkDyn.add(new Static1D(0.3f));
103
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(0,0,0), new Static4D(0,0,0,10) );
104
      mEffectsGrid.apply(sink);
105

    
106
      mScreen = new DistortedScreen();
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
   private void setDepthPriv()
112
     {
113
     if( mDepth ) mRoot.glEnable (GLES31.GL_DEPTH_TEST);
114
     else         mRoot.glDisable(GLES31.GL_DEPTH_TEST);
115

    
116
     mRoot.glDepthMask(mDepth);
117

    
118
     // we can also, to save memory, delete/recreate
119
     // the depth buffer each time. This is optional.
120
     mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
121
     }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
   void setDepth(boolean depth)
126
      {
127
      mDepth = depth;
128
      if( mRoot!=null ) setDepthPriv();
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
   
133
   public void onDrawFrame(GL10 glUnused)
134
      {
135
      mScreen.render(System.currentTimeMillis());
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
141
      {
142
      float hor   = width /mLisaAspectRatio;
143
      float ver   = height;
144
      float factor= hor>ver ? ver : hor;
145

    
146
      mScale.set( factor,factor,factor );
147
      mScreen.resize(width, height);
148
      }
149

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

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

    
176
      mLisaAspectRatio = ((float)bitmapLisa.getWidth())/bitmapLisa.getHeight();
177

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

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

    
187
      if( mMeshQuad ==null ) mMeshQuad = new MeshQuad();
188
      if( mMeshCubes==null )
189
        {
190
        mMeshCubes= new MeshCubes(GRID,GRID,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
191
        mMeshCubes.setStretch(GRID,GRID,1);
192
        }
193

    
194

    
195
      mRoot = new DistortedNode(mLisaTexture, mEffectsNode, mMeshQuad);
196
      mRoot.resizeFBO(NODE_FBO_SIZE,NODE_FBO_SIZE);
197
      mRoot.attach(mGridTexture, mEffectsGrid, mMeshCubes);
198

    
199
      setDepthPriv();
200

    
201
      mScreen.detachAll();
202
      mScreen.attach(mRoot);
203

    
204
      VertexEffectSink.enable();
205
      FragmentEffectChroma.enable();
206

    
207
      try
208
        {
209
        DistortedLibrary.onCreate(mView.getContext());
210
        }
211
      catch(Exception ex)
212
        {
213
        android.util.Log.e("ObjectTree", ex.getMessage() );
214
        }
215
      }
216
}
(2-2/3)