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