Project

General

Profile

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

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

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