Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 1f9a52f1

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.fbo;
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.DistortedEffects;
31
import org.distorted.library.DistortedFramebuffer;
32
import org.distorted.library.DistortedNode;
33
import org.distorted.library.DistortedScreen;
34
import org.distorted.library.Distorted;
35
import org.distorted.library.EffectNames;
36
import org.distorted.library.MeshCubes;
37
import org.distorted.library.MeshFlat;
38
import org.distorted.library.DistortedTexture;
39
import org.distorted.library.EffectTypes;
40
import org.distorted.library.type.Dynamic;
41
import org.distorted.library.type.Dynamic1D;
42
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static3D;
44

    
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLES30;
48
import android.opengl.GLSurfaceView;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class FBORenderer implements GLSurfaceView.Renderer 
53
{
54
   private GLSurfaceView mView;
55
   private DistortedEffects mEffects;
56
   private DistortedTexture mLisaTexture, mGridTexture;
57
   private DistortedScreen mScreen;
58
   private DistortedNode mRoot;
59
   private MeshFlat mMeshFlat;
60
   private MeshCubes mMeshCubes;
61
   private int lisaHeight, lisaWidth;
62
   private boolean mDepth;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
   FBORenderer(GLSurfaceView v)
67
      {
68
      mView   = v;
69
      mDepth  = true;
70
      mEffects= new DistortedEffects();
71
      mScreen = new DistortedScreen(mView);
72
      }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
   private void setDepthPriv()
77
     {
78
     if( mDepth ) mRoot.glEnable (GLES30.GL_DEPTH_TEST);
79
     else         mRoot.glDisable(GLES30.GL_DEPTH_TEST);
80

    
81
     mRoot.glDepthMask(mDepth);
82

    
83
     // we can also, to save memory, delete/recreate
84
     // the depth buffer each time. This is optional.
85
     DistortedFramebuffer fbo = mRoot.getFramebuffer();
86
     if( fbo!=null ) fbo.enableDepth(mDepth);
87
     }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
   void setDepth(boolean depth)
92
      {
93
      mDepth = depth;
94
      if( mRoot!=null ) setDepthPriv();
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
   
99
   public void onDrawFrame(GL10 glUnused)
100
      {
101
      mScreen.render(System.currentTimeMillis());
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
    
106
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
107
      { 
108
      mEffects.abortEffects(EffectTypes.MATRIX);
109
         
110
      if( (float)lisaHeight/lisaWidth > (float)height/width )
111
        {
112
        int w = (height*lisaWidth)/lisaHeight;
113
        float factor = (float)height/lisaHeight;
114

    
115
        mEffects.move( new Static3D((width-w)/2,0,0) );
116
        mEffects.scale(factor);
117
        }
118
      else
119
        {
120
        int h = (width*lisaHeight)/lisaWidth;
121
        float factor = (float)width/lisaWidth;
122

    
123
        mEffects.move( new Static3D(0,(height-h)/2,0) );
124
        mEffects.scale(factor);
125
        }
126
      
127
      mScreen.resize(width, height);
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
    
132
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
133
      {
134
      GLES30.glEnable(GLES30.GL_CULL_FACE);
135
      GLES30.glCullFace(GLES30.GL_BACK);
136
      GLES30.glFrontFace(GLES30.GL_CW);
137

    
138
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
139
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
140
      
141
      Bitmap bitmap1, bitmap2;
142
       
143
      try 
144
        {
145
        bitmap1 = BitmapFactory.decodeStream(is1);
146
        bitmap2 = BitmapFactory.decodeStream(is2);
147
        } 
148
      finally 
149
        {
150
        try 
151
          {
152
          is1.close();
153
          is2.close();
154
          } 
155
        catch(IOException e) { }
156
        }  
157
      
158
      lisaWidth     = bitmap1.getWidth();
159
      lisaHeight    = bitmap1.getHeight();
160
      int gridWidth = bitmap2.getWidth();
161
      int gridHeight= bitmap2.getHeight();
162

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

    
169
      mEffects.abortAllEffects();
170

    
171
      final int GRID = 10;
172

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

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

    
179
      setDepthPriv();
180

    
181
      mScreen.detachAll();
182
      mScreen.attach(mRoot);
183

    
184
      float factor = lisaWidth/(2.0f*gridWidth);
185

    
186
      gridEffects.move( new Static3D( (lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2, gridWidth/(2*GRID)) );
187
      gridEffects.scale(factor);
188

    
189
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
190
      rotDyn.add(new Static1D(  0));
191
      rotDyn.add(new Static1D(360));
192
      rotDyn.setMode(Dynamic.MODE_JUMP);
193

    
194
      gridEffects.rotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridWidth/(2*GRID)) );
195

    
196
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
197
      sinkDyn.add(new Static1D(1.0f));
198
      sinkDyn.add(new Static1D(0.3f));
199

    
200
      gridEffects.sink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
201

    
202
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
203
      chromaDyn.add(new Static1D(0.0f));
204
      chromaDyn.add(new Static1D(1.0f));
205

    
206
      mEffects.chroma(chromaDyn, new Static3D(0,0,1) );
207

    
208
      DistortedEffects.enableEffect(EffectNames.SINK);
209
      DistortedEffects.enableEffect(EffectNames.CHROMA);
210

    
211
      try
212
        {
213
        Distorted.onCreate(mView.getContext());
214
        }
215
      catch(Exception ex)
216
        {
217
        android.util.Log.e("FBO", ex.getMessage() );
218
        }
219
      }
220
}
(2-2/3)