Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 7451c98a

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 3c8433ae Leszek Koltunski
      mOffscreen.setAsInput();
165 e4237c21 Leszek Koltunski
166 5055b5d4 Leszek Koltunski
      GLES20.glReadPixels( 0, 0, fW, fH, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
167
      SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
168 e4237c21 Leszek Koltunski
169
      isSaving = false;
170
      }
171 5055b5d4 Leszek Koltunski
172 40eef4b9 Leszek Koltunski
    mQueues.draw(time, mGirl, mGrid);
173 b9615af9 Leszek Koltunski
    }
174
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177 5055b5d4 Leszek Koltunski
  private void applyMatrixEffects(int width, int height)
178
    {
179 40eef4b9 Leszek Koltunski
    mQueues.abortEffects(EffectTypes.MATRIX);
180 5055b5d4 Leszek Koltunski
181
    if( (float)bmpHeight/bmpWidth > (float)height/width )
182 b9615af9 Leszek Koltunski
      {
183
      int w = (height*bmpWidth)/bmpHeight;
184 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
185
186 40eef4b9 Leszek Koltunski
      mQueues.move( new Static3D((width-w)/2,0,0) );
187
      mQueues.scale(factor);
188 a2cb31e9 Leszek Koltunski
      }
189 b9615af9 Leszek Koltunski
    else
190
      {
191
      int h = (width*bmpHeight)/bmpWidth;
192 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
193
194 40eef4b9 Leszek Koltunski
      mQueues.move( new Static3D(0,(height-h)/2,0) );
195
      mQueues.scale(factor);
196 b9615af9 Leszek Koltunski
      }
197 5055b5d4 Leszek Koltunski
198 40eef4b9 Leszek Koltunski
    mQueues.scale(mScaleDyn);
199 5055b5d4 Leszek Koltunski
    }
200
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
203
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
204
    {
205
    scrWidth = width;
206
    scrHeight= height;
207
    applyMatrixEffects(width, height);
208 b9615af9 Leszek Koltunski
    Distorted.onSurfaceChanged(width, height);
209
    }
210 a2cb31e9 Leszek Koltunski
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
    
213 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
214
    {
215 e7a4ef16 Leszek Koltunski
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
216
217 b9615af9 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
218
    Bitmap bitmap;
219 a2cb31e9 Leszek Koltunski
        
220 b9615af9 Leszek Koltunski
    try
221
      {
222
      bitmap = BitmapFactory.decodeStream(is);
223
      }
224
    finally
225
      {
226
      try
227 a2cb31e9 Leszek Koltunski
        {
228 b9615af9 Leszek Koltunski
        is.close();
229
        }
230
      catch(IOException e) { }
231
      }
232 a2cb31e9 Leszek Koltunski
      
233 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
234
    bmpWidth  = bitmap.getWidth();
235 e8b6aa95 Leszek Koltunski
236 10b7e588 Leszek Koltunski
    mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
237 7451c98a Leszek Koltunski
    mGirl = new DistortedTexture(bmpWidth,bmpHeight);
238 e8b6aa95 Leszek Koltunski
    mGirl.setTexture(bitmap);
239 a2cb31e9 Leszek Koltunski
240 5055b5d4 Leszek Koltunski
    mOffscreen = new DistortedFramebuffer(bmpWidth,bmpHeight);
241 3c8433ae Leszek Koltunski
242 b9615af9 Leszek Koltunski
    try
243
      {
244
      Distorted.onSurfaceCreated(mView.getContext());
245
      }
246
    catch(Exception ex)
247
      {
248
      android.util.Log.e("Renderer", ex.getMessage() );
249 a2cb31e9 Leszek Koltunski
      }
250 b9615af9 Leszek Koltunski
    }
251
  }