Project

General

Profile

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

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

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