Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 6637d0f2

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

    
47
import android.graphics.Bitmap;
48
import android.graphics.BitmapFactory;
49
import android.opengl.GLES30;
50
import android.opengl.GLSurfaceView;
51
import android.os.Environment;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
class SaveRenderer implements GLSurfaceView.Renderer
56
  {
57
  private GLSurfaceView mView;
58
  private DistortedEffects mEffects;
59
  private DistortedFramebuffer mOffscreen;
60
  private DistortedScreen mScreen;
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
    Static3D pLeft = new Static3D(132, 264, 0);
81
    Static3D pRight= new Static3D(247, 264, 0);
82
      
83
    Static4D sinkRegion = new Static4D(0,0,60,60);
84
      
85
    s0 = new Static1D(boobsSink);
86
      
87
    Dynamic1D 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
    mEffects = new DistortedEffects();
96
    mEffects.sink( diSink, pLeft , sinkRegion);
97
    mEffects.sink( diSink, pRight, sinkRegion);
98

    
99
    mScreen = new DistortedScreen();
100
    }
101

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

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

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

    
133
      File file;
134
      int lowestNotFound = 1;
135

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

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

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
   
150
  public void onDrawFrame(GL10 glUnused)
151
    {
152
    GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
153

    
154
    long time = System.currentTimeMillis();
155

    
156
    if( isSaving )  // render to an offscreen buffer and read pixels
157
      {
158
      mEffects.abortEffects(EffectTypes.MATRIX);
159
      mEffects.scale(mScaleFactor);
160
      mOffscreen.render(time);
161
      applyMatrixEffects(scrWidth,scrHeight);
162

    
163
      int fW =(int)(mScale*bmpWidth);
164
      int fH =(int)(mScale*bmpHeight);
165
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
166
      buf.order(ByteOrder.LITTLE_ENDIAN);
167

    
168
      int textureID = mOffscreen.getTextureID();
169

    
170
      if( textureID>=0 )
171
        {
172
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureID);
173
        GLES30.glReadPixels( 0, 0, fW, fH, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, buf);
174
        SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
175
        }
176
      else
177
        {
178
        android.util.Log.e("Save", "Error trying to read from offscreen FBO, textureID="+textureID);
179
        }
180

    
181
      isSaving = false;
182
      }
183

    
184
    mScreen.render(time);
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

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

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

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

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

    
210
    mEffects.scale(mScaleDyn);
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

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

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

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

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

    
248
    MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
249
    DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
250
    texture.setTexture(bitmap);
251

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

    
254
    mOffscreen.detachAll();
255
    mOffscreen.attach(texture,mEffects,mesh);
256
    mScreen.detachAll();
257
    mScreen.attach(texture,mEffects,mesh);
258

    
259
    DistortedEffects.enableEffect(EffectNames.SINK);
260

    
261
    try
262
      {
263
      Distorted.onCreate(mView.getContext());
264
      }
265
    catch(Exception ex)
266
      {
267
      android.util.Log.e("Renderer", ex.getMessage() );
268
      }
269
    }
270
  }
(2-2/4)