Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 10b7e588

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

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

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

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

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

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

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  void Save()
122
    {
123
    if( !isSaving )
124
      {
125
      isSaving = true;
126

    
127
      File file;
128
      int lowestNotFound = 1;
129

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

    
135
        if( !file.exists() ) break;
136

    
137
        lowestNotFound++;
138
        }
139
      }
140
    }
141

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

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

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

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

    
164
      isSaving = false;
165
      }
166

    
167
    mGirl.draw(System.currentTimeMillis(), mGrid);
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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

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

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

    
193
    mGirl.scale(mScaleDyn);
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

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

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

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

    
231
    mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
232
    mGirl = new DistortedObject(bmpWidth,bmpHeight,1);
233
    mGirl.setTexture(bitmap);
234

    
235
    mGirl.sink( diSink, pLeft , sinkRegion);
236
    mGirl.sink( diSink, pRight, sinkRegion);
237

    
238
    mOffscreen = new DistortedFramebuffer(bmpWidth,bmpHeight);
239

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