Skip to content

Commit

Permalink
fix: remove usage of R class and implement dynamic way to access its …
Browse files Browse the repository at this point in the history
…resources
  • Loading branch information
alexgerardojacinto committed Apr 26, 2022
1 parent 5374f57 commit 740da31
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
20 changes: 8 additions & 12 deletions src/android/com/outsystems/plugins/arexample/ARPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,17 @@ class ARPlugin: CordovaImplementation(){

val intent = Intent(cordova.activity, ArTradeActivity::class.java)

val objToShow = args.getString(0)
val folderName = args.getString(0)

if(objToShow == "beer"){
intent.putExtra("obj_path", "www/beer/beer.obj")
intent.putExtra("texture_path", "www/beer/beer.png")
cordova.activity.startActivityForResult(intent, 1)
}
else if(objToShow == "chair"){
intent.putExtra("obj_path", "www/chair/chair.obj")
intent.putExtra("texture_path", "www/chair/chair.png")
cordova.activity.startActivityForResult(intent, 1)
}
else{//do nothing
if(folderName.isNullOrEmpty()){
return
//later we can return an error here
}

intent.putExtra("obj_path", "www/$folderName/mesh.obj")
intent.putExtra("texture_path", "www/$folderName/diffuse.png")

cordova.activity.startActivityForResult(intent, 1)
}

}
Expand Down
17 changes: 10 additions & 7 deletions src/android/com/outsystems/plugins/arexample/ArTradeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.google.ar.core.exceptions.UnavailableApkTooOldException;
import com.google.ar.core.exceptions.UnavailableArcoreNotInstalledException;
import com.google.ar.core.exceptions.UnavailableSdkTooOldException;
import com.outsystemsenterprise.enmobile11dev.ARAndroidExample.R;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -77,8 +76,8 @@ public class ArTradeActivity extends AppCompatActivity implements GLSurfaceView.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSurfaceView = findViewById(R.id.surfaceview);
setContentView(getResourceId("layout/activity_main"));
mSurfaceView = findViewById(getResourceId("id/surfaceview"));
mDisplayRotationHelper = new DisplayRotationHelper(/*context=*/ this);


Expand Down Expand Up @@ -222,14 +221,14 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

// Create the texture and pass it to ARCore session to be filled during update().
mBackgroundRenderer.createOnGlThread(/*context=*/ this);
mBackgroundRenderer.createOnGlThread(/*context=*/ this, getResourceId("raw/screenquad_vertex"), getResourceId("raw/screenquad_fragment_oes"));
if (mSession != null) {
mSession.setCameraTextureName(mBackgroundRenderer.getTextureId());
}

// Prepare the other rendering objects.
try {
mVirtualObject.createOnGlThread(/*context=*/this, objPath, texturePath);
mVirtualObject.createOnGlThread(/*context=*/this, objPath, texturePath, getResourceId("raw/object_vertex"), getResourceId("raw/object_fragment"));
mVirtualObject.setMaterialProperties(0.0f, 3.5f, 1.0f, 6.0f);

//mVirtualObjectShadow.createOnGlThread(/*context=*/this,
Expand All @@ -240,11 +239,11 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.e(TAG, "Failed to read obj file");
}
try {
mPlaneRenderer.createOnGlThread(/*context=*/this, "trigrid.png");
mPlaneRenderer.createOnGlThread(/*context=*/this, "trigrid.png", getResourceId("raw/plane_vertex"), getResourceId("raw/plane_fragment"));
} catch (IOException e) {
Log.e(TAG, "Failed to read plane texture");
}
mPointCloud.createOnGlThread(/*context=*/this);
mPointCloud.createOnGlThread(/*context=*/this, getResourceId("raw/point_cloud_vertex"), getResourceId("raw/passthrough_fragment"));
}

@Override
Expand Down Expand Up @@ -435,4 +434,8 @@ public void run() {
}
});
}

