Project

General

Profile

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

examples / src / main / java / org / distorted / examples / girl / GirlRenderer.java @ 061449ed

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