@@ -64,6 +64,11 @@ public class PSurfaceAR extends PSurfaceGLES {
64
64
private static String C_EXCEPT_UPDATE_APP = "Please update this app" ;
65
65
private static String C_DEVICE = "This device does not support AR" ;
66
66
67
+ // Made these public so they can be accessed from the sketch
68
+ protected Session session ;
69
+ public Frame frame ;
70
+ public Camera camera ;
71
+
67
72
protected GLSurfaceView surfaceView ;
68
73
protected AndroidARRenderer renderer ;
69
74
protected PGraphicsAR par ;
@@ -72,13 +77,8 @@ public class PSurfaceAR extends PSurfaceGLES {
72
77
protected static ArrayBlockingQueue <MotionEvent > queuedTaps = new ArrayBlockingQueue <>(16 );
73
78
protected static ArrayList <Anchor > anchors = new ArrayList <>();
74
79
75
- protected float [] projmtx ;
76
- protected float [] viewmtx ;
77
-
78
- protected float lightIntensity ;
79
-
80
- protected Session session ;
81
- protected Pose mainPose ;
80
+ protected float [] projmtx = new float [16 ];
81
+ protected float [] viewmtx = new float [16 ];
82
82
protected RotationHandler displayRotationHelper ;
83
83
84
84
protected PBackground backgroundRenderer = new PBackground ();
@@ -262,71 +262,76 @@ public void onDrawFrame(GL10 gl) {
262
262
}
263
263
}
264
264
265
- sketch .calculate ();
266
- sketch .handleDraw ();
265
+ displayRotationHelper .updateSessionIfNeeded (session );
266
+ try {
267
+ session .setCameraTextureName (backgroundRenderer .getTextureId ());
268
+ frame = session .update ();
269
+ camera = frame .getCamera ();
270
+
271
+ if (camera .getTrackingState () == TrackingState .PAUSED ) {
272
+ // Just draw the camera image and do nothing else
273
+ renderBackground ();
274
+ return ;
275
+ }
276
+
277
+ updateAnchors ();
278
+ updateMatrices ();
279
+
280
+ sketch .calculate ();
281
+ sketch .handleDraw ();
282
+
283
+ } catch (CameraNotAvailableException ex ) {
284
+ PGraphics .showWarning ("Camera is not available" );
285
+ }
267
286
}
268
287
}
269
288
270
- public void performRendering () {
271
- if (session == null ) return ;
272
-
273
- displayRotationHelper .updateSessionIfNeeded (session );
289
+ public void renderBackground () {
290
+ backgroundRenderer .draw (frame );
291
+ }
274
292
275
- try {
276
- session .setCameraTextureName (backgroundRenderer .getTextureId ());
277
- Frame frame = session .update ();
278
- Camera camera = frame .getCamera ();
279
-
280
- MotionEvent tap = queuedTaps .poll ();
281
- if (tap != null && camera .getTrackingState () == TrackingState .TRACKING ) {
282
- for (HitResult hit : frame .hitTest (tap )) {
283
- Trackable trackable = hit .getTrackable ();
284
- if ((trackable instanceof Plane && ((Plane ) trackable ).isPoseInPolygon (hit .getHitPose ()))
285
- || (trackable instanceof Point
286
- && ((Point ) trackable ).getOrientationMode ()
287
- == Point .OrientationMode .ESTIMATED_SURFACE_NORMAL )) {
288
- if (anchors .size () >= 20 ) {
289
- anchors .get (0 ).detach ();
290
- anchors .remove (0 );
291
- }
292
- anchors .add (hit .createAnchor ());
293
- break ;
293
+ protected void updateAnchors () {
294
+ MotionEvent tap = queuedTaps .poll ();
295
+ if (tap != null && camera .getTrackingState () == TrackingState .TRACKING ) {
296
+ for (HitResult hit : frame .hitTest (tap )) {
297
+ Trackable trackable = hit .getTrackable ();
298
+ if ((trackable instanceof Plane && ((Plane ) trackable ).isPoseInPolygon (hit .getHitPose ()))
299
+ || (trackable instanceof Point
300
+ && ((Point ) trackable ).getOrientationMode ()
301
+ == Point .OrientationMode .ESTIMATED_SURFACE_NORMAL )) {
302
+ if (anchors .size () >= 20 ) {
303
+ anchors .get (0 ).detach ();
304
+ anchors .remove (0 );
294
305
}
306
+ anchors .add (hit .createAnchor ());
307
+ break ;
295
308
}
296
309
}
310
+ }
311
+ }
297
312
298
- backgroundRenderer . draw ( frame );
299
- if ( camera .getTrackingState () == TrackingState . PAUSED ) {
300
- return ;
301
- }
313
+ protected void updateMatrices () {
314
+ camera .getProjectionMatrix ( projmtx , 0 , 0.1f , 100.0f );
315
+ camera . getViewMatrix ( viewmtx , 0 ) ;
316
+ }
302
317
303
- projmtx = new float [16 ];
304
- camera .getProjectionMatrix (projmtx , 0 , 0.1f , 100.0f );
305
- viewmtx = new float [16 ];
306
- camera .getViewMatrix (viewmtx , 0 );
307
- lightIntensity = frame .getLightEstimate ().getPixelIntensity ();
308
- PointCloud foundPointCloud = frame .acquirePointCloud ();
309
- pointCloud .update (foundPointCloud );
310
- pointCloud .draw (viewmtx , projmtx );
311
- foundPointCloud .release ();
312
-
313
- planeRenderer .drawPlanes (
314
- session .getAllTrackables (Plane .class ), camera .getDisplayOrientedPose (), projmtx );
315
-
316
- mainPose = camera .getDisplayOrientedPose ();
317
-
318
- float scaleFactor = 1.0f ;
319
- for (Anchor anchor : anchors ) {
320
- if (anchor .getTrackingState () != TrackingState .TRACKING ) {
321
- continue ;
322
- }
323
- anchor .getPose ().toMatrix (anchorMatrix , 0 );
318
+ protected void renderHelpers () {
319
+ PointCloud foundPointCloud = frame .acquirePointCloud ();
320
+ pointCloud .update (foundPointCloud );
321
+ pointCloud .draw (viewmtx , projmtx );
322
+ foundPointCloud .release ();
323
+
324
+ planeRenderer .drawPlanes (
325
+ session .getAllTrackables (Plane .class ), camera .getDisplayOrientedPose (), projmtx );
326
+ for (Anchor anchor : anchors ) {
327
+ if (anchor .getTrackingState () != TrackingState .TRACKING ) {
328
+ continue ;
324
329
}
325
- } catch (Throwable t ) {
326
- PGraphics .showWarning ("Exception on the OpenGL thread" );
330
+ anchor .getPose ().toMatrix (anchorMatrix , 0 );
327
331
}
328
332
}
329
333
334
+
330
335
@ Override
331
336
public void startThread () {
332
337
}
@@ -378,14 +383,11 @@ public void resumeThread() {
378
383
message (T_ALERT_MESSAGE , message + " -- " + exception );
379
384
}
380
385
381
-
382
386
Config config = new Config (session );
383
387
if (!session .isSupported (config )) {
384
388
message (T_PROMPT_MESSAGE , C_DEVICE );
385
389
}
386
390
session .configure (config );
387
-
388
-
389
391
}
390
392
try {
391
393
session .resume ();
0 commit comments