Author Topic: How to rotate model.  (Read 2176 times)

Offline Płoteck

  • byte
  • *
  • Posts: 3
    • View Profile
How to rotate model.
« on: December 07, 2014, 09:18:25 pm »
Hello everyone ;)

I want do in my application, control on model by rotate, scaling on touch screen of device, but i got no clue how to that in example code of integrating Vuforia with JPCT-AE.

Please give some advices or something ;)

P.S. Sorry about my english.



Code: [Select]
public class ImageTargetsRenderer implements GLSurfaceView.Renderer
{
    public boolean mIsActive = false;
   
    /** Reference to main activity **/
    public ImageTargets mActivity;

private FrameBuffer fb;

private World world;

private float[] modelViewMat;

private Light sun;

private Object3D cube;

private Camera cam;

private float fov;

private float fovy;

/* Rotation values */
private float xrot; //X Rotation
private float yrot; //Y Rotation

/* Rotation speed values */

private float xspeed; //X Rotation Speed ( NEW )
private float yspeed; //Y Rotation Speed ( NEW )

private float scale = 5.0f;

private float oldX;
    private float oldY;
private final float TOUCH_SCALE = 0.4f; //Proved to be good for normal rotation ( NEW )

private int z;
   
   
    /** Native function for initializing the renderer. */
    public native void initRendering();
   
   
    /** Native function to update the renderer. */
    public native void updateRendering(int width, int height);
   
    public ImageTargetsRenderer(ImageTargets activity) throws IOException {
this.mActivity = activity;

world = new World();
world.setAmbientLight(20, 20, 20);

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

    InputStream objStream = new FileInputStream("/mnt/sdcard/models/armchair.obj");
InputStream mtlStream = new FileInputStream("/mnt/sdcard/models/armchair.mtl");




Object3D[] model = Loader.loadOBJ(objStream, mtlStream, scale);
        Object3D o3d = new Object3D(0);
        Object3D temp = null;
       
        for (int i = 0; i < model.length; i++) {
            temp = model[i];
            temp.setCenter(SimpleVector.ORIGIN);
            temp.rotateMesh();
            temp.setRotationMatrix(new Matrix());
            o3d = Object3D.mergeObjects(o3d, temp);
            o3d.build();
        }

       
        o3d.rotateX(xrot);
        o3d.rotateY(yrot);
        o3d.rotateZ(z);
       
world.addObject(o3d);

cam = world.getCamera();

SimpleVector sv = new SimpleVector();
sv.set(o3d.getTransformedCenter());
sv.y -= 100;
sv.z -= 100;

sun.setPosition(sv);

MemoryHelper.compact();





        }
   
    public boolean onTouchEvent(MotionEvent event) {
//
float x = event.getX();
        float y = event.getY();
       
        //If a touch is moved on the screen
        if(event.getAction() == MotionEvent.ACTION_MOVE) {
        //Calculate the change
        float dx = x - oldX;
        float dy = y - oldY;
        //Define an upper area of 13% on the screen
        int upperArea = fb.getHeight() / 13;
       
       
        //Zoom in/out if the touch move has been made in the upper
        if(y < upperArea) {
        scale -= dx * TOUCH_SCALE / 2;
       
        //Rotate around the axis otherwise
        } else {       
            xrot += dy * TOUCH_SCALE;
            yrot += dx * TOUCH_SCALE;
        }       
       
        //A press on the screen
        } else if(event.getAction() == MotionEvent.ACTION_UP) {


        }
       
        //Remember the values
        oldX = x;
        oldY = y;
       
        //We handled the event
return true;
}
   
    public boolean onKeyUp(int keyCode, KeyEvent event) {
//
if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {

} else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {

} else if(keyCode == KeyEvent.KEYCODE_DPAD_UP) {
z -= 3;

} else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
z += 3;

} else if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {

}

//We handled the event
return true;
}
   
   
   
    /** Called when the surface is created or recreated. */
    public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
        DebugLog.LOGD("GLRenderer::onSurfaceCreated");
       
        // Call native function to initialize rendering:
        initRendering();
       
        // Call Vuforia function to (re)initialize rendering after first use
        // or after OpenGL ES context was lost (e.g. after onPause/onResume):
        QCAR.onSurfaceCreated();
    }
   
   
   
   
   
    /** Called when the surface changed size. */
    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
    DebugLog.LOGD(String.format("GLRenderer::onSurfaceChanged (%d, %d)", width, height));

if (fb != null) {
fb.dispose();
}
fb = new FrameBuffer(width, height);
Config.viewportOffsetAffectsRenderTarget=true;
       
        // Call native function to update rendering when render surface
        // parameters have changed:
        updateRendering(width, height);
       
        // Call Vuforia function to handle render surface size changes:
        QCAR.onSurfaceChanged(width, height);
    }
   
   
    /** The native render function. */
    public native void renderFrame();
   
   
    /** Called to draw the current frame. */
    public void onDrawFrame(GL10 gl)
    {
        if (!mIsActive)
            return;
       
        // Update render view (projection matrix and viewport) if needed:
        mActivity.updateRenderView();
       
        // Call our native function to render content
        renderFrame();
       
        updateCamera();

world.renderScene(fb);

world.draw(fb);
fb.display();
    }
   
    public void updateModelviewMatrix(float mat[]) {
modelViewMat = mat;
}
   
   
    public void updateCamera() {
if (modelViewMat != null) {
Matrix m = new Matrix();
m.setDump(modelViewMat);
cam.setBack(m);
cam.setFOV(fov);
cam.setYFOV(fovy);
}
}

public void setVideoSize(int videoWidth, int videoHeight) {

DisplayMetrics displaymetrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

int widestVideo = videoWidth > videoHeight? videoWidth: videoHeight;
int widestScreen = width > height? width: height;

float diff = (widestVideo - widestScreen) / 2;

Config.viewportOffsetY = diff / widestScreen;
}

public void setFov(float fov) {
this.fov = fov;
}

public void setFovy(float fovy) {
this.fovy = fovy;
}
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: How to rotate model.
« Reply #1 on: December 08, 2014, 08:23:01 am »
The example that comes with jPCT-AE actually does this: http://www.jpct.net/wiki/index.php/Hello_World_for_Android#onTouchEvent.28MotionEvent_me.29

I'm not sure how this relates to your Vuforia related stuff...are you actually using that or was this just an overcomplicated attempt to rotate things?

Offline Płoteck

  • byte
  • *
  • Posts: 3
    • View Profile
Re: How to rotate model.
« Reply #2 on: December 08, 2014, 10:39:09 am »
I use that, but I dont see effects of that, when i try to rotate.
I need that rotate and scaling to end my work finnaly.  :P

Thanks for the link, I will try this example and give answer is it work or not ;)

Offline Płoteck

  • byte
  • *
  • Posts: 3
    • View Profile
Re: How to rotate model.
« Reply #3 on: December 08, 2014, 03:59:43 pm »
Ok, rotation works.
Thanks for help now I can end my work ;p

I was left with only describe all the work , that is the worst part :(