Project

General

Profile

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

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

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