0

This is my class file which use to load 3d model . i am using jpct lib for loading 3d object . i want to apply pinch zoom to GLSurfaceView can any one tell me how to do ? i didnt found any tut/code for zoom ..

public class Model extends Activity {

// Used to handle pause and resume...
private static Model master = null;
ProgressDialog pDialog;
private GLSurfaceView mGLView;
private MyRenderer renderer = null;
private FrameBuffer fb = null;
static World world = null;
// private RGBColor back = new RGBColor(0, 50, 100);
Connection_Detector cd = new Connection_Detector(this);
private float touchTurn = 0;
private float touchTurnUp = 0;
private float xpos = -1;
private float ypos = -1;
private Object3D cube = null;
private int fps = 0;
private Light sun = null;
Camera mCamera;
SurfaceView mPreview;
String company, model, pid, reponse;
TextView name;
RelativeLayout call, info, email, tryit;
private AsyncTask<Void, Void, Void> details;

boolean IsInternetPresent;
TextureManager tm;
SharedPreferences atPrefs;
ProgressBar pb;
ScaleGestureDetector mScaleDetector;

protected void onCreate(Bundle savedInstanceState) {

    Logger.log("onCreate");

    if (master != null) {
        copy(master);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.model);
    Bundle bundle = getIntent().getExtras();

    ActionBar ab = getActionBar();

    ab.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#f9564b")));
    ab.setDisplayHomeAsUpEnabled(true);
    atPrefs = PreferenceManager.getDefaultSharedPreferences(Model.this);
    mGLView = (GLSurfaceView) findViewById(R.id.glsurfaceview);
    mGLView.setZOrderOnTop(true);
    mScaleDetector = new ScaleGestureDetector(getApplicationContext(),
            new ScaleListener());
    // set bg
    mGLView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    mGLView.getHolder().setFormat(PixelFormat.RGBA_8888);
    renderer = new MyRenderer();
    mGLView.setRenderer(renderer);
    pb = (ProgressBar) findViewById(R.id.progressBar1);
    // pb.setVisibility(View.VISIBLE);
    name = (TextView) findViewById(R.id.name);
    call = (RelativeLayout) findViewById(R.id.callrel);
    info = (RelativeLayout) findViewById(R.id.inforel);
    email = (RelativeLayout) findViewById(R.id.emailrel);
    tryit = (RelativeLayout) findViewById(R.id.tryel);
    name.setText(company + " " + model);
    call.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

        }
    });
    email.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });

    info.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });
    tryit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {


        }
    });
}

// public Model() {
// // TODO Auto-generated constructor stub
// mScaleDetector = new ScaleGestureDetector(getApplicationContext(),
// new ScaleListener());


@Override
protected void onPause() {
    super.onPause();
    mGLView.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    mGLView.onResume();
}

@Override
protected void onStop() {
    super.onStop();
}

private void copy(Object src) {
    try {
        Logger.log("Copying data from master Activity!");
        Field[] fs = src.getClass().getDeclaredFields();
        for (Field f : fs) {
            f.setAccessible(true);
            f.set(this, f.get(src));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

private class ScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float mScaleFactor = detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 10.0f));

        // pass it to your renderer
        renderer.setZoom(mScaleFactor);
        Log.d("SCALE", Float.toString(mScaleFactor));
        return true;
    }
}

public boolean onTouchEvent(MotionEvent me) {
    mScaleDetector.onTouchEvent(me);
    // if (me.getAction() == MotionEvent.ACTION_DOWN) {
    // xpos = me.getX();
    // ypos = me.getY();
    // return true;
    // }
    //
    // if (me.getAction() == MotionEvent.ACTION_UP) {
    // xpos = -1;
    // ypos = -1;
    // touchTurn = 0;
    // touchTurnUp = 0;
    // return true;
    // }
    //
    // if (me.getAction() == MotionEvent.ACTION_MOVE) {
    // float xd = me.getX() - xpos;
    // float yd = me.getY() - ypos;
    //
    // xpos = me.getX();
    // ypos = me.getY();
    //
    // touchTurn = xd / -100f;
    // touchTurnUp = yd / -100f;
    // return true;
    // }

    try {
        Thread.sleep(15);
    } catch (Exception e) {
    }

    return super.onTouchEvent(me);
}

protected boolean isFullscreenOpaque() {
    return true;
}

class MyRenderer implements GLSurfaceView.Renderer {

    private long time = System.currentTimeMillis();

