Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 5055b5d4

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

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

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

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

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

    
192
    mGirl.scale(mScaleDyn);
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

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

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

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

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

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

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