Project

General

Profile

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

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

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.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32
import org.distorted.library.effect.VertexEffectSink;
33
import org.distorted.library.effect.VertexEffectSwirl;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.mesh.MeshRectangles;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Dynamic3D;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

    
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class GirlRenderer implements GLSurfaceView.Renderer 
52
{
53
    private GLSurfaceView mView;
54
    private DistortedEffects mEffects;
55
    private DistortedScreen mScreen;
56
    private DistortedTexture mTexture;
57
    private MeshRectangles mMesh;
58
    private Static3D v0,v1,v2,v3;
59
    private Static1D dBegin, dMiddle, dEnd, s0;
60
    private Static3D mScale;
61
    private float mBmpRatio;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
// girl bitmap: 400x600
65

    
66
   GirlRenderer(GLSurfaceView v)
67
      {
68
      mView = v;
69

    
70
      Static3D pLeft = new Static3D(-68/400.0f, 36/600.0f, 0);
71
      Static3D pRight= new Static3D( 47/400.0f, 36/600.0f, 0);
72

    
73
      // Size
74
      Static4D sinkRegion = new Static4D(0,0,0,60/400.0f);
75
      
76
      s0 = new Static1D(1.0f);
77
      
78
      Dynamic1D diSink = new Dynamic1D(0,0.5f);
79
      diSink.add(s0);
80
      
81
      // Upper Movement
82
      Static4D Region = new Static4D(0,0,0,45/400.0f);
83
      
84
      Dynamic3D diL = new Dynamic3D(1000,0.0f);
85
      Dynamic3D diR = new Dynamic3D(1000,0.0f);
86

    
87
      v0 = new Static3D(0,0,0);
88
      v1 = new Static3D(0,0,0);
89
      v2 = new Static3D(0,0,0);
90
      v3 = new Static3D(0,0,0);
91
      
92
      diL.add(v0);
93
      diL.add(v1);
94
      diL.add(v2);
95
      diL.add(v3);
96
      
97
      diR.add(v0);
98
      diR.add(v3);
99
      diR.add(v2);
100
      diR.add(v1);
101
      
102
      // Lower Movement
103
      Static3D pHips      = new Static3D( 16/400.0f,-205/600.0f,0);
104
      Static4D HipsRegion = new Static4D(0,0,0,120/400.0f);
105
      Dynamic1D diHips    = new Dynamic1D(1500,0.0f);
106
      
107
      dBegin = new Static1D(0);
108
      dMiddle= new Static1D(0);
109
      dEnd   = new Static1D(0);
110
      
111
      diHips.add(dBegin);
112
      diHips.add(dMiddle);
113
      diHips.add(dEnd);
114
      diHips.add(dEnd);
115
      diHips.add(dMiddle);
116
      diHips.add(dBegin);
117

    
118
      mEffects = new DistortedEffects();
119

    
120
      mEffects.apply( new VertexEffectSink   ( diSink, pLeft , sinkRegion) );
121
      mEffects.apply( new VertexEffectSink   ( diSink, pRight, sinkRegion) );
122
      mEffects.apply( new VertexEffectDistort( diL   , pLeft , Region    ) );
123
      mEffects.apply( new VertexEffectDistort( diR   , pRight, Region    ) );
124
      mEffects.apply( new VertexEffectSwirl  ( diHips, pHips , HipsRegion) );
125

    
126
      mScale= new Static3D(1,1,1);
127
      mEffects.apply(new MatrixEffectScale(mScale));
128

    
129
      mTexture = new DistortedTexture();
130
      mScreen = new DistortedScreen();
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
   void setHips(int swirl)
136
     {      
137
     dBegin.set(-swirl);
138
     dEnd.set(+swirl);
139
     }
140
   
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
   void setSize(float size)
144
     {
145
     s0.set(size);
146
     }
147
   
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
   void setSwing(float swing)
151
     {
152
     v0.set( 0, swing, 0);
153
     v1.set( swing, 0, 0);
154
     v2.set( 0,-swing, 0);
155
     v3.set(-swing, 0, 0);
156
     }
157
   
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
   
160
   public void onDrawFrame(GL10 glUnused) 
161
      {
162
      mScreen.render( System.currentTimeMillis() );
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
    
167
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
168
     {
169
     if( width<height ) mScale.set( width,   width*mBmpRatio, width  );
170
     else               mScale.set( height/mBmpRatio, height, height );
171

    
172
     mScreen.resize(width, height);
173
     }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
    
177
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
178
     {
179
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
180
     Bitmap bitmap;
181
        
182
     try
183
       {
184
       bitmap = BitmapFactory.decodeStream(is);
185
       }
186
     finally
187
       {
188
       try
189
         {
190
         is.close();
191
         }
192
       catch(IOException e) { }
193
       }
194
      
195
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
196
     mTexture.setTexture(bitmap);
197

    
198
     if( mMesh==null ) mMesh = new MeshRectangles(30, (int)(30*mBmpRatio));
199

    
200
     mScreen.detachAll();
201
     mScreen.attach(mTexture,mEffects,mMesh);
202

    
203
     VertexEffectDistort.enable();
204
     VertexEffectSink.enable();
205
     VertexEffectSwirl.enable();
206

    
207
     try
208
       {
209
       DistortedLibrary.onCreate(mView.getContext());
210
       }
211
     catch(Exception ex)
212
       {
213
       android.util.Log.e("Girl", ex.getMessage() );
214
       }
215
     }
216
}
(2-2/3)