Skip to content

Commit a2530ba

Browse files
committed
break up performRendering into smaller units
1 parent 7ec73b3 commit a2530ba

File tree

4 files changed

+72
-68
lines changed

4 files changed

+72
-68
lines changed

mode/libraries/ar/src/assets/shaders/screenquad_fragment.glsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ precision mediump float;
44
varying vec2 v_TexCoord;
55
uniform samplerExternalOES sTexture;
66

7-
87
void main() {
9-
gl_FragColor = texture2D(sTexture, v_TexCoord);
8+
gl_FragColor = texture2D(sTexture, v_TexCoord);
109
}

mode/libraries/ar/src/assets/shaders/screenquad_vertex.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ attribute vec2 a_TexCoord;
44
varying vec2 v_TexCoord;
55

66
void main() {
7-
gl_Position = a_Position;
8-
v_TexCoord = a_TexCoord;
7+
gl_Position = a_Position;
8+
v_TexCoord = a_TexCoord;
99
}

mode/libraries/ar/src/processing/ar/PGraphicsAR.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ public void beginDraw() {
6464

6565
@Override
6666
protected void backgroundImpl() {
67-
surfar.performRendering();
67+
surfar.renderBackground();
68+
69+
// The helpers (planes, point clouds, should be drawn using Processing primitives, so this could
70+
// go after updateView() in beginDraw().
71+
surfar.renderHelpers();
6872
}
6973

7074

@@ -136,7 +140,6 @@ protected void updateView() {
136140
anchor[1], anchor[5], anchor[9], anchor[13],
137141
anchor[2], anchor[6], anchor[10], anchor[14],
138142
anchor[3], anchor[7], anchor[11], anchor[15]);
139-
140143
}
141144
}
142145
}

mode/libraries/ar/src/processing/ar/PSurfaceAR.java

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public class PSurfaceAR extends PSurfaceGLES {
6464
private static String C_EXCEPT_UPDATE_APP = "Please update this app";
6565
private static String C_DEVICE = "This device does not support AR";
6666

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+
6772
protected GLSurfaceView surfaceView;
6873
protected AndroidARRenderer renderer;
6974
protected PGraphicsAR par;
@@ -72,13 +77,8 @@ public class PSurfaceAR extends PSurfaceGLES {
7277
protected static ArrayBlockingQueue<MotionEvent> queuedTaps = new ArrayBlockingQueue<>(16);
7378
protected static ArrayList<Anchor> anchors = new ArrayList<>();
7479

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];
8282
protected RotationHandler displayRotationHelper;
8383

8484
protected PBackground backgroundRenderer = new PBackground();
@@ -262,71 +262,76 @@ public void onDrawFrame(GL10 gl) {
262262
}
263263
}
264264

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+
}
267286
}
268287
}
269288

270-
public void performRendering() {
271-
if (session == null) return;
272-
273-
displayRotationHelper.updateSessionIfNeeded(session);
289+
public void renderBackground() {
290+
backgroundRenderer.draw(frame);
291+
}
274292

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);
294305
}
306+
anchors.add(hit.createAnchor());
307+
break;
295308
}
296309
}
310+
}
311+
}
297312

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+
}
302317

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;
324329
}
325-
} catch (Throwable t) {
326-
PGraphics.showWarning("Exception on the OpenGL thread");
330+
anchor.getPose().toMatrix(anchorMatrix, 0);
327331
}
328332
}
329333

334+
330335
@Override
331336
public void startThread() {
332337
}
@@ -378,14 +383,11 @@ public void resumeThread() {
378383
message(T_ALERT_MESSAGE, message + " -- " + exception);
379384
}
380385

381-
382386
Config config = new Config(session);
383387
if (!session.isSupported(config)) {
384388
message(T_PROMPT_MESSAGE, C_DEVICE);
385389
}
386390
session.configure(config);
387-
388-
389391
}
390392
try {
391393
session.resume();

0 commit comments

Comments
 (0)