Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 7589635e

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.Static2D;
35
import org.distorted.library.type.Static3D;
36

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

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

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

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

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

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

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

    
131
      float factor = lisaWidth/(1.5f*textWidth);
132

    
133
      mText.move( new Static3D(lisaWidth/6,lisaHeight/3,0) );
134
      mText.scale( new Static3D(factor,factor,factor) );
135
      mText.sink(0.5f, new Static2D( textWidth/2, textHeight/2), 5000, 0.0f);
136
      mLisa.macroblock(4, 10000, 0.0f);
137
      
138
      try
139
        {
140
        Distorted.onSurfaceCreated(mView.getContext());
141
        }
142
      catch(Exception ex)
143
        {
144
        android.util.Log.e("MonaLisa", ex.getMessage() );
145
        }
146
      }
147
}
(2-2/3)