Project

General

Profile

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

examples / src / main / java / org / distorted / examples / objecttree / ObjectTreeRenderer.java @ 8eed34d3

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.MeshRectangles;
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.graphics.Bitmap;
49
import android.graphics.BitmapFactory;
50
import android.opengl.GLES31;
51
import android.opengl.GLSurfaceView;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
class ObjectTreeRenderer implements GLSurfaceView.Renderer
56
{
57
   private GLSurfaceView mView;
58
   private DistortedEffects mEffects;
59
   private DistortedTexture mLisaTexture, mGridTexture;
60
   private DistortedScreen mScreen;
61
   private DistortedNode mRoot;
62
   private MeshRectangles mMeshRectangles;
63
   private MeshCubes mMeshCubes;
64
   private boolean mDepth;
65
   private Static3D mScale;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
   ObjectTreeRenderer(GLSurfaceView v)
70
      {
71
      mView   = v;
72
      mDepth  = true;
73
      mScale  = new Static3D(1,1,1);
74
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
75
      chromaDyn.add(new Static1D(0.0f));
76
      chromaDyn.add(new Static1D(0.8f));
77

    
78
      mEffects= new DistortedEffects();
79
      mEffects.apply(new MatrixEffectScale(mScale));
80
      mEffects.apply(new FragmentEffectChroma(chromaDyn, new Static3D(0,0,1)));
81

    
82
      mScreen = new DistortedScreen();
83
      }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
   private void setDepthPriv()
88
     {
89
     if( mDepth ) mRoot.glEnable (GLES31.GL_DEPTH_TEST);
90
     else         mRoot.glDisable(GLES31.GL_DEPTH_TEST);
91

    
92
     mRoot.glDepthMask(mDepth);
93

    
94
     // we can also, to save memory, delete/recreate
95
     // the depth buffer each time. This is optional.
96
     mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
97
     }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
   void setDepth(boolean depth)
102
      {
103
      mDepth = depth;
104
      if( mRoot!=null ) setDepthPriv();
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
   
109
   public void onDrawFrame(GL10 glUnused)
110
      {
111
      mScreen.render(System.currentTimeMillis());
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
117
      {
118
      float horiRatio = (float)width / mLisaTexture.getWidth();
119
      float vertRatio = (float)height/ mLisaTexture.getHeight();
120
      float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
121

    
122
      mScale.set( factor,factor,factor );
123
      mScreen.resize(width, height);
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
129
      {
130
      // 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
      GLES31.glEnable(GLES31.GL_CULL_FACE);
134
      GLES31.glCullFace(GLES31.GL_BACK);
135
      GLES31.glFrontFace(GLES31.GL_CW);
136

    
137
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
138
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
139
      
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

    
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
      int lisaWidth = bitmap1.getWidth();
163
      int lisaHeight= bitmap1.getHeight();
164
      int gridWidth = bitmap2.getWidth();
165
      int gridHeight= bitmap2.getHeight();
166
      int gridDepth = gridWidth/GRID;
167

    
168
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture();
169
      if( mGridTexture==null ) mGridTexture = new DistortedTexture();
170
      mLisaTexture.setTexture(bitmap1);
171
      mGridTexture.setTexture(bitmap2);
172

    
173
      DistortedEffects gridEffects = new DistortedEffects();
174

    
175
      if( mMeshRectangles ==null )
176
        {
177
        mMeshRectangles = new MeshRectangles(1,1);
178
        mMeshRectangles.setStretch(lisaWidth,lisaHeight,0);
179
        }
180

    
181
      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

    
187
      mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshRectangles);
188
      mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
189

    
190
      setDepthPriv();
191

    
192
      mScreen.detachAll();
193
      mScreen.attach(mRoot);
194

    
195
      float factor = lisaWidth/(2.0f*gridWidth);
196
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
197
      gridEffects.apply(scale);
198

    
199
      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
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(0,0,0));
204
      gridEffects.apply(rotate);
205

    
206
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
207
      sinkDyn.add(new Static1D(1.0f));
208
      sinkDyn.add(new Static1D(0.3f));
209
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(0,0,0) );
210
      gridEffects.apply(sink);
211

    
212
      VertexEffectSink.enable();
213
      FragmentEffectChroma.enable();
214

    
215
      try
216
        {
217
        DistortedLibrary.onCreate(mView.getContext());
218
        }
219
      catch(Exception ex)
220
        {
221
        android.util.Log.e("ObjectTree", ex.getMessage() );
222
        }
223
      }
224
}
(2-2/3)