Skip to content

Commit c5754f4

Browse files
committed
refactor/cleanup
1 parent b6ee1de commit c5754f4

File tree

7 files changed

+412
-722
lines changed

7 files changed

+412
-722
lines changed

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

Lines changed: 0 additions & 34 deletions
This file was deleted.

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

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,75 +25,111 @@
2525
import android.view.SurfaceHolder;
2626

2727
import processing.android.AppComponent;
28-
import processing.core.PApplet;
29-
import processing.core.PMatrix3D;
28+
import processing.core.PGraphics;
3029
import processing.core.PSurface;
3130
import processing.opengl.PGL;
3231
import processing.opengl.PGLES;
3332
import processing.opengl.PGraphics3D;
3433
import processing.opengl.PGraphicsOpenGL;
3534

36-
import static processing.ar.PSurfaceAR.mainPose;
37-
import static processing.ar.PSurfaceAR.session;
38-
3935
public class PGraphicsAR extends PGraphics3D {
36+
// Convenience reference to the AR surface. It is the same object one gets from PApplet.getSurface().
37+
protected PSurfaceAR surfar;
38+
4039

4140
public PGraphicsAR() {
4241
}
4342

43+
4444
@Override
45-
public PSurface createSurface(AppComponent appComponent, SurfaceHolder surfaceHolder, boolean b) {
46-
if (b) pgl.resetFBOLayer();
47-
return new PSurfaceAR(this, appComponent, surfaceHolder);
45+
public PSurface createSurface(AppComponent appComponent, SurfaceHolder surfaceHolder, boolean reset) {
46+
if (reset) pgl.resetFBOLayer();
47+
surfar = new PSurfaceAR(this, appComponent, surfaceHolder);
48+
return surfar;
4849
}
4950

51+
5052
@Override
5153
protected PGL createPGL(PGraphicsOpenGL pGraphicsOpenGL) {
5254
return new PGLES(pGraphicsOpenGL);
5355
}
5456

57+
5558
@Override
5659
public void beginDraw() {
5760
super.beginDraw();
58-
updateInferences();
61+
updateView();
5962
}
6063

64+
6165
@Override
6266
protected void backgroundImpl() {
63-
if (session != null) {
64-
PSurfaceAR.performRendering();
65-
}
67+
surfar.performRendering();
68+
}
69+
70+
71+
@Override
72+
public void camera(float eyeX, float eyeY, float eyeZ,
73+
float centerX, float centerY, float centerZ,
74+
float upX, float upY, float upZ) {
75+
PGraphics.showWarning("The camera cannot be set in AR");
6676
}
6777

78+
6879
@Override
69-
public void surfaceChanged() {
80+
public void perspective(float fov, float aspect, float zNear, float zFar) {
81+
PGraphics.showWarning("Perspective cannot be set in AR");
7082
}
7183

72-
public void updateInferences() {
73-
setAR();
84+
85+
@Override
86+
protected void defaultCamera() {
87+
// do nothing
7488
}
7589

76-
protected void setAR() {
77-
if (PSurfaceAR.projmtx != null && PSurfaceAR.viewmtx != null && PSurfaceAR.anchorMatrix != null) {
78-
float[] prj = PSurfaceAR.projmtx;
79-
float[] view = PSurfaceAR.viewmtx;
80-
float[] anchor = PSurfaceAR.anchorMatrix;
90+
91+
@Override
92+
protected void defaultPerspective() {
93+
// do nothing
94+
}
95+
96+
97+
@Override
98+
protected void saveState() {
99+
}
100+
101+
102+
@Override
103+
protected void restoreState() {
104+
}
105+
106+
107+
@Override
108+
protected void restoreSurface() {
109+
}
110+
111+
112+
protected void updateView() {
113+
if (surfar.projmtx != null && surfar.viewmtx != null && surfar.anchorMatrix != null) {
114+
float[] prj = surfar.projmtx;
115+
float[] view = surfar.viewmtx;
116+
float[] anchor = surfar.anchorMatrix;
81117

82118
// Fist, set all matrices to identity
83119
resetProjection();
84120
resetMatrix();
85121

86122
// Apply the projection matrix
87123
applyProjection(prj[0], prj[4], prj[8], prj[12],
88-
prj[1], prj[5], prj[9], prj[13],
89-
prj[2], prj[6], prj[10], prj[14],
90-
prj[3], prj[7], prj[11], prj[15]);
124+
prj[1], prj[5], prj[9], prj[13],
125+
prj[2], prj[6], prj[10], prj[14],
126+
prj[3], prj[7], prj[11], prj[15]);
91127

92128
// make modelview = view
93129
applyMatrix(view[0], view[4], view[8], view[12],
94-
view[1], view[5], view[9], view[13],
95-
view[2], view[6], view[10], view[14],
96-
view[3], view[7], view[11], view[15]);
130+
view[1], view[5], view[9], view[13],
131+
view[2], view[6], view[10], view[14],
132+
view[3], view[7], view[11], view[15]);
97133

98134
// now, modelview = view * anchor
99135
applyMatrix(anchor[0], anchor[4], anchor[8], anchor[12],

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

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
package processing.ar;
2424

25-
import android.Manifest;
2625
import android.app.Activity;
2726
import android.app.ActivityManager;
2827
import android.app.AlertDialog;
@@ -31,7 +30,6 @@
3130
import android.content.DialogInterface;
3231
import android.content.Intent;
3332
import android.content.pm.ConfigurationInfo;
34-
import android.content.pm.PackageManager;
3533
import android.content.res.AssetManager;
3634
import android.opengl.GLES20;
3735
import android.opengl.GLSurfaceView;
@@ -42,7 +40,6 @@
4240

4341
import processing.android.AppComponent;
4442
import processing.ar.render.*;
45-
import processing.core.PApplet;
4643
import processing.core.PGraphics;
4744
import processing.opengl.PGLES;
4845
import processing.opengl.PGraphicsOpenGL;
@@ -58,35 +55,6 @@
5855
import java.util.concurrent.ArrayBlockingQueue;
5956

6057
public class PSurfaceAR extends PSurfaceGLES {
61-
62-
private GLSurfaceView surfaceView;
63-
protected AndroidARRenderer renderer;
64-
protected PGraphicsAR par;
65-
66-
public static float[] anchorMatrix = new float[16];
67-
public static float[] quaternionMatrix = new float[16];
68-
public static ArrayBlockingQueue<MotionEvent> queuedTaps = new ArrayBlockingQueue<>(16);
69-
public static ArrayList<Anchor> anchors = new ArrayList<>();
70-
71-
public static float[] projmtx;
72-
public static float[] viewmtx;
73-
74-
public static float lightIntensity;
75-
76-
public static Session session;
77-
public static Pose mainPose;
78-
public static RotationHandler displayRotationHelper;
79-
80-
public static String PLANE_TEXTURE = "grid.png";
81-
public static String OBJ_NAME = null;
82-
public static String OBJ_TEX = null;
83-
public static boolean PLACED = false;
84-
85-
public static PBackground backgroundRenderer = new PBackground();
86-
public static PPlane planeRenderer = new PPlane();
87-
public static PPointCloud pointCloud = new PPointCloud();
88-
public static PObject virtualObject = new PObject();
89-
9058
private static String T_ALERT_MESSAGE = "ALERT";
9159
private static String C_NOT_SUPPORTED = "ARCore SDK required to run this app type";
9260
private static String T_PROMPT_MESSAGE = "PROMPT";
@@ -95,11 +63,29 @@ public class PSurfaceAR extends PSurfaceGLES {
9563
private static String C_EXCEPT_UPDATE_SDK = "Please update ARCore";
9664
private static String C_EXCEPT_UPDATE_APP = "Please update this app";
9765
private static String C_DEVICE = "This device does not support AR";
98-
private static final int CAMERA_PERMISSION_CODE = 0;
99-
private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA;
10066

101-
ProgressDialog progressdialog = new ProgressDialog(activity);
67+
protected GLSurfaceView surfaceView;
68+
protected AndroidARRenderer renderer;
69+
protected PGraphicsAR par;
70+
71+
protected static float[] anchorMatrix = new float[16];
72+
protected static ArrayBlockingQueue<MotionEvent> queuedTaps = new ArrayBlockingQueue<>(16);
73+
protected static ArrayList<Anchor> anchors = new ArrayList<>();
10274

75+
protected float[] projmtx;
76+
protected float[] viewmtx;
77+
78+
protected float lightIntensity;
79+
80+
protected Session session;
81+
protected Pose mainPose;
82+
protected RotationHandler displayRotationHelper;
83+
84+
protected PBackground backgroundRenderer = new PBackground();
85+
protected PPlane planeRenderer = new PPlane();
86+
protected PPointCloud pointCloud = new PPointCloud();
87+
88+
protected ProgressDialog progressdialog = new ProgressDialog(activity);
10389

10490
public PSurfaceAR(PGraphics graphics, AppComponent appComponent, SurfaceHolder surfaceHolder) {
10591
super(graphics, appComponent, surfaceHolder);
@@ -186,8 +172,6 @@ public void dispose() {
186172
public class SurfaceViewAR extends GLSurfaceView {
187173
public SurfaceViewAR(Context context) {
188174
super(context);
189-
// sketch.setup();
190-
// sketch.draw();
191175

192176
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
193177
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
@@ -244,16 +228,8 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
244228
pgl.getGL(null);
245229
GLES20.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
246230
backgroundRenderer.createOnGlThread(activity);
247-
if (OBJ_NAME != null && OBJ_TEX != null) {
248-
try {
249-
virtualObject.createOnGlThread(activity, OBJ_NAME, OBJ_TEX);
250-
virtualObject.setMaterialProperties(0.0f, 3.5f, 1.0f, 6.0f);
251-
} catch (IOException e) {
252-
PGraphics.showWarning("Failed to read obj file");
253-
}
254-
}
255231
try {
256-
planeRenderer.createOnGlThread(activity, PLANE_TEXTURE);
232+
planeRenderer.createOnGlThread(activity);
257233
} catch (IOException e) {
258234
PGraphics.showWarning("Failed to read plane texture");
259235
}
@@ -274,12 +250,7 @@ public void onSurfaceChanged(GL10 gl, int width, int height) {
274250

275251
@Override
276252
public void onDrawFrame(GL10 gl) {
277-
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
278-
279-
if (session == null) {
280-
return;
281-
}
282-
performRendering();
253+
if (session == null) return;
283254

284255
if (progressdialog != null) {
285256
for (Plane plane : session.getAllTrackables(Plane.class)) {
@@ -296,7 +267,9 @@ public void onDrawFrame(GL10 gl) {
296267
}
297268
}
298269

299-
public static void performRendering() {
270+
public void performRendering() {
271+
if (session == null) return;
272+
300273
displayRotationHelper.updateSessionIfNeeded(session);
301274

302275
try {
@@ -348,11 +321,6 @@ public static void performRendering() {
348321
continue;
349322
}
350323
anchor.getPose().toMatrix(anchorMatrix, 0);
351-
352-
if ((OBJ_NAME != null && OBJ_TEX != null) && PLACED) {
353-
virtualObject.updateModelMatrix(anchorMatrix, scaleFactor);
354-
virtualObject.draw(viewmtx, projmtx, lightIntensity);
355-
}
356324
}
357325
} catch (Throwable t) {
358326
PGraphics.showWarning("Exception on the OpenGL thread");

0 commit comments

Comments
 (0)