Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ 625c67de

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.bean;
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 427ab7bf Leszek Koltunski
30 fa9b6494 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
36 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
37 b5cc7760 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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
42
import android.content.Context;
43
import android.content.pm.ConfigurationInfo;
44
import android.content.res.Resources;
45 427ab7bf Leszek Koltunski
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 dc10a48d Leszek Koltunski
class BeanRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
52 427ab7bf Leszek Koltunski
{
53 dc10a48d Leszek Koltunski
   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 f2085b96 Leszek Koltunski
   private MeshSquare mMesh;
61 a7a8f7bd Leszek Koltunski
   private float mBmpRatio;
62 fa9b6494 Leszek Koltunski
63 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64 a7a8f7bd Leszek Koltunski
// bean bitmap is 366 x 480. Initialize two dynamic Distorts of the left and right brow.
65 427ab7bf Leszek Koltunski
66 b56faf50 Leszek Koltunski
   BeanRenderer(GLSurfaceView v)
67 427ab7bf Leszek Koltunski
      {
68
      mView = v;
69 dc10a48d Leszek Koltunski
      mResources = v.getResources();
70
71 a7a8f7bd Leszek Koltunski
      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 dfae5d27 Leszek Koltunski
      Dynamic3D dynLeft   = new Dynamic3D(2000,0.0f);
77
      Dynamic3D dynRight  = new Dynamic3D(2000,0.0f);
78 f988589e Leszek Koltunski
79 a7a8f7bd Leszek Koltunski
      Static3D vect1 = new Static3D(         0,         0, 0);
80
      Static3D vect2 = new Static3D(-15/366.0f, 30/480.0f, 0);
81 dfae5d27 Leszek Koltunski
82 a4d59c0b Leszek Koltunski
      dynLeft.add(vect1);
83
      dynLeft.add(vect1);
84
      dynLeft.add(vect1);
85
      dynLeft.add(vect1);
86
      dynLeft.add(vect2);
87
      dynLeft.add(vect2);
88 427ab7bf Leszek Koltunski
      
89 a4d59c0b Leszek Koltunski
      dynRight.add(vect1);
90
      dynRight.add(vect2);
91
      dynRight.add(vect2);
92
      dynRight.add(vect1);
93
      dynRight.add(vect1);
94
      dynRight.add(vect1);
95 f6d884d5 Leszek Koltunski
96 fa9b6494 Leszek Koltunski
      mScale= new Static3D(1,1,1);
97
98 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
99 849e0034 Leszek Koltunski
      mEffects.apply( new VertexEffectDistort(dynLeft , pointLeft , regionLeft) );
100
      mEffects.apply( new VertexEffectDistort(dynRight, pointRight, regionRight));
101 dfae5d27 Leszek Koltunski
      mEffects.apply( new MatrixEffectScale(mScale) );
102 392e16fd Leszek Koltunski
103 4c42bc15 Leszek Koltunski
      mTexture = new DistortedTexture();
104
      mScreen  = new DistortedScreen();
105 427ab7bf Leszek Koltunski
      }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
   
109 fa9b6494 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
110 427ab7bf Leszek Koltunski
      {
111 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
112 427ab7bf Leszek Koltunski
      }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116 fa9b6494 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
117
     {
118 c7a31368 Leszek Koltunski
     if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
119
     else               mScale.set( height/mBmpRatio, height, 1 );
120 d381aa80 Leszek Koltunski
121
     mScreen.resize(width, height);
122 fa9b6494 Leszek Koltunski
     }
123 427ab7bf Leszek Koltunski
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
    
126 fa9b6494 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
127
     {
128
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
129
     Bitmap bitmap;
130 427ab7bf Leszek Koltunski
        
131 fa9b6494 Leszek Koltunski
     try
132
       {
133
       bitmap = BitmapFactory.decodeStream(is);
134
       }
135
     finally
136
       {
137
       try
138
         {
139
         is.close();
140
         }
141 3fbd81f1 Leszek Koltunski
       catch(IOException ignored) { }
142 fa9b6494 Leszek Koltunski
       }
143 427ab7bf Leszek Koltunski
      
144 a7a8f7bd Leszek Koltunski
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
145 fa9b6494 Leszek Koltunski
     mTexture.setTexture(bitmap);
146 26c4e181 Leszek Koltunski
147 a7a8f7bd Leszek Koltunski
     // we need a denser Mesh lattice this time for the Distorts to look good.
148 7c55263f Leszek Koltunski
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 f2085b96 Leszek Koltunski
     if( mMesh==null ) mMesh = new MeshSquare(xLoc,yLoc);
166 a4d59c0b Leszek Koltunski
167 fa9b6494 Leszek Koltunski
     mScreen.detachAll();
168
     mScreen.attach(mTexture,mEffects,mMesh);
169
170 885b9cca leszek
     VertexEffectDistort.enable();
171 fa9b6494 Leszek Koltunski
172 dc10a48d Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(this);
173 061449ed Leszek Koltunski
     }
174
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
   public void distortedException(Exception ex)
178
     {
179
     android.util.Log.e("Bean", ex.getMessage() );
180 fa9b6494 Leszek Koltunski
     }
181 dc10a48d Leszek Koltunski
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
    public InputStream localFile(int fileID)
185
      {
186
      return mResources.openRawResource(fileID);
187
      }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191
    public void logMessage(String message)
192
      {
193
      android.util.Log.e("Bean", message );
194
      }
195 427ab7bf Leszek Koltunski
}