Project

General

Profile

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

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

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.girl;
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.Distorted;
31
import org.distorted.library.DistortedScreen;
32
import org.distorted.library.MeshFlat;
33
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.DistortedEffects;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES30;
45
import android.opengl.GLSurfaceView;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class GirlRenderer implements GLSurfaceView.Renderer 
50
{
51
    private GLSurfaceView mView;
52
    private DistortedEffects mEffects;
53
    private DistortedScreen mScreen;
54
    private Static3D v0,v1,v2,v3;
55
    private Static1D dBegin, dMiddle, dEnd, s0;
56
    private int bmpHeight, bmpWidth;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
   GirlRenderer(GLSurfaceView v)
61
      {
62
      mView = v;
63

    
64
      Static3D pLeft = new Static3D(132, 264, 0);
65
      Static3D pRight= new Static3D(247, 264, 0);
66
      
67
      // Size
68
      Static4D sinkRegion = new Static4D(0,0,60,60);
69
      
70
      s0 = new Static1D(1.0f);
71
      
72
      Dynamic1D diSink = new Dynamic1D(0,0.5f);
73
      diSink.add(s0);
74
      
75
      // Upper Movement
76
      Static4D Region = new Static4D(0,0,45,45);
77
      
78
      Dynamic3D diL = new Dynamic3D(1000,0.0f);
79
      Dynamic3D diR = new Dynamic3D(1000,0.0f);
80

    
81
      v0 = new Static3D(0,0,0);
82
      v1 = new Static3D(0,0,0);
83
      v2 = new Static3D(0,0,0);
84
      v3 = new Static3D(0,0,0);
85
      
86
      diL.add(v0);
87
      diL.add(v1);
88
      diL.add(v2);
89
      diL.add(v3);
90
      
91
      diR.add(v0);
92
      diR.add(v3);
93
      diR.add(v2);
94
      diR.add(v1);
95
      
96
      // Lower Movement
97
      Static3D pHips = new Static3D(216,505,0);
98
      Static4D HipsRegion = new Static4D(0,0,120,120);
99
      Dynamic1D diHips = new Dynamic1D(1500,0.0f);
100
      
101
      dBegin = new Static1D(0);
102
      dMiddle= new Static1D(0);
103
      dEnd   = new Static1D(0);
104
      
105
      diHips.add(dBegin);
106
      diHips.add(dMiddle);
107
      diHips.add(dEnd);
108
      diHips.add(dEnd);
109
      diHips.add(dMiddle);
110
      diHips.add(dBegin);
111

    
112
      mEffects = new DistortedEffects();
113

    
114
      mEffects.sink( diSink, pLeft, sinkRegion );
115
      mEffects.sink( diSink, pRight,sinkRegion );
116

    
117
      mEffects.distort(diL, pLeft , Region);
118
      mEffects.distort(diR, pRight, Region);
119

    
120
      mEffects.swirl(diHips, pHips, HipsRegion );
121

    
122
      mScreen = new DistortedScreen();
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
   void setHips(int swirl)
128
     {      
129
     dBegin.set(-swirl);
130
     dEnd.set(+swirl);
131
     }
132
   
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
   void setSize(float size)
136
     {
137
     s0.set(size);
138
     }
139
   
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
   void setSwing(int swing)
143
     {
144
     v0.set( 0,-swing, 0);
145
     v1.set( swing, 0, 0);
146
     v2.set( 0, swing, 0);
147
     v3.set(-swing, 0, 0);
148
     }
149
   
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
   
152
   public void onDrawFrame(GL10 glUnused) 
153
      {
154
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
155
      mScreen.render( System.currentTimeMillis() );
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
    
160
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
161
      { 
162
      mEffects.abortEffects(EffectTypes.MATRIX);
163
      
164
      if( (float)bmpHeight/bmpWidth > (float)height/width )
165
        {
166
        int w = (height*bmpWidth)/bmpHeight;
167
        float factor = (float)height/bmpHeight;
168

    
169
        mEffects.move( new Static3D((width-w)/2,0,0) );
170
        mEffects.scale(factor);
171
        }
172
      else
173
        {
174
        int h = (width*bmpHeight)/bmpWidth;
175
        float factor = (float)width/bmpWidth;
176

    
177
        mEffects.move( new Static3D(0,(height-h)/2,0) );
178
        mEffects.scale(factor);
179
        }
180
      
181
      mScreen.resize(width, height);
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
    
186
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
187
      {
188
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
189

    
190
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
191
      Bitmap bitmap;
192
        
193
      try 
194
        {
195
        bitmap = BitmapFactory.decodeStream(is);
196
        } 
197
      finally 
198
        {
199
        try 
200
          {
201
          is.close();
202
          } 
203
        catch(IOException e) { }
204
        }  
205
      
206
      bmpHeight = bitmap.getHeight();
207
      bmpWidth  = bitmap.getWidth();
208

    
209
      MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
210
      DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
211
      texture.setTexture(bitmap);
212

    
213
      mScreen.detachAll();
214
      mScreen.attach(texture,mEffects,mesh);
215

    
216
      try
217
        {
218
        Distorted.onCreate(mView.getContext());
219
        }
220
      catch(Exception ex)
221
        {
222
        android.util.Log.e("Renderer", ex.getMessage() );
223
        }
224
      }
225
}
(2-2/3)