Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c6526577 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
37 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40 427ab7bf Leszek Koltunski
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 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
51 b0ebdf5e Leszek Koltunski
    private DistortedTexture mTexture;
52 f3ca895f Leszek Koltunski
    private MeshRectangles mMesh;
53 d218d64e leszek
    private DistortedScreen mScreen;
54 8ff32d4d Leszek Koltunski
    private int bmpHeight, bmpWidth;
55 c6526577 Leszek Koltunski
    private Static3D mMove, mScale;
56 7bf107f7 Leszek Koltunski
57 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 f6d884d5 Leszek Koltunski
    MonaLisaRenderer(GLSurfaceView v)
60 427ab7bf Leszek Koltunski
      {
61
      mView = v;
62 8ff32d4d Leszek Koltunski
63 a4d59c0b Leszek Koltunski
      // 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 8ff32d4d Leszek Koltunski
68 a4d59c0b Leszek Koltunski
      // 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 f6d884d5 Leszek Koltunski
      Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
75
      Dynamic3D dRight= new Dynamic3D(1000,0.0f);
76 a4d59c0b Leszek Koltunski
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 f6d884d5 Leszek Koltunski
87 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
88 c6526577 Leszek Koltunski
      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 dae661e9 Leszek Koltunski
      mEffects.apply(new MatrixEffectMove(mMove));
95 392e16fd Leszek Koltunski
96 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
97 427ab7bf Leszek Koltunski
      }
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
   
101
    public void onDrawFrame(GL10 glUnused) 
102
      {
103 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
104 427ab7bf Leszek Koltunski
      }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
109 630703d1 leszek
      { 
110
      if( (float)bmpHeight/bmpWidth > (float)height/width )
111
        {
112
        int w = (height*bmpWidth)/bmpHeight;
113
        float factor = (float)height/bmpHeight;
114 c6526577 Leszek Koltunski
115
        mMove.set((width-w)/2,0,0);
116
        mScale.set( factor,factor,factor );
117 630703d1 leszek
        }
118
      else
119
        {
120
        int h = (width*bmpHeight)/bmpWidth;
121
        float factor = (float)width/bmpWidth;
122 c6526577 Leszek Koltunski
123
        mMove.set(0,(height-h)/2,0);
124
        mScale.set( factor,factor,factor );
125 630703d1 leszek
        }
126 7589635e Leszek Koltunski
127 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
128 427ab7bf Leszek Koltunski
      }
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 3c28857f Leszek Koltunski
150 8ff32d4d Leszek Koltunski
      bmpHeight = bitmap.getHeight();
151
      bmpWidth  = bitmap.getWidth();
152
153 698ad0a8 Leszek Koltunski
      // 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 687263cc Leszek Koltunski
      // 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 698ad0a8 Leszek Koltunski
      if( mMesh==null )
168
        {
169
        mMesh = new MeshRectangles(9,9*bmpHeight/bmpWidth);
170
        mMesh.setStretch(bmpWidth,bmpHeight,0);
171
        }
172 5f2a53b6 leszek
173 b0ebdf5e Leszek Koltunski
      // 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 fe59d375 Leszek Koltunski
179
      mScreen.detachAll();
180 5f2a53b6 leszek
      mScreen.attach(mTexture,mEffects,mMesh);
181 7bf107f7 Leszek Koltunski
182 a4d59c0b Leszek Koltunski
      // All effects are by default disabled!
183 885b9cca leszek
      VertexEffectDistort.enable();
184 6637d0f2 Leszek Koltunski
185 427ab7bf Leszek Koltunski
      try
186
        {
187 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
188 427ab7bf Leszek Koltunski
        }
189
      catch(Exception ex)
190
        {
191
        android.util.Log.e("MonaLisa", ex.getMessage() );
192
        }
193
      }
194
}