Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 77e66c58

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 06cd94ba Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
34 e8bf2663 Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
35 8dfa45c4 leszek
import org.distorted.library.effect.VertexEffectSink;
36 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
37 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
41 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedFramebuffer;
42 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
43
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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
48
import android.content.Context;
49
import android.content.pm.ConfigurationInfo;
50
import android.content.res.Resources;
51 a2cb31e9 Leszek Koltunski
import android.graphics.Bitmap;
52
import android.graphics.BitmapFactory;
53 41a85bb7 Leszek Koltunski
import android.opengl.GLES31;
54 a2cb31e9 Leszek Koltunski
import android.opengl.GLSurfaceView;
55 7cfeeb63 Leszek Koltunski
import android.os.Environment;
56 a2cb31e9 Leszek Koltunski
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 dc10a48d Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
60 b9615af9 Leszek Koltunski
  {
61 dc10a48d Leszek Koltunski
  private final GLSurfaceView mView;
62
  private final DistortedEffects mEffects;
63
  private final DistortedScreen mScreen;
64
  private final Static1D s0;
65
  private final Static3D mScaleFactor, mScaleMain, mScaleVertex;
66
  private final Resources mResources;
67
68 d218d64e leszek
  private DistortedFramebuffer mOffscreen;
69 59540ba2 Leszek Koltunski
  private DistortedTexture mTexture;
70 f2085b96 Leszek Koltunski
  private MeshSquare mMesh;
71 5055b5d4 Leszek Koltunski
  private float mScale;
72 b9615af9 Leszek Koltunski
  private int bmpHeight, bmpWidth;
73 13a5c5cd Leszek Koltunski
  private int scrHeight, scrWidth;
74
  private float boobsSink;
75
  private boolean isSaving = false;
76
  private String mPath;
77 b9615af9 Leszek Koltunski
78 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80 13a5c5cd Leszek Koltunski
  SaveRenderer(GLSurfaceView v)
81 b9615af9 Leszek Koltunski
    {
82
    mView = v;
83 dc10a48d Leszek Koltunski
    mResources = v.getResources();
84
85 b9615af9 Leszek Koltunski
    boobsSink  = 1.0f;
86 627e0ea2 Leszek Koltunski
87
    Static3D pLeft = new Static3D(-68, 36, 0);
88
    Static3D pRight= new Static3D( 47, 36, 0);
89
90 9e7b6dbd Leszek Koltunski
    Static4D sinkRegion = new Static4D(0,0,0,60);
91 a2cb31e9 Leszek Koltunski
      
92 7589635e Leszek Koltunski
    s0 = new Static1D(boobsSink);
93 a2cb31e9 Leszek Koltunski
      
94 40eef4b9 Leszek Koltunski
    Dynamic1D diSink = new Dynamic1D();
95 b9615af9 Leszek Koltunski
    diSink.add(s0);
96 3c8433ae Leszek Koltunski
97 5055b5d4 Leszek Koltunski
    mScale = 1.0f;
98
    mScaleFactor = new Static3D(mScale,mScale,1.0f);
99 e8bf2663 Leszek Koltunski
    mScaleMain   = new Static3D(1,1,1);
100
    mScaleVertex = new Static3D(1,1,1);
101 40eef4b9 Leszek Koltunski
102 698ad0a8 Leszek Koltunski
    mEffects = new DistortedEffects();
103 e8bf2663 Leszek Koltunski
    mEffects.apply( new VertexEffectScale(mScaleVertex));
104 06cd94ba Leszek Koltunski
    mEffects.apply( new VertexEffectSink(diSink, pLeft , sinkRegion) );
105
    mEffects.apply( new VertexEffectSink(diSink, pRight, sinkRegion) );
106
    mEffects.apply( new MatrixEffectScale(mScaleMain));
107
    mEffects.apply( new MatrixEffectScale(mScaleFactor));
108 392e16fd Leszek Koltunski
109 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
110 b9615af9 Leszek Koltunski
    }
111 a2cb31e9 Leszek Koltunski
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114 13a5c5cd Leszek Koltunski
  void setSize(float s)
115 e4237c21 Leszek Koltunski
    {
116
    boobsSink = s;
117
    s0.set(boobsSink);
118 b9615af9 Leszek Koltunski
    }
119 a2cb31e9 Leszek Koltunski
120 5055b5d4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122
  void setScale(float s)
123
    {
124
    mScale = s;
125
    mScaleFactor.set(s,s,1.0f);
126
127
    // when we move our scroll bar, this does NOT keep allocating and deallocating
128
    // the whole WxH texture - the allocation happens only once, on next render, i.e. -
129
    // when one presses the 'SAVE' button.
130
131
    if( mOffscreen!=null )
132
      mOffscreen.resize( (int)(mScale*bmpWidth),(int)(mScale*bmpHeight));
133
    }
134
135 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
136 b9615af9 Leszek Koltunski
137 13a5c5cd Leszek Koltunski
  void Save()
138 b9615af9 Leszek Koltunski
    {
139 92040610 Leszek Koltunski
    if( !isSaving )
140 b9615af9 Leszek Koltunski
      {
141 e4237c21 Leszek Koltunski
      isSaving = true;
142 b9615af9 Leszek Koltunski
143 e4237c21 Leszek Koltunski
      File file;
144
      int lowestNotFound = 1;
145
146
      while(true)
147 a2cb31e9 Leszek Koltunski
        {
148 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
149
        file = new File(mPath);
150
151
        if( !file.exists() ) break;
152
153
        lowestNotFound++;
154 a2cb31e9 Leszek Koltunski
        }
155 b9615af9 Leszek Koltunski
      }
156
    }
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
   
160
  public void onDrawFrame(GL10 glUnused)
161
    {
162 3c8433ae Leszek Koltunski
    if( isSaving )  // render to an offscreen buffer and read pixels
163 e4237c21 Leszek Koltunski
      {
164 06cd94ba Leszek Koltunski
      mScaleMain.set(1,1,1);
165 d8b42d02 Leszek Koltunski
      mOffscreen.render(System.currentTimeMillis());
166 5055b5d4 Leszek Koltunski
      applyMatrixEffects(scrWidth,scrHeight);
167 3c8433ae Leszek Koltunski
168 5055b5d4 Leszek Koltunski
      int fW =(int)(mScale*bmpWidth);
169
      int fH =(int)(mScale*bmpHeight);
170
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
171 e4237c21 Leszek Koltunski
      buf.order(ByteOrder.LITTLE_ENDIAN);
172
173 29246ab4 Leszek Koltunski
      int textureID = mOffscreen.getTextureID();
174
175
      if( textureID>=0 )
176
        {
177 41a85bb7 Leszek Koltunski
        GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, textureID);
178
        GLES31.glReadPixels( 0, 0, fW, fH, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, buf);
179 29246ab4 Leszek Koltunski
        SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
180
        }
181
      else
182
        {
183 3f5b2cb3 leszek
        android.util.Log.e("Save", "Error trying to read from offscreen FBO, textureID="+textureID);
184 29246ab4 Leszek Koltunski
        }
185 e4237c21 Leszek Koltunski
      isSaving = false;
186
      }
187 5055b5d4 Leszek Koltunski
188 d8b42d02 Leszek Koltunski
    // Quite subtle issue here. Since we share the DistortedEffects object between the mOffscreen
189
    // and mScreen surfaces, we cannot render both of them in one go using the same time. That
190
    // would make the second render, in this case mScreen's render, re-use the values of the
191
    // Dynamics calculated the first time around and we don't want that (mOffscreen's scale and move
192
    // are different! What would happen is the mScreen would be - one render when we are 'saving' -
193
    // get drawn with different dimensions and on-screen position, i.e. we would see a flash.
194
195
    mScreen.render(System.currentTimeMillis());
196 b9615af9 Leszek Koltunski
    }
