Project

General

Profile

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

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

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.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 DistortedEffects mEffects;
58
  private GridFlat mGrid;
59
  private DistortedFramebuffer mScreen, 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
    mEffects = new DistortedEffects();
95
    mEffects.sink( diSink, pLeft , sinkRegion);
96
    mEffects.sink( diSink, pRight, sinkRegion);
97

    
98
    mScreen = new DistortedFramebuffer(0);
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

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

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

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

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  void Save()
127
    {
128
    if( !isSaving )
129
      {
130
      isSaving = true;
131

    
132
      File file;
133
      int lowestNotFound = 1;
134

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

    
140
        if( !file.exists() ) break;
141

    
142
        lowestNotFound++;
143
        }
144
      }
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
   
149
  public void onDrawFrame(GL10 glUnused)
150
    {
151
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
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
      mEffects.draw(time, mGirl, mGrid, mOffscreen);
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
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureID);
172
        GLES20.glReadPixels( 0, 0, fW, fH, GLES20.GL_RGBA, GLES20.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");
178
        }
179

    
180
      isSaving = false;
181
      }
182

    
183
    mEffects.draw(time, mGirl, mGrid, mScreen);
184
    }
185

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

    
188
  private void applyMatrixEffects(int width, int height)
189
    {
190
    mEffects.abortEffects(EffectTypes.MATRIX);
191

    
192
    if( (float)bmpHeight/bmpWidth > (float)height/width )
193
      {
194
      int w = (height*bmpWidth)/bmpHeight;
195
      float factor = (float)height/bmpHeight;
196

    
197
      mEffects.move( new Static3D((width-w)/2,0,0) );
198
      mEffects.scale(factor);
199
      }
200
    else
201
      {
202
      int h = (width*bmpHeight)/bmpWidth;
203
      float factor = (float)width/bmpWidth;
204

    
205
      mEffects.move( new Static3D(0,(height-h)/2,0) );
206
      mEffects.scale(factor);
207
      }
208

    
209
    mEffects.scale(mScaleDyn);
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
215
    {
216
    scrWidth = width;
217
    scrHeight= height;
218
    applyMatrixEffects(width, height);
219
    mScreen.resize(width, height);
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
    
224
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
225
    {
226
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
227

    
228
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
229
    Bitmap bitmap;
230

    
231
    try
232
      {
233
      bitmap = BitmapFactory.decodeStream(is);
234
      }
235
    finally
236
      {
237
      try
238
        {
239
        is.close();
240
        }
241
      catch(IOException e) { }
242
      }
243
      
244
    bmpHeight = bitmap.getHeight();
245
    bmpWidth  = bitmap.getWidth();
246

    
247
    mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
248
    mGirl = new DistortedTexture(bmpWidth,bmpHeight);
249
    mGirl.setTexture(bitmap);
250

    
251
    mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight) );
252

    
253
    try
254
      {
255
      Distorted.onSurfaceCreated(mView.getContext());
256
      }
257
    catch(Exception ex)
258
      {
259
      android.util.Log.e("Renderer", ex.getMessage() );
260
      }
261
    }
262
  }
(2-2/4)