Project

General

Profile

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

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

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

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class SaveRenderer implements GLSurfaceView.Renderer
52
  {
53
  private GLSurfaceView mView;
54
  private DistortedBitmap mGirl;
55
  private DistortedFramebuffer mOffscreen;
56
  private Static3D pLeft, pRight;
57
  private Static4D sinkRegion;
58
  private Dynamic1D diSink;
59
  private Static1D s0;
60
  private Dynamic3D mScaleDyn;
61
  private Static3D mScaleFactor;
62

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

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

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

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

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  void setSize(float s)
97
    {
98
    boobsSink = s;
99
    s0.set(boobsSink);
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  void setScale(float s)
105
    {
106
    mScale = s;
107
    mScaleFactor.set(s,s,1.0f);
108

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

    
113
    if( mOffscreen!=null )
114
      mOffscreen.resize( (int)(mScale*bmpWidth),(int)(mScale*bmpHeight));
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  void Save()
120
    {
121
    if( !isSaving )
122
      {
123
      isSaving = true;
124

    
125
      File file;
126
      int lowestNotFound = 1;
127

    
128
      while(true)
129
        {
130
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
131
        file = new File(mPath);
132

    
133
        if( !file.exists() ) break;
134

    
135
        lowestNotFound++;
136
        }
137
      }
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
   
142
  public void onDrawFrame(GL10 glUnused)
143
    {
144
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
145

    
146
    if( isSaving )  // render to an offscreen buffer and read pixels
147
      {
148
      mGirl.abortEffects(EffectTypes.MATRIX);
149
      mGirl.scale(mScaleFactor);
150
      mGirl.draw(System.currentTimeMillis(), mOffscreen);
151
      applyMatrixEffects(scrWidth,scrHeight);
152

    
153
      int fW =(int)(mScale*bmpWidth);
154
      int fH =(int)(mScale*bmpHeight);
155
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
156
      buf.order(ByteOrder.LITTLE_ENDIAN);
157
      mOffscreen.setAsInput();
158

    
159
      GLES20.glReadPixels( 0, 0, fW, fH, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
160
      SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
161

    
162
      isSaving = false;
163
      }
164

    
165
    mGirl.draw(System.currentTimeMillis());
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private void applyMatrixEffects(int width, int height)
171
    {
172
    mGirl.abortEffects(EffectTypes.MATRIX);
173

    
174
    if( (float)bmpHeight/bmpWidth > (float)height/width )
175
      {
176
      int w = (height*bmpWidth)/bmpHeight;
177
      float factor = (float)height/bmpHeight;
178

    
179
      mGirl.move( new Static3D((width-w)/2,0,0) );
180
      mGirl.scale(factor);
181
      }
182
    else
183
      {
184
      int h = (width*bmpHeight)/bmpWidth;
185
      float factor = (float)width/bmpWidth;
186

    
187
      mGirl.move( new Static3D(0,(height-h)/2,0) );
188
      mGirl.scale(factor);
189
      }
190

    
191
    mGirl.scale(mScaleDyn);
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
197
    {
198
    scrWidth = width;
199
    scrHeight= height;
200
    applyMatrixEffects(width, height);
201
    Distorted.onSurfaceChanged(width, height);
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
    
206
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
207
    {
208
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
209

    
210
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
211
    Bitmap bitmap;
212
        
213
    try
214
      {
215
      bitmap = BitmapFactory.decodeStream(is);
216
      }
217
    finally
218
      {
219
      try
220
        {
221
        is.close();
222
        }
223
      catch(IOException e) { }
224
      }
225
      
226
    bmpHeight = bitmap.getHeight();
227
    bmpWidth  = bitmap.getWidth();
228
      
229
    mGirl = new DistortedBitmap(bitmap, 30);
230

    
231
    mGirl.sink( diSink, pLeft , sinkRegion);
232
    mGirl.sink( diSink, pRight, sinkRegion);
233

    
234
    mOffscreen = new DistortedFramebuffer(bmpWidth,bmpHeight);
235

    
236
    try
237
      {
238
      Distorted.onSurfaceCreated(mView.getContext());
239
      }
240
    catch(Exception ex)
241
      {
242
      android.util.Log.e("Renderer", ex.getMessage() );
243
      }
244
    }
245
  }
(2-2/4)