Project

General

Profile

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

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

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