Project

General

Profile

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

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

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 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
31 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
32 8744aa41 Leszek Koltunski
import org.distorted.library.DistortedNode;
33 d218d64e leszek
import org.distorted.library.DistortedScreen;
34 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
35 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
36 b01acdaf Leszek Koltunski
import org.distorted.library.MeshCubes;
37
import org.distorted.library.MeshFlat;
38 f6d884d5 Leszek Koltunski
import org.distorted.library.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 392e16fd Leszek Koltunski
   private int lisaHeight, lisaWidth;
60 b5636dbf leszek
   private boolean mDepth;
61 392e16fd Leszek Koltunski
62 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64 7e6110e2 Leszek Koltunski
   FBORenderer(GLSurfaceView v)
65 427ab7bf Leszek Koltunski
      {
66 392e16fd Leszek Koltunski
      mView   = v;
67 b5636dbf leszek
      mDepth  = true;
68 d04a4886 Leszek Koltunski
      mEffects= new DistortedEffects();
69 d218d64e leszek
      mScreen = new DistortedScreen();
70 427ab7bf Leszek Koltunski
      }
71
72 1746198f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 b5636dbf leszek
   private void setDepthPriv()
75
     {
76
     if( mDepth ) mRoot.glEnable (GLES30.GL_DEPTH_TEST);
77
     else         mRoot.glDisable(GLES30.GL_DEPTH_TEST);
78 fc695380 leszek
79 b5636dbf leszek
     mRoot.glDepthMask(mDepth);
80 fc695380 leszek
81 b5636dbf leszek
     // we can also, to save memory, delete/recreate
82
     // the depth buffer each time. This is optional.
83
     DistortedFramebuffer fbo = mRoot.getFramebuffer();
84
     if( fbo!=null ) fbo.enableDepth(mDepth);
85
     }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89
   void setDepth(boolean depth)
90
      {
91
      mDepth = depth;
92
      if( mRoot!=null ) setDepthPriv();
93 1746198f Leszek Koltunski
      }
94
95 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
   
97 1746198f Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
98 427ab7bf Leszek Koltunski
      {
99 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
100 427ab7bf Leszek Koltunski
      }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
    
104 1746198f Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
105 427ab7bf Leszek Koltunski
      { 
106 d79a56d3 Leszek Koltunski
      float qx = (float)width /lisaWidth;
107
      float qy = (float)height/lisaHeight;
108 7589635e Leszek Koltunski
109 d79a56d3 Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
110
      mEffects.scale(  qx<qy ? (new Static3D(1,qx/qy,1)) : (new Static3D(qy/qx,1,1)) );
111 427ab7bf Leszek Koltunski
      
112 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
113 427ab7bf Leszek Koltunski
      }
114
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
    
117 1746198f Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
118 427ab7bf Leszek Koltunski
      {
119 3c44dcc7 leszek
      GLES30.glEnable(GLES30.GL_CULL_FACE);
120
      GLES30.glCullFace(GLES30.GL_BACK);
121
      GLES30.glFrontFace(GLES30.GL_CW);
122 e7a4ef16 Leszek Koltunski
123 427ab7bf Leszek Koltunski
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
124 c0486401 Leszek Koltunski
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
125 427ab7bf Leszek Koltunski
      
126
      Bitmap bitmap1, bitmap2;
127
       
128
      try 
129
        {
130
        bitmap1 = BitmapFactory.decodeStream(is1);
131
        bitmap2 = BitmapFactory.decodeStream(is2);
132
        } 
133
      finally 
134
        {
135
        try 
136
          {
137
          is1.close();
138
          is2.close();
139
          } 
140
        catch(IOException e) { }
141
        }  
142
      
143 f6d884d5 Leszek Koltunski
      lisaWidth     = bitmap1.getWidth();
144
      lisaHeight    = bitmap1.getHeight();
145 371d99fa Leszek Koltunski
      int gridWidth = bitmap2.getWidth();
146
      int gridHeight= bitmap2.getHeight();
147 f6d884d5 Leszek Koltunski
148 59540ba2 Leszek Koltunski
      if( mLisaTexture==null ) mLisaTexture = new DistortedTexture(lisaWidth,lisaHeight);
149
      if( mGridTexture==null ) mGridTexture = new DistortedTexture(gridWidth,gridHeight);
150
      mLisaTexture.setTexture(bitmap1);
151
      mGridTexture.setTexture(bitmap2);
152 371d99fa Leszek Koltunski
      DistortedEffects gridEffects = new DistortedEffects();
153 e8b6aa95 Leszek Koltunski
154 59540ba2 Leszek Koltunski
      mRoot = new DistortedNode(mLisaTexture, mEffects,new MeshFlat(1,1));
155
      mRoot.attach(mGridTexture,gridEffects,new MeshCubes(10,10,false));
156 7589635e Leszek Koltunski
157 b5636dbf leszek
      setDepthPriv();
158
159 fe59d375 Leszek Koltunski
      mScreen.detachAll();
160
      mScreen.attach(mRoot);
161
162 d79a56d3 Leszek Koltunski
      gridEffects.scale(0.5f);
163 7bf107f7 Leszek Koltunski
164 c0486401 Leszek Koltunski
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
165
      rotDyn.add(new Static1D(  0));
166
      rotDyn.add(new Static1D(360));
167
      rotDyn.setMode(Dynamic.MODE_JUMP);
168
169 d79a56d3 Leszek Koltunski
      gridEffects.rotate(rotDyn, new Static3D(1,0,0), new Static3D(0,0,1.0f/10) );
170 c0486401 Leszek Koltunski
171
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
172 7bf107f7 Leszek Koltunski
      sinkDyn.add(new Static1D(1.0f));
173 c0486401 Leszek Koltunski
      sinkDyn.add(new Static1D(0.3f));
174 7bf107f7 Leszek Koltunski
175 371d99fa Leszek Koltunski
      gridEffects.sink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
176 59759251 Leszek Koltunski
177 c0486401 Leszek Koltunski
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
178 70d7a31f Leszek Koltunski
      chromaDyn.add(new Static1D(0.0f));
179 c0486401 Leszek Koltunski
      chromaDyn.add(new Static1D(1.0f));
180 59759251 Leszek Koltunski
181 392e16fd Leszek Koltunski
      mEffects.chroma(chromaDyn, new Static3D(0,0,1) );
182 6637d0f2 Leszek Koltunski
183
      DistortedEffects.enableEffect(EffectNames.SINK);
184
      DistortedEffects.enableEffect(EffectNames.CHROMA);
185
186 427ab7bf Leszek Koltunski
      try
187
        {
188 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
189 427ab7bf Leszek Koltunski
        }
190
      catch(Exception ex)
191
        {
192 e8b6aa95 Leszek Koltunski
        android.util.Log.e("FBO", ex.getMessage() );
193 427ab7bf Leszek Koltunski
        }
194
      }
195
}