Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.java @ f209a803

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.MatrixEffectScale;
30
import org.distorted.library.effect.VertexEffectDistort;
31 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
32 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
36 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
37 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39 427ab7bf Leszek Koltunski
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 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
50 b0ebdf5e Leszek Koltunski
    private DistortedTexture mTexture;
51 f3ca895f Leszek Koltunski
    private MeshRectangles mMesh;
52 d218d64e leszek
    private DistortedScreen mScreen;
53 d381aa80 Leszek Koltunski
    private Static3D mScale;
54 67038dda Leszek Koltunski
    private float mBmpRatio;
55 7bf107f7 Leszek Koltunski
56 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 f6d884d5 Leszek Koltunski
    MonaLisaRenderer(GLSurfaceView v)
59 427ab7bf Leszek Koltunski
      {
60
      mView = v;
61 8ff32d4d Leszek Koltunski
62 849e0034 Leszek Koltunski
      // MonaLisa bitmap is 320x366, this is thus (90,108) pixels from the lower-left corner
63 67038dda Leszek Koltunski
      Static3D centerLeft  = new Static3D( ( 90-320*0.5f)/320.0f, (108-366*0.5f)/366.0f, 0);
64 849e0034 Leszek Koltunski
      // (176,111) from the lower left
65 67038dda Leszek Koltunski
      Static3D centerRight = new Static3D( (176-320*0.5f)/320.0f, (111-366*0.5f)/366.0f, 0);
66 8ff32d4d Leszek Koltunski
67 a4d59c0b Leszek Koltunski
      // two regions defining the areas affected by the Distort effect
68 67038dda Leszek Koltunski
      Static4D rLeft  = new Static4D( -10/320.0f, 10/366.0f, 0, 25/320.0f);
69
      Static4D rRight = new Static4D(  10/320.0f,  5/366.0f, 0, 25/320.0f);
70 a4d59c0b Leszek Koltunski
71
      // two dynamics for interpolating through vectors of distortion. Interpolate
72 f209a803 Leszek Koltunski
      // every 1000 milliseconds, indefinitely ('0.0f').
73 f6d884d5 Leszek Koltunski
      Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
74
      Dynamic3D dRight= new Dynamic3D(1000,0.0f);
75 a4d59c0b Leszek Koltunski
76
      // two vectors of distortion the left tip of the mouth gets distorted with -
77 67038dda Leszek Koltunski
      // interpolated from (0,0,0) - i.e. no distortion - to (-20 pixels, 20 pixels, 0), i.e.
78 a4d59c0b Leszek Koltunski
      // slightly to the top left.
79 67038dda Leszek Koltunski
      dLeft.add ( new Static3D(         0,         0, 0) );
80
      dLeft.add ( new Static3D(-20/320.0f, 20/366.0f, 0) );
81 a4d59c0b Leszek Koltunski
82
      // likewise two vectors the right tip is distorted with.
83 67038dda Leszek Koltunski
      dRight.add( new Static3D(         0,         0, 0) );
84
      dRight.add( new Static3D( 20/320.0f, 10/366.0f, 0) );
85 f6d884d5 Leszek Koltunski
86 67038dda Leszek Koltunski
      // Equip MonaLisa with the Effects we want to draw her with - i.e. two Distorts of the mouth
87 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
88 849e0034 Leszek Koltunski
      mEffects.apply( new VertexEffectDistort(dLeft , centerLeft , rLeft ) );
89
      mEffects.apply( new VertexEffectDistort(dRight, centerRight, rRight) );
90 c6526577 Leszek Koltunski
91 67038dda Leszek Koltunski
      // ... and a Scale - so far by (1,1,1), i.e. no scale. The mScale point will be computed
92
      // later, in onSurfaceChanged, when we actually know the size of the screen and know how
93
      // much to scale the initial Mesh (which is 1x1x0 in size)
94 c6526577 Leszek Koltunski
      mScale= new Static3D(1,1,1);
95
      mEffects.apply(new MatrixEffectScale(mScale));
96 392e16fd Leszek Koltunski
97 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
98 427ab7bf Leszek Koltunski
      }
99
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
   
102
    public void onDrawFrame(GL10 glUnused) 
103
      {
104 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
105 427ab7bf Leszek Koltunski
      }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
110 d381aa80 Leszek Koltunski
      {
111 67038dda Leszek Koltunski
      float min= width>height ? height : width;
112 7589635e Leszek Koltunski
113 67038dda Leszek Koltunski
      mScale.set( min, min*mBmpRatio, 1 );
114 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
115 427ab7bf Leszek Koltunski
      }
116
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
120
      {
121
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
122
      Bitmap bitmap;
123
        
124
      try 
125
        {
126
        bitmap = BitmapFactory.decodeStream(is);
127
        } 
128
      finally 
129
        {
130
        try 
131
          {
132
          is.close();
133
          } 
134
        catch(IOException e) { }
135
        }  
136 3c28857f Leszek Koltunski
137 67038dda Leszek Koltunski
      mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
138 8ff32d4d Leszek Koltunski
139 698ad0a8 Leszek Koltunski
      // We could have gotten here after the activity went to the background
140
      // for a brief amount of time; in this case mTexture is already created.
141 67038dda Leszek Koltunski
      // Do not create it the second time around then.
142 698ad0a8 Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture();
143
144
      // likewise the Mesh
145 67038dda Leszek Koltunski
      // Create an underlying Mesh of 9x10 vertices - this is for the Distort
146
      // effect to have vertices to distort. We multiply by mBmpRatio here so
147
      // that the Mesh's cells are as close to squares as possible (when they
148
      // finally be rendered with a Scale - see mScale)
149
      if( mMesh==null ) mMesh = new MeshRectangles(9, (int)(9*mBmpRatio) );
150 5f2a53b6 leszek
151 b0ebdf5e Leszek Koltunski
      // even if mTexture wasn't null, we still need to call setTexture() on it
152
      // because every time activity goes to background, its OpenGL resources
153
      // - including Textures - get deleted. We always need to call setTexture()
154
      // to recreate the internal OpenGL textures.
155
      mTexture.setTexture(bitmap);
156 fe59d375 Leszek Koltunski
157 67038dda Leszek Koltunski
      // Build the Scene Graph - attach all Objects we want to be rendered to the Screen.
158 fe59d375 Leszek Koltunski
      mScreen.detachAll();
159 5f2a53b6 leszek
      mScreen.attach(mTexture,mEffects,mMesh);
160 7bf107f7 Leszek Koltunski
161 67038dda Leszek Koltunski
      // All effects are by default disabled! Enable all we need (and better only those)
162
      // before the call to DistortedLibrary.onCreate(). Best done here.
163 885b9cca leszek
      VertexEffectDistort.enable();
164 6637d0f2 Leszek Koltunski
165 427ab7bf Leszek Koltunski
      try
166
        {
167 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
168 427ab7bf Leszek Koltunski
        }
169
      catch(Exception ex)
170
        {
171
        android.util.Log.e("MonaLisa", ex.getMessage() );
172
        }
173
      }
174
}