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