Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 08eabc44

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.Float2D;
35

    
36
import android.graphics.Bitmap;
37
import android.graphics.BitmapFactory;
38
import android.opengl.GLES20;
39
import android.opengl.GLSurfaceView;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

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

    
53
   public FBORenderer(GLSurfaceView v) 
54
      {
55
      mView = v;
56
      }
57

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

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
    
70
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
71
      { 
72
      mLisa.abortEffects(EffectTypes.MATRIX);
73
         
74
      if( lisaHeight/lisaWidth > height/width )
75
        {
76
        int w = (height*lisaWidth)/lisaHeight;
77
        mLisa.move((width-w)/2 ,0, 0);
78
        mLisa.scale((float)height/lisaHeight);
79
        }
80
      else
81
        {
82
        int h = (width*lisaHeight)/lisaWidth;
83
        mLisa.move(0 ,(height-h)/2, 0);
84
        mLisa.scale((float)width/lisaWidth);
85
        }
86
      
87
      Distorted.onSurfaceChanged(width, height); 
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
    
92
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
93
      {
94
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
95
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.fbo);
96
      
97
      Bitmap bitmap1, bitmap2;
98
       
99
      try 
100
        {
101
        bitmap1 = BitmapFactory.decodeStream(is1);
102
        bitmap2 = BitmapFactory.decodeStream(is2);
103
        } 
104
      finally 
105
        {
106
        try 
107
          {
108
          is1.close();
109
          is2.close();
110
          } 
111
        catch(IOException e) { }
112
        }  
113
      
114
      lisaHeight = bitmap1.getHeight();
115
      lisaWidth  = bitmap1.getWidth();
116
      
117
      mLisa = new DistortedBitmap(bitmap1, 20);
118
      mText = new DistortedBitmap(bitmap2, 20);
119
      
120
      mRoot = new DistortedNode(mLisa);
121
      mRoot.attach(mText);
122
     
123
      int textWidth  = mText.getWidth();
124
      int textHeight = mText.getHeight();
125
      
126
      mText.move(lisaWidth/6 , lisaHeight/3, 0);
127
      mText.scale(lisaWidth/(1.5f*textWidth));
128
      mText.sink(0.5f, new Float2D( textWidth/2, textHeight/2), 5000, 0.0f);
129
      mLisa.macroblock(4, 10000, 0.0f);
130
      
131
      try
132
        {
133
        Distorted.onSurfaceCreated(mView.getContext());
134
        }
135
      catch(Exception ex)
136
        {
137
        android.util.Log.e("MonaLisa", ex.getMessage() );
138
        }
139
      }
140
}
(2-2/3)