Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.java @ 26c4e181

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
    private Static3D mCenterLeft, mCenterRight;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

    
62
      // two points, centers of the Distort effect
63
      // the left and right tip of the mouth
64
      // (0,0,0) for now because we don't know the size of the bitmap yet.
65
      mCenterLeft  = new Static3D(0,0,0);
66
      mCenterRight = new Static3D(0,0,0);
67

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

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

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

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

    
87
      mEffects = new DistortedEffects();
88
      mEffects.apply( new VertexEffectDistort(dLeft , mCenterLeft , rLeft ) );
89
      mEffects.apply( new VertexEffectDistort(dRight, mCenterRight, rRight) );
90

    
91
      mScale= new Static3D(1,1,1);
92
      mEffects.apply(new MatrixEffectScale(mScale));
93

    
94
      mScreen = new DistortedScreen();
95
      }
96

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

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

    
112
      mScale.set( factor,factor,factor );
113
      mScreen.resize(width, height);
114
      }
115

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

    
136
      int bmpHeight = bitmap.getHeight();
137
      int bmpWidth  = bitmap.getWidth();
138

    
139
      // Now we know the size of the bitmap, we can set the centers of effects
140
      // to (90,108) from the lower-left corner (CenterLeft) and (176,111) from the corner.
141
      mCenterLeft .set( 90 - bmpWidth/2, 108 - bmpHeight/2, 0);
142
      mCenterRight.set(176 - bmpWidth/2, 111 - bmpHeight/2, 0);
143

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

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

    
164
      // even if mTexture wasn't null, we still need to call setTexture() on it
165
      // because every time activity goes to background, its OpenGL resources
166
      // - including Textures - get deleted. We always need to call setTexture()
167
      // to recreate the internal OpenGL textures.
168
      mTexture.setTexture(bitmap);
169

    
170
      mScreen.detachAll();
171
      mScreen.attach(mTexture,mEffects,mMesh);
172

    
173
      // All effects are by default disabled!
174
      VertexEffectDistort.enable();
175

    
176
      try
177
        {
178
        DistortedLibrary.onCreate(mView.getContext());
179
        }
180
      catch(Exception ex)
181
        {
182
        android.util.Log.e("MonaLisa", ex.getMessage() );
183
        }
184
      }
185
}
(2-2/3)