Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.java @ 698ad0a8

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class MonaLisaRenderer implements GLSurfaceView.Renderer 
48
{
49
    private GLSurfaceView mView;
50
    private DistortedEffects mEffects;
51
    private DistortedTexture mTexture;
52
    private MeshRectangles mMesh;
53
    private DistortedScreen mScreen;
54
    private int bmpHeight, bmpWidth;
55
    private Static3D mMove, mScale;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

    
63
      // two points, centers of the Distort effect
64
      // the left and right tip of the mouth
65
      Static3D pLeft  = new Static3D( 90, 108, 0);
66
      Static3D pRight = new Static3D(176, 111, 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 , pLeft , rLeft ) );
89
      mEffects.apply( new VertexEffectDistort(dRight, pRight, rRight) );
90

    
91
      mMove = new Static3D(0,0,0);
92
      mScale= new Static3D(1,1,1);
93
      mEffects.apply(new MatrixEffectScale(mScale));
94
      mEffects.apply(new MatrixEffectMove(mMove));
95

    
96
      mScreen = new DistortedScreen();
97
      }
98

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
109
      { 
110
      if( (float)bmpHeight/bmpWidth > (float)height/width )
111
        {
112
        int w = (height*bmpWidth)/bmpHeight;
113
        float factor = (float)height/bmpHeight;
114

    
115
        mMove.set((width-w)/2,0,0);
116
        mScale.set( factor,factor,factor );
117
        }
118
      else
119
        {
120
        int h = (width*bmpHeight)/bmpWidth;
121
        float factor = (float)width/bmpWidth;
122

    
123
        mMove.set(0,(height-h)/2,0);
124
        mScale.set( factor,factor,factor );
125
        }
126

    
127
      mScreen.resize(width, height);
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
    
132
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
133
      {
134
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
135
      Bitmap bitmap;
136
        
137
      try 
138
        {
139
        bitmap = BitmapFactory.decodeStream(is);
140
        } 
141
      finally 
142
        {
143
        try 
144
          {
145
          is.close();
146
          } 
147
        catch(IOException e) { }
148
        }  
149

    
150
      bmpHeight = bitmap.getHeight();
151
      bmpWidth  = bitmap.getWidth();
152

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

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

    
173
      // even if mTexture wasn't null, we still need to call setTexture() on it
174
      // because every time activity goes to background, its OpenGL resources
175
      // - including Textures - get deleted. We always need to call setTexture()
176
      // to recreate the internal OpenGL textures.
177
      mTexture.setTexture(bitmap);
178

    
179
      mScreen.detachAll();
180
      mScreen.attach(mTexture,mEffects,mMesh);
181

    
182
      // All effects are by default disabled!
183
      VertexEffectDistort.enable();
184

    
185
      try
186
        {
187
        DistortedLibrary.onCreate(mView.getContext());
188
        }
189
      catch(Exception ex)
190
        {
191
        android.util.Log.e("MonaLisa", ex.getMessage() );
192
        }
193
      }
194
}
(2-2/3)