    public MyRenderer() {
    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        if (fb != null) {
            fb.dispose();
        }

        fb = new FrameBuffer(gl, w, h);
        if (master == null) {

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

            sun = new Light(world);
            sun.setIntensity(250, 250, 250);
            File sdRoot = Environment.getExternalStorageDirectory();
            File imgFile = new File(sdRoot + "/folder/1.obj");
            InputStream inputStream = null;
            try {
                inputStream = new FileInputStream(file);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            File imgFile = new File(sdRoot + "/folder/1.jpg");
            cube = Object3D.mergeAll(Loader.loadOBJ(inputStream, null, 5f));

            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
                    .getAbsolutePath());
            tm = TextureManager.getInstance();

            Texture test = new Texture(myBitmap);
            tm.addTexture("text", test);
            // load text
            TextureInfo ti = new TextureInfo(TextureManager.getInstance()
                    .getTextureID("text"));

            cube.setTexture(ti);

            cube.build();
            cube.strip();
            myBitmap.recycle();
            world.addObject(cube);

            //
            Camera cam = world.getCamera();
            cam.moveCamera(Camera.CAMERA_MOVEOUT, 50);
            cam.lookAt(cube.getTransformedCenter());
            // runOnUiThread(new Runnable() {
            // @Override
            // public void run() {
            //
            // pb.setVisibility(View.GONE);
            //
            // }
            // });
            SimpleVector sv = new SimpleVector();
            sv.set(cube.getTransformedCenter());
            sv.y -= 100;
            sv.z -= 100;
            sun.setPosition(sv);
            MemoryHelper.compact();

            if (master == null) {
                Logger.log("Saving master Activity!");
                master = Model.this;
            }
        }
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

    }

    public void onDrawFrame(GL10 gl) {
        if (touchTurn != 0) {
            cube.rotateY(touchTurn);
            touchTurn = 0;
        }

        if (touchTurnUp != 0) {
            cube.rotateX(touchTurnUp);
            touchTurnUp = 0;
        }

        // fb.clear(back);
        world.renderScene(fb);
        world.draw(fb);
        fb.display();

        if (System.currentTimeMillis() - time >= 1000) {
            Logger.log(fps + "fps");
            fps = 0;
            time = System.currentTimeMillis();
        }
        fps++;
    }

    public void setZoom(float scale) {
        cube.setScale(scale);
    }

}

@Override
public void onBackPressed() {
    Intent back = new Intent(getApplicationContext(), HomeScreen.class);
    startActivity(back);
    // reset 3d model data and text
    master = null;
    tm.removeTexture("text");
    finish();

    super.onBackPressed();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        onBackPressed();

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

 }xml file 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.opengl.GLSurfaceView
    android:id="@+id/glsurfaceview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >
</android.opengl.GLSurfaceView>

user3405716
  • 13
  • 1
  • 9
  • Where is your perspective matrix? Something like frustum is most common. Anyway if you are using this somewhere it is easiest to simply play around with this method: Simply multiply the up, down... parameters with the zoom. Multiplying by .5 for instance would decrease the field of view making it seem like zoomed in. – Matic Oblak Aug 28 '14 at 13:23
  • Can you get to the perspective matrix somehow and post it? Your camera object is most likely responsible for it. It is probably using frustum which you can modify to implement zooming. – Matic Oblak Sep 02 '14 at 09:30
  • no i am not getting it .. i have posted full code .. i am using jpct lib to load model – user3405716 Sep 02 '14 at 09:34
  • u want camera preview class code also ? – user3405716 Sep 02 '14 at 10:06
  • I guess it would be great if you find something like frustum, aspect ration, angle of view... If all fails I guess you could bring the camera closer to the object which would have the same result to some value.. – Matic Oblak Sep 02 '14 at 10:17
  • if there are any snippet to pinch zoom GLSurfaceView – user3405716 Sep 02 '14 at 10:19

2 Answers2

1

use scale listener on ImageTarget.java

//imageTragets.java 

    ImageTarget()
    {

    ScaleGestureDetector mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener());

    }
//in onTouch function 
    onTouch()
    {

    mScaleDetector.onTouchEvent(event);

    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
              float  mScaleFactor *= detector.getScaleFactor();

                // Don't let the object get too small or too large.
                mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 4.0f));

    //pass it to your renderer
                Renderer.setZoom(mScaleFactor);
                Log.d("SCALE",Float.toString(mScaleFactor));
                return true;
            }

        }

get the float value in ImageTargetsRenderer.java and set it to the object

public void setZoom(float scale)
    {
        obj.setScale(scale);
    }

thats it you can zoom in and zoom out object .. hope thats wat you want ..

hari
  • 69
  • 4
0

imageTarget.java - is the agument class. imagetargetRender.java - is it renderer

these class name are used by me so i hav give them..

for the above example HelloWorld.java is imageTarget.java and MyRenderer.java is imageTargetRenderer.java

hari
  • 69
  • 4
  • where u calling ImageTarget() ,onTouch()? – user3405716 Sep 11 '14 at 05:56
  • onTouch in the above code is `onTouchEvent(MotionEvent me)` and `ImageTarget()` is my constructor. – hari Sep 11 '14 at 11:02
  • ya its correct , is it working fine ? and just wanted to know why have you included `sleep()` inside onTouchEvent ? – hari Sep 12 '14 at 06:49
  • no ,its not working , object is not zooming ... for sleep it was their in jpct tut i havent change logic for that – user3405716 Sep 12 '14 at 07:23
  • can u please remove that `try { Thread.sleep(15); } catch (Exception e) { } return super.onTouchEvent(me);` piece of code or comment it and just add `return true;` then clean and Rerun your project – hari Sep 15 '14 at 07:35
  • can you mail me your project so that i could analyse it completely – hari Sep 16 '14 at 12:47