Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ f5c28afc

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 06cd94ba Leszek Koltunski
import org.distorted.library.effect.EffectName;
34
import org.distorted.library.effect.MatrixEffectMove;
35
import org.distorted.library.effect.MatrixEffectScale;
36 8dfa45c4 leszek
import org.distorted.library.effect.VertexEffectSink;
37 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedScreen;
40
import org.distorted.library.main.DistortedTexture;
41
import org.distorted.library.main.MeshFlat;
42
import org.distorted.library.main.DistortedFramebuffer;
43 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
44
import org.distorted.library.type.Static1D;
45
import org.distorted.library.type.Static3D;
46
import org.distorted.library.type.Static4D;
47 a2cb31e9 Leszek Koltunski
48
import android.graphics.Bitmap;
49
import android.graphics.BitmapFactory;
50 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
51 a2cb31e9 Leszek Koltunski
import android.opengl.GLSurfaceView;
52 7cfeeb63 Leszek Koltunski
import android.os.Environment;
53 a2cb31e9 Leszek Koltunski
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 e4237c21 Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer
57 b9615af9 Leszek Koltunski
  {
58
  private GLSurfaceView mView;
59 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
60 d218d64e leszek
  private DistortedFramebuffer mOffscreen;
61 59540ba2 Leszek Koltunski
  private DistortedTexture mTexture;
62 5f2a53b6 leszek
  private MeshFlat mMesh;
63 d218d64e leszek
  private DistortedScreen mScreen;
64 13a5c5cd Leszek Koltunski
  private Static1D s0;
65 06cd94ba Leszek Koltunski
  private Static3D mScaleFactor, mScaleMain, mMove;
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
    mScaleFactor = new Static3D(mScale,mScale,1.0f);
94 06cd94ba Leszek Koltunski
    mMove = new Static3D(0,0,0);
95
    mScaleMain = new Static3D(1,1,1);
96 40eef4b9 Leszek Koltunski
97 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
98 06cd94ba Leszek Koltunski
    mEffects.apply( new VertexEffectSink(diSink, pLeft , sinkRegion) );
99
    mEffects.apply( new VertexEffectSink(diSink, pRight, sinkRegion) );
100
    mEffects.apply( new MatrixEffectMove(mMove));
101
    mEffects.apply( new MatrixEffectScale(mScaleMain));
102
    mEffects.apply( new MatrixEffectScale(mScaleFactor));
103 392e16fd Leszek Koltunski
104 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
105 b9615af9 Leszek Koltunski
    }
106 a2cb31e9 Leszek Koltunski
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109 13a5c5cd Leszek Koltunski
  void setSize(float s)
110 e4237c21 Leszek Koltunski
    {
111
    boobsSink = s;
112
    s0.set(boobsSink);
113 b9615af9 Leszek Koltunski
    }
114 a2cb31e9 Leszek Koltunski
115 5055b5d4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
116
117
  void setScale(float s)
118
    {
119
    mScale = s;
120
    mScaleFactor.set(s,s,1.0f);
121
122
    // when we move our scroll bar, this does NOT keep allocating and deallocating
123
    // the whole WxH texture - the allocation happens only once, on next render, i.e. -
124
    // when one presses the 'SAVE' button.
125
126
    if( mOffscreen!=null )
127
      mOffscreen.resize( (int)(mScale*bmpWidth),(int)(mScale*bmpHeight));
128
    }
129
130 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
131 b9615af9 Leszek Koltunski
132 13a5c5cd Leszek Koltunski
  void Save()
133 b9615af9 Leszek Koltunski
    {
134 92040610 Leszek Koltunski
    if( !isSaving )
135 b9615af9 Leszek Koltunski
      {
136 e4237c21 Leszek Koltunski
      isSaving = true;
137 b9615af9 Leszek Koltunski
138 e4237c21 Leszek Koltunski
      File file;
139
      int lowestNotFound = 1;
140
141
      while(true)
142 a2cb31e9 Leszek Koltunski
        {
143 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
144
        file = new File(mPath);
145
146
        if( !file.exists() ) break;
147
148
        lowestNotFound++;
149 a2cb31e9 Leszek Koltunski
        }
150 b9615af9 Leszek Koltunski
      }
151
    }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
   
155
  public void onDrawFrame(GL10 glUnused)
