Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 855ba24e

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 a2cb31e9 Leszek Koltunski
20
package org.distorted.examples.save;
21
22 7cfeeb63 Leszek Koltunski
import java.io.File;
23 a2cb31e9 Leszek Koltunski
import java.io.IOException;
24
import java.io.InputStream;
25 e4237c21 Leszek Koltunski
import java.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27 a2cb31e9 Leszek Koltunski
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 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
35 d218d64e leszek
import org.distorted.library.DistortedScreen;
36 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedTexture;
37 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
38 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
39 3c8433ae Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
40 a2cb31e9 Leszek Koltunski
import org.distorted.library.EffectTypes;
41 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
42 5055b5d4 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
43 7589635e Leszek Koltunski
import org.distorted.library.type.Static1D;
44
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46 a2cb31e9 Leszek Koltunski
47
import android.graphics.Bitmap;
48
import android.graphics.BitmapFactory;
49 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
50 a2cb31e9 Leszek Koltunski
import android.opengl.GLSurfaceView;
51 7cfeeb63 Leszek Koltunski
import android.os.Environment;
52 a2cb31e9 Leszek Koltunski
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 e4237c21 Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer
56 b9615af9 Leszek Koltunski
  {
57
  private GLSurfaceView mView;
58 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
59 d218d64e leszek
  private DistortedFramebuffer mOffscreen;
60
  private DistortedScreen mScreen;
61 13a5c5cd Leszek Koltunski
  private Static1D s0;
62 5055b5d4 Leszek Koltunski
  private Dynamic3D mScaleDyn;
63
  private Static3D mScaleFactor;
64 b9615af9 Leszek Koltunski
65 5055b5d4 Leszek Koltunski
  private float mScale;
66 b9615af9 Leszek Koltunski
  private int bmpHeight, bmpWidth;
67 13a5c5cd Leszek Koltunski
  private int scrHeight, scrWidth;
68
  private float boobsSink;
69
  private boolean isSaving = false;
70
  private String mPath;
71 b9615af9 Leszek Koltunski
72 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 13a5c5cd Leszek Koltunski
  SaveRenderer(GLSurfaceView v)
75 b9615af9 Leszek Koltunski
    {
76
    mView = v;
77 a2cb31e9 Leszek Koltunski
      
78 b9615af9 Leszek Koltunski
    boobsSink  = 1.0f;
79 a2cb31e9 Leszek Koltunski
      
80 40eef4b9 Leszek Koltunski
    Static3D pLeft = new Static3D(132, 264, 0);
81
    Static3D pRight= new Static3D(247, 264, 0);
82 a2cb31e9 Leszek Koltunski
      
83 40eef4b9 Leszek Koltunski
    Static4D sinkRegion = new Static4D(0,0,60,60);
84 a2cb31e9 Leszek Koltunski
      
85 7589635e Leszek Koltunski
    s0 = new Static1D(boobsSink);
86 a2cb31e9 Leszek Koltunski
      
87 40eef4b9 Leszek Koltunski
    Dynamic1D diSink = new Dynamic1D();
88 b9615af9 Leszek Koltunski
    diSink.add(s0);
89 3c8433ae Leszek Koltunski
90 5055b5d4 Leszek Koltunski
    mScale = 1.0f;
91
    mScaleDyn = new Dynamic3D();
92
    mScaleFactor = new Static3D(mScale,mScale,1.0f);
93
    mScaleDyn.add(mScaleFactor);
94 40eef4b9 Leszek Koltunski
95 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
96 392e16fd Leszek Koltunski
    mEffects.sink( diSink, pLeft , sinkRegion);
97
    mEffects.sink( diSink, pRight, sinkRegion);
98
99 d218d64e leszek
    mScreen = new DistortedScreen();
100 b9615af9 Leszek Koltunski
    }
101 a2cb31e9 Leszek Koltunski
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104 13a5c5cd Leszek Koltunski
  void setSize(float s)
105 e4237c21 Leszek Koltunski
    {
106
    boobsSink = s;
107
    s0.set(boobsSink);
108 b9615af9 Leszek Koltunski
    }
109 a2cb31e9 Leszek Koltunski
110 5055b5d4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
126 b9615af9 Leszek Koltunski
127 13a5c5cd Leszek Koltunski
  void Save()
128 b9615af9 Leszek Koltunski
    {
129 92040610 Leszek Koltunski
    if( !isSaving )
130 b9615af9 Leszek Koltunski
      {
131 e4237c21 Leszek Koltunski
      isSaving = true;
132 b9615af9 Leszek Koltunski
133 e4237c21 Leszek Koltunski
      File file;
134
      int lowestNotFound = 1;
135
136
      while(true)
137 a2cb31e9 Leszek Koltunski
        {
138 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
139
        file = new File(mPath);
140
141
        if( !file.exists() ) break;
142
143
        lowestNotFound++;
144 a2cb31e9 Leszek Koltunski
        }
145 b9615af9 Leszek Koltunski
      }
146
    }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
   
150
  public void onDrawFrame(GL10 glUnused)
151
    {
152 40eef4b9 Leszek Koltunski
    long time = System.currentTimeMillis();
153
154 3c8433ae Leszek Koltunski
    if( isSaving )  // render to an offscreen buffer and read pixels
155 e4237c21 Leszek Koltunski
      {
156 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
157
      mEffects.scale(mScaleFactor);
158 fe59d375 Leszek Koltunski
      mOffscreen.render(time);
159 5055b5d4 Leszek Koltunski
      applyMatrixEffects(scrWidth,scrHeight);
160 3c8433ae Leszek Koltunski
161 5055b5d4 Leszek Koltunski
      int fW =(int)(mScale*bmpWidth);
162
      int fH =(int)(mScale*bmpHeight);
163
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
164 e4237c21 Leszek Koltunski
      buf.order(ByteOrder.LITTLE_ENDIAN);
165
166 29246ab4 Leszek Koltunski
      int textureID = mOffscreen.getTextureID();
167
168
      if( textureID>=0 )
169
        {
170 41a81a14 Leszek Koltunski
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureID);
171
        GLES30.glReadPixels( 0, 0, fW, fH, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, buf);
172 29246ab4 Leszek Koltunski
        SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
173
        }
174
      else
175
        {
176 3f5b2cb3 leszek
        android.util.Log.e("Save", "Error trying to read from offscreen FBO, textureID="+textureID);
177 29246ab4 Leszek Koltunski
        }
178 e4237c21 Leszek Koltunski
179
      isSaving = false;
180
      }
181 5055b5d4 Leszek Koltunski
182 fe59d375 Leszek Koltunski
    mScreen.render(time);
183 b9615af9 Leszek Koltunski
    }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187 5055b5d4 Leszek Koltunski
  private void applyMatrixEffects(int width, int height)
