Project

General

Profile

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

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

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