将addMarker与ARToolKit一起使用

时间:2017-09-05 10:42:15

标签: android artoolkit

我目前正在开发一个Android应用程序,我想要包含ARToolKit来扫描图片并显示3D模型。

所以我将ARToolKit 6添加到我的Android Studio中,并且我自己快速创建了程序。接下来我想用addMarker添加标记,所以基本上我的代码就是这个。

class ARTrackingRenderer extends ARRenderer {

private static final class Trackable{
    String name;
    float height;

    Trackable(String name, float height)
    {
        this.name = name;
        this.height = height;
    }
}

private static final Trackable trackables[] = new Trackable[]{
        new Trackable("Alterra_Ticket_1.jpg", 95.3f),
        new Trackable("Alterra_Postcard_2.jpg", 95.3f),
        new Trackable("Alterra_Postcard_3.jpg", 127.0f),
        new Trackable("Alterra_Postcard_4.jpg", 95.3f)
};

private int trackableUIDs[] = new int[trackables.length];
private Cube cube;
private Cube cube2;

@Override
public boolean configureARScene() {
    int i = 0;
    for (Trackable trackable : trackables){
        trackableUIDs[i] = ARToolKit.getInstance().addMarker("2d;Data/2d/" + trackable.name + ";" + trackable.height);
        if (trackableUIDs[1] < 0) return false;
    }
    NativeInterface.arwSetTrackerOptionInt(NativeInterface.ARW_TRACKER_OPTION_2D_MAX_IMAGES, trackables.length);
    return true;
}

//Shader calls should be within a GL thread. GL threads are onSurfaceChanged(), onSurfaceCreated() or onDravFrame()
//As the cube instantiates the shader during setShaderProgram call  we need to create the cube here.
@Override
public void onSurfaceCreated(GL10 unused, javax.microedition.khronos.egl.EGLConfig config){
    this.shaderProgram = new SimpleShaderProgram(new SimpleVertexShader(), new SimpleFragmentShader());
    cube = new Cube(30.0f, 0.0f, 0.0f, 0.0f); //Das erste ist die Größe, das zweite die horizontale Ausrichtung, das dritte die vertikale und das vierte der Abstand vom boden aus
    cube2 = new Cube(15.0f, 50.0f, 0.0f, 20.0f);
    cube.setShaderProgram(shaderProgram);
    cube2.setShaderProgram(shaderProgram);
    super.onSurfaceCreated(unused, config);
}

@Override
public void draw(){
    super.draw();

    GLES20.glEnable(GLES20.GL_CULL_FACE);
    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glFrontFace(GLES20.GL_CCW);

    for (int trackableUID:trackableUIDs){
        if (ARToolKit.getInstance().queryMarkerVisible(trackableUID)){
            float[] projectionMatrix = ARToolKit.getInstance().getProjectionMatrix();
            float[] modelVievMatrix = ARToolKit.getInstance().queryMarkerTransformation(trackableUID);
            cube.draw(projectionMatrix, modelVievMatrix);
            cube2.draw(projectionMatrix, modelVievMatrix);
        }
    }
}

}

我从YouTube上的教程中获得了代码。我想添加新标记。我怎样才能做到这一点?或者基本上我怎么才能添加新图片,所以每次拍摄照片都不会扫描?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

此代码:

private static final Trackable trackables[] = new Trackable[]{
        new Trackable("Alterra_Ticket_1.jpg", 95.3f),
        new Trackable("Alterra_Postcard_2.jpg", 95.3f),
        new Trackable("Alterra_Postcard_3.jpg", 127.0f),
        new Trackable("Alterra_Postcard_4.jpg", 95.3f)
};

负责将名为Trackable的'标记'添加到ARToolKit。您可以将标记添加到trackables数组。

.jpg文件需要在与Alterra * .jpg文件平行的assets目录中可用。

只能使用.jpg文件。