Project

General

Profile

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

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

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 b01acdaf Leszek Koltunski
import org.distorted.library.MeshCubes;
36
import org.distorted.library.MeshFlat;
37 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
38 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
39 c0486401 Leszek Koltunski
import org.distorted.library.type.Dynamic;
40 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
41
import org.distorted.library.type.Static1D;
42 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
43 427ab7bf Leszek Koltunski
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
47 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51
class FBORenderer implements GLSurfaceView.Renderer 
52
{
53
   private GLSurfaceView mView;
54 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
55 d218d64e leszek
   private DistortedScreen mScreen;
56 8744aa41 Leszek Koltunski
   private DistortedNode mRoot;
57 392e16fd Leszek Koltunski
   private int lisaHeight, lisaWidth;
58
59 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 7e6110e2 Leszek Koltunski
   FBORenderer(GLSurfaceView v)
62 427ab7bf Leszek Koltunski
      {
63 392e16fd Leszek Koltunski
      mView   = v;
64 d04a4886 Leszek Koltunski
      mEffects= new DistortedEffects();
65 d218d64e leszek
      mScreen = new DistortedScreen();
66 427ab7bf Leszek Koltunski
      }
67
68 1746198f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70
   void setDepth(boolean depth)
71
      {
72
      if( mRoot!=null )
73
        {
74 fc695380 leszek
        if( depth ) mRoot.glEnable (GLES30.GL_DEPTH_TEST);
75
        else        mRoot.glDisable(GLES30.GL_DEPTH_TEST);
76
77 af662543 leszek
        // TODO for some reason this does not work - investigate.
78
        //mRoot.glDepthMask(depth);
79 fc695380 leszek
80
        // we can also, to save memory, delete/recreate
81
        // the depth buffer each time. This is optional.
82 596714ec Leszek Koltunski
        DistortedFramebuffer fbo = mRoot.getFramebuffer();
83 d2aee6cc leszek
        fbo.enableDepth(depth);
84 1746198f Leszek Koltunski
        }
85
      }
86
87 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
88
   
89 1746198f Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
90 427ab7bf Leszek Koltunski
      {
91 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
92 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
93 427ab7bf Leszek Koltunski
      }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97 1746198f Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
98 427ab7bf Leszek Koltunski
      { 
99 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
100 427ab7bf Leszek Koltunski
         
101 5055b5d4 Leszek Koltunski
      if( (float)lisaHeight/lisaWidth > (float)height/width )
102 427ab7bf Leszek Koltunski
        {
103
        int w = (height*lisaWidth)/lisaHeight;
104 7589635e Leszek Koltunski
        float factor = (float)height/lisaHeight;
105
106 392e16fd Leszek Koltunski
        mEffects.move( new Static3D((width-w)/2,0,0) );
107
        mEffects.scale(factor);
108 427ab7bf Leszek Koltunski
        }
109
      else
110
        {
111
        int h = (width*lisaHeight)/lisaWidth;
112 7589635e Leszek Koltunski
        float factor = (float)width/lisaWidth;
113
114 392e16fd Leszek Koltunski
        mEffects.move( new Static3D(0,(height-h)/2,0) );
115
        mEffects.scale(factor);
116 427ab7bf Leszek Koltunski
        }
117
      
118 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
119 427ab7bf Leszek Koltunski
      }
120
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123 1746198f Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
124 427ab7bf Leszek Koltunski
      {
125 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
126 3c44dcc7 leszek
      GLES30.glEnable(GLES30.GL_CULL_FACE);
127
      GLES30.glCullFace(GLES30.GL_BACK);
128
      GLES30.glFrontFace(GLES30.GL_CW);
129 e7a4ef16 Leszek Koltunski
130 427ab7bf Leszek Koltunski
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
131 c0486401 Leszek Koltunski
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
132 427ab7bf Leszek Koltunski
      
133
      Bitmap bitmap1, bitmap2;
134
       
135
      try 
136
        {
137
        bitmap1 = BitmapFactory.decodeStream(is1);
138
        bitmap2 = BitmapFactory.decodeStream(is2);
139
        } 
140
      finally 
141
        {
142
        try 
143
          {
144
          is1.close();
145
          is2.close();
146
          } 
147
        catch(IOException e) { }
148
        }  
149
      
150 f6d884d5 Leszek Koltunski
      lisaWidth     = bitmap1.getWidth();
151
      lisaHeight    = bitmap1.getHeight();
152 371d99fa Leszek Koltunski
      int gridWidth = bitmap2.getWidth();
153
      int gridHeight= bitmap2.getHeight();
154 f6d884d5 Leszek Koltunski
155 7451c98a Leszek Koltunski
      DistortedTexture lisa = new DistortedTexture(lisaWidth,lisaHeight);
156 371d99fa Leszek Koltunski
      DistortedTexture grid = new DistortedTexture(gridWidth,gridHeight);
157 f6d884d5 Leszek Koltunski
      lisa.setTexture(bitmap1);
158 371d99fa Leszek Koltunski
      grid.setTexture(bitmap2);
159
      DistortedEffects gridEffects = new DistortedEffects();
160 e8b6aa95 Leszek Koltunski
161 392e16fd Leszek Koltunski
      mEffects.abortAllEffects();
162 fd89ecb6 Leszek Koltunski
163 8744aa41 Leszek Koltunski
      mRoot = new DistortedNode(lisa, mEffects,new MeshFlat(1,1));
164 371d99fa Leszek Koltunski
      mRoot.attach(grid,gridEffects,new MeshCubes(10,10,false));
165 7589635e Leszek Koltunski
166 fe59d375 Leszek Koltunski
      mScreen.detachAll();
167
      mScreen.attach(mRoot);
168
169 371d99fa Leszek Koltunski
      float factor = lisaWidth/(2.0f*gridWidth);
170 7589635e Leszek Koltunski
171 371d99fa Leszek Koltunski
      gridEffects.move( new Static3D( (lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2,0) );
172
      gridEffects.scale(factor);
173 7bf107f7 Leszek Koltunski
174 c0486401 Leszek Koltunski
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
175
      rotDyn.add(new Static1D(  0));
176
      rotDyn.add(new Static1D(360));
177
      rotDyn.setMode(Dynamic.MODE_JUMP);
178
179 371d99fa Leszek Koltunski
      gridEffects.rotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridHeight/10) );
180 c0486401 Leszek Koltunski
181
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
182 7bf107f7 Leszek Koltunski
      sinkDyn.add(new Static1D(1.0f));
183 c0486401 Leszek Koltunski
      sinkDyn.add(new Static1D(0.3f));
184 7bf107f7 Leszek Koltunski
185 371d99fa Leszek Koltunski
      gridEffects.sink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
186 59759251 Leszek Koltunski
187 c0486401 Leszek Koltunski
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
188 70d7a31f Leszek Koltunski
      chromaDyn.add(new Static1D(0.0f));
189 c0486401 Leszek Koltunski
      chromaDyn.add(new Static1D(1.0f));
190 59759251 Leszek Koltunski
191 392e16fd Leszek Koltunski
      mEffects.chroma(chromaDyn, new Static3D(0,0,1) );
192 427ab7bf Leszek Koltunski
      
193
      try
194
        {
195 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
196 427ab7bf Leszek Koltunski
        }
197
      catch(Exception ex)
198
        {
199 e8b6aa95 Leszek Koltunski
        android.util.Log.e("FBO", ex.getMessage() );
200 427ab7bf Leszek Koltunski
        }
201
      }
202
}