Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.java @ 5055b5d4

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