Project

General

Profile

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

examples / src / main / java / org / distorted / examples / save / SaveRenderer.java @ 10b7e588

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 e8b6aa95 Leszek Koltunski
import org.distorted.library.DistortedObject;
35 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
36 3c8433ae Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
37 a2cb31e9 Leszek Koltunski
import org.distorted.library.EffectTypes;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
39 5055b5d4 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43 a2cb31e9 Leszek Koltunski
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLES20;
47
import android.opengl.GLSurfaceView;
48 7cfeeb63 Leszek Koltunski
import android.os.Environment;
49 a2cb31e9 Leszek Koltunski
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52 e4237c21 Leszek Koltunski
class SaveRenderer implements GLSurfaceView.Renderer
53 b9615af9 Leszek Koltunski
  {
54
  private GLSurfaceView mView;
55 e8b6aa95 Leszek Koltunski
  private DistortedObject mGirl;
56 10b7e588 Leszek Koltunski
  private GridFlat mGrid;
57 3c8433ae Leszek Koltunski
  private DistortedFramebuffer mOffscreen;
58 334c13fa Leszek Koltunski
  private Static3D pLeft, pRight;
59 7589635e Leszek Koltunski
  private Static4D sinkRegion;
60 13a5c5cd Leszek Koltunski
  private Dynamic1D diSink;
61
  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 334c13fa Leszek Koltunski
    pLeft = new Static3D(132, 264, 0);
81
    pRight= new Static3D(247, 264, 0);
82 a2cb31e9 Leszek Koltunski
      
83 7589635e Leszek Koltunski
    sinkRegion = new Static4D(0,0,60,60);
84 a2cb31e9 Leszek Koltunski
      
85 7589635e Leszek Koltunski
    s0 = new Static1D(boobsSink);
86 a2cb31e9 Leszek Koltunski
      
87 5055b5d4 Leszek Koltunski
    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 b9615af9 Leszek Koltunski
    }
95 a2cb31e9 Leszek Koltunski
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
98 13a5c5cd Leszek Koltunski
  void setSize(float s)
99 e4237c21 Leszek Koltunski
    {
100
    boobsSink = s;
101
    s0.set(boobsSink);
102 b9615af9 Leszek Koltunski
    }
103 a2cb31e9 Leszek Koltunski
104 5055b5d4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
  void setScale(float s)
107
    {
108
    mScale = s;
109
    mScaleFactor.set(s,s,1.0f);
110
111
    // when we move our scroll bar, this does NOT keep allocating and deallocating
112
    // the whole WxH texture - the allocation happens only once, on next render, i.e. -
113
    // when one presses the 'SAVE' button.
114
115
    if( mOffscreen!=null )
116
      mOffscreen.resize( (int)(mScale*bmpWidth),(int)(mScale*bmpHeight));
117
    }
118
119 a2cb31e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120 b9615af9 Leszek Koltunski
121 13a5c5cd Leszek Koltunski
  void Save()
122 b9615af9 Leszek Koltunski
    {
123 92040610 Leszek Koltunski
    if( !isSaving )
124 b9615af9 Leszek Koltunski
      {
125 e4237c21 Leszek Koltunski
      isSaving = true;
126 b9615af9 Leszek Koltunski
127 e4237c21 Leszek Koltunski
      File file;
128
      int lowestNotFound = 1;
129
130
      while(true)
131 a2cb31e9 Leszek Koltunski
        {
132 e4237c21 Leszek Koltunski
        mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
133
        file = new File(mPath);
134
135
        if( !file.exists() ) break;
136
137
        lowestNotFound++;
138 a2cb31e9 Leszek Koltunski
        }
139 b9615af9 Leszek Koltunski
      }
140
    }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
   
144
  public void onDrawFrame(GL10 glUnused)
