Project

General

Profile

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

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

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 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
38 3c8433ae Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
39 a2cb31e9 Leszek Koltunski
import org.distorted.library.EffectTypes;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
41 5055b5d4 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
42 7589635e Leszek Koltunski
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 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
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 bc29e409 Leszek Koltunski
  private DistortedTexture mTexture;
58 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
59 b01acdaf Leszek Koltunski
  private MeshFlat mMesh;
60 d218d64e leszek
  private DistortedFramebuffer mOffscreen;
61
  private DistortedScreen mScreen;
62 13a5c5cd Leszek Koltunski
  private Static1D s0;
63 5055b5d4 Leszek Koltunski
  private Dynamic3D mScaleDyn;
64
  private Static3D mScaleFactor;
65 b9615af9 Leszek Koltunski
66 5055b5d4 Leszek Koltunski
  private float mScale;
67 b9615af9 Leszek Koltunski
  private int bmpHeight, bmpWidth;
68 13a5c5cd Leszek Koltunski
  private int scrHeight, scrWidth;
69
  private float boobsSink;
70
  private boolean isSaving = false;
71
  private String mPath;
72 b9615af9 Leszek Koltunski
73 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 13a5c5cd Leszek Koltunski
  SaveRenderer(GLSurfaceView v)
76 b9615af9 Leszek Koltunski
    {
77
    mView = v;
78 a2cb31e9 Leszek Koltunski
      
79 b9615af9 Leszek Koltunski
    boobsSink  = 1.0f;
80 a2cb31e9 Leszek Koltunski
      
81 40eef4b9 Leszek Koltunski
    Static3D pLeft = new Static3D(132, 264, 0);
82
    Static3D pRight= new Static3D(247, 264, 0);
83 a2cb31e9 Leszek Koltunski
      
84 40eef4b9 Leszek Koltunski
    Static4D sinkRegion = new Static4D(0,0,60,60);
85 a2cb31e9 Leszek Koltunski
      
86 7589635e Leszek Koltunski
    s0 = new Static1D(boobsSink);
87 a2cb31e9 Leszek Koltunski
      
88 40eef4b9 Leszek Koltunski
    Dynamic1D diSink = new Dynamic1D();
89 b9615af9 Leszek Koltunski
    diSink.add(s0);
90 3c8433ae Leszek Koltunski
91 5055b5d4 Leszek Koltunski
    mScale = 1.0f;
92
    mScaleDyn = new Dynamic3D();
93
    mScaleFactor = new Static3D(mScale,mScale,1.0f);
94
    mScaleDyn.add(mScaleFactor);
95 40eef4b9 Leszek Koltunski
96 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
97 392e16fd Leszek Koltunski
    mEffects.sink( diSink, pLeft , sinkRegion);
98
    mEffects.sink( diSink, pRight, sinkRegion);
99
100 d218d64e leszek
    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 41a81a14 Leszek Koltunski
    GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
154 e4237c21 Leszek Koltunski
155 40eef4b9 Leszek Koltunski
    long time = System.currentTimeMillis();
156
157 3c8433ae Leszek Koltunski
    if( isSaving )  // render to an offscreen buffer and read pixels
158 e4237c21 Leszek Koltunski
      {
159 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
160
      mEffects.scale(mScaleFactor);
161 b01acdaf Leszek Koltunski
      mOffscreen.renderTo(mTexture, mMesh, mEffects, time );
162 5055b5d4 Leszek Koltunski
      applyMatrixEffects(scrWidth,scrHeight);
163 3c8433ae Leszek Koltunski
164 5055b5d4 Leszek Koltunski
      int fW =(int)(mScale*bmpWidth);
165
      int fH =(int)(mScale*bmpHeight);
166
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
167 e4237c21 Leszek Koltunski
      buf.order(ByteOrder.LITTLE_ENDIAN);
168
169 29246ab4 Leszek Koltunski
      int textureID = mOffscreen.getTextureID();
170
171
      if( textureID>=0 )
172
        {
173 41a81a14 Leszek Koltunski
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureID);
174
        GLES30.glReadPixels( 0, 0, fW, fH, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, buf);
175 29246ab4 Leszek Koltunski
        SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
176
        }
177
      else
178
        {
179
        android.util.Log.e("Save", "Error trying to read from offscreen FBO");
180
        }
181 e4237c21 Leszek Koltunski
182
      isSaving = false;
183
      }
184 5055b5d4 Leszek Koltunski
185 b01acdaf Leszek Koltunski
    mScreen.renderTo(mTexture, mMesh, mEffects, time );
186 b9615af9 Leszek Koltunski
    }
187
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
190 5055b5d4 Leszek Koltunski
  private void applyMatrixEffects(int width, int height)
191
    {
192 392e16fd Leszek Koltunski
    mEffects.abortEffects(EffectTypes.MATRIX);
193 5055b5d4 Leszek Koltunski
194
    if( (float)bmpHeight/bmpWidth > (float)height/width )
195 b9615af9 Leszek Koltunski
      {
196
      int w = (height*bmpWidth)/bmpHeight;
197 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
198
199 392e16fd Leszek Koltunski
      mEffects.move( new Static3D((width-w)/2,0,0) );
200
      mEffects.scale(factor);
201 a2cb31e9 Leszek Koltunski
      }
202 b9615af9 Leszek Koltunski
    else
203
      {
204
      int h = (width*bmpHeight)/bmpWidth;
205 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
206
207 392e16fd Leszek Koltunski
      mEffects.move( new Static3D(0,(height-h)/2,0) );
208
      mEffects.scale(factor);
209 b9615af9 Leszek Koltunski
      }
210 5055b5d4 Leszek Koltunski
211 392e16fd Leszek Koltunski
    mEffects.scale(mScaleDyn);
212 5055b5d4 Leszek Koltunski
    }
213
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
216
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
217
    {
218
    scrWidth = width;
219
    scrHeight= height;
220
    applyMatrixEffects(width, height);
221 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
222 b9615af9 Leszek Koltunski
    }
223 a2cb31e9 Leszek Koltunski
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
    
226 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
227
    {
228 41a81a14 Leszek Koltunski
    GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
229 e7a4ef16 Leszek Koltunski
230 b9615af9 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
231
    Bitmap bitmap;
232 6fd3af4d Leszek Koltunski
233 b9615af9 Leszek Koltunski
    try
234
      {
235
      bitmap = BitmapFactory.decodeStream(is);
236
      }
237
    finally
238
      {
239
      try
240 a2cb31e9 Leszek Koltunski
        {
241 b9615af9 Leszek Koltunski
        is.close();
242
        }
243
      catch(IOException e) { }
244
      }
245 a2cb31e9 Leszek Koltunski
      
246 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
247
    bmpWidth  = bitmap.getWidth();
248 e8b6aa95 Leszek Koltunski
249 b01acdaf Leszek Koltunski
    mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
250 bc29e409 Leszek Koltunski
    mTexture = new DistortedTexture(bmpWidth,bmpHeight);
251
    mTexture.setTexture(bitmap);
252 a2cb31e9 Leszek Koltunski
253 6fd3af4d Leszek Koltunski
    mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight) );
254 3c8433ae Leszek Koltunski
255 b9615af9 Leszek Koltunski
    try
256
      {
257 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
258 b9615af9 Leszek Koltunski
      }
259
    catch(Exception ex)
260
      {
261
      android.util.Log.e("Renderer", ex.getMessage() );
262 a2cb31e9 Leszek Koltunski
      }
263 b9615af9 Leszek Koltunski
    }
264
  }