197
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
200 5055b5d4 Leszek Koltunski
  private void applyMatrixEffects(int width, int height)
201
    {
202 e8bf2663 Leszek Koltunski
    float horiRatio = (float)width / mScaleVertex.get0();
203
    float vertRatio = (float)height/ mScaleVertex.get1();
204
    float factor    = Math.min(horiRatio, vertRatio);
205 0de83d97 Leszek Koltunski
    mScaleMain.set(factor,factor,factor);
206 5055b5d4 Leszek Koltunski
    }
207
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
211
    {
212
    scrWidth = width;
213
    scrHeight= height;
214
    applyMatrixEffects(width, height);
215 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
216 b9615af9 Leszek Koltunski
    }
217 a2cb31e9 Leszek Koltunski
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
    
220 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
221
    {
222
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
223
    Bitmap bitmap;
224 6fd3af4d Leszek Koltunski
225 b9615af9 Leszek Koltunski
    try
226
      {
227
      bitmap = BitmapFactory.decodeStream(is);
228
      }
229
    finally
230
      {
231
      try
232 a2cb31e9 Leszek Koltunski
        {
233 b9615af9 Leszek Koltunski
        is.close();
234
        }
235 e8bf2663 Leszek Koltunski
      catch(IOException ignored) { }
236 b9615af9 Leszek Koltunski
      }
237 a2cb31e9 Leszek Koltunski
      
238 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
239
    bmpWidth  = bitmap.getWidth();
240 e8b6aa95 Leszek Koltunski
241 e8bf2663 Leszek Koltunski
    mScaleVertex.set(bmpWidth,bmpHeight,0);
242
243 f2085b96 Leszek Koltunski
    if( mMesh   ==null ) mMesh = new MeshSquare(30,30*bmpHeight/bmpWidth);
244 687263cc Leszek Koltunski
    if( mTexture==null ) mTexture = new DistortedTexture();
245 59540ba2 Leszek Koltunski
    mTexture.setTexture(bitmap);
246 a2cb31e9 Leszek Koltunski
247 f5c28afc Leszek Koltunski
    if( mOffscreen==null ) mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight),
248 a929d64d Leszek Koltunski
                                                                  1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
249 3c8433ae Leszek Koltunski
250 fe59d375 Leszek Koltunski
    mOffscreen.detachAll();
251 5f2a53b6 leszek
    mOffscreen.attach(mTexture,mEffects,mMesh);
252 fe59d375 Leszek Koltunski
    mScreen.detachAll();
253 5f2a53b6 leszek
    mScreen.attach(mTexture,mEffects,mMesh);
254 fe59d375 Leszek Koltunski
255 e8bf2663 Leszek Koltunski
    VertexEffectScale.enable();
256 885b9cca leszek
    VertexEffectSink.enable();
257 6637d0f2 Leszek Koltunski
258 dc10a48d Leszek Koltunski
    DistortedLibrary.onSurfaceCreated(this);
259 061449ed Leszek Koltunski
    }
260
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263
  public void distortedException(Exception ex)
264
    {
265
    android.util.Log.e("Save", ex.getMessage() );
266 b9615af9 Leszek Koltunski
    }
267 dc10a48d Leszek Koltunski
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270
  public InputStream localFile(int fileID)
271
     {
272
     return mResources.openRawResource(fileID);
273
     }
274
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277
  public void logMessage(String message)
278
     {
279
     android.util.Log.e("Save", message );
280
     }
281 b9615af9 Leszek Koltunski
  }