Project

General

Profile

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

examples / src / main / java / org / distorted / examples / objecttree / ObjectTreeRenderer.java @ 061449ed

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19 427ab7bf Leszek Koltunski
20 5ceea148 Leszek Koltunski
package org.distorted.examples.objecttree;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 6646c476 leszek
import org.distorted.library.effect.FragmentEffectChroma;
31
import org.distorted.library.effect.MatrixEffectRotate;
32
import org.distorted.library.effect.MatrixEffectScale;
33 410dfc37 Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
34 6646c476 leszek
import org.distorted.library.effect.VertexEffectSink;
35 01782e85 Leszek Koltunski
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 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
40 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
41 5a683295 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
42 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
43 c0486401 Leszek Koltunski
import org.distorted.library.type.Dynamic;
44 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
45
import org.distorted.library.type.Static1D;
46 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
47 5b4a2d76 Leszek Koltunski
import org.distorted.library.type.Static4D;
48 427ab7bf Leszek Koltunski
49 2e779512 Leszek Koltunski
import android.content.res.Resources;
50 427ab7bf Leszek Koltunski
import android.graphics.Bitmap;
51
import android.graphics.BitmapFactory;
52 41a85bb7 Leszek Koltunski
import android.opengl.GLES31;
53 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
54
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57 061449ed Leszek Koltunski
class ObjectTreeRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
58 427ab7bf Leszek Koltunski
{
59 5a683295 Leszek Koltunski
   private static final int NODE_FBO_SIZE = 300;
60 410dfc37 Leszek Koltunski
   private static final int GRID = 10;
61 5a683295 Leszek Koltunski
62 427ab7bf Leszek Koltunski
   private GLSurfaceView mView;
63 5a683295 Leszek Koltunski
   private DistortedEffects mEffectsNode, mEffectsGrid;
64 59540ba2 Leszek Koltunski
   private DistortedTexture mLisaTexture, mGridTexture;
65 d218d64e leszek
   private DistortedScreen mScreen;
66 8744aa41 Leszek Koltunski
   private DistortedNode mRoot;
67 5a683295 Leszek Koltunski
   private MeshQuad mMeshQuad;
68 5f2a53b6 leszek
   private MeshCubes mMeshCubes;
69 b5636dbf leszek
   private boolean mDepth;
70 16b22aab Leszek Koltunski
   private Static3D mScale;
71 5a683295 Leszek Koltunski
   private float mLisaAspectRatio;
72 392e16fd Leszek Koltunski
73 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 5ceea148 Leszek Koltunski
   ObjectTreeRenderer(GLSurfaceView v)
76 427ab7bf Leszek Koltunski
      {
77 392e16fd Leszek Koltunski
      mView   = v;
78 b5636dbf leszek
      mDepth  = true;
79 6646c476 leszek
      mScale  = new Static3D(1,1,1);
80 3c70973c Leszek Koltunski
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
81
      chromaDyn.add(new Static1D(0.0f));
82
      chromaDyn.add(new Static1D(0.8f));
83
84 5a683295 Leszek Koltunski
      mEffectsNode= new DistortedEffects();
85
      mEffectsNode.apply(new MatrixEffectScale(mScale));
86
      mEffectsNode.apply(new FragmentEffectChroma(chromaDyn, new Static3D(0,0,1)));
87
88
      mEffectsGrid = new DistortedEffects();
89
90 513b2e9c Leszek Koltunski
      float factor = NODE_FBO_SIZE/(2.0f*GRID);
91
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
92 5a683295 Leszek Koltunski
      mEffectsGrid.apply(scale);
93
94
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
95
      rotDyn.add(new Static1D(  0));
96
      rotDyn.add(new Static1D(360));
97
      rotDyn.setMode(Dynamic.MODE_JUMP);
98
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(0,0,0));
99
      mEffectsGrid.apply(rotate);
100
101 410dfc37 Leszek Koltunski
      VertexEffectScale vScale = new VertexEffectScale( new Static3D(GRID,GRID,1) );
102
      mEffectsGrid.apply(vScale);
103
104 5a683295 Leszek Koltunski
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
105
      sinkDyn.add(new Static1D(1.0f));
106
      sinkDyn.add(new Static1D(0.3f));
107 410dfc37 Leszek Koltunski
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(0,0,0), new Static4D(0,0,0,GRID*0.5f) );
108 5a683295 Leszek Koltunski
      mEffectsGrid.apply(sink);
109 3c70973c Leszek Koltunski
110 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
111 427ab7bf Leszek Koltunski
      }
