Project

General

Profile

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

examples / src / main / java / org / distorted / examples / girl / GirlRenderer.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.girl;
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 2890c5df Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32 3bb0d218 Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
33 2890c5df Leszek Koltunski
import org.distorted.library.effect.VertexEffectSink;
34
import org.distorted.library.effect.VertexEffectSwirl;
35 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
37 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
38 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.main.DistortedEffects;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
41
import org.distorted.library.type.Dynamic3D;
42
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45 427ab7bf Leszek Koltunski
46 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
47
import android.content.Context;
48
import android.content.pm.ConfigurationInfo;
49
import android.content.res.Resources;
50 427ab7bf Leszek Koltunski
import android.graphics.Bitmap;
51
import android.graphics.BitmapFactory;
52
import android.opengl.GLSurfaceView;
53
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 dc10a48d Leszek Koltunski
class GirlRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
57 427ab7bf Leszek Koltunski
{
58 dc10a48d Leszek Koltunski
    private final GLSurfaceView mView;
59
    private final Resources mResources;
60
    private final DistortedEffects mEffects;
61
    private final DistortedScreen mScreen;
62
    private final DistortedTexture mTexture;
63
    private final Static3D v0,v1,v2,v3;
64
    private final Static1D dBegin, dMiddle, dEnd, s0;
65
    private final Static3D mScaleMatrix, mScaleVertex;
66
67 f2085b96 Leszek Koltunski
    private MeshSquare mMesh;
68 25eeb4d4 Leszek Koltunski
    private float mBmpRatio;
69 f6d884d5 Leszek Koltunski
70 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
71 df80818c Leszek Koltunski
// girl bitmap: 400x600 but we setStretch() the Mesh so
72 427ab7bf Leszek Koltunski
73 abc3054d Leszek Koltunski
   GirlRenderer(GLSurfaceView v)
74 427ab7bf Leszek Koltunski
      {
75
      mView = v;
76 dc10a48d Leszek Koltunski
      mResources = v.getResources();
77 f6d884d5 Leszek Koltunski
78 df80818c Leszek Koltunski
      Static3D pLeft = new Static3D(-68/400.0f, 36/400.0f, 0);
79
      Static3D pRight= new Static3D( 47/400.0f, 36/400.0f, 0);
80 849e0034 Leszek Koltunski
81 f988589e Leszek Koltunski
      // Size
82 25eeb4d4 Leszek Koltunski
      Static4D sinkRegion = new Static4D(0,0,0,60/400.0f);
83 427ab7bf Leszek Koltunski
      
84 f6d884d5 Leszek Koltunski
      s0 = new Static1D(1.0f);
85 427ab7bf Leszek Koltunski
      
86 f6d884d5 Leszek Koltunski
      Dynamic1D diSink = new Dynamic1D(0,0.5f);
87 427ab7bf Leszek Koltunski
      diSink.add(s0);
88
      
89 f988589e Leszek Koltunski
      // Upper Movement
90 25eeb4d4 Leszek Koltunski
      Static4D Region = new Static4D(0,0,0,45/400.0f);
91 427ab7bf Leszek Koltunski
      
92 f6d884d5 Leszek Koltunski
      Dynamic3D diL = new Dynamic3D(1000,0.0f);
93
      Dynamic3D diR = new Dynamic3D(1000,0.0f);
94 f988589e Leszek Koltunski
95 f6d884d5 Leszek Koltunski
      v0 = new Static3D(0,0,0);
96
      v1 = new Static3D(0,0,0);
97
      v2 = new Static3D(0,0,0);
98
      v3 = new Static3D(0,0,0);
99 427ab7bf Leszek Koltunski
      
100
      diL.add(v0);
101
      diL.add(v1);
102
      diL.add(v2);
103
      diL.add(v3);
104
      
105
      diR.add(v0);
106
      diR.add(v3);
107
      diR.add(v2);
108
      diR.add(v1);
109
      
110 f988589e Leszek Koltunski
      // Lower Movement
111 df80818c Leszek Koltunski
      Static3D pHips      = new Static3D( 16/400.0f,-205/400.0f,0);
112 25eeb4d4 Leszek Koltunski
      Static4D HipsRegion = new Static4D(0,0,0,120/400.0f);
113 a4d59c0b Leszek Koltunski
      Dynamic1D diHips    = new Dynamic1D(1500,0.0f);
114 427ab7bf Leszek Koltunski
      
115 f6d884d5 Leszek Koltunski
      dBegin = new Static1D(0);
116 f969f51d Leszek Koltunski
      dMiddle= new Static1D(0);
117 f6d884d5 Leszek Koltunski
      dEnd   = new Static1D(0);
118 427ab7bf Leszek Koltunski
      
119 f969f51d Leszek Koltunski
      diHips.add(dBegin);
120
      diHips.add(dMiddle);
121
      diHips.add(dEnd);
122
      diHips.add(dEnd);
123
      diHips.add(dMiddle);
124
      diHips.add(dBegin);
125 f6d884d5 Leszek Koltunski
126 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
127 f6d884d5 Leszek Koltunski
128 3bb0d218 Leszek Koltunski
      mScaleMatrix= new Static3D(1,1,1);
129
      mScaleVertex= new Static3D(1,1,1);
130
      mEffects.apply(new MatrixEffectScale(mScaleMatrix));
131
      mEffects.apply(new VertexEffectScale(mScaleVertex));
132
133 a4d59c0b Leszek Koltunski
      mEffects.apply( new VertexEffectSink   ( diSink, pLeft , sinkRegion) );
134
      mEffects.apply( new VertexEffectSink   ( diSink, pRight, sinkRegion) );
135
      mEffects.apply( new VertexEffectDistort( diL   , pLeft , Region    ) );
136
      mEffects.apply( new VertexEffectDistort( diR   , pRight, Region    ) );
137
      mEffects.apply( new VertexEffectSwirl  ( diHips, pHips , HipsRegion) );
138 f6d884d5 Leszek Koltunski
139 25eeb4d4 Leszek Koltunski
      mTexture = new DistortedTexture();
140 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
141 427ab7bf Leszek Koltunski
      }
142
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
145 f6d884d5 Leszek Koltunski
   void setHips(int swirl)
146 427ab7bf Leszek Koltunski
     {      
147 f6d884d5 Leszek Koltunski
     dBegin.set(-swirl);
148
     dEnd.set(+swirl);
149 427ab7bf Leszek Koltunski
     }
150
   
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153 f6d884d5 Leszek Koltunski
   void setSize(float size)
154 427ab7bf Leszek Koltunski
     {
155 f6d884d5 Leszek Koltunski
     s0.set(size);
156 427ab7bf Leszek Koltunski
     }
157
   
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160 25eeb4d4 Leszek Koltunski
   void setSwing(float swing)
161 427ab7bf Leszek Koltunski
     {
162 a4d59c0b Leszek Koltunski
     v0.set( 0, swing, 0);
163 f6d884d5 Leszek Koltunski
     v1.set( swing, 0, 0);
164 a4d59c0b Leszek Koltunski
     v2.set( 0,-swing, 0);
165 f6d884d5 Leszek Koltunski
     v3.set(-swing, 0, 0);
166 427ab7bf Leszek Koltunski
     }
167
   
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
   
170
   public void onDrawFrame(GL10 glUnused) 
171
      {
172 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
173 427ab7bf Leszek Koltunski
      }
174
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
    
177 e4330c89 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
178
     {
179 df80818c Leszek Koltunski
     float factor = width<height ? width : height/mBmpRatio;
180 3bb0d218 Leszek Koltunski
     mScaleMatrix.set( factor,factor,factor );
181 e4330c89 Leszek Koltunski
     mScreen.resize(width, height);
182
     }
183 427ab7bf Leszek Koltunski
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
    
186 e4330c89 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
187
     {
188
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
189
     Bitmap bitmap;
190 427ab7bf Leszek Koltunski
        
191 e4330c89 Leszek Koltunski
     try
192
       {
193
       bitmap = BitmapFactory.decodeStream(is);
194
       }
195
     finally
196
       {
197
       try
198
         {
199
         is.close();
200
         }
201 dc10a48d Leszek Koltunski
       catch(IOException ignored) { }
202 e4330c89 Leszek Koltunski
       }
203 427ab7bf Leszek Koltunski
      
204 25eeb4d4 Leszek Koltunski
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
205 e4330c89 Leszek Koltunski
     mTexture.setTexture(bitmap);
206 67c3a83b leszek
207 3bb0d218 Leszek Koltunski
     mScaleVertex.set(1.0f,mBmpRatio,1.0f);
208
209 f2085b96 Leszek Koltunski
     if( mMesh==null ) mMesh = new MeshSquare(30, (int)(30*mBmpRatio));
210 5f2a53b6 leszek
211 e4330c89 Leszek Koltunski
     mScreen.detachAll();
212
     mScreen.attach(mTexture,mEffects,mMesh);
213 67c3a83b leszek
214 3bb0d218 Leszek Koltunski
     VertexEffectScale.enable();
215 885b9cca leszek
     VertexEffectDistort.enable();
216
     VertexEffectSink.enable();
217
     VertexEffectSwirl.enable();
218 67c3a83b leszek
219 dc10a48d Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(this);
220 061449ed Leszek Koltunski
     }
221
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
224
   public void distortedException(Exception ex)
225
     {
226
     android.util.Log.e("Girl", ex.getMessage() );
227 e4330c89 Leszek Koltunski
     }
228 dc10a48d Leszek Koltunski
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
   public int openGlVersion()
232
      {
233
      Context context = mView.getContext();
234
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
235
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
236
      int glESversion = configurationInfo.reqGlEsVersion;
237
      int major = glESversion >> 16;
238
      int minor = glESversion & 0xff;
239
240
      return 100*major + 10*minor;
241
      }
242
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245
   public InputStream localFile(int fileID)
246
      {
247
      return mResources.openRawResource(fileID);
248
      }
249
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
252
   public void logMessage(String message)
253
      {
254
      android.util.Log.e("Girl", message );
255
      }
256 427ab7bf Leszek Koltunski
}