Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 5055b5d4

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;
50
   private int lisaHeight, lisaWidth;
51
    
52
   private DistortedNode mRoot;
53
    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
   
63
    public void onDrawFrame(GL10 glUnused) 
64
      {
65
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
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( (float)lisaHeight/lisaWidth > (float)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(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(factor);
90
        }
91
      
92
      Distorted.onSurfaceChanged(width, height); 
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
98
      {
99
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
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
      DistortedBitmap text = new DistortedBitmap(bitmap2, 20);
126
      
127
      mRoot = new DistortedNode(mLisa);
128
      mRoot.attach(text);
129
     
130
      int textWidth  = text.getWidth();
131
      int textHeight = text.getHeight();
132

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

    
135
      text.move( new Static3D(lisaWidth/6,lisaHeight/3,0) );
136
      text.scale(factor);
137

    
138
      Dynamic1D sinkDyn = new Dynamic1D(5000,0.0f);
139
      sinkDyn.add(new Static1D(1.0f));
140
      sinkDyn.add(new Static1D(0.5f));
141

    
142
      text.sink(sinkDyn, new Static3D(textWidth/2,textHeight/2, 0));
143

    
144
      Dynamic1D chromaDyn = new Dynamic1D(10000,0.0f);
145
      chromaDyn.add(new Static1D(0.0f));
146
      chromaDyn.add(new Static1D(0.5f));
147

    
148
      mLisa.chroma(chromaDyn, new Static3D(0,0,1) );
149
      
150
      try
151
        {
152
        Distorted.onSurfaceCreated(mView.getContext());
153
        }
154
      catch(Exception ex)
155
        {
156
        android.util.Log.e("MonaLisa", ex.getMessage() );
157
        }
158
      }
159
}
(2-2/3)