1 |
4e2382f3
|
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 |
|
|
|
20 |
|
|
package org.distorted.library;
|
21 |
|
|
|
22 |
|
|
import android.graphics.Bitmap;
|
23 |
|
|
import android.graphics.Matrix;
|
24 |
|
|
import android.opengl.GLES20;
|
25 |
|
|
import android.opengl.GLUtils;
|
26 |
|
|
|
27 |
1942537e
|
Leszek Koltunski
|
import java.util.Iterator;
|
28 |
|
|
import java.util.LinkedList;
|
29 |
4e2382f3
|
Leszek Koltunski
|
|
30 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
31 |
e0b6c593
|
Leszek Koltunski
|
/**
|
32 |
|
|
* Class which represents a OpenGL Texture object.
|
33 |
|
|
* <p>
|
34 |
|
|
* Create a Texture of arbitrary size and feed some pixels to it via the setTexture() method.
|
35 |
|
|
* <p>
|
36 |
|
|
* Keep all objects created in a static LinkedList. The point: we need to be able to mark
|
37 |
|
|
* Textures for deletion, and delete all marked objects later at a convenient time (that's
|
38 |
|
|
* because we can only delete from a thread that holds the OpenGL context so here we provide a
|
39 |
|
|
* framework where one is able to mark for deletion at any time and actual deletion takes place
|
40 |
|
|
* on the next render).
|
41 |
|
|
*/
|
42 |
4e2382f3
|
Leszek Koltunski
|
public class DistortedTexture
|
43 |
|
|
{
|
44 |
1942537e
|
Leszek Koltunski
|
private static boolean mListMarked = false;
|
45 |
|
|
private static LinkedList<DistortedTexture> mList = new LinkedList<>();
|
46 |
4e2382f3
|
Leszek Koltunski
|
|
47 |
f8686932
|
Leszek Koltunski
|
private static int mTextureH;
|
48 |
|
|
|
49 |
3ef3364d
|
Leszek Koltunski
|
private int mSizeX, mSizeY; // in screen space
|
50 |
|
|
float mHalfX, mHalfY; // halves of the above
|
51 |
4e2382f3
|
Leszek Koltunski
|
private long mID;
|
52 |
1942537e
|
Leszek Koltunski
|
private boolean mMarked;
|
53 |
4e2382f3
|
Leszek Koltunski
|
|
54 |
e7a20702
|
Leszek Koltunski
|
private Bitmap mBmp= null;
|
55 |
|
|
private int[] mTextureDataH;
|
56 |
|
|
private boolean mBitmapSet;
|
57 |
4e2382f3
|
Leszek Koltunski
|
|
58 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59 |
|
|
// We have to flip vertically every single Bitmap that we get fed with.
|
60 |
|
|
//
|
61 |
|
|
// Reason: textures read from files are the only objects in OpenGL which have their origins at the
|
62 |
|
|
// upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
|
63 |
|
|
// The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
|
64 |
|
|
// i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
|
65 |
421c2728
|
Leszek Koltunski
|
// then take the FBO and render to screen, (DistortedTree does so!) things get inverted as textures
|
66 |
4e2382f3
|
Leszek Koltunski
|
// created from FBO have their origins in the lower-left... Mindfuck!
|
67 |
|
|
|
68 |
|
|
private static Bitmap flipBitmap(Bitmap src)
|
69 |
|
|
{
|
70 |
|
|
Matrix matrix = new Matrix();
|
71 |
|
|
matrix.preScale(1.0f,-1.0f);
|
72 |
|
|
|
73 |
|
|
return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77 |
1942537e
|
Leszek Koltunski
|
// must be called from a thread holding OpenGL Context
|
78 |
4e2382f3
|
Leszek Koltunski
|
|
79 |
1942537e
|
Leszek Koltunski
|
void createTexture()
|
80 |
4e2382f3
|
Leszek Koltunski
|
{
|
81 |
e7a20702
|
Leszek Koltunski
|
if( mBmp!=null && mTextureDataH!=null )
|
82 |
4e2382f3
|
Leszek Koltunski
|
{
|
83 |
1942537e
|
Leszek Koltunski
|
//android.util.Log.e("Texture", "creating "+mID);
|
84 |
|
|
|
85 |
05403bba
|
Leszek Koltunski
|
if( mTextureDataH[0]==0 )
|
86 |
|
|
{
|
87 |
|
|
GLES20.glGenTextures(1, mTextureDataH, 0);
|
88 |
|
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
|
89 |
|
|
GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR );
|
90 |
|
|
GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR );
|
91 |
|
|
GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE );
|
92 |
|
|
GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE );
|
93 |
|
|
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(mBmp), 0);
|
94 |
|
|
}
|
95 |
|
|
else
|
96 |
|
|
{
|
97 |
|
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
|
98 |
|
|
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0,0,0,flipBitmap(mBmp));
|
99 |
|
|
}
|
100 |
4e2382f3
|
Leszek Koltunski
|
|
101 |
e7a20702
|
Leszek Koltunski
|
mBmp = null;
|
102 |
4e2382f3
|
Leszek Koltunski
|
}
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106 |
1942537e
|
Leszek Koltunski
|
// must be called from a thread holding OpenGL Context
|
107 |
4e2382f3
|
Leszek Koltunski
|
|
108 |
1942537e
|
Leszek Koltunski
|
private void deleteTexture()
|
109 |
4e2382f3
|
Leszek Koltunski
|
{
|
110 |
1942537e
|
Leszek Koltunski
|
if( mTextureDataH!=null && mTextureDataH[0]>0 )
|
111 |
|
|
{
|
112 |
|
|
//android.util.Log.e("Texture", "deleting "+mID);
|
113 |
4e2382f3
|
Leszek Koltunski
|
|
114 |
1942537e
|
Leszek Koltunski
|
GLES20.glDeleteTextures(1, mTextureDataH, 0);
|
115 |
4e2382f3
|
Leszek Koltunski
|
|
116 |
1942537e
|
Leszek Koltunski
|
mTextureDataH[0] = 0;
|
117 |
e7a20702
|
Leszek Koltunski
|
mBitmapSet= false;
|
118 |
1942537e
|
Leszek Koltunski
|
}
|
119 |
|
|
|
120 |
|
|
mMarked = false;
|
121 |
4e2382f3
|
Leszek Koltunski
|
}
|
122 |
|
|
|
123 |
d558d9bd
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124 |
|
|
|
125 |
|
|
long getID()
|
126 |
1942537e
|
Leszek Koltunski
|
{
|
127 |
|
|
return mID;
|
128 |
|
|
}
|
129 |
d558d9bd
|
Leszek Koltunski
|
|
130 |
e7a20702
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131 |
|
|
|
132 |
|
|
boolean setAsInput()
|
133 |
|
|
{
|
134 |
|
|
if( mBitmapSet )
|
135 |
|
|
{
|
136 |
|
|
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
|
137 |
|
|
return true;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
return false;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
f8686932
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144 |
|
|
|
145 |
|
|
static void getUniforms(int mProgramH)
|
146 |
|
|
{
|
147 |
|
|
mTextureH= GLES20.glGetUniformLocation( mProgramH, "u_Texture");
|
148 |
|
|
|
149 |
|
|
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
|
150 |
|
|
GLES20.glUniform1i(mTextureH, 0);
|
151 |
|
|
}
|
152 |
|
|
|
153 |
4e2382f3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
154 |
|
|
|
155 |
7b8086eb
|
Leszek Koltunski
|
static synchronized void onDestroy()
|
156 |
4e2382f3
|
Leszek Koltunski
|
{
|
157 |
1942537e
|
Leszek Koltunski
|
mListMarked = false;
|
158 |
|
|
mList.clear();
|
159 |
4e2382f3
|
Leszek Koltunski
|
}
|
160 |
|
|
|
161 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
162 |
1942537e
|
Leszek Koltunski
|
// must be called form a thread holding OpenGL Context
|
163 |
4e2382f3
|
Leszek Koltunski
|
|
164 |
1942537e
|
Leszek Koltunski
|
static synchronized void deleteAllMarked()
|
165 |
4e2382f3
|
Leszek Koltunski
|
{
|
166 |
1942537e
|
Leszek Koltunski
|
if( mListMarked )
|
167 |
4e2382f3
|
Leszek Koltunski
|
{
|
168 |
1942537e
|
Leszek Koltunski
|
DistortedTexture tmp;
|
169 |
|
|
Iterator<DistortedTexture> iterator = mList.iterator();
|
170 |
|
|
|
171 |
|
|
while(iterator.hasNext())
|
172 |
|
|
{
|
173 |
|
|
tmp = iterator.next();
|
174 |
|
|
|
175 |
|
|
if( tmp.mMarked )
|
176 |
|
|
{
|
177 |
|
|
tmp.deleteTexture();
|
178 |
|
|
iterator.remove();
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
4e2382f3
|
Leszek Koltunski
|
|
182 |
1942537e
|
Leszek Koltunski
|
mListMarked = false;
|
183 |
|
|
}
|
184 |
4e2382f3
|
Leszek Koltunski
|
}
|
185 |
|
|
|
186 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187 |
|
|
// PUBLIC API
|
188 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189 |
|
|
/**
|
190 |
|
|
* Create empty texture of given dimensions.
|
191 |
|
|
*/
|
192 |
3ef3364d
|
Leszek Koltunski
|
public DistortedTexture(int width, int height)
|
193 |
4e2382f3
|
Leszek Koltunski
|
{
|
194 |
3ef3364d
|
Leszek Koltunski
|
mSizeX= width ; mHalfX = mSizeX/2.0f;
|
195 |
|
|
mSizeY= height; mHalfY = mSizeY/2.0f;
|
196 |
|
|
|
197 |
4e2382f3
|
Leszek Koltunski
|
mTextureDataH = new int[1];
|
198 |
|
|
mTextureDataH[0]= 0;
|
199 |
e7a20702
|
Leszek Koltunski
|
mBmp = null;
|
200 |
|
|
mBitmapSet = false;
|
201 |
1942537e
|
Leszek Koltunski
|
mID = 0;
|
202 |
|
|
mMarked = false;
|
203 |
4e2382f3
|
Leszek Koltunski
|
|
204 |
1942537e
|
Leszek Koltunski
|
mList.add(this);
|
205 |
4e2382f3
|
Leszek Koltunski
|
}
|
206 |
|
|
|
207 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208 |
|
|
/**
|
209 |
1942537e
|
Leszek Koltunski
|
* Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
|
210 |
4e2382f3
|
Leszek Koltunski
|
*/
|
211 |
1942537e
|
Leszek Koltunski
|
public void markForDeletion()
|
212 |
4e2382f3
|
Leszek Koltunski
|
{
|
213 |
1942537e
|
Leszek Koltunski
|
//android.util.Log.e("Texture", "marking for deletion "+mID);
|
214 |
|
|
|
215 |
|
|
mListMarked = true;
|
216 |
|
|
mMarked = true;
|
217 |
4e2382f3
|
Leszek Koltunski
|
}
|
218 |
|
|
|
219 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
220 |
|
|
/**
|
221 |
1942537e
|
Leszek Koltunski
|
* Sets the underlying android.graphics.Bitmap object.
|
222 |
4e2382f3
|
Leszek Koltunski
|
* <p>
|
223 |
|
|
* You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call
|
224 |
432442f9
|
Leszek Koltunski
|
* to GLSurfaceView.onSurfaceCreated) because only after this point can the Library upload it to the GPU!
|
225 |
4e2382f3
|
Leszek Koltunski
|
*
|
226 |
|
|
* @param bmp The android.graphics.Bitmap object to apply effects to and display.
|
227 |
|
|
*/
|
228 |
|
|
|
229 |
|
|
public void setTexture(Bitmap bmp)
|
230 |
|
|
{
|
231 |
e7a20702
|
Leszek Koltunski
|
mBitmapSet = true;
|
232 |
|
|
mBmp = bmp;
|
233 |
|
|
mID = bmp.hashCode();
|
234 |
4e2382f3
|
Leszek Koltunski
|
|
235 |
1942537e
|
Leszek Koltunski
|
//android.util.Log.e("Texture", "setting new bitmap "+mID);
|
236 |
4e2382f3
|
Leszek Koltunski
|
}
|
237 |
|
|
|
238 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
239 |
|
|
/**
|
240 |
46e25345
|
Leszek Koltunski
|
* Returns the height of the Texture.
|
241 |
4e2382f3
|
Leszek Koltunski
|
*
|
242 |
|
|
* @return height of the object, in pixels.
|
243 |
|
|
*/
|
244 |
|
|
public int getWidth()
|
245 |
1942537e
|
Leszek Koltunski
|
{
|
246 |
|
|
return mSizeX;
|
247 |
|
|
}
|
248 |
4e2382f3
|
Leszek Koltunski
|
|
249 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250 |
|
|
/**
|
251 |
46e25345
|
Leszek Koltunski
|
* Returns the width of the Texture.
|
252 |
4e2382f3
|
Leszek Koltunski
|
*
|
253 |
|
|
* @return width of the Object, in pixels.
|
254 |
|
|
*/
|
255 |
|
|
public int getHeight()
|
256 |
1942537e
|
Leszek Koltunski
|
{
|
257 |
|
|
return mSizeY;
|
258 |
|
|
}
|
259 |
4e2382f3
|
Leszek Koltunski
|
|
260 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
261 |
|
|
/**
|
262 |
46e25345
|
Leszek Koltunski
|
* Returns the depth of the Texture.
|
263 |
3ef3364d
|
Leszek Koltunski
|
* <p>
|
264 |
05403bba
|
Leszek Koltunski
|
* Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine
|
265 |
|
|
* 'depth' of a texture when rendered based only on the texture itself, that depends on the Mesh it is
|
266 |
3ef3364d
|
Leszek Koltunski
|
* rendered with.
|
267 |
4e2382f3
|
Leszek Koltunski
|
*
|
268 |
|
|
* @return depth of the Object, in pixels.
|
269 |
|
|
*/
|
270 |
05403bba
|
Leszek Koltunski
|
public int getDepth(MeshObject mesh)
|
271 |
1942537e
|
Leszek Koltunski
|
{
|
272 |
05403bba
|
Leszek Koltunski
|
return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
|
273 |
1942537e
|
Leszek Koltunski
|
}
|
274 |
4e2382f3
|
Leszek Koltunski
|
}
|