Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 5055b5d4

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 a2cb31e9 Leszek Koltunski
20
package org.distorted.examples.save;
21
22 7cfeeb63 Leszek Koltunski
import java.io.File;
23 a2cb31e9 Leszek Koltunski
import java.io.IOException;
24
import java.io.InputStream;
25 e4237c21 Leszek Koltunski
import java.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27 a2cb31e9 Leszek Koltunski
28
import javax.microedition.khronos.egl.EGLConfig;
29
import javax.microedition.khronos.opengles.GL10;
30
31
import org.distorted.examples.R;
32
33
import org.distorted.library.Distorted;
34
import org.distorted.library.DistortedBitmap;
35 3c8433ae Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
36 a2cb31e9 Leszek Koltunski
import org.distorted.library.EffectTypes;
37 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
38 5055b5d4 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
39 7589635e Leszek Koltunski
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42 a2cb31e9 Leszek Koltunski
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45
import android.opengl.GLES20;
46
import android.opengl.GLSurfaceView;
47 7cfeeb63 Leszek Koltunski
import android.os.Environment;
48 a2cb31e9 Leszek Koltunski
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 e4237c21 Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer
52 b9615af9 Leszek Koltunski
  {
53
  private GLSurfaceView mView;
54 13a5c5cd Leszek Koltunski
  private DistortedBitmap mGirl;
55 3c8433ae Leszek Koltunski
  private DistortedFramebuffer mOffscreen;
56 334c13fa Leszek Koltunski
  private Static3D pLeft, pRight;
57 7589635e Leszek Koltunski
  private Static4D sinkRegion;
58 13a5c5cd Leszek Koltunski
  private Dynamic1D diSink;
59
  private Static1D s0;
60 5055b5d4 Leszek Koltunski
  private Dynamic3D mScaleDyn;
61
  private Static3D mScaleFactor;
62 b9615af9 Leszek Koltunski
63 5055b5d4 Leszek Koltunski
  private float mScale;
64 b9615af9 Leszek Koltunski
  private int bmpHeight, bmpWidth;
65 13a5c5cd Leszek Koltunski
  private int scrHeight, scrWidth;
66
  private float boobsSink;
67
  private boolean isSaving = false;
68
  private String mPath;
69 b9615af9 Leszek Koltunski
70 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72 13a5c5cd Leszek Koltunski
  SaveRenderer(GLSurfaceView v)
73 b9615af9 Leszek Koltunski
    {
74
    mView = v;
75 a2cb31e9 Leszek Koltunski
      
76 b9615af9 Leszek Koltunski
    boobsSink  = 1.0f;
77 a2cb31e9 Leszek Koltunski
      
78 334c13fa Leszek Koltunski
    pLeft = new Static3D(132, 264, 0);
79
    pRight= new Static3D(247, 264, 0);
80 a2cb31e9 Leszek Koltunski
      
81 7589635e Leszek Koltunski
    sinkRegion = new Static4D(0,0,60,60);
82 a2cb31e9 Leszek Koltunski
      
83 7589635e Leszek Koltunski
    s0 = new Static1D(boobsSink);
84 a2cb31e9 Leszek Koltunski
      
85 5055b5d4 Leszek Koltunski
    diSink = new Dynamic1D();
86 b9615af9 Leszek Koltunski
    diSink.add(s0);
87 3c8433ae Leszek Koltunski
88 5055b5d4 Leszek Koltunski
    mScale = 1.0f;
89
    mScaleDyn = new Dynamic3D();
90
    mScaleFactor = new Static3D(mScale,mScale,1.0f);
91
    mScaleDyn.add(mScaleFactor);
92 b9615af9 Leszek Koltunski
    }
93 a2cb31e9 Leszek Koltunski
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96 13a5c5cd Leszek Koltunski
  void setSize(float s)
97 e4237c21 Leszek Koltunski
    {
98
    boobsSink = s;
99
    s0.set(boobsSink);
100 b9615af9 Leszek Koltunski
    }
101 a2cb31e9 Leszek Koltunski
102 5055b5d4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
  void setScale(float s)
105
    {
106
    mScale = s;
107
    mScaleFactor.set(s,s,1.0f);
108
109
    // when we move our scroll bar, this does NOT keep allocating and deallocating
110
    // the whole WxH texture - the allocation happens only once, on next render, i.e. -
111
    // when one presses the 'SAVE' button.
112
113
    if( mOffscreen!=null )
114
      mOffscreen.resize( (int)(mScale*bmpWidth),(int)(mScale*bmpHeight));
115
    }
116
117 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118 b9615af9 Leszek Koltunski
119 13a5c5cd Leszek Koltunski
  void Save()
120 b9615af9 Leszek Koltunski
    {
121 92040610 Leszek Koltunski
    if( !isSaving )
122 b9615af9 Leszek Koltunski
      {
123 e4237c21 Leszek Koltunski
      isSaving = true;
124 b9615af9 Leszek Koltunski
125 e4237c21 Leszek Koltunski
      File file;
126
      int lowestNotFound = 1;
127
128
      while(true)
129 a2cb31e9 Leszek Koltunski
        {
130 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
131
        file = new File(mPath);
132
133
        if( !file.exists() ) break;
134
135
        lowestNotFound++;
136 a2cb31e9 Leszek Koltunski
        }
137 b9615af9 Leszek Koltunski
      }
138
    }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
   
142
  public void onDrawFrame(GL10 glUnused)
143
    {
144
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
145 e4237c21 Leszek Koltunski
146 3c8433ae Leszek Koltunski
    if( isSaving )  // render to an offscreen buffer and read pixels
147 e4237c21 Leszek Koltunski
      {
148 5055b5d4 Leszek Koltunski
      mGirl.abortEffects(EffectTypes.MATRIX);
149
      mGirl.scale(mScaleFactor);
150 3c8433ae Leszek Koltunski
      mGirl.draw(System.currentTimeMillis(), mOffscreen);
151 5055b5d4 Leszek Koltunski
      applyMatrixEffects(scrWidth,scrHeight);
152 3c8433ae Leszek Koltunski
153 5055b5d4 Leszek Koltunski
      int fW =(int)(mScale*bmpWidth);
154
      int fH =(int)(mScale*bmpHeight);
155
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
156 e4237c21 Leszek Koltunski
      buf.order(ByteOrder.LITTLE_ENDIAN);
157 3c8433ae Leszek Koltunski
      mOffscreen.setAsInput();
158 e4237c21 Leszek Koltunski
159 5055b5d4 Leszek Koltunski
      GLES20.glReadPixels( 0, 0, fW, fH, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
160
      SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
161 e4237c21 Leszek Koltunski
162
      isSaving = false;
163
      }
164 5055b5d4 Leszek Koltunski
165
    mGirl.draw(System.currentTimeMillis());
166 b9615af9 Leszek Koltunski
    }
167
168 5055b5d4 Leszek Koltunski
169 b9615af9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171 5055b5d4 Leszek Koltunski
  private void applyMatrixEffects(int width, int height)
172
    {
173 b9615af9 Leszek Koltunski
    mGirl.abortEffects(EffectTypes.MATRIX);
174 5055b5d4 Leszek Koltunski
175
    if( (float)bmpHeight/bmpWidth > (float)height/width )
176 b9615af9 Leszek Koltunski
      {
177
      int w = (height*bmpWidth)/bmpHeight;
178 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
179
180
      mGirl.move( new Static3D((width-w)/2,0,0) );
181 f988589e Leszek Koltunski
      mGirl.scale(factor);
182 a2cb31e9 Leszek Koltunski
      }
183 b9615af9 Leszek Koltunski
    else
184
      {
185
      int h = (width*bmpHeight)/bmpWidth;
186 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
187
188
      mGirl.move( new Static3D(0,(height-h)/2,0) );
189 f988589e Leszek Koltunski
      mGirl.scale(factor);
190 b9615af9 Leszek Koltunski
      }
191 5055b5d4 Leszek Koltunski
192
    mGirl.scale(mScaleDyn);
193
    }
194
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
197
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
198
    {
199
    scrWidth = width;
200
    scrHeight= height;
201
    applyMatrixEffects(width, height);
202 b9615af9 Leszek Koltunski
    Distorted.onSurfaceChanged(width, height);
203
    }
204 a2cb31e9 Leszek Koltunski
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
    
207 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
208
    {
209 e7a4ef16 Leszek Koltunski
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
210
211 b9615af9 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
212
    Bitmap bitmap;
213 a2cb31e9 Leszek Koltunski
        
214 b9615af9 Leszek Koltunski
    try
215
      {
216
      bitmap = BitmapFactory.decodeStream(is);
217
      }
218
    finally
219
      {
220
      try
221 a2cb31e9 Leszek Koltunski
        {
222 b9615af9 Leszek Koltunski
        is.close();
223
        }
224
      catch(IOException e) { }
225
      }
226 a2cb31e9 Leszek Koltunski
      
227 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
228
    bmpWidth  = bitmap.getWidth();
229 a2cb31e9 Leszek Koltunski
      
230 b9615af9 Leszek Koltunski
    mGirl = new DistortedBitmap(bitmap, 30);
231 a2cb31e9 Leszek Koltunski
232 7bf107f7 Leszek Koltunski
    mGirl.sink( diSink, pLeft , sinkRegion);
233
    mGirl.sink( diSink, pRight, sinkRegion);
234 a2cb31e9 Leszek Koltunski
235 5055b5d4 Leszek Koltunski
    mOffscreen = new DistortedFramebuffer(bmpWidth,bmpHeight);
236 3c8433ae Leszek Koltunski
237 b9615af9 Leszek Koltunski
    try
238
      {
239
      Distorted.onSurfaceCreated(mView.getContext());
240
      }
241
    catch(Exception ex)
242
      {
243
      android.util.Log.e("Renderer", ex.getMessage() );
244 a2cb31e9 Leszek Koltunski
      }
245 b9615af9 Leszek Koltunski
    }
246
  }