Project

General

Profile

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

examples / src / main / java / org / distorted / examples / objecttree / ObjectTreeRenderer.java @ 16b22aab

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.Distorted;
39
import org.distorted.library.mesh.MeshCubes;
40
import org.distorted.library.mesh.MeshFlat;
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 MeshFlat mMeshFlat;
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
      int lisaWidth     = bitmap1.getWidth();
158
      int lisaHeight    = bitmap1.getHeight();
159
      int gridWidth = bitmap2.getWidth();
160
      int gridHeight= bitmap2.getHeight();
161

    
162
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture(lisaWidth,lisaHeight);
163
      if( mGridTexture==null ) mGridTexture = new DistortedTexture(gridWidth,gridHeight);
164
      mLisaTexture.setTexture(bitmap1);
165
      mGridTexture.setTexture(bitmap2);
166
      DistortedEffects gridEffects = new DistortedEffects();
167

    
168
      final int GRID = 20;
169
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f     ,1.0f     );
170
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRID,1.0f     );
171
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f     ,1.0f/GRID);
172

    
173
      if( mMeshFlat==null ) mMeshFlat = new MeshFlat(1,1);
174
      if( mMeshCubes==null) mMeshCubes= new MeshCubes(GRID,GRID,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
175

    
176
      int gridDepth = mGridTexture.getDepth(mMeshCubes);
177

    
178
      mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshFlat);
179
      mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
180

    
181
      setDepthPriv();
182

    
183
      mScreen.detachAll();
184
      mScreen.attach(mRoot);
185

    
186
      float factor = lisaWidth/(2.0f*gridWidth);
187
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
188
      gridEffects.apply(scale);
189

    
190
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
191
      rotDyn.add(new Static1D(  0));
192
      rotDyn.add(new Static1D(360));
193
      rotDyn.setMode(Dynamic.MODE_JUMP);
194
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(0,0,0));
195
      gridEffects.apply(rotate);
196

    
197
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
198
      sinkDyn.add(new Static1D(1.0f));
199
      sinkDyn.add(new Static1D(0.3f));
200
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2.0f, gridHeight/2.0f, gridDepth/2.0f) );
201
      gridEffects.apply(sink);
202

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

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