Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 51554e47

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.save;
21

    
22
import java.io.File;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27

    
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
import org.distorted.library.DistortedEffects;
35
import org.distorted.library.DistortedScreen;
36
import org.distorted.library.DistortedTexture;
37
import org.distorted.library.EffectNames;
38
import org.distorted.library.MeshFlat;
39
import org.distorted.library.DistortedFramebuffer;
40
import org.distorted.library.EffectTypes;
41
import org.distorted.library.type.Dynamic1D;
42
import org.distorted.library.type.Dynamic3D;
43
import org.distorted.library.type.Static1D;
44
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46

    
47
import android.graphics.Bitmap;
48
import android.graphics.BitmapFactory;
49
import android.opengl.GLES30;
50
import android.opengl.GLSurfaceView;
51
import android.os.Environment;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
class SaveRenderer implements GLSurfaceView.Renderer
56
  {
57
  private GLSurfaceView mView;
58
  private DistortedEffects mEffects;
59
  private DistortedFramebuffer mOffscreen;
60
  private DistortedTexture mTexture;
61
  private DistortedScreen mScreen;
62
  private Static1D s0;
63
  private Dynamic3D mScaleDyn;
64
  private Static3D mScaleFactor;
65

    
66
  private float mScale;
67
  private int bmpHeight, bmpWidth;
68
  private int scrHeight, scrWidth;
69
  private float boobsSink;
70
  private boolean isSaving = false;
71
  private String mPath;
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  SaveRenderer(GLSurfaceView v)
76
    {
77
    mView = v;
78
      
79
    boobsSink  = 1.0f;
80
      
81
    Static3D pLeft = new Static3D(132, 264, 0);
82
    Static3D pRight= new Static3D(247, 264, 0);
83
      
84
    Static4D sinkRegion = new Static4D(0,0,60,60);
85
      
86
    s0 = new Static1D(boobsSink);
87
      
88
    Dynamic1D diSink = new Dynamic1D();
89
    diSink.add(s0);
90

    
91
    mScale = 1.0f;
92
    mScaleDyn = new Dynamic3D();
93
    mScaleFactor = new Static3D(mScale,mScale,1.0f);
94
    mScaleDyn.add(mScaleFactor);
95

    
96
    mEffects = new DistortedEffects();
97
    mEffects.sink( diSink, pLeft , sinkRegion);
98
    mEffects.sink( diSink, pRight, sinkRegion);
99

    
100
    mScreen = new DistortedScreen();
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  void setSize(float s)
106
    {
107
    boobsSink = s;
108
    s0.set(boobsSink);
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  void setScale(float s)
114
    {
115
    mScale = s;
116
    mScaleFactor.set(s,s,1.0f);
117

    
118
    // when we move our scroll bar, this does NOT keep allocating and deallocating
119
    // the whole WxH texture - the allocation happens only once, on next render, i.e. -
120
    // when one presses the 'SAVE' button.
121

    
122
    if( mOffscreen!=null )
123
      mOffscreen.resize( (int)(mScale*bmpWidth),(int)(mScale*bmpHeight));
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  void Save()
129
    {
130
    if( !isSaving )
131
      {
132
      isSaving = true;
133

    
134
      File file;
135
      int lowestNotFound = 1;
136

    
137
      while(true)
138
        {
139
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
140
        file = new File(mPath);
141

    
142
        if( !file.exists() ) break;
143

    
144
        lowestNotFound++;
145
        }
146
      }
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
   
151
  public void onDrawFrame(GL10 glUnused)
152
    {
153
    long time = System.currentTimeMillis();
154

    
155
    if( isSaving )  // render to an offscreen buffer and read pixels
156
      {
157
      mEffects.abortEffects(EffectTypes.MATRIX);
158
      mEffects.scale(mScaleFactor);
159
      mOffscreen.render(time);
160
      applyMatrixEffects(scrWidth,scrHeight);
161

    
162
      int fW =(int)(mScale*bmpWidth);
163
      int fH =(int)(mScale*bmpHeight);
164
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
165
      buf.order(ByteOrder.LITTLE_ENDIAN);
166

    
167
      int textureID = mOffscreen.getTextureID();
168

    
169
      if( textureID>=0 )
170
        {
171
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureID);
172
        GLES30.glReadPixels( 0, 0, fW, fH, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, buf);
173
        SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
174
        }
175
      else
176
        {
177
        android.util.Log.e("Save", "Error trying to read from offscreen FBO, textureID="+textureID);
178
        }
179

    
180
      isSaving = false;
181
      }
182

    
183
    mScreen.render(time);
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void applyMatrixEffects(int width, int height)
189
    {
190
    float qx = (float)width /bmpWidth;
191
    float qy = (float)height/bmpHeight;
192

    
193
    mEffects.abortEffects(EffectTypes.MATRIX);
194
    mEffects.scale(  qx<qy ? (new Static3D(1,qx/qy,1)) : (new Static3D(qy/qx,1,1)) );
195

    
196
    mEffects.scale(mScaleDyn);
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
202
    {
203
    scrWidth = width;
204
    scrHeight= height;
205
    applyMatrixEffects(width, height);
206
    mScreen.resize(width, height);
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
    
211
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
212
    {
213
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
214
    Bitmap bitmap;
215

    
216
    try
217
      {
218
      bitmap = BitmapFactory.decodeStream(is);
219
      }
220
    finally
221
      {
222
      try
223
        {
224
        is.close();
225
        }
226
      catch(IOException e) { }
227
      }
228
      
229
    bmpHeight = bitmap.getHeight();
230
    bmpWidth  = bitmap.getWidth();
231

    
232
    MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
233
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
234
    mTexture.setTexture(bitmap);
235

    
236
    if( mOffscreen==null ) mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight) );
237

    
238
    mOffscreen.detachAll();
239
    mOffscreen.attach(mTexture,mEffects,mesh);
240
    mScreen.detachAll();
241
    mScreen.attach(mTexture,mEffects,mesh);
242

    
243
    DistortedEffects.enableEffect(EffectNames.SINK);
244

    
245
    try
246
      {
247
      Distorted.onCreate(mView.getContext());
248
      }
249
    catch(Exception ex)
250
      {
251
      android.util.Log.e("Renderer", ex.getMessage() );
252
      }
253
    }
254
  }
(2-2/4)