Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.java @ 7bf107f7

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.monalisa;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29
import org.distorted.library.Distorted;
30
import org.distorted.library.DistortedBitmap;
31 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
32 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
33 7589635e Leszek Koltunski
import org.distorted.library.type.Static2D;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36 427ab7bf Leszek Koltunski
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 MonaLisaRenderer implements GLSurfaceView.Renderer 
45
{
46
    private GLSurfaceView mView;
47
    private DistortedBitmap monaLisa;
48 7589635e Leszek Koltunski
    private Static2D pLeft, pRight;
49
    private Static4D rLeft, rRight;
50 7bf107f7 Leszek Koltunski
    private Dynamic3D dLeft, dRight;
51
52 427ab7bf Leszek Koltunski
    private int bmpHeight, bmpWidth;
53
    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56
    public MonaLisaRenderer(GLSurfaceView v) 
57
      {
58
      mView = v;
59
      
60 7589635e Leszek Koltunski
      pLeft = new Static2D( 90, 258);
61
      pRight= new Static2D(176, 255);
62 427ab7bf Leszek Koltunski
      
63 7589635e Leszek Koltunski
      rLeft = new Static4D(-10,-10,25,25);
64
      rRight= new Static4D( 10, -5,25,25);
65 7bf107f7 Leszek Koltunski
66
      dLeft = new Dynamic3D();
67
      dRight= new Dynamic3D();
68
69
      dLeft.setDuration(1000);
70
      dRight.setDuration(1000);
71
72
      dLeft.setCount(0);
73
      dRight.setCount(0);
74
75
      dLeft.add( new Static3D(  0,  0,0) );
76
      dLeft.add( new Static3D(-20,-20,0) );
77
78
      dRight.add( new Static3D(  0,  0,0) );
79
      dRight.add( new Static3D( 20,-10,0) );
80 427ab7bf Leszek Koltunski
      }
81
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
   
84
    public void onDrawFrame(GL10 glUnused) 
85
      {
86
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
87
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
88
      
89
      monaLisa.draw(System.currentTimeMillis());
90
      }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
    
94
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
95
      { 
96 a8c3ada7 Leszek Koltunski
      monaLisa.abortEffects(EffectTypes.MATRIX);
97 427ab7bf Leszek Koltunski
         
98
      if( bmpHeight/bmpWidth > height/width )
99
        {
100
        int w = (height*bmpWidth)/bmpHeight;
101 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
102
103
        monaLisa.move( new Static3D((width-w)/2,0,0) );
104
        monaLisa.scale( new Static3D(factor,factor,factor) );
105 427ab7bf Leszek Koltunski
        }
106
      else
107
        {
108
        int h = (width*bmpHeight)/bmpWidth;
109 7589635e Leszek Koltunski
        float factor = (float)width/bmpWidth;
110
111
        monaLisa.move( new Static3D(0,(height-h)/2,0) );
112
        monaLisa.scale( new Static3D(factor,factor,factor) );
113 427ab7bf Leszek Koltunski
        }
114
      
115
      Distorted.onSurfaceChanged(width, height); 
116
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
121
      {
122
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
123
      Bitmap bitmap;
124
        
125
      try 
126
        {
127
        bitmap = BitmapFactory.decodeStream(is);
128
        } 
129
      finally 
130
        {
131
        try 
132
          {
133
          is.close();
134
          } 
135
        catch(IOException e) { }
136
        }  
137
      
138
      bmpHeight = bitmap.getHeight();
139
      bmpWidth  = bitmap.getWidth();
140
      
141
      monaLisa = new DistortedBitmap(bitmap, 10);
142 7bf107f7 Leszek Koltunski
      monaLisa.distort( dLeft, pLeft , rLeft );
143
      monaLisa.distort(dRight, pRight, rRight);
144
145 427ab7bf Leszek Koltunski
      try
146
        {
147 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
148 427ab7bf Leszek Koltunski
        }
149
      catch(Exception ex)
150
        {
151
        android.util.Log.e("MonaLisa", ex.getMessage() );
152
        }
153
      }
154
}