188
    {
189 392e16fd Leszek Koltunski
    mEffects.abortEffects(EffectTypes.MATRIX);
190 5055b5d4 Leszek Koltunski
191
    if( (float)bmpHeight/bmpWidth > (float)height/width )
192 b9615af9 Leszek Koltunski
      {
193
      int w = (height*bmpWidth)/bmpHeight;
194 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
195
196 392e16fd Leszek Koltunski
      mEffects.move( new Static3D((width-w)/2,0,0) );
197
      mEffects.scale(factor);
198 a2cb31e9 Leszek Koltunski
      }
199 b9615af9 Leszek Koltunski
    else
200
      {
201
      int h = (width*bmpHeight)/bmpWidth;
202 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
203
204 392e16fd Leszek Koltunski
      mEffects.move( new Static3D(0,(height-h)/2,0) );
205
      mEffects.scale(factor);
206 b9615af9 Leszek Koltunski
      }
207 5055b5d4 Leszek Koltunski
208 392e16fd Leszek Koltunski
    mEffects.scale(mScaleDyn);
209 5055b5d4 Leszek Koltunski
    }
210
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
213
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
214
    {
215
    scrWidth = width;
216
    scrHeight= height;
217
    applyMatrixEffects(width, height);
218 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
219 b9615af9 Leszek Koltunski
    }
220 a2cb31e9 Leszek Koltunski
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
    
223 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
224
    {
225
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
226
    Bitmap bitmap;
227 6fd3af4d Leszek Koltunski
228 b9615af9 Leszek Koltunski
    try
229
      {
230
      bitmap = BitmapFactory.decodeStream(is);
231
      }
232
    finally
233
      {
234
      try
235 a2cb31e9 Leszek Koltunski
        {
236 b9615af9 Leszek Koltunski
        is.close();
237
        }
238
      catch(IOException e) { }
239
      }
240 a2cb31e9 Leszek Koltunski
      
241 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
242
    bmpWidth  = bitmap.getWidth();
243 e8b6aa95 Leszek Koltunski
244 fe59d375 Leszek Koltunski
    MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
245
    DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
246
    texture.setTexture(bitmap);
247 a2cb31e9 Leszek Koltunski
248 6fd3af4d Leszek Koltunski
    mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight) );
249 3c8433ae Leszek Koltunski
250 fe59d375 Leszek Koltunski
    mOffscreen.detachAll();
251
    mOffscreen.attach(texture,mEffects,mesh);
252
    mScreen.detachAll();
253
    mScreen.attach(texture,mEffects,mesh);
254
255 6637d0f2 Leszek Koltunski
    DistortedEffects.enableEffect(EffectNames.SINK);
256
257 b9615af9 Leszek Koltunski
    try
258
      {
259 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
260 b9615af9 Leszek Koltunski
      }
261
    catch(Exception ex)
262
      {
263
      android.util.Log.e("Renderer", ex.getMessage() );
264 a2cb31e9 Leszek Koltunski
      }
265 b9615af9 Leszek Koltunski
    }
266
  }