Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 653d09dc

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 DistortedScreen mScreen;
57
   private DistortedNode mRoot;
58
   private int lisaHeight, lisaWidth;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
   FBORenderer(GLSurfaceView v)
63
      {
64
      mView   = v;
65
      mEffects= new DistortedEffects();
66
      mScreen = new DistortedScreen();
67
      }
68

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

    
71
   void setDepth(boolean depth)
72
      {
73
      if( mRoot!=null )
74
        {
75
        if( depth ) mRoot.glEnable (GLES30.GL_DEPTH_TEST);
76
        else        mRoot.glDisable(GLES30.GL_DEPTH_TEST);
77

    
78
        mRoot.glDepthMask(depth);
79

    
80
        // we can also, to save memory, delete/recreate
81
        // the depth buffer each time. This is optional.
82
        DistortedFramebuffer fbo = mRoot.getFramebuffer();
83
        fbo.enableDepth(depth);
84
        }
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
   
89
   public void onDrawFrame(GL10 glUnused)
90
      {
91
      mScreen.render(System.currentTimeMillis());
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
    
96
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
97
      { 
98
      mEffects.abortEffects(EffectTypes.MATRIX);
99
         
100
      if( (float)lisaHeight/lisaWidth > (float)height/width )
101
        {
102
        int w = (height*lisaWidth)/lisaHeight;
103
        float factor = (float)height/lisaHeight;
104

    
105
        mEffects.move( new Static3D((width-w)/2,0,0) );
106
        mEffects.scale(factor);
107
        }
108
      else
109
        {
110
        int h = (width*lisaHeight)/lisaWidth;
111
        float factor = (float)width/lisaWidth;
112

    
113
        mEffects.move( new Static3D(0,(height-h)/2,0) );
114
        mEffects.scale(factor);
115
        }
116
      
117
      mScreen.resize(width, height);
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
123
      {
124
      GLES30.glEnable(GLES30.GL_CULL_FACE);
125
      GLES30.glCullFace(GLES30.GL_BACK);
126
      GLES30.glFrontFace(GLES30.GL_CW);
127

    
128
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
129
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
130
      
131
      Bitmap bitmap1, bitmap2;
132
       
133
      try 
134
        {
135
        bitmap1 = BitmapFactory.decodeStream(is1);
136
        bitmap2 = BitmapFactory.decodeStream(is2);
137
        } 
138
      finally 
139
        {
140
        try 
141
          {
142
          is1.close();
143
          is2.close();
144
          } 
145
        catch(IOException e) { }
146
        }  
147
      
148
      lisaWidth     = bitmap1.getWidth();
149
      lisaHeight    = bitmap1.getHeight();
150
      int gridWidth = bitmap2.getWidth();
151
      int gridHeight= bitmap2.getHeight();
152

    
153
      DistortedTexture lisa = new DistortedTexture(lisaWidth,lisaHeight);
154
      DistortedTexture grid = new DistortedTexture(gridWidth,gridHeight);
155
      lisa.setTexture(bitmap1);
156
      grid.setTexture(bitmap2);
157
      DistortedEffects gridEffects = new DistortedEffects();
158

    
159
      mEffects.abortAllEffects();
160

    
161
      mRoot = new DistortedNode(lisa, mEffects,new MeshFlat(1,1));
162
      mRoot.attach(grid,gridEffects,new MeshCubes(10,10,false));
163

    
164
      mScreen.detachAll();
165
      mScreen.attach(mRoot);
166

    
167
      float factor = lisaWidth/(2.0f*gridWidth);
168

    
169
      gridEffects.move( new Static3D( (lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2,0) );
170
      gridEffects.scale(factor);
171

    
172
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
173
      rotDyn.add(new Static1D(  0));
174
      rotDyn.add(new Static1D(360));
175
      rotDyn.setMode(Dynamic.MODE_JUMP);
176

    
177
      gridEffects.rotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridHeight/10) );
178

    
179
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
180
      sinkDyn.add(new Static1D(1.0f));
181
      sinkDyn.add(new Static1D(0.3f));
182

    
183
      gridEffects.sink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
184

    
185
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
186
      chromaDyn.add(new Static1D(0.0f));
187
      chromaDyn.add(new Static1D(1.0f));
188

    
189
      mEffects.chroma(chromaDyn, new Static3D(0,0,1) );
190

    
191
      DistortedEffects.enableEffect(EffectNames.SINK);
192
      DistortedEffects.enableEffect(EffectNames.CHROMA);
193

    
194
      try
195
        {
196
        Distorted.onCreate(mView.getContext());
197
        }
198
      catch(Exception ex)
199
        {
200
        android.util.Log.e("FBO", ex.getMessage() );
201
        }
202
      }
203
}
(2-2/3)