Project

General

Profile

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

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

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

    
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLES20;
48
import android.opengl.GLSurfaceView;
49
import android.os.Environment;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
class SaveRenderer implements GLSurfaceView.Renderer
54
  {
55
  private GLSurfaceView mView;
56
  private DistortedTexture mGirl;
57
  private DistortedEffectQueues mQueues;
58
  private GridFlat mGrid;
59
  private DistortedFramebuffer mOffscreen;
60
  private Static1D s0;
61
  private Dynamic3D mScaleDyn;
62
  private Static3D mScaleFactor;
63

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

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

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

    
94
    mQueues = new DistortedEffectQueues();
95
    mQueues.sink( diSink, pLeft , sinkRegion);
96
    mQueues.sink( diSink, pRight, sinkRegion);
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  void setSize(float s)
102
    {
103
    boobsSink = s;
104
    s0.set(boobsSink);
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  void Save()
125
    {
126
    if( !isSaving )
127
      {
128
      isSaving = true;
129

    
130
      File file;
131
      int lowestNotFound = 1;
132

    
133
      while(true)
134
        {
135
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
136
        file = new File(mPath);
137

    
138
        if( !file.exists() ) break;
139

    
140
        lowestNotFound++;
141
        }
142
      }
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

    
151
    long time = System.currentTimeMillis();
152

    
153
    if( isSaving )  // render to an offscreen buffer and read pixels
154
      {
155
      mQueues.abortEffects(EffectTypes.MATRIX);
156
      mQueues.scale(mScaleFactor);
157
      mQueues.draw(time, mGirl, mGrid, mOffscreen);
158
      applyMatrixEffects(scrWidth,scrHeight);
159

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

    
166
      GLES20.glReadPixels( 0, 0, fW, fH, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
167
      SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
168

    
169
      isSaving = false;
170
      }
171

    
172
    mQueues.draw(time, mGirl, mGrid);
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  private void applyMatrixEffects(int width, int height)
178
    {
179
    mQueues.abortEffects(EffectTypes.MATRIX);
180

    
181
    if( (float)bmpHeight/bmpWidth > (float)height/width )
182
      {
183
      int w = (height*bmpWidth)/bmpHeight;
184
      float factor = (float)height/bmpHeight;
185

    
186
      mQueues.move( new Static3D((width-w)/2,0,0) );
187
      mQueues.scale(factor);
188
      }
189
    else
190
      {
191
      int h = (width*bmpHeight)/bmpWidth;
192
      float factor = (float)width/bmpWidth;
193

    
194
      mQueues.move( new Static3D(0,(height-h)/2,0) );
195
      mQueues.scale(factor);
196
      }
197

    
198
    mQueues.scale(mScaleDyn);
199
    }
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
    Distorted.onSurfaceChanged(width, height);
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
    
213
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
214
    {
215
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
216

    
217
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
218
    Bitmap bitmap;
219
        
220
    try
221
      {
222
      bitmap = BitmapFactory.decodeStream(is);
223
      }
224
    finally
225
      {
226
      try
227
        {
228
        is.close();
229
        }
230
      catch(IOException e) { }
231
      }
232
      
233
    bmpHeight = bitmap.getHeight();
234
    bmpWidth  = bitmap.getWidth();
235

    
236
    mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
237
    mGirl = new DistortedTexture(bmpWidth,bmpHeight);
238
    mGirl.setTexture(bitmap);
239

    
240
    mOffscreen = new DistortedFramebuffer(bmpWidth,bmpHeight);
241

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