Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ fdddb0b2

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.effect.EffectName;
31
import org.distorted.library.effect.FragmentEffectChroma;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectRotate;
34
import org.distorted.library.effect.MatrixEffectScale;
35
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedFramebuffer;
38
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedScreen;
40
import org.distorted.library.main.Distorted;
41
import org.distorted.library.main.MeshCubes;
42
import org.distorted.library.main.MeshFlat;
43
import org.distorted.library.main.DistortedTexture;
44
import org.distorted.library.type.Dynamic;
45
import org.distorted.library.type.Dynamic1D;
46
import org.distorted.library.type.Static1D;
47
import org.distorted.library.type.Static3D;
48

    
49
import android.graphics.Bitmap;
50
import android.graphics.BitmapFactory;
51
import android.opengl.GLES30;
52
import android.opengl.GLSurfaceView;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
class FBORenderer 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 MeshFlat mMeshFlat;
64
   private MeshCubes mMeshCubes;
65
   private int lisaHeight, lisaWidth;
66
   private boolean mDepth;
67
   private Static3D mScale, mMove;
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
   FBORenderer(GLSurfaceView v)
72
      {
73
      mView   = v;
74
      mDepth  = true;
75
      mEffects= new DistortedEffects();
76
      mScale  = new Static3D(1,1,1);
77
      mMove   = new Static3D(0,0,0);
78
      mEffects.apply(new MatrixEffectMove(mMove));
79
      mEffects.apply(new MatrixEffectScale(mScale));
80
      mScreen = new DistortedScreen(mView);
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
   private void setDepthPriv()
86
     {
87
     if( mDepth ) mRoot.glEnable (GLES30.GL_DEPTH_TEST);
88
     else         mRoot.glDisable(GLES30.GL_DEPTH_TEST);
89

    
90
     mRoot.glDepthMask(mDepth);
91

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

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
   void setDepth(boolean depth)
100
      {
101
      mDepth = depth;
102
      if( mRoot!=null ) setDepthPriv();
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107
   public void onDrawFrame(GL10 glUnused)
108
      {
109
      mScreen.render(System.currentTimeMillis());
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
115
      { 
116
      if( (float)lisaHeight/lisaWidth > (float)height/width )
117
        {
118
        int w = (height*lisaWidth)/lisaHeight;
119
        float factor = (float)height/lisaHeight;
120

    
121
        mMove.set((width-w)/2,0,0);
122
        mScale.set(factor,factor,factor);
123
        }
124
      else
125
        {
126
        int h = (width*lisaHeight)/lisaWidth;
127
        float factor = (float)width/lisaWidth;
128

    
129
        mMove.set(0,(height-h)/2,0);
130
        mScale.set(factor,factor,factor);
131
        }
132
      
133
      mScreen.resize(width, height);
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
139
      {
140
      GLES30.glEnable(GLES30.GL_CULL_FACE);
141
      GLES30.glCullFace(GLES30.GL_BACK);
142
      GLES30.glFrontFace(GLES30.GL_CW);
143

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

    
169
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture(lisaWidth,lisaHeight);
170
      if( mGridTexture==null ) mGridTexture = new DistortedTexture(gridWidth,gridHeight);
171
      mLisaTexture.setTexture(bitmap1);
172
      mGridTexture.setTexture(bitmap2);
173
      DistortedEffects gridEffects = new DistortedEffects();
174

    
175
      mEffects.abortAllEffects();
176

    
177
      final int GRID = 10;
178

    
179
      if( mMeshFlat==null ) mMeshFlat = new MeshFlat(1,1);
180
      if( mMeshCubes==null) mMeshCubes= new MeshCubes(GRID,GRID,1);
181

    
182
      mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshFlat);
183
      mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
184

    
185
      setDepthPriv();
186

    
187
      mScreen.detachAll();
188
      mScreen.attach(mRoot);
189

    
190
      float factor = lisaWidth/(2.0f*gridWidth);
191
      MatrixEffectMove move = new MatrixEffectMove( new Static3D((lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2, gridWidth/(2.0f*GRID)));
192
      MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
193
      gridEffects.apply(move);
194
      gridEffects.apply(scale);
195

    
196
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
197
      rotDyn.add(new Static1D(  0));
198
      rotDyn.add(new Static1D(360));
199
      rotDyn.setMode(Dynamic.MODE_JUMP);
200
      MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridWidth/(2*GRID)));
201
      gridEffects.apply(rotate);
202

    
203
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
204
      sinkDyn.add(new Static1D(1.0f));
205
      sinkDyn.add(new Static1D(0.3f));
206
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
207
      gridEffects.apply(sink);
208

    
209
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
210
      chromaDyn.add(new Static1D(0.0f));
211
      chromaDyn.add(new Static1D(1.0f));
212
      FragmentEffectChroma chroma = new FragmentEffectChroma(chromaDyn, new Static3D(0,0,1));
213
      mEffects.apply(chroma);
214

    
215
      DistortedEffects.enableEffect(EffectName.SINK);
216
      DistortedEffects.enableEffect(EffectName.CHROMA);
217

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