Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 627e0ea2

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.MatrixEffectScale;
34 8dfa45c4 leszek
import org.distorted.library.effect.VertexEffectSink;
35 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
40 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedFramebuffer;
41 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
42
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45 a2cb31e9 Leszek Koltunski
46
import android.graphics.Bitmap;
47
import android.graphics.BitmapFactory;
48 41a85bb7 Leszek Koltunski
import android.opengl.GLES31;
49 a2cb31e9 Leszek Koltunski
import android.opengl.GLSurfaceView;
50 7cfeeb63 Leszek Koltunski
import android.os.Environment;
51 a2cb31e9 Leszek Koltunski
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 e4237c21 Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer
55 b9615af9 Leszek Koltunski
  {
56
  private GLSurfaceView mView;
57 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
58 d218d64e leszek
  private DistortedFramebuffer mOffscreen;
59 59540ba2 Leszek Koltunski
  private DistortedTexture mTexture;
60 f3ca895f Leszek Koltunski
  private MeshRectangles mMesh;
61 d218d64e leszek
  private DistortedScreen mScreen;
62 13a5c5cd Leszek Koltunski
  private Static1D s0;
63 0de83d97 Leszek Koltunski
  private Static3D mScaleFactor, mScaleMain;
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 627e0ea2 Leszek Koltunski
80
    Static3D pLeft = new Static3D(-68, 36, 0);
81
    Static3D pRight= new Static3D( 47, 36, 0);
82
83 9e7b6dbd Leszek Koltunski
    Static4D sinkRegion = new Static4D(0,0,0,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
    mScaleFactor = new Static3D(mScale,mScale,1.0f);
92 06cd94ba Leszek Koltunski
    mScaleMain = new Static3D(1,1,1);
93 40eef4b9 Leszek Koltunski
94 698ad0a8 Leszek Koltunski
    mEffects = new DistortedEffects();
95 06cd94ba Leszek Koltunski
    mEffects.apply( new VertexEffectSink(diSink, pLeft , sinkRegion) );
96
    mEffects.apply( new VertexEffectSink(diSink, pRight, sinkRegion) );
97
    mEffects.apply( new MatrixEffectScale(mScaleMain));
98
    mEffects.apply( new MatrixEffectScale(mScaleFactor));
99 392e16fd Leszek Koltunski
100 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
101 b9615af9 Leszek Koltunski
    }
102 a2cb31e9 Leszek Koltunski
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
105 13a5c5cd Leszek Koltunski
  void setSize(float s)
106 e4237c21 Leszek Koltunski
    {
107
    boobsSink = s;
108
    s0.set(boobsSink);
109 b9615af9 Leszek Koltunski
    }
110 a2cb31e9 Leszek Koltunski
111 5055b5d4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
112
113
  void setScale(float s)
114
    {
115
    mScale = s;
116
    mScaleFactor.set(s,s,1.0f);
117
118
    // when we move our scroll bar, this does NOT keep allocating and deallocating
119
    // the whole WxH texture - the allocation happens only once, on next render, i.e. -
120
    // when one presses the 'SAVE' button.
121
122
    if( mOffscreen!=null )
123
      mOffscreen.resize( (int)(mScale*bmpWidth),(int)(mScale*bmpHeight));
124
    }
125
126 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
127 b9615af9 Leszek Koltunski
128 13a5c5cd Leszek Koltunski
  void Save()
129 b9615af9 Leszek Koltunski
    {
130 92040610 Leszek Koltunski
    if( !isSaving )
131 b9615af9 Leszek Koltunski
      {
132 e4237c21 Leszek Koltunski
      isSaving = true;
133 b9615af9 Leszek Koltunski
134 e4237c21 Leszek Koltunski
      File file;
135
      int lowestNotFound = 1;
136
137
      while(true)
138 a2cb31e9 Leszek Koltunski
        {
139 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
140
        file = new File(mPath);
141
142
        if( !file.exists() ) break;
143
144
        lowestNotFound++;
145 a2cb31e9 Leszek Koltunski
        }
146 b9615af9 Leszek Koltunski
      }
147
    }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
   
151
  public void onDrawFrame(GL10 glUnused)
152
    {
153 3c8433ae Leszek Koltunski
    if( isSaving )  // render to an offscreen buffer and read pixels
154 e4237c21 Leszek Koltunski
      {
155 06cd94ba Leszek Koltunski
      mScaleMain.set(1,1,1);
156 d8b42d02 Leszek Koltunski
      mOffscreen.render(System.currentTimeMillis());
157 5055b5d4 Leszek Koltunski
      applyMatrixEffects(scrWidth,scrHeight);
158 3c8433ae Leszek Koltunski
159 5055b5d4 Leszek Koltunski
      int fW =(int)(mScale*bmpWidth);
160
      int fH =(int)(mScale*bmpHeight);
161
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
162 e4237c21 Leszek Koltunski
      buf.order(ByteOrder.LITTLE_ENDIAN);
163
164 29246ab4 Leszek Koltunski
      int textureID = mOffscreen.getTextureID();
165
166
      if( textureID>=0 )
167
        {
168 41a85bb7 Leszek Koltunski
        GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, textureID);
169
        GLES31.glReadPixels( 0, 0, fW, fH, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, buf);
170 29246ab4 Leszek Koltunski
        SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
171
        }
172
      else
173
        {
174 3f5b2cb3 leszek
        android.util.Log.e("Save", "Error trying to read from offscreen FBO, textureID="+textureID);
175 29246ab4 Leszek Koltunski
        }
176 e4237c21 Leszek Koltunski
      isSaving = false;
177
      }
178 5055b5d4 Leszek Koltunski
179 d8b42d02 Leszek Koltunski
    // Quite subtle issue here. Since we share the DistortedEffects object between the mOffscreen
180
    // and mScreen surfaces, we cannot render both of them in one go using the same time. That
181
    // would make the second render, in this case mScreen's render, re-use the values of the
182
    // Dynamics calculated the first time around and we don't want that (mOffscreen's scale and move
183
    // are different! What would happen is the mScreen would be - one render when we are 'saving' -
184
    // get drawn with different dimensions and on-screen position, i.e. we would see a flash.
185
186
    mScreen.render(System.currentTimeMillis());
187 b9615af9 Leszek Koltunski
    }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191 5055b5d4 Leszek Koltunski
  private void applyMatrixEffects(int width, int height)
