Project

General

Profile

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

examples / src / main / java / org / distorted / examples / girl / GirlRenderer.java @ 10b7e588

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.Distorted;
31
import org.distorted.library.GridFlat;
32
import org.distorted.library.DistortedObject;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic3D;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42
import android.opengl.GLES20;
43
import android.opengl.GLSurfaceView;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class GirlRenderer implements GLSurfaceView.Renderer 
48
{
49
    private GLSurfaceView mView;
50
    private DistortedObject mGirl;
51
    private GridFlat mGrid;
52
    private Static3D pLeft, pRight, pHips;
53
    private Static4D Region, sinkRegion, HipsRegion;
54
    private Dynamic3D diL, diR;
55
    private Dynamic1D diHips, diSink;
56
    private Static3D v0,v1,v2,v3;
57
    private Static1D dBegin, dMiddle, dEnd, s0;
58
    private int bmpHeight, bmpWidth;
59
    private int boobsSwing;
60
    private int hipsSwirl;
61
    private float boobsSink;
62
    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
   GirlRenderer(GLSurfaceView v)
66
      {
67
      mView = v;
68
      
69
      boobsSwing = 0;
70
      hipsSwirl  = 0;
71
      boobsSink  = 1.0f;
72
      
73
      pLeft = new Static3D(132, 264, 0);
74
      pRight= new Static3D(247, 264, 0);
75
      
76
      // Size
77
      sinkRegion = new Static4D(0,0,60,60);
78
      
79
      s0 = new Static1D(boobsSink);
80
      
81
      diSink = new Dynamic1D(0,0.5f);
82
      diSink.add(s0);
83
      
84
      // Upper Movement
85
      Region = new Static4D(0,0,45,45);
86
      
87
      diL = new Dynamic3D(1000,0.0f);
88
      diR = new Dynamic3D(1000,0.0f);
89

    
90
      v0 = new Static3D( 0,-boobsSwing, 0);
91
      v1 = new Static3D( boobsSwing, 0, 0);
92
      v2 = new Static3D( 0, boobsSwing, 0);
93
      v3 = new Static3D(-boobsSwing, 0, 0);
94
      
95
      diL.add(v0);
96
      diL.add(v1);
97
      diL.add(v2);
98
      diL.add(v3);
99
      
100
      diR.add(v0);
101
      diR.add(v3);
102
      diR.add(v2);
103
      diR.add(v1);
104
      
105
      // Lower Movement
106
      pHips = new Static3D(216,505,0);
107
      HipsRegion = new Static4D(0,0,120,120);
108
      diHips = new Dynamic1D(1500,0.0f);
109
      
110
      dBegin = new Static1D(-hipsSwirl);
111
      dMiddle= new Static1D(0);
112
      dEnd   = new Static1D(+hipsSwirl);
113
      
114
      diHips.add(dBegin);
115
      diHips.add(dMiddle);
116
      diHips.add(dEnd);
117
      diHips.add(dEnd);
118
      diHips.add(dMiddle);
119
      diHips.add(dBegin);
120
      }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
   void setHips(int s)
125
     {      
126
     hipsSwirl = s;
127
     dBegin.set(-hipsSwirl);
128
     dEnd.set(+hipsSwirl);
129
     }
130
   
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
   void setSize(float s)
134
     {
135
     boobsSink = s;
136
     s0.set(boobsSink);
137
     }
138
   
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
   void setSwing(int s)
142
     {
143
     boobsSwing = s; 
144
     
145
     v0.set( 0,-boobsSwing, 0);
146
     v1.set( boobsSwing, 0, 0);
147
     v2.set( 0, boobsSwing, 0);
148
     v3.set(-boobsSwing, 0, 0);
149
     }
150
   
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
   
153
   public void onDrawFrame(GL10 glUnused) 
154
      {
155
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
156
      mGirl.draw(System.currentTimeMillis(),mGrid);
157
      }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
    
161
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
162
      { 
163
      mGirl.abortEffects(EffectTypes.MATRIX);
164
      
165
      if( (float)bmpHeight/bmpWidth > (float)height/width )
166
        {
167
        int w = (height*bmpWidth)/bmpHeight;
168
        float factor = (float)height/bmpHeight;
169

    
170
        mGirl.move( new Static3D((width-w)/2,0,0) );
171
        mGirl.scale(factor);
172
        }
173
      else
174
        {
175
        int h = (width*bmpHeight)/bmpWidth;
176
        float factor = (float)width/bmpWidth;
177

    
178
        mGirl.move( new Static3D(0,(height-h)/2,0) );
179
        mGirl.scale(factor);
180
        }
181
      
182
      Distorted.onSurfaceChanged(width, height); 
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
    
187
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
188
      {
189
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
190

    
191
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
192
      Bitmap bitmap;
193
        
194
      try 
195
        {
196
        bitmap = BitmapFactory.decodeStream(is);
197
        } 
198
      finally 
199
        {
200
        try 
201
          {
202
          is.close();
203
          } 
204
        catch(IOException e) { }
205
        }  
206
      
207
      bmpHeight = bitmap.getHeight();
208
      bmpWidth  = bitmap.getWidth();
209

    
210
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
211
      mGirl = new DistortedObject(bmpWidth,bmpHeight,1);
212
      mGirl.setTexture(bitmap);
213

    
214
      mGirl.sink( diSink, pLeft, sinkRegion );
215
      mGirl.sink( diSink, pRight,sinkRegion );
216

    
217
      mGirl.distort(diL, pLeft , Region);
218
      mGirl.distort(diR, pRight, Region);
219
         
220
      mGirl.swirl(diHips, pHips, HipsRegion );
221
      
222
      try
223
        {
224
        Distorted.onSurfaceCreated(mView.getContext());
225
        }
226
      catch(Exception ex)
227
        {
228
        android.util.Log.e("Renderer", ex.getMessage() );
229
        }
230
      }
231
}
(2-2/3)