112
113 1746198f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115 b5636dbf leszek
   private void setDepthPriv()
116
     {
117 41a85bb7 Leszek Koltunski
     if( mDepth ) mRoot.glEnable (GLES31.GL_DEPTH_TEST);
118
     else         mRoot.glDisable(GLES31.GL_DEPTH_TEST);
119 fc695380 leszek
120 b5636dbf leszek
     mRoot.glDepthMask(mDepth);
121 fc695380 leszek
122 b5636dbf leszek
     // we can also, to save memory, delete/recreate
123
     // the depth buffer each time. This is optional.
124 559da65d Leszek Koltunski
     mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
125 b5636dbf leszek
     }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129
   void setDepth(boolean depth)
130
      {
131
      mDepth = depth;
132
      if( mRoot!=null ) setDepthPriv();
133 1746198f Leszek Koltunski
      }
134
135 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
136
   
137 1746198f Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
138 427ab7bf Leszek Koltunski
      {
139 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
140 427ab7bf Leszek Koltunski
      }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
    
144 1746198f Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
145 16b22aab Leszek Koltunski
      {
146 410dfc37 Leszek Koltunski
      float factor= Math.min(width/mLisaAspectRatio, height);
147 16b22aab Leszek Koltunski
      mScale.set( factor,factor,factor );
148 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
149 427ab7bf Leszek Koltunski
      }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
    
153 1746198f Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
154 427ab7bf Leszek Koltunski
      {
155 2e779512 Leszek Koltunski
      Resources res = mView.getContext().getResources();
156
157 5a683295 Leszek Koltunski
      InputStream isLisa = res.openRawResource(R.raw.monalisa);
158
      InputStream isGrid = res.openRawResource(R.raw.grid);
159 427ab7bf Leszek Koltunski
      
160 5a683295 Leszek Koltunski
      Bitmap bitmapLisa, bitmapGrid;
161 427ab7bf Leszek Koltunski
       
162
      try 
163
        {
164 5a683295 Leszek Koltunski
        bitmapLisa = BitmapFactory.decodeStream(isLisa);
165
        bitmapGrid = BitmapFactory.decodeStream(isGrid);
166 427ab7bf Leszek Koltunski
        } 
167
      finally 
168
        {
169
        try 
170
          {
171 5a683295 Leszek Koltunski
          isLisa.close();
172
          isGrid.close();
173 427ab7bf Leszek Koltunski
          } 
174
        catch(IOException e) { }
175
        }  
176 687263cc Leszek Koltunski
177 5a683295 Leszek Koltunski
      mLisaAspectRatio = ((float)bitmapLisa.getWidth())/bitmapLisa.getHeight();
178 f6d884d5 Leszek Koltunski
179 687263cc Leszek Koltunski
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture();
180
      if( mGridTexture==null ) mGridTexture = new DistortedTexture();
181 5a683295 Leszek Koltunski
      mLisaTexture.setTexture(bitmapLisa);
182
      mGridTexture.setTexture(bitmapGrid);
183 698ad0a8 Leszek Koltunski
184 5a683295 Leszek Koltunski
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f     ,1.0f     );
185
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRID,1.0f     );
186
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f     ,1.0f/GRID);
187 4b676b3f Leszek Koltunski
188 5a683295 Leszek Koltunski
      if( mMeshQuad ==null ) mMeshQuad = new MeshQuad();
189 410dfc37 Leszek Koltunski
      if( mMeshCubes==null ) mMeshCubes= new MeshCubes(GRID,GRID,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
190 5b4a2d76 Leszek Koltunski
191 5a683295 Leszek Koltunski
      mRoot = new DistortedNode(mLisaTexture, mEffectsNode, mMeshQuad);
192
      mRoot.resizeFBO(NODE_FBO_SIZE,NODE_FBO_SIZE);
193
      mRoot.attach(mGridTexture, mEffectsGrid, mMeshCubes);
194 7589635e Leszek Koltunski
195 b5636dbf leszek
      setDepthPriv();
196
197 fe59d375 Leszek Koltunski
      mScreen.detachAll();
198
      mScreen.attach(mRoot);
199
200 410dfc37 Leszek Koltunski
      VertexEffectScale.enable();
201 885b9cca leszek
      VertexEffectSink.enable();
202
      FragmentEffectChroma.enable();
203 6637d0f2 Leszek Koltunski
204 061449ed Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext(), this);
205
      }
206
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
209
    public void distortedException(Exception ex)
210
      {
211
      android.util.Log.e("ObjectTree", ex.getMessage() );
212 427ab7bf Leszek Koltunski
      }
213
}