Project

General

Profile

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

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

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 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
41
import android.content.Context;
42
import android.content.pm.ConfigurationInfo;
43
import android.content.res.Resources;
44 427ab7bf Leszek Koltunski
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLSurfaceView;
47
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 dc10a48d Leszek Koltunski
class MonaLisaRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
51 427ab7bf Leszek Koltunski
{
52 dc10a48d Leszek Koltunski
    private final GLSurfaceView mView;
53
    private final Resources mResources;
54
    private final DistortedEffects mEffects;
55
    private final DistortedTexture mTexture;
56
    private final DistortedScreen mScreen;
57
    private final Static3D mScale;
58
59 f2085b96 Leszek Koltunski
    private MeshSquare mMesh;
60 67038dda Leszek Koltunski
    private float mBmpRatio;
61 7bf107f7 Leszek Koltunski
62 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64 f6d884d5 Leszek Koltunski
    MonaLisaRenderer(GLSurfaceView v)
65 427ab7bf Leszek Koltunski
      {
66
      mView = v;
67 dc10a48d Leszek Koltunski
      mResources = v.getResources();
68 8ff32d4d Leszek Koltunski
69 4c42bc15 Leszek Koltunski
      // Centers of the Effects.
70
      // center = (0,0,0) would be the midpoint of the Bitmap.
71 849e0034 Leszek Koltunski
      // MonaLisa bitmap is 320x366, this is thus (90,108) pixels from the lower-left corner
72 67038dda Leszek Koltunski
      Static3D centerLeft  = new Static3D( ( 90-320*0.5f)/320.0f, (108-366*0.5f)/366.0f, 0);
73 849e0034 Leszek Koltunski
      // (176,111) from the lower left
74 67038dda Leszek Koltunski
      Static3D centerRight = new Static3D( (176-320*0.5f)/320.0f, (111-366*0.5f)/366.0f, 0);
75 8ff32d4d Leszek Koltunski
76 4c42bc15 Leszek Koltunski
      // two Regions defining the areas affected by the Distort effect
77
      // a Region is like a mask that specifies which area around the Center is affected by
78
      // the Distort. Here (-10,10,0) pixels from the left center, with radius 25 pixels
79
      Static4D regionLeft  = new Static4D( -10/320.0f, 10/366.0f, 0, 25/320.0f);
80
      // and likewise (10,5,0) pixels from the right Center, radius 25 pixels.
81
      Static4D regionRight = new Static4D(  10/320.0f,  5/366.0f, 0, 25/320.0f);
82 a4d59c0b Leszek Koltunski
83
      // two dynamics for interpolating through vectors of distortion. Interpolate
84 f209a803 Leszek Koltunski
      // every 1000 milliseconds, indefinitely ('0.0f').
85 4c42bc15 Leszek Koltunski
      Dynamic3D degLeft = new Dynamic3D(1000,0.0f);
86
      Dynamic3D degRight= new Dynamic3D(1000,0.0f);
87 a4d59c0b Leszek Koltunski
88
      // two vectors of distortion the left tip of the mouth gets distorted with -
89 4c42bc15 Leszek Koltunski
      // interpolated from (0,0,0) - i.e. no distortion - to (-20 pixels, 20 pixels, 0),
90
      // i.e. slightly to the top left.
91
      degLeft.add ( new Static3D(         0,         0, 0) );
92
      degLeft.add ( new Static3D(-20/320.0f, 20/366.0f, 0) );
93 a4d59c0b Leszek Koltunski
94
      // likewise two vectors the right tip is distorted with.
95 4c42bc15 Leszek Koltunski
      degRight.add( new Static3D(         0,         0, 0) );
96
      degRight.add( new Static3D( 20/320.0f, 10/366.0f, 0) );
97 f6d884d5 Leszek Koltunski
98 67038dda Leszek Koltunski
      // Equip MonaLisa with the Effects we want to draw her with - i.e. two Distorts of the mouth
99 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
100 4c42bc15 Leszek Koltunski
      mEffects.apply( new VertexEffectDistort( degLeft , centerLeft , regionLeft ) );
101
      mEffects.apply( new VertexEffectDistort( degRight, centerRight, regionRight) );
102 c6526577 Leszek Koltunski
103 67038dda Leszek Koltunski
      // ... and a Scale - so far by (1,1,1), i.e. no scale. The mScale point will be computed
104 4c42bc15 Leszek Koltunski
      // later, in onSurfaceChanged, when we actually know the size of the screen and thus how
105 67038dda Leszek Koltunski
      // much to scale the initial Mesh (which is 1x1x0 in size)
106 c6526577 Leszek Koltunski
      mScale= new Static3D(1,1,1);
107
      mEffects.apply(new MatrixEffectScale(mScale));
108 392e16fd Leszek Koltunski
109 4c42bc15 Leszek Koltunski
      mTexture = new DistortedTexture();
110
      mScreen  = new DistortedScreen();
111 427ab7bf Leszek Koltunski
      }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
   
115
    public void onDrawFrame(GL10 glUnused) 
116
      {
117 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
118 427ab7bf Leszek Koltunski
      }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
123 d381aa80 Leszek Koltunski
      {
124 c7a31368 Leszek Koltunski
      if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
125
      else               mScale.set( height/mBmpRatio, height, 1 );
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 dc10a48d Leszek Koltunski
        catch(IOException ignored) { }
148 427ab7bf Leszek Koltunski
        }  
149 3c28857f Leszek Koltunski
150 67038dda Leszek Koltunski
      mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
151 8ff32d4d Leszek Koltunski
152 67038dda Leszek Koltunski
      // Create an underlying Mesh of 9x10 vertices - this is for the Distort
153
      // effect to have vertices to distort. We multiply by mBmpRatio here so
154
      // that the Mesh's cells are as close to squares as possible (when they
155
      // finally be rendered with a Scale - see mScale)
156 f2085b96 Leszek Koltunski
      if( mMesh==null ) mMesh = new MeshSquare(9, (int)(9*mBmpRatio) );
157 5f2a53b6 leszek
158 4c42bc15 Leszek Koltunski
      // Every time activity goes to background, its OpenGL resources - including
159
      // Textures - get deleted. We always need to call setTexture() here to
160
      // recreate the internal OpenGL textures.
161 b0ebdf5e Leszek Koltunski
      mTexture.setTexture(bitmap);
162 fe59d375 Leszek Koltunski
163 67038dda Leszek Koltunski
      // Build the Scene Graph - attach all Objects we want to be rendered to the Screen.
164 fe59d375 Leszek Koltunski
      mScreen.detachAll();
165 5f2a53b6 leszek
      mScreen.attach(mTexture,mEffects,mMesh);
166 7bf107f7 Leszek Koltunski
167 67038dda Leszek Koltunski
      // All effects are by default disabled! Enable all we need (and better only those)
168
      // before the call to DistortedLibrary.onCreate(). Best done here.
169 885b9cca leszek
      VertexEffectDistort.enable();
170 6637d0f2 Leszek Koltunski
171 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
172 061449ed Leszek Koltunski
      }
173
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
176
    public void distortedException(Exception ex)
177
      {
178
      android.util.Log.e("MonaLisa", ex.getMessage() );
179 427ab7bf Leszek Koltunski
      }
180 dc10a48d Leszek Koltunski
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
183
    public int openGlVersion()
184
      {
185
      Context context = mView.getContext();
186
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
187
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
188
      int glESversion = configurationInfo.reqGlEsVersion;
189
      int major = glESversion >> 16;
190
      int minor = glESversion & 0xff;
191
192
      return 100*major + 10*minor;
193
      }
194
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
197
    public InputStream localFile(int fileID)
198
      {
199
      return mResources.openRawResource(fileID);
200
      }
201
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
204
    public void logMessage(String message)
205
      {
206
      android.util.Log.e("MonaLisa", message );
207
      }
208 427ab7bf Leszek Koltunski
}