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.opengl.GLES30;
|
23
|
|
24
|
import java.nio.FloatBuffer;
|
25
|
import java.util.HashMap;
|
26
|
import java.util.LinkedList;
|
27
|
|
28
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
29
|
/**
|
30
|
* Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
|
31
|
* 2) normals 3) texture coordinates.
|
32
|
* <p>
|
33
|
* If you want to render to a particular shape, extend from here, construct the three FloatBuffers and
|
34
|
* provide correct dataLength, i.e. the number of vertices.
|
35
|
*/
|
36
|
public abstract class MeshObject
|
37
|
{
|
38
|
static final int BYTES_PER_FLOAT = 4; //
|
39
|
static final int POSITION_DATA_SIZE= 3; // Size of the position data in elements
|
40
|
static final int NORMAL_DATA_SIZE = 3; // Size of the normal data in elements.
|
41
|
static final int TEX_DATA_SIZE = 2; // Size of the texture coordinate data in elements.
|
42
|
|
43
|
///// CREATING/DELETING Vertex Buffer Objects ///////////////////////////
|
44
|
private static final int JOB_CREATE = 0;
|
45
|
private static final int JOB_DELETE = 1;
|
46
|
|
47
|
private class Job
|
48
|
{
|
49
|
MeshObject mesh;
|
50
|
int action;
|
51
|
|
52
|
Job(MeshObject o, int a)
|
53
|
{
|
54
|
mesh = o;
|
55
|
action = a;
|
56
|
}
|
57
|
}
|
58
|
|
59
|
private static boolean mToDo = false;
|
60
|
private static LinkedList<MeshObject> mDoneList = new LinkedList<>();
|
61
|
private static HashMap<Long,Job> mToDoMap = new HashMap<>();
|
62
|
/////////////////////////////////////////////////////////////////////////
|
63
|
|
64
|
private static long mNextID = 0;
|
65
|
private long mID;
|
66
|
|
67
|
int dataLength;
|
68
|
FloatBuffer mMeshPositions, mMeshNormals, mMeshTexture;
|
69
|
int[] mPosVBO = new int[1];
|
70
|
int[] mNorVBO = new int[1];
|
71
|
int[] mTexVBO = new int[1];
|
72
|
|
73
|
final float zFactor; // strange workaround for the fact that we need to somehow store the 'depth'
|
74
|
// of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
|
78
|
MeshObject(float factor)
|
79
|
{
|
80
|
zFactor = factor;
|
81
|
mID = mNextID++;
|
82
|
|
83
|
recreate();
|
84
|
|
85
|
mToDoMap.put(mID, new Job(this,JOB_CREATE) );
|
86
|
mToDo = true;
|
87
|
}
|
88
|
|
89
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
90
|
// must be called from a thread holding OpenGL Context
|
91
|
|
92
|
static synchronized boolean toDo()
|
93
|
{
|
94
|
if( mToDo )
|
95
|
{
|
96
|
Job job;
|
97
|
MeshObject mesh;
|
98
|
|
99
|
for(Long key: mToDoMap.keySet())
|
100
|
{
|
101
|
job = mToDoMap.get(key);
|
102
|
mesh = job.mesh;
|
103
|
|
104
|
//android.util.Log.d("MESH", " ---> need to "+(job.action==JOB_CREATE ? "create":"delete") );
|
105
|
|
106
|
if( job.action==JOB_CREATE )
|
107
|
{
|
108
|
mesh.create();
|
109
|
mDoneList.add(mesh);
|
110
|
}
|
111
|
else if( job.action==JOB_DELETE )
|
112
|
{
|
113
|
mesh.delete();
|
114
|
}
|
115
|
}
|
116
|
|
117
|
mToDoMap.clear();
|
118
|
mToDo = false;
|
119
|
return true;
|
120
|
}
|
121
|
|
122
|
return false;
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
// must be called from a thread holding OpenGL Context
|
127
|
//
|
128
|
// Do NOT release mMeshPositions etc as we will need them when we need to re-create the buffers after
|
129
|
// a loss of OpenGL context!
|
130
|
|
131
|
private void create()
|
132
|
{
|
133
|
if( mPosVBO[0]<0 )
|
134
|
{
|
135
|
GLES30.glGenBuffers(1, mPosVBO, 0);
|
136
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mPosVBO[0]);
|
137
|
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength*POSITION_DATA_SIZE*BYTES_PER_FLOAT, mMeshPositions, GLES30.GL_STATIC_READ);
|
138
|
}
|
139
|
if( mNorVBO[0]<0 )
|
140
|
{
|
141
|
GLES30.glGenBuffers(1, mNorVBO, 0);
|
142
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mNorVBO[0]);
|
143
|
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength* NORMAL_DATA_SIZE*BYTES_PER_FLOAT, mMeshNormals , GLES30.GL_STATIC_READ);
|
144
|
}
|
145
|
if( mTexVBO[0]<0 )
|
146
|
{
|
147
|
GLES30.glGenBuffers(1, mTexVBO, 0);
|
148
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mTexVBO[0]);
|
149
|
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength* TEX_DATA_SIZE*BYTES_PER_FLOAT, mMeshTexture , GLES30.GL_STATIC_READ);
|
150
|
}
|
151
|
|
152
|
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
|
153
|
}
|
154
|
|
155
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156
|
// must be called from a thread holding OpenGL Context
|
157
|
|
158
|
private void delete()
|
159
|
{
|
160
|
if( mPosVBO[0]>=0 )
|
161
|
{
|
162
|
GLES30.glDeleteBuffers(1, mPosVBO, 0);
|
163
|
mPosVBO[0] = -1;
|
164
|
}
|
165
|
if( mNorVBO[0]>=0 )
|
166
|
{
|
167
|
GLES30.glDeleteBuffers(1, mNorVBO, 0);
|
168
|
mNorVBO[0] = -1;
|
169
|
}
|
170
|
if( mTexVBO[0]>=0 )
|
171
|
{
|
172
|
GLES30.glDeleteBuffers(1, mTexVBO, 0);
|
173
|
mTexVBO[0] = -1;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
178
|
|
179
|
private void recreate()
|
180
|
{
|
181
|
mPosVBO[0] = -1;
|
182
|
mNorVBO[0] = -1;
|
183
|
mTexVBO[0] = -1;
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
static synchronized void onPause()
|
189
|
{
|
190
|
MeshObject mesh;
|
191
|
int num = mDoneList.size();
|
192
|
|
193
|
for(int i=0; i<num; i++)
|
194
|
{
|
195
|
mesh = mDoneList.removeFirst();
|
196
|
mToDoMap.put(mesh.getID(), mesh.new Job(mesh,JOB_CREATE) );
|
197
|
mesh.recreate();
|
198
|
}
|
199
|
|
200
|
mToDo = true;
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
static synchronized void onDestroy()
|
206
|
{
|
207
|
mToDoMap.clear();
|
208
|
mDoneList.clear();
|
209
|
|
210
|
mToDo = true;
|
211
|
mNextID = 0;
|
212
|
}
|
213
|
|
214
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
215
|
|
216
|
@SuppressWarnings("unused")
|
217
|
static void debugLists()
|
218
|
{
|
219
|
android.util.Log.e("Mesh", "Done list:");
|
220
|
|
221
|
MeshObject mesh;
|
222
|
int num = mDoneList.size();
|
223
|
|
224
|
for(int i=0; i<num; i++)
|
225
|
{
|
226
|
mesh = mDoneList.get(i);
|
227
|
mesh.print(i, "");
|
228
|
}
|
229
|
|
230
|
android.util.Log.e("Mesh", "ToDo list:");
|
231
|
|
232
|
Job job;
|
233
|
int i=0;
|
234
|
|
235
|
for(Long key: mToDoMap.keySet())
|
236
|
{
|
237
|
job = mToDoMap.get(key);
|
238
|
job.mesh.print(i++, job.action==JOB_CREATE ? " create":" delete");
|
239
|
}
|
240
|
}
|
241
|
|
242
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
243
|
|
244
|
private void print(int i, String extra)
|
245
|
{
|
246
|
String str;
|
247
|
|
248
|
if( this instanceof MeshFlat ) str = (i+": MeshFlat ");
|
249
|
else if( this instanceof MeshCubes) str = (i+": MeshCubes ");
|
250
|
else str = (i+": UNKNOWN ");
|
251
|
|
252
|
str += ( "dataLength: "+dataLength+" meshID:"+getID());
|
253
|
|
254
|
android.util.Log.e("Mesh", str+extra);
|
255
|
}
|
256
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
257
|
/**
|
258
|
* Get the minimal set of Vertices which have the same convex hull as the whole set.
|
259
|
* <p>
|
260
|
* In case of Flat Meshes, the set is obviously just the 4 corners. In case of the Cubes Mesh,
|
261
|
* it is a subset of the set of each rightmost- and leftmost- corners in each row.
|
262
|
* <p>
|
263
|
* This is used to be able to quickly compute, in window coordinates, the Mesh'es bounding rectangle.
|
264
|
*/
|
265
|
abstract float[] getBoundingVertices();
|
266
|
|
267
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
268
|
|
269
|
synchronized void markForCreation()
|
270
|
{
|
271
|
mDoneList.remove(this);
|
272
|
mToDoMap.put(mID, new Job(this,JOB_CREATE) );
|
273
|
mToDo = true;
|
274
|
}
|
275
|
|
276
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
277
|
// PUBLIC API
|
278
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
279
|
/**
|
280
|
* Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
|
281
|
*/
|
282
|
synchronized public void markForDeletion()
|
283
|
{
|
284
|
mDoneList.remove(this);
|
285
|
mToDoMap.put(mID, new Job(this,JOB_DELETE) );
|
286
|
mToDo = true;
|
287
|
}
|
288
|
|
289
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
290
|
/**
|
291
|
* Return unique ID of this Mesh.
|
292
|
*/
|
293
|
public long getID()
|
294
|
{
|
295
|
return mID;
|
296
|
}
|
297
|
}
|