Project

General

Profile

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

examples / src / main / java / org / distorted / examples / girl / GirlRenderer.java @ 849e0034

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
   GirlRenderer(GLSurfaceView v)
65
      {
66
      mView = v;
67

    
68
      Static3D pLeft = new Static3D(-68, 36, 0);
69
      Static3D pRight= new Static3D( 47, 36, 0);
70

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

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

    
116
      mEffects = new DistortedEffects();
117

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

    
124
      mScale= new Static3D(1,1,1);
125
      mEffects.apply(new MatrixEffectScale(mScale));
126

    
127
      mScreen = new DistortedScreen();
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
   void setHips(int swirl)
133
     {      
134
     dBegin.set(-swirl);
135
     dEnd.set(+swirl);
136
     }
137
   
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
   void setSize(float size)
141
     {
142
     s0.set(size);
143
     }
144
   
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

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

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
    
164
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
165
     {
166
     float horiRatio = (float)width / mTexture.getWidth();
167
     float vertRatio = (float)height/ mTexture.getHeight();
168
     float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
169

    
170
     mScale.set( factor,factor,factor );
171
     mScreen.resize(width, height);
172
     }
173

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

    
197
     if( mTexture==null ) mTexture = new DistortedTexture();
198
     mTexture.setTexture(bitmap);
199

    
200
     if( mMesh==null )
201
       {
202
       mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
203
       mMesh.setStretch(bmpWidth,bmpHeight,0);
204
       }
205

    
206
     mScreen.detachAll();
207
     mScreen.attach(mTexture,mEffects,mMesh);
208

    
209
     VertexEffectDistort.enable();
210
     VertexEffectSink.enable();
211
     VertexEffectSwirl.enable();
212

    
213
     try
214
       {
215
       DistortedLibrary.onCreate(mView.getContext());
216
       }
217
     catch(Exception ex)
218
       {
219
       android.util.Log.e("Girl", ex.getMessage() );
220
       }
221
     }
222
}
(2-2/3)