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