192
    {
193 0de83d97 Leszek Koltunski
    float horiRatio = (float)width / mTexture.getWidth();
194
    float vertRatio = (float)height/ mTexture.getHeight();
195
    float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
196
    mScaleMain.set(factor,factor,factor);
197 5055b5d4 Leszek Koltunski
    }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
202
    {
203
    scrWidth = width;
204
    scrHeight= height;
205
    applyMatrixEffects(width, height);
206 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
207 b9615af9 Leszek Koltunski
    }
208 a2cb31e9 Leszek Koltunski
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
    
211 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
212
    {
213
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
214
    Bitmap bitmap;
215 6fd3af4d Leszek Koltunski
216 b9615af9 Leszek Koltunski
    try
217
      {
218
      bitmap = BitmapFactory.decodeStream(is);
219
      }
220
    finally
221
      {
222
      try
223 a2cb31e9 Leszek Koltunski
        {
224 b9615af9 Leszek Koltunski
        is.close();
225
        }
226
      catch(IOException e) { }
227
      }
228 a2cb31e9 Leszek Koltunski
      
229 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
230
    bmpWidth  = bitmap.getWidth();
231 e8b6aa95 Leszek Koltunski
232 698ad0a8 Leszek Koltunski
    if( mMesh==null )
233
      {
234
      mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
235
      mMesh.setStretch(bmpWidth,bmpHeight,0);
236
      }
237 687263cc Leszek Koltunski
    if( mTexture==null ) mTexture = new DistortedTexture();
238 59540ba2 Leszek Koltunski
    mTexture.setTexture(bitmap);
239 a2cb31e9 Leszek Koltunski
240 f5c28afc Leszek Koltunski
    if( mOffscreen==null ) mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight),
241 a929d64d Leszek Koltunski
                                                                  1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
242 3c8433ae Leszek Koltunski
243 fe59d375 Leszek Koltunski
    mOffscreen.detachAll();
244 5f2a53b6 leszek
    mOffscreen.attach(mTexture,mEffects,mMesh);
245 fe59d375 Leszek Koltunski
    mScreen.detachAll();
246 5f2a53b6 leszek
    mScreen.attach(mTexture,mEffects,mMesh);
247 fe59d375 Leszek Koltunski
248 885b9cca leszek
    VertexEffectSink.enable();
249 6637d0f2 Leszek Koltunski
250 b9615af9 Leszek Koltunski
    try
251
      {
252 e3900503 Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext());
253 b9615af9 Leszek Koltunski
      }
254
    catch(Exception ex)
255
      {
256
      android.util.Log.e("Renderer", ex.getMessage() );
257 a2cb31e9 Leszek Koltunski
      }
258 b9615af9 Leszek Koltunski
    }
259
  }