Project

General

Profile

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

examples / src / main / java / org / distorted / examples / girl / GirlRenderer.java @ 7451c98a

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 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
31 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
32 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.DistortedEffectQueues;
34 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
35 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40 427ab7bf Leszek Koltunski
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLES20;
44
import android.opengl.GLSurfaceView;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
class GirlRenderer implements GLSurfaceView.Renderer 
49
{
50
    private GLSurfaceView mView;
51 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
52
    private DistortedEffectQueues mQueues;
53 10b7e588 Leszek Koltunski
    private GridFlat mGrid;
54 abc3054d Leszek Koltunski
    private Static3D v0,v1,v2,v3;
55
    private Static1D dBegin, dMiddle, dEnd, s0;
56 427ab7bf Leszek Koltunski
    private int bmpHeight, bmpWidth;
57 f6d884d5 Leszek Koltunski
58 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 abc3054d Leszek Koltunski
   GirlRenderer(GLSurfaceView v)
61 427ab7bf Leszek Koltunski
      {
62
      mView = v;
63 f6d884d5 Leszek Koltunski
64
      Static3D pLeft = new Static3D(132, 264, 0);
65
      Static3D pRight= new Static3D(247, 264, 0);
66 427ab7bf Leszek Koltunski
      
67 f988589e Leszek Koltunski
      // Size
68 f6d884d5 Leszek Koltunski
      Static4D sinkRegion = new Static4D(0,0,60,60);
69 427ab7bf Leszek Koltunski
      
70 f6d884d5 Leszek Koltunski
      s0 = new Static1D(1.0f);
71 427ab7bf Leszek Koltunski
      
72 f6d884d5 Leszek Koltunski
      Dynamic1D diSink = new Dynamic1D(0,0.5f);
73 427ab7bf Leszek Koltunski
      diSink.add(s0);
74
      
75 f988589e Leszek Koltunski
      // Upper Movement
76 f6d884d5 Leszek Koltunski
      Static4D Region = new Static4D(0,0,45,45);
77 427ab7bf Leszek Koltunski
      
78 f6d884d5 Leszek Koltunski
      Dynamic3D diL = new Dynamic3D(1000,0.0f);
79
      Dynamic3D diR = new Dynamic3D(1000,0.0f);
80 f988589e Leszek Koltunski
81 f6d884d5 Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      
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 f988589e Leszek Koltunski
      // Lower Movement
97 f6d884d5 Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      
101 f6d884d5 Leszek Koltunski
      dBegin = new Static1D(0);
102 f969f51d Leszek Koltunski
      dMiddle= new Static1D(0);
103 f6d884d5 Leszek Koltunski
      dEnd   = new Static1D(0);
104 427ab7bf Leszek Koltunski
      
105 f969f51d Leszek Koltunski
      diHips.add(dBegin);
106
      diHips.add(dMiddle);
107
      diHips.add(dEnd);
108
      diHips.add(dEnd);
109
      diHips.add(dMiddle);
110
      diHips.add(dBegin);
111 f6d884d5 Leszek Koltunski
112
      mQueues = new DistortedEffectQueues();
113
114
      mQueues.sink( diSink, pLeft, sinkRegion );
115
      mQueues.sink( diSink, pRight,sinkRegion );
116
117
      mQueues.distort(diL, pLeft , Region);
118
      mQueues.distort(diR, pRight, Region);
119
120
      mQueues.swirl(diHips, pHips, HipsRegion );
121 427ab7bf Leszek Koltunski
      }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125 f6d884d5 Leszek Koltunski
   void setHips(int swirl)
126 427ab7bf Leszek Koltunski
     {      
127 f6d884d5 Leszek Koltunski
     dBegin.set(-swirl);
128
     dEnd.set(+swirl);
129 427ab7bf Leszek Koltunski
     }
130
   
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
133 f6d884d5 Leszek Koltunski
   void setSize(float size)
134 427ab7bf Leszek Koltunski
     {
135 f6d884d5 Leszek Koltunski
     s0.set(size);
136 427ab7bf Leszek Koltunski
     }
137
   
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140 f6d884d5 Leszek Koltunski
   void setSwing(int swing)
141 427ab7bf Leszek Koltunski
     {
142 f6d884d5 Leszek Koltunski
     v0.set( 0,-swing, 0);
143
     v1.set( swing, 0, 0);
144
     v2.set( 0, swing, 0);
145
     v3.set(-swing, 0, 0);
146 427ab7bf Leszek Koltunski
     }
147
   
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
   
150
   public void onDrawFrame(GL10 glUnused) 
151
      {
152
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
153 f6d884d5 Leszek Koltunski
      mQueues.draw(System.currentTimeMillis(),mTexture,mGrid);
154 427ab7bf Leszek Koltunski
      }
155
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
    
158
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
159
      { 
160 f6d884d5 Leszek Koltunski
      mQueues.abortEffects(EffectTypes.MATRIX);
161 427ab7bf Leszek Koltunski
      
162 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/bmpWidth > (float)height/width )
163 427ab7bf Leszek Koltunski
        {
164
        int w = (height*bmpWidth)/bmpHeight;
165 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
166
167 f6d884d5 Leszek Koltunski
        mQueues.move( new Static3D((width-w)/2,0,0) );
168
        mQueues.scale(factor);
169 427ab7bf Leszek Koltunski
        }
170
      else
171
        {
172
        int h = (width*bmpHeight)/bmpWidth;
173 7589635e Leszek Koltunski
        float factor = (float)width/bmpWidth;
174
175 f6d884d5 Leszek Koltunski
        mQueues.move( new Static3D(0,(height-h)/2,0) );
176
        mQueues.scale(factor);
177 427ab7bf Leszek Koltunski
        }
178
      
179
      Distorted.onSurfaceChanged(width, height); 
180
      }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
    
184
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
185 e7a4ef16 Leszek Koltunski
      {
186
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
187
188 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
189
      Bitmap bitmap;
190
        
191
      try 
192
        {
193
        bitmap = BitmapFactory.decodeStream(is);
194
        } 
195
      finally 
196
        {
197
        try 
198
          {
199
          is.close();
200
          } 
201
        catch(IOException e) { }
202
        }  
203
      
204
      bmpHeight = bitmap.getHeight();
205
      bmpWidth  = bitmap.getWidth();
206 e8b6aa95 Leszek Koltunski
207 10b7e588 Leszek Koltunski
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
208 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(bmpWidth,bmpHeight);
209 f6d884d5 Leszek Koltunski
      mTexture.setTexture(bitmap);
210 427ab7bf Leszek Koltunski
211
      try
212
        {
213 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
214 427ab7bf Leszek Koltunski
        }
215
      catch(Exception ex)
216
        {
217
        android.util.Log.e("Renderer", ex.getMessage() );
218
        }
219
      }
220
}