Project

General

Profile

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

examples / src / main / java / org / distorted / examples / girl / GirlRenderer.java @ d79a56d3

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 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 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
31 d218d64e leszek
import org.distorted.library.DistortedScreen;
32 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
33 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
34 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
35 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
36 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
37 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Dynamic3D;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42 427ab7bf Leszek Koltunski
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45
import android.opengl.GLSurfaceView;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
class GirlRenderer implements GLSurfaceView.Renderer 
50
{
51
    private GLSurfaceView mView;
52 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
53 d218d64e leszek
    private DistortedScreen mScreen;
54 b0ebdf5e Leszek Koltunski
    private DistortedTexture mTexture;
55 abc3054d Leszek Koltunski
    private Static3D v0,v1,v2,v3;
56
    private Static1D dBegin, dMiddle, dEnd, s0;
57 427ab7bf Leszek Koltunski
    private int bmpHeight, bmpWidth;
58 f6d884d5 Leszek Koltunski
59 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 abc3054d Leszek Koltunski
   GirlRenderer(GLSurfaceView v)
62 427ab7bf Leszek Koltunski
      {
63
      mView = v;
64 f6d884d5 Leszek Koltunski
65
      Static3D pLeft = new Static3D(132, 264, 0);
66
      Static3D pRight= new Static3D(247, 264, 0);
67 427ab7bf Leszek Koltunski
      
68 f988589e Leszek Koltunski
      // Size
69 f6d884d5 Leszek Koltunski
      Static4D sinkRegion = new Static4D(0,0,60,60);
70 427ab7bf Leszek Koltunski
      
71 f6d884d5 Leszek Koltunski
      s0 = new Static1D(1.0f);
72 427ab7bf Leszek Koltunski
      
73 f6d884d5 Leszek Koltunski
      Dynamic1D diSink = new Dynamic1D(0,0.5f);
74 427ab7bf Leszek Koltunski
      diSink.add(s0);
75
      
76 f988589e Leszek Koltunski
      // Upper Movement
77 f6d884d5 Leszek Koltunski
      Static4D Region = new Static4D(0,0,45,45);
78 427ab7bf Leszek Koltunski
      
79 f6d884d5 Leszek Koltunski
      Dynamic3D diL = new Dynamic3D(1000,0.0f);
80
      Dynamic3D diR = new Dynamic3D(1000,0.0f);
81 f988589e Leszek Koltunski
82 f6d884d5 Leszek Koltunski
      v0 = new Static3D(0,0,0);
83
      v1 = new Static3D(0,0,0);
84
      v2 = new Static3D(0,0,0);
85
      v3 = new Static3D(0,0,0);
86 427ab7bf Leszek Koltunski
      
87
      diL.add(v0);
88
      diL.add(v1);
89
      diL.add(v2);
90
      diL.add(v3);
91
      
92
      diR.add(v0);
93
      diR.add(v3);
94
      diR.add(v2);
95
      diR.add(v1);
96
      
97 f988589e Leszek Koltunski
      // Lower Movement
98 f6d884d5 Leszek Koltunski
      Static3D pHips = new Static3D(216,505,0);
99
      Static4D HipsRegion = new Static4D(0,0,120,120);
100
      Dynamic1D diHips = new Dynamic1D(1500,0.0f);
101 427ab7bf Leszek Koltunski
      
102 f6d884d5 Leszek Koltunski
      dBegin = new Static1D(0);
103 f969f51d Leszek Koltunski
      dMiddle= new Static1D(0);
104 f6d884d5 Leszek Koltunski
      dEnd   = new Static1D(0);
105 427ab7bf Leszek Koltunski
      
106 f969f51d Leszek Koltunski
      diHips.add(dBegin);
107
      diHips.add(dMiddle);
108
      diHips.add(dEnd);
109
      diHips.add(dEnd);
110
      diHips.add(dMiddle);
111
      diHips.add(dBegin);
112 f6d884d5 Leszek Koltunski
113 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
114 f6d884d5 Leszek Koltunski
115 392e16fd Leszek Koltunski
      mEffects.sink( diSink, pLeft, sinkRegion );
116
      mEffects.sink( diSink, pRight,sinkRegion );
117 f6d884d5 Leszek Koltunski
118 392e16fd Leszek Koltunski
      mEffects.distort(diL, pLeft , Region);
119
      mEffects.distort(diR, pRight, Region);
120 f6d884d5 Leszek Koltunski
121 392e16fd Leszek Koltunski
      mEffects.swirl(diHips, pHips, HipsRegion );
122
123 d218d64e leszek
      mScreen = new DistortedScreen();
124 427ab7bf Leszek Koltunski
      }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
128 f6d884d5 Leszek Koltunski
   void setHips(int swirl)
129 427ab7bf Leszek Koltunski
     {      
130 f6d884d5 Leszek Koltunski
     dBegin.set(-swirl);
131
     dEnd.set(+swirl);
132 427ab7bf Leszek Koltunski
     }
133
   
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136 f6d884d5 Leszek Koltunski
   void setSize(float size)
137 427ab7bf Leszek Koltunski
     {
138 f6d884d5 Leszek Koltunski
     s0.set(size);
139 427ab7bf Leszek Koltunski
     }
140
   
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143 f6d884d5 Leszek Koltunski
   void setSwing(int swing)
144 427ab7bf Leszek Koltunski
     {
145 f6d884d5 Leszek Koltunski
     v0.set( 0,-swing, 0);
146
     v1.set( swing, 0, 0);
147
     v2.set( 0, swing, 0);
148
     v3.set(-swing, 0, 0);
149 427ab7bf Leszek Koltunski
     }
150
   
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
   
153
   public void onDrawFrame(GL10 glUnused) 
154
      {
155 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
156 427ab7bf Leszek Koltunski
      }
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
    
160
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
161
      { 
162 d79a56d3 Leszek Koltunski
      float qx = (float)width /bmpWidth;
163
      float qy = (float)height/bmpHeight;
164
165 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
166 d79a56d3 Leszek Koltunski
      mEffects.scale(  qx<qy ? (new Static3D(1,qx/qy,1)) : (new Static3D(qy/qx,1,1)) );
167 427ab7bf Leszek Koltunski
      
168 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
169 427ab7bf Leszek Koltunski
      }
170
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
    
173
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
174 e7a4ef16 Leszek Koltunski
      {
175 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
176
      Bitmap bitmap;
177
        
178
      try 
179
        {
180
        bitmap = BitmapFactory.decodeStream(is);
181
        } 
182
      finally 
183
        {
184
        try 
185
          {
186
          is.close();
187
          } 
188
        catch(IOException e) { }
189
        }  
190
      
191
      bmpHeight = bitmap.getHeight();
192
      bmpWidth  = bitmap.getWidth();
193 e8b6aa95 Leszek Koltunski
194 b0ebdf5e Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
195
      mTexture.setTexture(bitmap);
196 fe59d375 Leszek Koltunski
197
      mScreen.detachAll();
198 b0ebdf5e Leszek Koltunski
      mScreen.attach(mTexture,mEffects,new MeshFlat(30,30*bmpHeight/bmpWidth));
199 427ab7bf Leszek Koltunski
200 6637d0f2 Leszek Koltunski
      DistortedEffects.enableEffect(EffectNames.DISTORT);
201
      DistortedEffects.enableEffect(EffectNames.SINK);
202
      DistortedEffects.enableEffect(EffectNames.SWIRL);
203
204 427ab7bf Leszek Koltunski
      try
205
        {
206 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
207 427ab7bf Leszek Koltunski
        }
208
      catch(Exception ex)
209
        {
210
        android.util.Log.e("Renderer", ex.getMessage() );
211
        }
212
      }
213
}