Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8804849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:42:47+00:00 2026-06-14T01:42:47+00:00

I’m trying to implement a pan and pinch-to-zoom feature into an OpenGL application I

  • 0

I’m trying to implement a pan and pinch-to-zoom feature into an OpenGL application I have. I was able to implement the pan feature relatively easily with a simple glTranslatef, but using the glScalef function is presenting much more difficulty. I have three classes, one for the GLSurfaceView, one for the GLTriangle, and the third (below) for the GLRenderer, which is where the glScalef and glTranslatef are used.

GLRenderer Class:

public class GLRendererEx implements Renderer, OnTouchListener {

    private GLTriangleEx tri;
    float ratio;
    float x = 0, y = 0;
    float dx = 0, dy = 0;
    float sx = 0, sy = 0;
    float tx = 0, ty = 0;
    float xtwo = 0, ytwo = 0;
    float sxtwo = 0, sytwo = 0;
    float screenX, screenY;
    float xscale = 1, yscale = 1;
    float xscaletwo = 1;
    float xscaletotal = 1, yscaletotal = 1;
    int NONE = 0, DRAG = 1, ZOOM = 2;
    int mode = NONE;
    int width, height;
    boolean touched = true;
    Display getOrient = getWindowManager().getDefaultDisplay();
    int orientation;

    public GLRendererEx() {
        tri = new GLTriangleEx();
        screenX = ourSurface.getWidth();
        screenY = ourSurface.getHeight();
        ourSurface.setOnTouchListener(this);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        gl.glClearColor(0f, 0f, 0f, 1f);
        gl.glClearDepthf(1f);
        orientation = getOrient.getRotation();
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2, 0);

        //warning - x is backwards
        gl.glTranslatef(tx, ty, 0);
        gl.glScalef(xscaletotal, yscaletotal, 1);
        tri.draw(gl);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        ratio = ((float) width) / height;
        screenX = width;
        screenY = height;
        orientation = getOrient.getRotation();
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 50);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            event.getActionIndex();
            sx = x = event.getX(0);
            sy = y = event.getY(0);
            mode = DRAG;
            break;
        }
        case MotionEvent.ACTION_POINTER_DOWN: {
            event.getActionIndex();
            sxtwo = xtwo = event.getX(1);
            sytwo = ytwo = event.getY(1);
            mode = ZOOM;
            touched = true;
            break;
        }
        case MotionEvent.ACTION_MOVE: { 
            x = event.getX(0);
            y = event.getY(0);
            xtwo = event.getX(1);
            ytwo = event.getY(1);
            if (mode == ZOOM)
                midPoint(x, y, xtwo, ytwo);
            if (mode == DRAG) {
                dx = x - sx;
                dy = y - sy;
                dx *= -8;
                dy *= -8;
                if (orientation == Configuration.ORIENTATION_LANDSCAPE) 
                    dx *= 2;
                dx /= screenX;
                dy /= screenY;
                tx += dx;
                ty += dy;
                sx = x;
                sy = y;
            }
            break;
        }       
        case MotionEvent.ACTION_POINTER_UP: {
            mode = DRAG;
            break;
        }
        case MotionEvent.ACTION_UP: {
            mode = NONE;
            break;
        }
        }
        return true;
    }

    private float midPoint(float x, float y, float xtwo, float ytwo) {
        x -= xtwo;
        y -= ytwo;
        if (touched) {
            sxtwo = x;
            sytwo = y;
            xscaletwo = FloatMath.sqrt(sxtwo * sxtwo + sytwo * sytwo);
                            touched = false;
        }
                    xscale -= xscaletwo;
        xscale  = FloatMath.sqrt(x * x + y * y);
        xscaletotal += xscale;
        yscaletotal = xscaletotal;
        return xscale;
    }

}

x/y‘s are coordinates, dx/dy are the change in the coords, sx/sy‘s are starting coords, tx/ty are the total changes in coords (for panning), xscale/yscale‘s are the floats used to calculate the scale for zooming, xscaletotal/yscaletotal are total changes in zoom scale, none drag zoom and mode represent the mode.

As it stands, I can zoom in by pulling my fingers apart, but a pinch also zooms instead of zooming out. Also, the zoom is much too sensitive.

I don’t really know how to solve this problem from here. Any help would be more than appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:42:48+00:00Added an answer on June 14, 2026 at 1:42 am

    It would seem your “midPoint” has some work to do.. Try doing something like this:

    float scale = 1.0; //factor used in glScalef
    float previousPinchScale; //needed to remember from previous methode call
    private float midPoint(float x, float y, float xtwo, float ytwo) {
        float currentPinchScale = FloatMath.sqrt((x-xTwo)*(x-xTwo) + (y-yTwo)*(y-yTwo)); //current distance between both "touches"
        if (touched) { //gesture just began:
            previousPinchScale = currentPinchScale; //Save relative information about touch positions
            touched = false;
        }
        else { //gesture in progress:
            /*
             At this point we have 2 values of "PinchScale" for this and previous "midPoint" method call.
             The factor of relative change in this distance should be applied to current scene scale:
             currentPinchScale/previousPinchScale is the "relative change", for instance if the distance between the 2 touches would be 10% this factor would return 1.1
             Since we have to consider that scene might already be zoomed you need to take previous scale and multiply it with this change..
             */
            scale *= currentPinchScale/previousPinchScale; //scale used in glScalef
            previousPinchScale = currentPinchScale; //remember the new position of "midPoint" call
        }
        return scale;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.