public int getResourceId(String typeAndName) {
return getApplication().getResources().getIdentifier(typeAndName, null, getApplication().getPackageName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.opengl.GLSurfaceView;
import com.google.ar.core.Frame;
import com.google.ar.core.Session;
import com.outsystemsenterprise.enmobile11dev.ARAndroidExample.R;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -63,7 +62,7 @@ public int getTextureId() {
*
* @param context Needed to access shader source.
*/
public void createOnGlThread(Context context) {
public void createOnGlThread(Context context, int screenQuadVertex, int screenQuadFragment) {
// Generate the background texture.
int[] textures = new int[1];
GLES20.glGenTextures(1, textures, 0);
Expand Down Expand Up @@ -99,9 +98,9 @@ public void createOnGlThread(Context context) {
mQuadTexCoordTransformed = bbTexCoordsTransformed.asFloatBuffer();

int vertexShader = ShaderUtil.loadGLShader(TAG, context,
GLES20.GL_VERTEX_SHADER, R.raw.screenquad_vertex);
GLES20.GL_VERTEX_SHADER, screenQuadVertex);
int fragmentShader = ShaderUtil.loadGLShader(TAG, context,
GLES20.GL_FRAGMENT_SHADER, R.raw.screenquad_fragment_oes);
GLES20.GL_FRAGMENT_SHADER, screenQuadFragment);

mQuadProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(mQuadProgram, vertexShader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.opengl.Matrix;
import com.outsystemsenterprise.enmobile11dev.ARAndroidExample.R;

import de.javagl.obj.Obj;
import de.javagl.obj.ObjData;
Expand Down Expand Up @@ -110,8 +109,12 @@ public ObjectRenderer() {
* @param objAssetName Name of the OBJ file containing the model geometry.
* @param diffuseTextureAssetName Name of the PNG file containing the diffuse texture map.
*/
public void createOnGlThread(Context context, String objAssetName,
String diffuseTextureAssetName) throws IOException {
public void createOnGlThread(Context context,
String objAssetName,
String diffuseTextureAssetName,
int rawObjectVertex,
int rawObjectFragment
) throws IOException {
// Read the texture.
Bitmap textureBitmap = BitmapFactory.decodeStream(
context.getAssets().open(diffuseTextureAssetName));
Expand Down Expand Up @@ -192,9 +195,9 @@ public void createOnGlThread(Context context, String objAssetName,
ShaderUtil.checkGLError(TAG, "OBJ buffer load");

final int vertexShader = ShaderUtil.loadGLShader(TAG, context,
GLES20.GL_VERTEX_SHADER, R.raw.object_vertex);
GLES20.GL_VERTEX_SHADER, rawObjectVertex);
final int fragmentShader = ShaderUtil.loadGLShader(TAG, context,
GLES20.GL_FRAGMENT_SHADER, R.raw.object_fragment);
GLES20.GL_FRAGMENT_SHADER, rawObjectFragment);

mProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(mProgram, vertexShader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.ar.core.Plane;
import com.google.ar.core.Pose;
import com.google.ar.core.TrackingState;
import com.outsystemsenterprise.enmobile11dev.ARAndroidExample.R;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -114,12 +113,14 @@ public PlaneRenderer() {
* @param context Needed to access shader source and texture PNG.
* @param gridDistanceTextureName Name of the PNG file containing the grid texture.
*/
public void createOnGlThread(Context context, String gridDistanceTextureName)
public void createOnGlThread(Context context, String gridDistanceTextureName,
int rawPlaneVertex, int rawPlaneFragment)
throws IOException {

int vertexShader = ShaderUtil.loadGLShader(TAG, context,
GLES20.GL_VERTEX_SHADER, R.raw.plane_vertex);
GLES20.GL_VERTEX_SHADER, rawPlaneVertex);
int passthroughShader = ShaderUtil.loadGLShader(TAG, context,
GLES20.GL_FRAGMENT_SHADER, R.raw.plane_fragment);
GLES20.GL_FRAGMENT_SHADER, rawPlaneFragment);

mPlaneProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(mPlaneProgram, vertexShader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.opengl.GLSurfaceView;
import android.opengl.Matrix;
import com.google.ar.core.PointCloud;
import com.outsystemsenterprise.enmobile11dev.ARAndroidExample.R;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
Expand Down Expand Up @@ -60,7 +59,7 @@ public PointCloudRenderer() {
*
* @param context Needed to access shader source.
*/
public void createOnGlThread(Context context) {
public void createOnGlThread(Context context, int rawPointCloudVertex, int rawPassthroughFragment) {
ShaderUtil.checkGLError(TAG, "before create");

int[] buffers = new int[1];
Expand All @@ -75,9 +74,9 @@ public void createOnGlThread(Context context) {
ShaderUtil.checkGLError(TAG, "buffer alloc");

int vertexShader = ShaderUtil.loadGLShader(TAG, context,
GLES20.GL_VERTEX_SHADER, R.raw.point_cloud_vertex);
GLES20.GL_VERTEX_SHADER, rawPointCloudVertex);
int passthroughShader = ShaderUtil.loadGLShader(TAG, context,
GLES20.GL_FRAGMENT_SHADER, R.raw.passthrough_fragment);
GLES20.GL_FRAGMENT_SHADER, rawPassthroughFragment);

mProgramName = GLES20.glCreateProgram();
GLES20.glAttachShader(mProgramName, vertexShader);
Expand Down

0 comments on commit 740da31

Please sign in to comment.