Revision 226144d0
Added by Leszek Koltunski about 7 years ago
src/main/java/org/distorted/library/MeshObject.java | ||
---|---|---|
22 | 22 |
import android.opengl.GLES30; |
23 | 23 |
|
24 | 24 |
import java.nio.FloatBuffer; |
25 |
import java.util.HashMap; |
|
26 |
import java.util.LinkedList; |
|
27 | 25 |
|
28 | 26 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
29 | 27 |
/** |
... | ... | |
33 | 31 |
* If you want to render to a particular shape, extend from here, construct the three FloatBuffers and |
34 | 32 |
* provide correct dataLength, i.e. the number of vertices. |
35 | 33 |
*/ |
36 |
public abstract class MeshObject |
|
34 |
public abstract class MeshObject extends DistortedObject
|
|
37 | 35 |
{ |
38 | 36 |
static final int BYTES_PER_FLOAT = 4; // |
39 | 37 |
static final int POSITION_DATA_SIZE= 3; // Size of the position data in elements |
40 | 38 |
static final int NORMAL_DATA_SIZE = 3; // Size of the normal data in elements. |
41 | 39 |
static final int TEX_DATA_SIZE = 2; // Size of the texture coordinate data in elements. |
42 | 40 |
|
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 | 41 |
int dataLength; |
68 | 42 |
FloatBuffer mMeshPositions, mMeshNormals, mMeshTexture; |
69 | 43 |
int[] mPosVBO = new int[1]; |
... | ... | |
77 | 51 |
|
78 | 52 |
MeshObject(float factor) |
79 | 53 |
{ |
80 |
zFactor = factor; |
|
81 |
mID = mNextID++; |
|
54 |
super(DistortedObject.NOT_CREATED_YET,DistortedObject.TYPE_USER); |
|
82 | 55 |
|
56 |
zFactor = factor; |
|
83 | 57 |
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 | 58 |
} |
124 | 59 |
|
125 | 60 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
128 | 63 |
// Do NOT release mMeshPositions etc as we will need them when we need to re-create the buffers after |
129 | 64 |
// a loss of OpenGL context! |
130 | 65 |
|
131 |
private void create()
|
|
66 |
void create() |
|
132 | 67 |
{ |
133 | 68 |
if( mPosVBO[0]<0 ) |
134 | 69 |
{ |
... | ... | |
155 | 90 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
156 | 91 |
// must be called from a thread holding OpenGL Context |
157 | 92 |
|
158 |
private void delete()
|
|
93 |
void delete() |
|
159 | 94 |
{ |
160 | 95 |
if( mPosVBO[0]>=0 ) |
161 | 96 |
{ |
... | ... | |
176 | 111 |
|
177 | 112 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
178 | 113 |
|
179 |
private void recreate()
|
|
114 |
void recreate() |
|
180 | 115 |
{ |
181 | 116 |
mPosVBO[0] = -1; |
182 | 117 |
mNorVBO[0] = -1; |
... | ... | |
184 | 119 |
} |
185 | 120 |
|
186 | 121 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
122 |
// debugging only |
|
187 | 123 |
|
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()); |
|
124 |
String printDetails() |
|
125 |
{ |
|
126 |
return getClass().getSimpleName()+" vertices:"+dataLength; |
|
127 |
} |
|
253 | 128 |
|
254 |
android.util.Log.e("Mesh", str+extra); |
|
255 |
} |
|
256 | 129 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
257 | 130 |
/** |
258 | 131 |
* Get the minimal set of Vertices which have the same convex hull as the whole set. |
... | ... | |
263 | 136 |
* This is used to be able to quickly compute, in window coordinates, the Mesh'es bounding rectangle. |
264 | 137 |
*/ |
265 | 138 |
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 | 139 |
} |
140 |
|
Also available in: Unified diff
Progress with VBOs - this time abstract out a new class, DistortedObject - i.e. everything that uploads something to GPU and thus needs to be auto re-created upon loss of the context.