145
    {
146
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
147 e4237c21 Leszek Koltunski
148 3c8433ae Leszek Koltunski
    if( isSaving )  // render to an offscreen buffer and read pixels
149 e4237c21 Leszek Koltunski
      {
150 5055b5d4 Leszek Koltunski
      mGirl.abortEffects(EffectTypes.MATRIX);
151
      mGirl.scale(mScaleFactor);
152 e8b6aa95 Leszek Koltunski
      mGirl.draw(System.currentTimeMillis(), mGrid, mOffscreen);
153 5055b5d4 Leszek Koltunski
      applyMatrixEffects(scrWidth,scrHeight);
154 3c8433ae Leszek Koltunski
155 5055b5d4 Leszek Koltunski
      int fW =(int)(mScale*bmpWidth);
156
      int fH =(int)(mScale*bmpHeight);
157
      ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
158 e4237c21 Leszek Koltunski
      buf.order(ByteOrder.LITTLE_ENDIAN);
159 3c8433ae Leszek Koltunski
      mOffscreen.setAsInput();
160 e4237c21 Leszek Koltunski
161 5055b5d4 Leszek Koltunski
      GLES20.glReadPixels( 0, 0, fW, fH, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
162
      SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
163 e4237c21 Leszek Koltunski
164
      isSaving = false;
165
      }
166 5055b5d4 Leszek Koltunski
167 e8b6aa95 Leszek Koltunski
    mGirl.draw(System.currentTimeMillis(), mGrid);
168 b9615af9 Leszek Koltunski
    }
169
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172 5055b5d4 Leszek Koltunski
  private void applyMatrixEffects(int width, int height)
173
    {
174 b9615af9 Leszek Koltunski
    mGirl.abortEffects(EffectTypes.MATRIX);
175 5055b5d4 Leszek Koltunski
176
    if( (float)bmpHeight/bmpWidth > (float)height/width )
177 b9615af9 Leszek Koltunski
      {
178
      int w = (height*bmpWidth)/bmpHeight;
179 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
180
181
      mGirl.move( new Static3D((width-w)/2,0,0) );
182 f988589e Leszek Koltunski
      mGirl.scale(factor);
183 a2cb31e9 Leszek Koltunski
      }
184 b9615af9 Leszek Koltunski
    else
185
      {
186
      int h = (width*bmpHeight)/bmpWidth;
187 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
188
189
      mGirl.move( new Static3D(0,(height-h)/2,0) );
190 f988589e Leszek Koltunski
      mGirl.scale(factor);
191 b9615af9 Leszek Koltunski
      }
192 5055b5d4 Leszek Koltunski
193
    mGirl.scale(mScaleDyn);
194
    }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
199
    {
200
    scrWidth = width;
201
    scrHeight= height;
202
    applyMatrixEffects(width, height);
203 b9615af9 Leszek Koltunski
    Distorted.onSurfaceChanged(width, height);
204
    }
205 a2cb31e9 Leszek Koltunski
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
    
208 b9615af9 Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
209
    {
210 e7a4ef16 Leszek Koltunski
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
211
212 b9615af9 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
213
    Bitmap bitmap;
214 a2cb31e9 Leszek Koltunski
        
215 b9615af9 Leszek Koltunski
    try
216
      {
217
      bitmap = BitmapFactory.decodeStream(is);
218
      }
219
    finally
220
      {
221
      try
222 a2cb31e9 Leszek Koltunski
        {
223 b9615af9 Leszek Koltunski
        is.close();
224
        }
225
      catch(IOException e) { }
226
      }
227 a2cb31e9 Leszek Koltunski
      
228 b9615af9 Leszek Koltunski
    bmpHeight = bitmap.getHeight();
229
    bmpWidth  = bitmap.getWidth();
230 e8b6aa95 Leszek Koltunski
231 10b7e588 Leszek Koltunski
    mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
232 e8b6aa95 Leszek Koltunski
    mGirl = new DistortedObject(bmpWidth,bmpHeight,1);
233
    mGirl.setTexture(bitmap);
234 a2cb31e9 Leszek Koltunski
235 7bf107f7 Leszek Koltunski
    mGirl.sink( diSink, pLeft , sinkRegion);
236
    mGirl.sink( diSink, pRight, sinkRegion);
237 a2cb31e9 Leszek Koltunski
238 5055b5d4 Leszek Koltunski
    mOffscreen = new DistortedFramebuffer(bmpWidth,bmpHeight);
239 3c8433ae Leszek Koltunski
240 b9615af9 Leszek Koltunski
    try
241
      {
242
      Distorted.onSurfaceCreated(mView.getContext());
243
      }
244
    catch(Exception ex)
245
      {
246
      android.util.Log.e("Renderer", ex.getMessage() );
247 a2cb31e9 Leszek Koltunski
      }
248 b9615af9 Leszek Koltunski
    }
249
  }