改变简单的ARToolkit和JPCT-AE来显示3d模型

时间:2017-05-03 07:38:22

标签: android opengl-es artoolkit jpct

使用jpct-ae引擎

更改简单的ARToolkit android应用程序以显示3d模型

我想在Hiro标记上显示3d模型。我下载了简单的ARToolkit应用程序并添加了jpct-ae引擎作为依赖项。然后将应用程序中的SimpleRenderer.java更改为以下代码。 我尝试了以下代码。当我只将“立方体”添加到世界时,它工作得很好但是当我向世界添加“坦克”时它不会在标记上显示任何内容。我觉得我错过了什么。

   package org.artoolkit.ar.samples.ARSimple;

    import android.content.Context;
    import android.content.res.AssetManager;
    import android.util.Log;

    import com.threed.jpct.Camera;
    import com.threed.jpct.FrameBuffer;
    import com.threed.jpct.Light;
    import com.threed.jpct.Loader;
    import com.threed.jpct.Matrix;
    import com.threed.jpct.Object3D;
    import com.threed.jpct.Primitives;
    import com.threed.jpct.SimpleVector;
    import com.threed.jpct.Texture;
    import com.threed.jpct.TextureManager;
    import com.threed.jpct.World;
    import com.threed.jpct.util.BitmapHelper;

    import javax.microedition.khronos.opengles.GL10;

    import org.artoolkit.ar.base.ARToolKit;
    import org.artoolkit.ar.base.rendering.ARRenderer;
    import org.artoolkit.ar.base.rendering.Cube;
    import org.artoolkit.ar.samples.R;

    import java.io.IOException;

    /**
     * A very simple Renderer that adds a marker and draws a cube on it.
     */
    public class SimpleRenderer extends ARRenderer {

        private int markerId1 = -1;

        private float angle = 0.0f;
        private boolean spinning = false;

        Matrix projMatrix=new Matrix(); //the matrix having the camera projection
        Matrix markerMatrix=new Matrix(); //the matrix having the transformation to place the tank in the marker
        Matrix modelMatrix=new Matrix(); //the matrix having the model (tank) rotation and translation
        private boolean markerVisible;
        private Camera camera;
        private World world;
        private Object3D tank;
        private Object3D cube;
        private float movementX;
        private float movementY;
        private FrameBuffer frameBuffer;
        private Cube cube2 = new Cube(40.0f, 0.0f, 0.0f, 20.0f);

        @Override
        public boolean configureARScene() {
            configureJPCTWorld();
            markerId1 = ARToolKit.getInstance().addMarker("single;Data/patt.hiro;80");
            if (markerId1 < 0) return false;

            return true;

        }


        public void click() {
            spinning = !spinning;
        }

        @Override
        public void onSurfaceChanged(GL10 unused, int w, int h) {
            super.onSurfaceChanged(unused, w, h);
            if(frameBuffer!=null){
                frameBuffer.dispose();
            }
            frameBuffer=new FrameBuffer(unused,w,h);
        }

        public void configureJPCTWorld(){
            world=new World();
            world.setAmbientLight(20, 20, 20);

            Light sun=new Light(world);
            sun.setIntensity(250, 250, 250);

            camera=world.getCamera();

            Texture cubeText=
                    new Texture(
                            BitmapHelper.rescale(
                                    BitmapHelper.convert(
                                            ARSimpleApplication.context.
                                                    getResources().
                                                    getDrawable(R.drawable.crate_1)
                                    ),128,128
                            )
                    );

            TextureManager.getInstance().addTexture("Crate_1",cubeText);

            cube = Primitives.getCube(40);
            cube.calcTextureWrapSpherical();
            cube.setTexture("Crate_1");
            cube.strip();
            cube.build();

            try {
                AssetManager assetManager =
                        ARSimpleApplication.context.getAssets();
                Object3D[] objects =
                        Loader.load3DS(assetManager.open("Crate1.3ds"), 0.1f);

                tank= Object3D.mergeAll(objects);
                tank.setTexture("Crate_1");
                tank.strip();
                tank.build();

            }catch(IOException e){
                Log.e("RENDERER-JPCT","Error loading tank",e);
            }

            //MemoryHelper.compact();

        }



           @Override
            public void draw(GL10 gl) {

                world.removeAllObjects();
                markerVisible=false;

                float[] projectionCamera=ARToolKit.getInstance().getProjectionMatrix();


                projMatrix.setIdentity();

                projMatrix.setDump(projectionCamera);
                projMatrix.transformToGL();
                SimpleVector translation=projMatrix.getTranslation();
                SimpleVector dir=projMatrix.getZAxis();
                SimpleVector up=projMatrix.getYAxis();
                camera.setPosition(translation);
                camera.setOrientation(dir, up);

                if(ARToolKit.getInstance().queryMarkerVisible(markerId1)){
                    //Matrix markerMatrix=new Matrix();
                    markerMatrix.setIdentity();
                    //Log.e("RENDERER", "Marker1 detected");

                    float[] marker1Transformation=
                            ARToolKit.
                                    getInstance().
                                    queryMarkerTransformation(markerId1);

                    markerMatrix.setDump(marker1Transformation);
                    markerMatrix.transformToGL();

                    //cube.clearTranslation();
                    //cube.clearRotation();

                    cube.setRotationMatrix(markerMatrix);
                    cube.setTranslationMatrix(markerMatrix);

                    world.addObject(cube);// This works fine
                    world.addObject(tank); // This is not displayed

                }

                frameBuffer.clear();
                world.renderScene(frameBuffer);
                world.draw(frameBuffer);
                frameBuffer.display();

                //if(markerVisible)updateMovement(4,2);

            }


    }

0 个答案:

没有答案