156
    {
157 3c8433ae Leszek Koltunski
    if( isSaving )  // render to an offscreen buffer and read pixels
158 e4237c21 Leszek Koltunski
      {
159 06cd94ba Leszek Koltunski
      mMove.set(0,0,0);
160
      mScaleMain.set(1,1,1);
161 d8b42d02 Leszek Koltunski
      mOffscreen.render(System.currentTimeMillis());
162 5055b5d4 Leszek Koltunski
      applyMatrixEffects(scrWidth,scrHeight);
163 3c8433ae Leszek Koltunski
164 5055b5d4 Leszek Koltunski
      int fW =(int)(mScale*bmpWidth);
165
      int fH =(int)(mScale*bmpHeight);
166
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
167 e4237c21 Leszek Koltunski
      buf.order(ByteOrder.LITTLE_ENDIAN);
168
169 29246ab4 Leszek Koltunski
      int textureID = mOffscreen.getTextureID();
170
171
      if( textureID>=0 )
172
        {
173 41a81a14 Leszek Koltunski
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureID);
174
        GLES30.glReadPixels( 0, 0, fW, fH, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, buf);
175 29246ab4 Leszek Koltunski
        SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
176
        }
177
      else
178
        {
179 3f5b2cb3 leszek
        android.util.Log.e("Save", "Error trying to read from offscreen FBO, textureID="+textureID);
180 29246ab4 Leszek Koltunski
        }
181 e4237c21 Leszek Koltunski
      isSaving = false;
182
      }
183 5055b5d4 Leszek Koltunski
184 d8b42d02 Leszek Koltunski
    // Quite subtle issue here. Since we share the DistortedEffects object between the mOffscreen
185
    // and mScreen surfaces, we cannot render both of them in one go using the same time. That
186
    // would make the second render, in this case mScreen's render, re-use the values of the
187
    // Dynamics calculated the first time around and we don't want that (mOffscreen's scale and move
188
    // are different! What would happen is the mScreen would be - one render when we are 'saving' -
189
    // get drawn with different dimensions and on-screen position, i.e. we would see a flash.
190
191
    mScreen.render(System.currentTimeMillis());
192 b9615af9 Leszek Koltunski
    }
193
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196 5055b5d4 Leszek Koltunski
  private void applyMatrixEffects(int width, int height)
197
    {
198 67c3a83b leszek
    if( (float)bmpHeight/bmpWidth > (float)height/width )
199
      {
200
      int w = (height*bmpWidth)/bmpHeight;
201
      float factor = (float)height/bmpHeight;
202
203 06cd94ba Leszek Koltunski
      mMove.set((width-w)/2,0,0);
204
      mScaleMain.set(factor,factor,factor);
205 67c3a83b leszek
      }
206
    else
207
      {
208
      int h = (width*bmpHeight)/bmpWidth;
209
      float factor = (float)width/bmpWidth;
210
211 06cd94ba Leszek Koltunski
      mMove.set(0,(height-h)/2,0);
212
      mScaleMain.set(factor,factor,factor);
213 67c3a83b leszek
      }
214 5055b5d4 Leszek Koltunski
    }
215
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
219
    {
220
    scrWidth = width;
221
    scrHeight= height;
222
    applyMatrixEffects(width, height);
223 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
224 b9615af9 Leszek Koltunski
    }
225 a2cb31e9 Leszek Koltunski
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
    
228 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
229
    {
230
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
231
    Bitmap bitmap;
232 6fd3af4d Leszek Koltunski
233 b9615af9 Leszek Koltunski
    try
234
      {
235
      bitmap = BitmapFactory.decodeStream(is);
236
      }
237
    finally
238
      {
239
      try
240 a2cb31e9 Leszek Koltunski
        {
241 b9615af9 Leszek Koltunski
        is.close();
242
        }
243
      catch(IOException e) { }
244
      }
245 a2cb31e9 Leszek Koltunski
      
246 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
247
    bmpWidth  = bitmap.getWidth();
248 e8b6aa95 Leszek Koltunski
249 5f2a53b6 leszek
    if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
250 59540ba2 Leszek Koltunski
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
251
    mTexture.setTexture(bitmap);
252 a2cb31e9 Leszek Koltunski
253 f5c28afc Leszek Koltunski
    if( mOffscreen==null ) mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight),
254
                                                                  0, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
255 3c8433ae Leszek Koltunski
256 fe59d375 Leszek Koltunski
    mOffscreen.detachAll();
257 5f2a53b6 leszek
    mOffscreen.attach(mTexture,mEffects,mMesh);
258 fe59d375 Leszek Koltunski
    mScreen.detachAll();
259 5f2a53b6 leszek
    mScreen.attach(mTexture,mEffects,mMesh);
260 fe59d375 Leszek Koltunski
261 885b9cca leszek
    VertexEffectSink.enable();
262 6637d0f2 Leszek Koltunski
263 b9615af9 Leszek Koltunski
    try
264
      {
265 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
266 b9615af9 Leszek Koltunski
      }
267
    catch(Exception ex)
268
      {
269
      android.util.Log.e("Renderer", ex.getMessage() );
270 a2cb31e9 Leszek Koltunski
      }
271 b9615af9 Leszek Koltunski
    }
272
  }