Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 6fd3af4d

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