Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.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.monalisa;
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
import org.distorted.library.Distorted;
30
import org.distorted.library.DistortedBitmap;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Float2D;
33
import org.distorted.library.type.Float3D;
34
import org.distorted.library.type.Float4D;
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 MonaLisaRenderer implements GLSurfaceView.Renderer 
44
{
45
    private GLSurfaceView mView;
46
    private DistortedBitmap monaLisa;
47
    private Float2D pLeft, pRight;
48
    private Float4D rLeft, rRight;
49
    private Float3D vLeft, vRight;
50
    private int bmpHeight, bmpWidth;
51
    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
    public MonaLisaRenderer(GLSurfaceView v) 
55
      {
56
      mView = v;
57
      
58
      pLeft = new Float2D( 90, 258);
59
      pRight= new Float2D(176, 255);
60
      
61
      rLeft = new Float4D(-10,-10,25,25);
62
      rRight= new Float4D( 10, -5,25,25);
63
      
64
      vLeft = new Float3D(-20,-20,0);
65
      vRight= new Float3D( 20,-10,0);
66
      }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
   
70
    public void onDrawFrame(GL10 glUnused) 
71
      {
72
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
73
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
74
      
75
      monaLisa.draw(System.currentTimeMillis());
76
      }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
    
80
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
81
      { 
82
      monaLisa.abortEffects(EffectTypes.MATRIX);
83
         
84
      if( bmpHeight/bmpWidth > height/width )
85
        {
86
        int w = (height*bmpWidth)/bmpHeight;
87
        monaLisa.move((width-w)/2 ,0, 0);
88
        monaLisa.scale((float)height/bmpHeight);
89
        }
90
      else
91
        {
92
        int h = (width*bmpHeight)/bmpWidth;
93
        monaLisa.move(0 ,(height-h)/2, 0);
94
        monaLisa.scale((float)width/bmpWidth);
95
        }
96
      
97
      Distorted.onSurfaceChanged(width, height); 
98
      }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
103
      {
104
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
105
      Bitmap bitmap;
106
        
107
      try 
108
        {
109
        bitmap = BitmapFactory.decodeStream(is);
110
        } 
111
      finally 
112
        {
113
        try 
114
          {
115
          is.close();
116
          } 
117
        catch(IOException e) { }
118
        }  
119
      
120
      bmpHeight = bitmap.getHeight();
121
      bmpWidth  = bitmap.getWidth();
122
      
123
      monaLisa = new DistortedBitmap(bitmap, 10);
124
      monaLisa.distort( vLeft, rLeft , pLeft, 1000, 0);
125
      monaLisa.distort(vRight, rRight, pRight,1000, 0);
126
      
127
      try
128
        {
129
        Distorted.onSurfaceCreated(mView.getContext());
130
        }
131
      catch(Exception ex)
132
        {
133
        android.util.Log.e("MonaLisa", ex.getMessage() );
134
        }
135
      }
136
}
(2-2/3)