Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 01782e85

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