Author Topic: Change object texture in loop.  (Read 176 times)

Offline Plantaer

  • byte
  • *
  • Posts: 2
    • View Profile
Change object texture in loop.
« on: April 25, 2017, 02:02:05 pm »
Hi. I'm working on android ARToolkit and JPCT-AE app. I'd like to switch 2 textures of 3D object in some amount of time.
There I define objects:

Code: [Select]
public final List<TrackableObject3d> getTrackableObject3DList() {
        List<TrackableObject3d> list = new ArrayList<TrackableObject3d>();
        populateTrackableObjects(list);
        return list;
    }

    protected void populateTrackableObjects(List<TrackableObject3d> list) {
        Texture green = new Texture(ContextCompat.getDrawable(this, getTextureRes()[0]));
        TextureManager.getInstance().addTexture(TEXTURE_NAME, green);

        Texture red = new Texture(ContextCompat.getDrawable(this, getTextureRes()[1]));
        TextureManager.getInstance().addTexture(TEXTURE_NAME_RED, red);

        String extention = ".patt;80";

        for (String name : markerString().keySet()) {
            String type = markerString().get(name).toLowerCase();
            switch (type){
                case "single" :
                    extention = ".patt;80";
                    break;
                case "nft" :
                    extention = "";
                    break;
                case "multi" :
                    extention = ".dat";

            }
            list.add(getTrackableObject(type+";Data/"+name+extention));
        }
    }

    private TrackableObject3d getTrackableObject(String name) {
        TrackableObject3d result = new TrackableObject3d(name);

        try {
            Object3D[] object = Loader.loadOBJ(getAssets().open(OBJ),
                    getAssets().open(MTL), 300);
            object[0].setOrigin(new SimpleVector(80, -75, 0));
            object[0].setTexture(TEXTURE_NAME_RED);
            object[0].setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);

            result.addChild(object[0]);

            Object3D object3D = Primitives.getPlane(2, 100);
            object3D.rotateX((float) Math.PI);
            object3D.setOrigin(new SimpleVector(0, 0, -5));
            object3D.setTransparencyMode(Object3D.TRANSPARENCY_MODE_DEFAULT);
            object3D.setTransparency(0);
            object3D.setLighting(Object3D.LIGHTING_NO_LIGHTS);
            object3D.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);

            result.addChild(object3D);

        } catch (Exception ignored) {
        }
        return result;
    }

My questions:
1. Where i should do animation, in "draw(GL10 gl)" method?
2. Is this should be animation or is any simpler way to change textures?

Additionally my draw(GL10 gl)
Code: [Select]
public final void draw(GL10 gl) {
        mBuffer.clear();

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

        projMatrix.setDump(projection);
        mCamera.setBack(projMatrix);
        SimpleVector translation = projMatrix.getTranslation();

        if (!mFovSet) {
            // Calculate FOV based on projection values, but do it only once
            float value1 = projection[5];
            float vFov = (float) Math.atan2(1, value1)*2;
            mCamera.setYFovAngle(vFov);
            float aspect = projection[5] / projection[0];


            float fov = (float) (2 * Math.atan2(mCamera.getYFOV() , 2 ) * aspect);
            mCamera.setFovAngle(fov);
            mFovSet = true;

        }

        SimpleVector dir = projMatrix.getZAxis();
        SimpleVector up = projMatrix.getYAxis();
        mCamera.setPosition(translation);
        mCamera.setOrientation(dir, up);

        for (int i=0; i<mTrackableObjects.size(); i++) {
            TrackableObject3d trackableObject = mTrackableObjects.get(i);
            trackableObject.updateMarkerTransformation();
        }

        mWorld.renderScene(mBuffer);
        mWorld.draw(mBuffer);
        mBuffer.display();
    }

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11747
    • View Profile
    • http://www.jpct.net
Re: Change object texture in loop.
« Reply #1 on: April 25, 2017, 02:45:20 pm »
It should happen somewhere in the drawing thread. What kind of animation is that? Just two textures that change constantly?

Offline Plantaer

  • byte
  • *
  • Posts: 2
    • View Profile
Re: Change object texture in loop.
« Reply #2 on: April 25, 2017, 04:03:09 pm »
I got it. I tried to setTexture() on Parent, not its Child and thought that it is impossible in draw method . Thanks for quick reply.  ;)

Code: [Select]
long current = System.currentTimeMillis();

        for (int i = 0; i < mTrackableObjects.size(); i++) {
            TrackableObject3d trackableObject = mTrackableObjects.get(i);

            if (mTrackableObjects.get(i).isObjectVisible()) {
                List<Object3D> mChildren = trackableObject.getChildren();
                Object3D  first = mChildren.get(0);
                if (current - START_TIME > delay) {

                    if (delay % 2 != 0) {
                        first.setTexture("green");
                    } else {
                         first.setTexture("red");
                    }
                    delay += 301;
                }
            }
            trackableObject.updateMarkerTransformation();
        }