Project

General

Profile

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

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

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.DistortedNode;
31
import org.distorted.library.Distorted;
32
import org.distorted.library.DistortedBitmap;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static2D;
37
import org.distorted.library.type.Static3D;
38

    
39
import android.graphics.Bitmap;
40
import android.graphics.BitmapFactory;
41
import android.opengl.GLES20;
42
import android.opengl.GLSurfaceView;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class FBORenderer implements GLSurfaceView.Renderer 
47
{
48
   private GLSurfaceView mView;
49
   private DistortedBitmap mLisa, mText;
50
   private int lisaHeight, lisaWidth;
51
    
52
   private DistortedNode mRoot;
53
    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
   public FBORenderer(GLSurfaceView v) 
57
      {
58
      mView = v;
59
      }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
   
63
    public void onDrawFrame(GL10 glUnused) 
64
      {
65
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
66
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
67
      
68
      mRoot.draw(System.currentTimeMillis());
69
      }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
    
73
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
74
      { 
75
      mLisa.abortEffects(EffectTypes.MATRIX);
76
         
77
      if( lisaHeight/lisaWidth > height/width )
78
        {
79
        int w = (height*lisaWidth)/lisaHeight;
80
        float factor = (float)height/lisaHeight;
81

    
82
        mLisa.move( new Static3D((width-w)/2,0,0) );
83
        mLisa.scale( new Static3D(factor,factor,factor));
84
        }
85
      else
86
        {
87
        int h = (width*lisaHeight)/lisaWidth;
88
        float factor = (float)width/lisaWidth;
89

    
90
        mLisa.move(  new Static3D(0,(height-h)/2,0) );
91
        mLisa.scale( new Static3D(factor,factor,factor) );
92
        }
93
      
94
      Distorted.onSurfaceChanged(width, height); 
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
    
99
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
100
      {
101
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
102
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.fbo);
103
      
104
      Bitmap bitmap1, bitmap2;
105
       
106
      try 
107
        {
108
        bitmap1 = BitmapFactory.decodeStream(is1);
109
        bitmap2 = BitmapFactory.decodeStream(is2);
110
        } 
111
      finally 
112
        {
113
        try 
114
          {
115
          is1.close();
116
          is2.close();
117
          } 
118
        catch(IOException e) { }
119
        }  
120
      
121
      lisaHeight = bitmap1.getHeight();
122
      lisaWidth  = bitmap1.getWidth();
123
      
124
      mLisa = new DistortedBitmap(bitmap1, 20);
125
      mText = new DistortedBitmap(bitmap2, 20);
126
      
127
      mRoot = new DistortedNode(mLisa);
128
      mRoot.attach(mText);
129
     
130
      int textWidth  = mText.getWidth();
131
      int textHeight = mText.getHeight();
132

    
133
      float factor = lisaWidth/(1.5f*textWidth);
134

    
135
      mText.move( new Static3D(lisaWidth/6,lisaHeight/3,0) );
136
      mText.scale( new Static3D(factor,factor,factor) );
137
      mText.sink(0.5f, new Static2D( textWidth/2, textHeight/2), 5000, 0.0f);
138

    
139
      Dynamic1D macroblockDyn = new Dynamic1D();
140
      macroblockDyn.setDuration(10000);
141
      macroblockDyn.setCount(0);
142
      macroblockDyn.add(new Static1D(1));
143
      macroblockDyn.add(new Static1D(5));
144

    
145
      mLisa.macroblock(macroblockDyn);
146
      
147
      try
148
        {
149
        Distorted.onSurfaceCreated(mView.getContext());
150
        }
151
      catch(Exception ex)
152
        {
153
        android.util.Log.e("MonaLisa", ex.getMessage() );
154
        }
155
      }
156
}
(2-2/3)