Project

General

Profile

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

examples / src / main / java / org / distorted / examples / objecttree / ObjectTreeRenderer.java @ 2e779512

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.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 GLSurfaceView mView;
59
   private DistortedEffects mEffects;
60
   private DistortedTexture mLisaTexture, mGridTexture;
61
   private DistortedScreen mScreen;
62
   private DistortedNode mRoot;
63
   private MeshRectangles mMeshRectangles;
64
   private MeshCubes mMeshCubes;
65
   private boolean mDepth;
66
   private Static3D mScale;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

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

    
83
      mScreen = new DistortedScreen();
84
      }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

    
93
     mRoot.glDepthMask(mDepth);
94

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

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

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

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

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

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
130
      {
131
      // TODO
132
      // This appears to be needed in case we create the app anew with NO DEPTH
133
      // (i.e. we set 'no depth' and then rotate screen). Investigate.
134
      GLES31.glEnable(GLES31.GL_CULL_FACE);
135
      GLES31.glCullFace(GLES31.GL_BACK);
136
      GLES31.glFrontFace(GLES31.GL_CW);
137

    
138
      Resources res = mView.getContext().getResources();
139

    
140
      InputStream is1 = res.openRawResource(R.raw.monalisa);
141
      InputStream is2 = res.openRawResource(R.raw.grid);
142
      
143
      Bitmap bitmap1, bitmap2;
144
       
145
      try 
146
        {
147
        bitmap1 = BitmapFactory.decodeStream(is1);
148
        bitmap2 = BitmapFactory.decodeStream(is2);
149
        } 
150
      finally 
151
        {
152
        try 
153
          {
154
          is1.close();
155
          is2.close();
156
          } 
157
        catch(IOException e) { }
158
        }  
159

    
160
      final int GRID = 20;
161
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f     ,1.0f     );
162
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRID,1.0f     );
163
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f     ,1.0f/GRID);
164

    
165
      int lisaWidth = bitmap1.getWidth();
166
      int lisaHeight= bitmap1.getHeight();
167
      int gridWidth = bitmap2.getWidth();
168
      int gridHeight= bitmap2.getHeight();
169
      int gridDepth = gridWidth/GRID;
170

    
171
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture();
172
      if( mGridTexture==null ) mGridTexture = new DistortedTexture();
173
      mLisaTexture.setTexture(bitmap1);
174
      mGridTexture.setTexture(bitmap2);
175

    
176
      DistortedEffects gridEffects = new DistortedEffects();
177

    
178
      if( mMeshRectangles ==null )
179
        {
180
        mMeshRectangles = new MeshRectangles(1,1);
181
        mMeshRectangles.setStretch(lisaWidth,lisaHeight,0);
182
        }
183

    
184
      if( mMeshCubes==null)
185
        {
186
        mMeshCubes= new MeshCubes(GRID,GRID,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
187
        mMeshCubes.setStretch(gridWidth,gridHeight,gridDepth);
188
        }
189

    
190
      mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshRectangles);
191
      mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
192

    
193
      setDepthPriv();
194

    
195
      mScreen.detachAll();
196
      mScreen.attach(mRoot);
197

    
198
      float factor = lisaWidth/(2.0f*gridWidth);
199
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
200
      gridEffects.apply(scale);
201

    
202
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
203
      rotDyn.add(new Static1D(  0));
204
      rotDyn.add(new Static1D(360));
205
      rotDyn.setMode(Dynamic.MODE_JUMP);
206
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(0,0,0));
207
      gridEffects.apply(rotate);
208

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

    
215
      VertexEffectSink.enable();
216
      FragmentEffectChroma.enable();
217

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