Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 01782e85

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.fbo;
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 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
31
import org.distorted.library.main.DistortedFramebuffer;
32
import org.distorted.library.main.DistortedNode;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.Distorted;
35 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.MeshCubes;
37
import org.distorted.library.main.MeshFlat;
38
import org.distorted.library.main.DistortedTexture;
39 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
40 c0486401 Leszek Koltunski
import org.distorted.library.type.Dynamic;
41 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
42
import org.distorted.library.type.Static1D;
43 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
44 427ab7bf Leszek Koltunski
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
48 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
49
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52
class FBORenderer implements GLSurfaceView.Renderer 
53
{
54
   private GLSurfaceView mView;
55 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
56 59540ba2 Leszek Koltunski
   private DistortedTexture mLisaTexture, mGridTexture;
57 d218d64e leszek
   private DistortedScreen mScreen;
58 8744aa41 Leszek Koltunski
   private DistortedNode mRoot;
59 5f2a53b6 leszek
   private MeshFlat mMeshFlat;
60
   private MeshCubes mMeshCubes;
61 392e16fd Leszek Koltunski
   private int lisaHeight, lisaWidth;
62 b5636dbf leszek
   private boolean mDepth;
63 392e16fd Leszek Koltunski
64 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 7e6110e2 Leszek Koltunski
   FBORenderer(GLSurfaceView v)
67 427ab7bf Leszek Koltunski
      {
68 392e16fd Leszek Koltunski
      mView   = v;
69 b5636dbf leszek
      mDepth  = true;
70 d04a4886 Leszek Koltunski
      mEffects= new DistortedEffects();
71 1f9a52f1 leszek
      mScreen = new DistortedScreen(mView);
72 427ab7bf Leszek Koltunski
      }
73
74 1746198f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76 b5636dbf leszek
   private void setDepthPriv()
77
     {
78
     if( mDepth ) mRoot.glEnable (GLES30.GL_DEPTH_TEST);
79
     else         mRoot.glDisable(GLES30.GL_DEPTH_TEST);
80 fc695380 leszek
81 b5636dbf leszek
     mRoot.glDepthMask(mDepth);
82 fc695380 leszek
83 b5636dbf leszek
     // we can also, to save memory, delete/recreate
84
     // the depth buffer each time. This is optional.
85 559da65d Leszek Koltunski
     mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
86 b5636dbf leszek
     }
87
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90
   void setDepth(boolean depth)
91
      {
92
      mDepth = depth;
93
      if( mRoot!=null ) setDepthPriv();
94 1746198f Leszek Koltunski
      }
95
96 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
97
   
98 1746198f Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
99 427ab7bf Leszek Koltunski
      {
100 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
101 427ab7bf Leszek Koltunski
      }
102
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
    
105 1746198f Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
106 427ab7bf Leszek Koltunski
      { 
107 d79a56d3 Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
108 630703d1 leszek
         
109
      if( (float)lisaHeight/lisaWidth > (float)height/width )
110
        {
111
        int w = (height*lisaWidth)/lisaHeight;
112
        float factor = (float)height/lisaHeight;
113
114
        mEffects.move( new Static3D((width-w)/2,0,0) );
115
        mEffects.scale(factor);
116
        }
117
      else
118
        {
119
        int h = (width*lisaHeight)/lisaWidth;
120
        float factor = (float)width/lisaWidth;
121
122
        mEffects.move( new Static3D(0,(height-h)/2,0) );
123
        mEffects.scale(factor);
124
        }
125 427ab7bf Leszek Koltunski
      
126 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
127 427ab7bf Leszek Koltunski
      }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131 1746198f Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
132 427ab7bf Leszek Koltunski
      {
133 3c44dcc7 leszek
      GLES30.glEnable(GLES30.GL_CULL_FACE);
134
      GLES30.glCullFace(GLES30.GL_BACK);
135
      GLES30.glFrontFace(GLES30.GL_CW);
136 e7a4ef16 Leszek Koltunski
137 427ab7bf Leszek Koltunski
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
138 c0486401 Leszek Koltunski
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
139 427ab7bf Leszek Koltunski
      
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 f6d884d5 Leszek Koltunski
      lisaWidth     = bitmap1.getWidth();
158
      lisaHeight    = bitmap1.getHeight();
159 371d99fa Leszek Koltunski
      int gridWidth = bitmap2.getWidth();
160
      int gridHeight= bitmap2.getHeight();
161 f6d884d5 Leszek Koltunski
162 59540ba2 Leszek Koltunski
      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 371d99fa Leszek Koltunski
      DistortedEffects gridEffects = new DistortedEffects();
167 e8b6aa95 Leszek Koltunski
168 630703d1 leszek
      mEffects.abortAllEffects();
169
170 4b676b3f Leszek Koltunski
      final int GRID = 10;
171
172 5f2a53b6 leszek
      if( mMeshFlat==null ) mMeshFlat = new MeshFlat(1,1);
173 58e9e190 leszek
      if( mMeshCubes==null) mMeshCubes= new MeshCubes(GRID,GRID,1);
174 5f2a53b6 leszek
175
      mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshFlat);
176
      mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
177 7589635e Leszek Koltunski
178 b5636dbf leszek
      setDepthPriv();
179
180 fe59d375 Leszek Koltunski
      mScreen.detachAll();
181
      mScreen.attach(mRoot);
182
183 630703d1 leszek
      float factor = lisaWidth/(2.0f*gridWidth);
184
185 4b676b3f Leszek Koltunski
      gridEffects.move( new Static3D( (lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2, gridWidth/(2*GRID)) );
186 630703d1 leszek
      gridEffects.scale(factor);
187 7bf107f7 Leszek Koltunski
188 c0486401 Leszek Koltunski
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
189
      rotDyn.add(new Static1D(  0));
190
      rotDyn.add(new Static1D(360));
191
      rotDyn.setMode(Dynamic.MODE_JUMP);
192
193 4b676b3f Leszek Koltunski
      gridEffects.rotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridWidth/(2*GRID)) );
194 c0486401 Leszek Koltunski
195
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
196 7bf107f7 Leszek Koltunski
      sinkDyn.add(new Static1D(1.0f));
197 c0486401 Leszek Koltunski
      sinkDyn.add(new Static1D(0.3f));
198 7bf107f7 Leszek Koltunski
199 371d99fa Leszek Koltunski
      gridEffects.sink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
200 59759251 Leszek Koltunski
201 c0486401 Leszek Koltunski
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
202 70d7a31f Leszek Koltunski
      chromaDyn.add(new Static1D(0.0f));
203 c0486401 Leszek Koltunski
      chromaDyn.add(new Static1D(1.0f));
204 59759251 Leszek Koltunski
205 392e16fd Leszek Koltunski
      mEffects.chroma(chromaDyn, new Static3D(0,0,1) );
206 6637d0f2 Leszek Koltunski
207
      DistortedEffects.enableEffect(EffectNames.SINK);
208
      DistortedEffects.enableEffect(EffectNames.CHROMA);
209
210 427ab7bf Leszek Koltunski
      try
211
        {
212 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
213 427ab7bf Leszek Koltunski
        }
214
      catch(Exception ex)
215
        {
216 e8b6aa95 Leszek Koltunski
        android.util.Log.e("FBO", ex.getMessage() );
217 427ab7bf Leszek Koltunski
        }
218
      }
219
}