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 8298001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:36:37+00:00 2026-06-08T15:36:37+00:00

I’m building an Android application that uses OpenGL ES 2.0 and I’ve run into

  • 0

I’m building an Android application that uses OpenGL ES 2.0 and I’ve run into a wall. I’m trying to convert screen coordinates (where the user touches) to world coordinates. I’ve tried reading and playing around with GLU.gluUnProject but I’m either doing it wrong or just don’t understand it.

This is my attempt….

public void getWorldFromScreen(float x, float y) {
    int viewport[] = { 0, 0, width , height};

    float startY = ((float) (height) - y);
    float[] near = { 0.0f, 0.0f, 0.0f, 0.0f };
    float[] far = { 0.0f, 0.0f, 0.0f, 0.0f };

    float[] mv = new float[16];
    Matrix.multiplyMM(mv, 0, mViewMatrix, 0, mModelMatrix, 0);

    GLU.gluUnProject(x, startY, 0, mv, 0, mProjectionMatrix, 0, viewport, 0, near, 0);
    GLU.gluUnProject(x, startY, 1, mv, 0, mProjectionMatrix, 0, viewport, 0, far, 0);

    float nearX = near[0] / near[3];
    float nearY = near[1] / near[3];
    float nearZ = near[2] / near[3];

    float farX = far[0] / far[3];
    float farY = far[1] / far[3];
    float farZ = far[2] / far[3];
}

The numbers I am getting don’t seem right, is this the right way to utilize this method? Does it work for OpenGL ES 2.0? Should I make the Model Matrix an identity matrix before these calculations (Matrix.setIdentityM(mModelMatix, 0))?

As a follow up, if this is correct, how do I pick the output Z? Basically, I always know at what distance I want the world coordinates to be at, but the Z parameter in GLU.gluUnProject appears to be some kind of interpolation between the near and far plane. Is it just a linear interpolation?

Thanks in advance

  • 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-08T15:36:38+00:00Added an answer on June 8, 2026 at 3:36 pm
    /**
        * Calculates the transform from screen coordinate
        * system to world coordinate system coordinates
        * for a specific point, given a camera position.
        *
        * @param touch Vec2 point of screen touch, the
          actual position on physical screen (ej: 160, 240)
        * @param cam camera object with x,y,z of the
          camera and screenWidth and screenHeight of
          the device.
        * @return position in WCS.
        */
       public Vec2 GetWorldCoords( Vec2 touch, Camera cam)
       {  
           // Initialize auxiliary variables.
           Vec2 worldPos = new Vec2();
    
           // SCREEN height & width (ej: 320 x 480)
           float screenW = cam.GetScreenWidth();
           float screenH = cam.GetScreenHeight();
    
           // Auxiliary matrix and vectors
           // to deal with ogl.
           float[] invertedMatrix, transformMatrix,
               normalizedInPoint, outPoint;
           invertedMatrix = new float[16];
           transformMatrix = new float[16];
           normalizedInPoint = new float[4];
           outPoint = new float[4];
    
           // Invert y coordinate, as android uses
           // top-left, and ogl bottom-left.
           int oglTouchY = (int) (screenH - touch.Y());
    
           /* Transform the screen point to clip
           space in ogl (-1,1) */       
           normalizedInPoint[0] =
            (float) ((touch.X()) * 2.0f / screenW - 1.0);
           normalizedInPoint[1] =
            (float) ((oglTouchY) * 2.0f / screenH - 1.0);
           normalizedInPoint[2] = - 1.0f;
           normalizedInPoint[3] = 1.0f;
    
           /* Obtain the transform matrix and
           then the inverse. */
           Print("Proj", getCurrentProjection(gl));
           Print("Model", getCurrentModelView(gl));
           Matrix.multiplyMM(
               transformMatrix, 0,
               getCurrentProjection(gl), 0,
               getCurrentModelView(gl), 0);
           Matrix.invertM(invertedMatrix, 0,
               transformMatrix, 0);       
    
           /* Apply the inverse to the point
           in clip space */
           Matrix.multiplyMV(
               outPoint, 0,
               invertedMatrix, 0,
               normalizedInPoint, 0);
    
           if (outPoint[3] == 0.0)
           {
               // Avoid /0 error.
               Log.e("World coords", "ERROR!");
               return worldPos;
           }
    
           // Divide by the 3rd component to find
           // out the real position.
           worldPos.Set(
               outPoint[0] / outPoint[3],
               outPoint[1] / outPoint[3]);
    
           return worldPos;       
       }
    

    Algorithm is further explained here.

    • 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 a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
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
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.