Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ dc10a48d

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.bean;
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

    
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshSquare;
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.app.ActivityManager;
42
import android.content.Context;
43
import android.content.pm.ConfigurationInfo;
44
import android.content.res.Resources;
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class BeanRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
52
{
53
   private final GLSurfaceView mView;
54
   private final Resources mResources;
55
   private final DistortedEffects mEffects;
56
   private final DistortedScreen mScreen;
57
   private final DistortedTexture mTexture;
58
   private final Static3D mScale;
59

    
60
   private MeshSquare mMesh;
61
   private float mBmpRatio;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
// bean bitmap is 366 x 480. Initialize two dynamic Distorts of the left and right brow.
65

    
66
   BeanRenderer(GLSurfaceView v)
67
      {
68
      mView = v;
69
      mResources = v.getResources();
70

    
71
      Static3D pointLeft  = new Static3D(-85/366.0f,  57/480.0f, 0);
72
      Static3D pointRight = new Static3D( 50/366.0f, 100/480.0f, 0);
73
      Static4D regionLeft = new Static4D( -3/366.0f,  33/480.0f, 0, 47/400.0f);
74
      Static4D regionRight= new Static4D(-14/366.0f,  33/480.0f, 0, 47/400.0f);
75

    
76
      Dynamic3D dynLeft   = new Dynamic3D(2000,0.0f);
77
      Dynamic3D dynRight  = new Dynamic3D(2000,0.0f);
78

    
79
      Static3D vect1 = new Static3D(         0,         0, 0);
80
      Static3D vect2 = new Static3D(-15/366.0f, 30/480.0f, 0);
81

    
82
      dynLeft.add(vect1);
83
      dynLeft.add(vect1);
84
      dynLeft.add(vect1);
85
      dynLeft.add(vect1);
86
      dynLeft.add(vect2);
87
      dynLeft.add(vect2);
88
      
89
      dynRight.add(vect1);
90
      dynRight.add(vect2);
91
      dynRight.add(vect2);
92
      dynRight.add(vect1);
93
      dynRight.add(vect1);
94
      dynRight.add(vect1);
95

    
96
      mScale= new Static3D(1,1,1);
97

    
98
      mEffects = new DistortedEffects();
99
      mEffects.apply( new VertexEffectDistort(dynLeft , pointLeft , regionLeft) );
100
      mEffects.apply( new VertexEffectDistort(dynRight, pointRight, regionRight));
101
      mEffects.apply( new MatrixEffectScale(mScale) );
102

    
103
      mTexture = new DistortedTexture();
104
      mScreen  = new DistortedScreen();
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
   
109
   public void onDrawFrame(GL10 glUnused)
110
      {
111
      mScreen.render( System.currentTimeMillis() );
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
117
     {
118
     if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
119
     else               mScale.set( height/mBmpRatio, height, 1 );
120

    
121
     mScreen.resize(width, height);
122
     }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
    
126
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
127
     {
128
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
129
     Bitmap bitmap;
130
        
131
     try
132
       {
133
       bitmap = BitmapFactory.decodeStream(is);
134
       }
135
     finally
136
       {
137
       try
138
         {
139
         is.close();
140
         }
141
       catch(IOException ignored) { }
142
       }
143
      
144
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
145
     mTexture.setTexture(bitmap);
146

    
147
     // we need a denser Mesh lattice this time for the Distorts to look good.
148

    
149
     float PART = 0.47f;
150

    
151
     int width = 25;
152
     int height= (int)((25*mBmpRatio)*PART);
153

    
154
     float[] xLoc = new float[width +1];
155
     float[] yLoc = new float[height+1];
156

    
157
     xLoc[0] = -0.5f;
158
     yLoc[0] = -0.5f;
159

    
160
     for(int i=1; i<=width; i++) xLoc[i] = 1.0f/width;
161
     for(int i=1; i<height; i++) yLoc[i] = PART/height;
162

    
163
     yLoc[height] = (height-(height-1)*PART)/height;
164

    
165
     if( mMesh==null ) mMesh = new MeshSquare(xLoc,yLoc);
166

    
167
     mScreen.detachAll();
168
     mScreen.attach(mTexture,mEffects,mMesh);
169

    
170
     VertexEffectDistort.enable();
171

    
172
     DistortedLibrary.onSurfaceCreated(this);
173
     }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
   public void distortedException(Exception ex)
178
     {
179
     android.util.Log.e("Bean", ex.getMessage() );
180
     }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
    public int openGlVersion()
185
      {
186
      Context context = mView.getContext();
187
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
188
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
189
      int glESversion = configurationInfo.reqGlEsVersion;
190
      int major = glESversion >> 16;
191
      int minor = glESversion & 0xff;
192

    
193
      return 100*major + 10*minor;
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    public InputStream localFile(int fileID)
199
      {
200
      return mResources.openRawResource(fileID);
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
    public void logMessage(String message)
206
      {
207
      android.util.Log.e("Bean", message );
208
      }
209
}
(2-2/3)