Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.java @ 849e0034

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.effect.MatrixEffectScale;
30
import org.distorted.library.effect.VertexEffectDistort;
31
import org.distorted.library.main.DistortedLibrary;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.mesh.MeshRectangles;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42
import android.opengl.GLSurfaceView;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class MonaLisaRenderer implements GLSurfaceView.Renderer 
47
{
48
    private GLSurfaceView mView;
49
    private DistortedEffects mEffects;
50
    private DistortedTexture mTexture;
51
    private MeshRectangles mMesh;
52
    private DistortedScreen mScreen;
53
    private Static3D mScale;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
    MonaLisaRenderer(GLSurfaceView v)
58
      {
59
      mView = v;
60

    
61
      // MonaLisa bitmap is 320x366, this is thus (90,108) pixels from the lower-left corner
62
      Static3D centerLeft  = new Static3D( 90-320/2, 108-366/2,0);
63
      // (176,111) from the lower left
64
      Static3D centerRight = new Static3D(176-320/2, 111-366/2,0);
65

    
66
      // two regions defining the areas affected by the Distort effect
67
      Static4D rLeft  = new Static4D(-10, 10, 0, 25);
68
      Static4D rRight = new Static4D( 10,  5, 0, 25);
69

    
70
      // two dynamics for interpolating through vectors of distortion. Interpolate
71
      // every 1000 miliseconds, indefinately ('0.0f').
72
      Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
73
      Dynamic3D dRight= new Dynamic3D(1000,0.0f);
74

    
75
      // two vectors of distortion the left tip of the mouth gets distorted with -
76
      // interpolated from (0,0,0) - i.e. no distortion - to (-20,20,0), i.e.
77
      // slightly to the top left.
78
      dLeft.add ( new Static3D(  0,  0, 0) );
79
      dLeft.add ( new Static3D(-20, 20, 0) );
80

    
81
      // likewise two vectors the right tip is distorted with.
82
      dRight.add( new Static3D(  0,  0, 0) );
83
      dRight.add( new Static3D( 20, 10, 0) );
84

    
85
      mEffects = new DistortedEffects();
86
      mEffects.apply( new VertexEffectDistort(dLeft , centerLeft , rLeft ) );
87
      mEffects.apply( new VertexEffectDistort(dRight, centerRight, rRight) );
88

    
89
      mScale= new Static3D(1,1,1);
90
      mEffects.apply(new MatrixEffectScale(mScale));
91

    
92
      mScreen = new DistortedScreen();
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
   
97
    public void onDrawFrame(GL10 glUnused) 
98
      {
99
      mScreen.render( System.currentTimeMillis() );
100
      }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
    
104
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
105
      {
106
      float horiRatio = (float)width / mMesh.getStretchX();
107
      float vertRatio = (float)height/ mMesh.getStretchY();
108
      float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
109

    
110
      mScale.set( factor,factor,factor );
111
      mScreen.resize(width, height);
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
117
      {
118
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
119
      Bitmap bitmap;
120
        
121
      try 
122
        {
123
        bitmap = BitmapFactory.decodeStream(is);
124
        } 
125
      finally 
126
        {
127
        try 
128
          {
129
          is.close();
130
          } 
131
        catch(IOException e) { }
132
        }  
133

    
134
      int bmpHeight = bitmap.getHeight();
135
      int bmpWidth  = bitmap.getWidth();
136

    
137
      // We could have gotten here after the activity went to the background
138
      // for a brief amount of time; in this case mTexture is already created.
139
      // Do not leak memory by creating it the second time around.
140
      if( mTexture==null ) mTexture = new DistortedTexture();
141

    
142
      // likewise the Mesh
143
      // This will make the Mesh stretched by bmpWidth x bmpHeight even before any effects
144
      // are applied to it (the Mesh - MeshRectangles - is flat, so the third parameter does not
145
      // not matter). bmpWight x bmpHeight is the size of the Bitmap, thus this means that we can
146
      // conveniently work with Effects thinking in Bitmap's native size in pixels. The origin is
147
      // in Bitmap's lower-left corner, thus e.g. a rotation which is meant to rotate the bitmap
148
      // around its center has to be centered at (bmpWidth/2, bmpHeight/2, 0).
149
      // Without this call, the default size of the Mesh is 1x1x0 ( or 1x1x1 in case of not-flat
150
      // Meshes) so we would need to be rotating around (0.5,0.5,0.0).
151
      if( mMesh==null )
152
        {
153
        mMesh = new MeshRectangles(9,9*bmpHeight/bmpWidth);
154
        mMesh.setStretch(bmpWidth,bmpHeight,0);
155
        }
156

    
157
      // even if mTexture wasn't null, we still need to call setTexture() on it
158
      // because every time activity goes to background, its OpenGL resources
159
      // - including Textures - get deleted. We always need to call setTexture()
160
      // to recreate the internal OpenGL textures.
161
      mTexture.setTexture(bitmap);
162

    
163
      mScreen.detachAll();
164
      mScreen.attach(mTexture,mEffects,mMesh);
165

    
166
      // All effects are by default disabled!
167
      VertexEffectDistort.enable();
168

    
169
      try
170
        {
171
        DistortedLibrary.onCreate(mView.getContext());
172
        }
173
      catch(Exception ex)
174
        {
175
        android.util.Log.e("MonaLisa", ex.getMessage() );
176
        }
177
      }
178
}
(2-2/3)