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.effect.MatrixEffectScale;
|
34
|
import org.distorted.library.effect.VertexEffectScale;
|
35
|
import org.distorted.library.effect.VertexEffectSink;
|
36
|
import org.distorted.library.main.DistortedLibrary;
|
37
|
import org.distorted.library.main.DistortedEffects;
|
38
|
import org.distorted.library.main.DistortedScreen;
|
39
|
import org.distorted.library.main.DistortedTexture;
|
40
|
import org.distorted.library.mesh.MeshSquare;
|
41
|
import org.distorted.library.main.DistortedFramebuffer;
|
42
|
import org.distorted.library.type.Dynamic1D;
|
43
|
import org.distorted.library.type.Static1D;
|
44
|
import org.distorted.library.type.Static3D;
|
45
|
import org.distorted.library.type.Static4D;
|
46
|
|
47
|
import android.app.ActivityManager;
|
48
|
import android.content.Context;
|
49
|
import android.content.pm.ConfigurationInfo;
|
50
|
import android.content.res.Resources;
|
51
|
import android.graphics.Bitmap;
|
52
|
import android.graphics.BitmapFactory;
|
53
|
import android.opengl.GLES31;
|
54
|
import android.opengl.GLSurfaceView;
|
55
|
import android.os.Environment;
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
class SaveRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
|
60
|
{
|
61
|
private final GLSurfaceView mView;
|
62
|
private final DistortedEffects mEffects;
|
63
|
private final DistortedScreen mScreen;
|
64
|
private final Static1D s0;
|
65
|
private final Static3D mScaleFactor, mScaleMain, mScaleVertex;
|
66
|
private final Resources mResources;
|
67
|
|
68
|
private DistortedFramebuffer mOffscreen;
|
69
|
private DistortedTexture mTexture;
|
70
|
private MeshSquare mMesh;
|
71
|
private float mScale;
|
72
|
private int bmpHeight, bmpWidth;
|
73
|
private int scrHeight, scrWidth;
|
74
|
private float boobsSink;
|
75
|
private boolean isSaving = false;
|
76
|
private String mPath;
|
77
|
|
78
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79
|
|
80
|
SaveRenderer(GLSurfaceView v)
|
81
|
{
|
82
|
mView = v;
|
83
|
mResources = v.getResources();
|
84
|
|
85
|
boobsSink = 1.0f;
|
86
|
|
87
|
Static3D pLeft = new Static3D(-68, 36, 0);
|
88
|
Static3D pRight= new Static3D( 47, 36, 0);
|
89
|
|
90
|
Static4D sinkRegion = new Static4D(0,0,0,60);
|
91
|
|
92
|
s0 = new Static1D(boobsSink);
|
93
|
|
94
|
Dynamic1D diSink = new Dynamic1D();
|
95
|
diSink.add(s0);
|
96
|
|
97
|
mScale = 1.0f;
|
98
|
mScaleFactor = new Static3D(mScale,mScale,1.0f);
|
99
|
mScaleMain = new Static3D(1,1,1);
|
100
|
mScaleVertex = new Static3D(1,1,1);
|
101
|
|
102
|
mEffects = new DistortedEffects();
|
103
|
mEffects.apply( new VertexEffectScale(mScaleVertex));
|
104
|
mEffects.apply( new VertexEffectSink(diSink, pLeft , sinkRegion) );
|
105
|
mEffects.apply( new VertexEffectSink(diSink, pRight, sinkRegion) );
|
106
|
mEffects.apply( new MatrixEffectScale(mScaleMain));
|
107
|
mEffects.apply( new MatrixEffectScale(mScaleFactor));
|
108
|
|
109
|
mScreen = new DistortedScreen();
|
110
|
}
|
111
|
|
112
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
113
|
|
114
|
void setSize(float s)
|
115
|
{
|
116
|
boobsSink = s;
|
117
|
s0.set(boobsSink);
|
118
|
}
|
119
|
|
120
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
121
|
|
122
|
void setScale(float s)
|
123
|
{
|
124
|
mScale = s;
|
125
|
mScaleFactor.set(s,s,1.0f);
|
126
|
|
127
|
// when we move our scroll bar, this does NOT keep allocating and deallocating
|
128
|
// the whole WxH texture - the allocation happens only once, on next render, i.e. -
|
129
|
// when one presses the 'SAVE' button.
|
130
|
|
131
|
if( mOffscreen!=null )
|
132
|
mOffscreen.resize( (int)(mScale*bmpWidth),(int)(mScale*bmpHeight));
|
133
|
}
|
134
|
|
135
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
|
137
|
void Save()
|
138
|
{
|
139
|
if( !isSaving )
|
140
|
{
|
141
|
isSaving = true;
|
142
|
|
143
|
File file;
|
144
|
int lowestNotFound = 1;
|
145
|
|
146
|
while(true)
|
147
|
{
|
148
|
mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/girl" + lowestNotFound +".png";
|
149
|
file = new File(mPath);
|
150
|
|
151
|
if( !file.exists() ) break;
|
152
|
|
153
|
lowestNotFound++;
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public void onDrawFrame(GL10 glUnused)
|
161
|
{
|
162
|
if( isSaving ) // render to an offscreen buffer and read pixels
|
163
|
{
|
164
|
mScaleMain.set(1,1,1);
|
165
|
mOffscreen.render(System.currentTimeMillis());
|
166
|
applyMatrixEffects(scrWidth,scrHeight);
|
167
|
|
168
|
int fW =(int)(mScale*bmpWidth);
|
169
|
int fH =(int)(mScale*bmpHeight);
|
170
|
ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
|
171
|
buf.order(ByteOrder.LITTLE_ENDIAN);
|
172
|
|
173
|
int textureID = mOffscreen.getTextureID();
|
174
|
|
175
|
if( textureID>=0 )
|
176
|
{
|
177
|
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, textureID);
|
178
|
GLES31.glReadPixels( 0, 0, fW, fH, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, buf);
|
179
|
SaveWorkerThread.newBuffer(buf,fW,fH,mPath);
|
180
|
}
|
181
|
else
|
182
|
{
|
183
|
android.util.Log.e("Save", "Error trying to read from offscreen FBO, textureID="+textureID);
|
184
|
}
|
185
|
isSaving = false;
|
186
|
}
|
187
|
|
188
|
// Quite subtle issue here. Since we share the DistortedEffects object between the mOffscreen
|
189
|
// and mScreen surfaces, we cannot render both of them in one go using the same time. That
|
190
|
// would make the second render, in this case mScreen's render, re-use the values of the
|
191
|
// Dynamics calculated the first time around and we don't want that (mOffscreen's scale and move
|
192
|
// are different! What would happen is the mScreen would be - one render when we are 'saving' -
|
193
|
// get drawn with different dimensions and on-screen position, i.e. we would see a flash.
|
194
|
|
195
|
mScreen.render(System.currentTimeMillis());
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
private void applyMatrixEffects(int width, int height)
|
201
|
{
|
202
|
float horiRatio = (float)width / mScaleVertex.get0();
|
203
|
float vertRatio = (float)height/ mScaleVertex.get1();
|
204
|
float factor = Math.min(horiRatio, vertRatio);
|
205
|
mScaleMain.set(factor,factor,factor);
|
206
|
}
|
207
|
|
208
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
209
|
|
210
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
211
|
{
|
212
|
scrWidth = width;
|
213
|
scrHeight= height;
|
214
|
applyMatrixEffects(width, height);
|
215
|
mScreen.resize(width, height);
|
216
|
}
|
217
|
|
218
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
219
|
|
220
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
221
|
{
|
222
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
|
223
|
Bitmap bitmap;
|
224
|
|
225
|
try
|
226
|
{
|
227
|
bitmap = BitmapFactory.decodeStream(is);
|
228
|
}
|
229
|
finally
|
230
|
{
|
231
|
try
|
232
|
{
|
233
|
is.close();
|
234
|
}
|
235
|
catch(IOException ignored) { }
|
236
|
}
|
237
|
|
238
|
bmpHeight = bitmap.getHeight();
|
239
|
bmpWidth = bitmap.getWidth();
|
240
|
|
241
|
mScaleVertex.set(bmpWidth,bmpHeight,0);
|
242
|
|
243
|
if( mMesh ==null ) mMesh = new MeshSquare(30,30*bmpHeight/bmpWidth);
|
244
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
245
|
mTexture.setTexture(bitmap);
|
246
|
|
247
|
if( mOffscreen==null ) mOffscreen = new DistortedFramebuffer( (int)(mScale*bmpWidth) , (int)(mScale*bmpHeight),
|
248
|
1, DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
|
249
|
|
250
|
mOffscreen.detachAll();
|
251
|
mOffscreen.attach(mTexture,mEffects,mMesh);
|
252
|
mScreen.detachAll();
|
253
|
mScreen.attach(mTexture,mEffects,mMesh);
|
254
|
|
255
|
VertexEffectScale.enable();
|
256
|
VertexEffectSink.enable();
|
257
|
|
258
|
DistortedLibrary.onSurfaceCreated(this);
|
259
|
}
|
260
|
|
261
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
262
|
|
263
|
public void distortedException(Exception ex)
|
264
|
{
|
265
|
android.util.Log.e("Save", ex.getMessage() );
|
266
|
}
|
267
|
|
268
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
269
|
|
270
|
public int openGlVersion()
|
271
|
{
|
272
|
Context context = mView.getContext();
|
273
|
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
274
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
275
|
int glESversion = configurationInfo.reqGlEsVersion;
|
276
|
int major = glESversion >> 16;
|
277
|
int minor = glESversion & 0xff;
|
278
|
|
279
|
return 100*major + 10*minor;
|
280
|
}
|
281
|
|
282
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
283
|
|
284
|
public InputStream localFile(int fileID)
|
285
|
{
|
286
|
return mResources.openRawResource(fileID);
|
287
|
}
|
288
|
|
289
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290
|
|
291
|
public void logMessage(String message)
|
292
|
{
|
293
|
android.util.Log.e("Save", message );
|
294
|
}
|
295
|
}
|