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

  • Home
  • SEARCH
  • 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 8422833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:34:01+00:00 2026-06-10T03:34:01+00:00

I need to detect mouse motion and draw a ball at the mouse’s position.

  • 0

I need to detect mouse motion and draw a ball at the mouse’s position. I need the ball to be in world coordinate. So I’m trying to use glUnProject for this task and seems I’m not succeeding till now. This is my motionFunc:

void motionFunc( int x, int y)
{


    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;
    GLdouble posX, posY, posZ;

    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetIntegerv( GL_VIEWPORT, viewport );

    winX = (float)x;
    winY = (float)viewport[3] - (float)y;
    glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

    gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);      //printf("winz: %f posz: %f\n",winZ,posZ);
    Ball.x=posX;
    Ball.y=posY;
    Ball.z=posZ;
    //printf("%f %f %f\n",Ball.x,Ball.y,posZ);
    glutPostRedisplay();
}

now, I added a breakpoint on glutPostRedisplay. Turns out when I click-drag mouse, the Ball’s coordinates(Ball.x,Ball.y,Ball.z) are something like:

(Ball).x -727.175354
(Ball).y 407.310242
(Ball).z -865.000610

why is the z coordinate so far? My camera is at z=+135. And other objects in my model are like, at z= -3 to +3. I need the ball’s z coordinate to be in the same range.

now, what exactly is winZ? Here, I checked it always turns out to be 1.00. I tried to hardcode winZ and I found at winZ=0.85, the ball seems like to be always under the mouse(I can drag the ball with my mouse and the ball is always under the pointer). But then the Ball’s coordinates are like:

(Ball).x -4.67813921
(Ball).y 2.57806134
(Ball).z 128.370895

which is so close to the camera but x and y coordinates are not good for me. they always come out to be near the origin. which is not what I want. My other objects’ x and y coordinates have a wider range.

Finally, my question is, what is the correct way to do glUnproject or something of the same sort?

  • 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-10T03:34:02+00:00Added an answer on June 10, 2026 at 3:34 am

    A 2D mouse coordinate cannot unambiguously unmapped to a 3D world coordinate. The 2D coordinate corresponds with a line in 3D space. The winz influences which point on this line is returned. When you use 0.0 for winz you will get the point at the near clipping plane. When you use 1.0 you will get the point at the far clipping plane.

    If you are using a depth buffer you could retrieve the value from the depth buffer using the glReadPixels function and use that as winz. Here a piece of code from a toy project of mine in Java

        FloatBuffer depth = BufferUtils.createFloatBuffer(1);
        glReadPixels(mouse_x, mouse_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, depth);
    
        depth.rewind();
        FloatBuffer farPos = BufferUtils.createFloatBuffer(3);
        GLU.gluUnProject(mouse_x, mouse_y, depth.get(), 
                mainContext.getModelviewMatrix(), 
                mainContext.getProjectionMatrix(), viewport, farPos);
    

    Most is the same in C/C++ except for the weird NIO buffers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to detect character encoding in HTTP responses. To do this I look
I have a CListBox with custom drawing being used, and need to detect mouse-clicks
I'm trying to detect the distance the mouse has moved, in pixels. I am
I need to detect when the user moves the mouse over the Form and
I need to detect when the mouse or keyboard activity is present. My application
I need a JavaScript method to detect when the mouse leaves an element with
I need to detect all the red pixels in an image using Java. What
I need to detect IE7 (and IE6) using the user agent string: I have
I need to detect android device orientation change without playing manually with sensor data,
I need to detect which version of SQL I am dealing with to perorm

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.