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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:03:44+00:00 2026-06-02T22:03:44+00:00

my basic question is – what is the proper way to use ‘gluUnProject’ to

  • 0

my basic question is – what is the proper way to use ‘gluUnProject’ to get a Ray or normal vector from my mouse/cursor position.

I have converted the ‘gluUnProject’ code to C# from the mesa glu C code.

public Vector3 UnProject(Matrix4 projectionMatrix, Matrix4 viewMatrix, int viewX, int viewY, int viewWidth, int viewHeight)
{
    var finalMatrix = viewMatrix.Multiply(projectionMatrix).Invert();

    var vec = new Vector4(this, 1);
    vec.X = (vec.X - viewX) / viewWidth;
    vec.Y = (vec.Y - viewY) / viewHeight;
    vec = (vec * 2) - 1;
    vec.Y = -vec.Y;

    var outVec = vec.Transform(finalMatrix);
    outVec.X /= outVec.W;
    outVec.Y /= outVec.W;
    outVec.Z /= outVec.W;

    return outVec.ToVector3();
}

I then call this method from my Camera object:

public Vector3 UnProject(Vector3 screenPosition)
{
    return screenPosition.UnProject(ProjectionMatrix, ViewMatrix, ViewPort.Location.X, ViewPort.Location.Y, ViewPort.Size.X, ViewPort.Size.Y);
}

To get my normal vector from the ray I do:

var rayLoc = camera.UnProject(new Vector3(Game.Mouse.Location, 0));
var rayLoc2 = camera.UnProject(new Vector3(Game.Mouse.Location, 1));
var rayDirection = (rayLoc2 - rayLoc).Normalize();

Everything works fine until I move my LookAt point over say 1 in the X and Rotate the camera a little on the X and Y axis. After that the vector is no longer correct. I don’t know what i’m doing wrong??

EDIT: After a closer look I think my Matrix4.Invert method may be the cause. I’m trying to use Row major, any ideas?

public Matrix4 Invert()
{
    var mat = new Matrix4(
        new Vector4(
            Y.Y*Z.Z*W.W - Y.Y*Z.W*W.Z - Z.Y*Y.Z*W.W + Z.Y*Y.W*W.Z + W.Y*Y.Z*Z.W - W.Y*Y.W*Z.Z,
            -Y.X*Z.Z*W.W + Y.X*Z.W*W.Z + Z.X*Y.Z*W.W - Z.X*Y.W*W.Z - W.X*Y.Z*Z.W + W.X*Y.W*Z.Z,
            Y.X*Z.Y*W.W - Y.X*Z.W*W.Y - Z.X*Y.Y*W.W + Z.X*Y.Y*W.Y + W.X*Y.Y*Z.W - W.X*Y.W*Z.Y,
            -Y.X*Z.Y*W.Z + Y.X*Z.Z*W.Y + Z.X*Y.Y*W.Z - Z.X*Y.Z*W.Y - W.X*Y.Y*Z.Z + W.X*Y.Z*W.Y),
        new Vector4(
            -X.Y*Z.Z*W.W + X.Y*Z.W*W.Z + Z.Y*X.Z*W.W - Z.Y*X.W*W.Z - W.Y*X.Z*Z.W + W.Y*X.W*Z.Z,
            X.X*Z.Z*W.W - X.X*Z.W*W.Z - Z.X*X.Z*W.W + Z.X*X.W*W.Z + W.X*X.Z*Z.W - W.X*X.W*Z.Z,
            -X.X*Z.Y*W.W + X.X*Z.W*W.Y + Z.X*X.Y*W.W - Z.X*X.W*W.Y - W.X*X.Y*Z.W + W.X*X.W*Z.Y,
            X.X*Z.Y*W.Z - X.X*Z.Z*W.Y - Z.X*X.Y*W.Z + Z.X*X.Z*W.Y + W.X*X.Y*Z.Z - W.X*X.Z*Z.Y),
        new Vector4(
            X.Y*Y.Z*W.W - X.Y*Y.W*W.Z - Y.Y*X.Z*W.W + Y.Y*X.W*W.Z + W.Y*X.Z*Y.W - W.Y*X.W*Y.Z,
            -X.X*Y.Z*W.W + X.X*Y.W*W.Z + Y.X*X.Z*W.W - Y.X*X.W*W.Z - W.X*X.Z*Y.W + W.X*X.W*Y.Z,
            X.X*Y.Y*W.W - X.X*Y.W*W.Y - Y.X*X.Y*W.W + Y.X*X.W*W.Y + W.X*X.Y*Y.W - W.X*X.W*Y.Y,
            -X.X*Y.Y*W.Z + X.X*Y.Z*W.Y + Y.X*X.Y*W.Z - Y.X*X.Z*W.Y - W.X*X.Y*Y.Z + W.X*X.Z*Y.Y),
        new Vector4(
            -X.Y*Y.Z*Z.W + X.Y*Y.W*Z.Z + Y.Y*X.Z*Z.W - Y.Y*X.W*Z.Z - Z.Y*X.Z*Y.W + Z.Y*X.W*Y.Z,
            X.X*Y.Z*Z.W - X.X*Y.W*Z.Z - Y.X*X.Z*Z.W + Y.X*X.W*Z.Z + Z.X*X.Z*Y.W - Z.X*X.Z*Y.Z,
            -X.X*Y.Y*Z.W + X.X*Y.W*Z.Y + Y.X*X.Y*Z.W - Y.X*X.W*Z.Y - Z.X*X.Y*Y.W + Z.X*X.W*Y.Y,
            X.X*Y.Y*Z.Z - X.X*Y.Z*Z.Y - Y.X*X.Y*Z.Z + Y.X*X.Z*Z.Y + Z.X*X.Y*Y.Z - Z.X*X.Z*Y.Y));

    Num delta = X.X*mat.X.X + X.Y*mat.Y.X + X.Z*mat.Z.X + X.W*mat.W.X;
    delta = 1 / delta;
    return mat * delta;
}
  • 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-02T22:03:45+00:00Added an answer on June 2, 2026 at 10:03 pm

    It turns out my Matrix4.Invert method was wrong. I used the OpenTK for reference in making a Row-Major Invert method.

    EDIT: Actually ‘https://github.com/Dav1dde/gl3n’ had a better way of going about it.

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

Sidebar

Related Questions

Very basic question - how to get one value from a generator in Python?
Basic question - is it possible to access the current Page from a static
Basic question so I feel dumb but..., Whats the proper syntax below in the
Basic question: How do I disassociate a git repo from the origin from which
Basic question about the way git works. I clone a repo, make some changes,
Basic Question: I have a k dimensional box. I have a vector of upper
BASIC QUESTION: Is there a way to set the mime-type (content-type) of elements inside
Basic question but I've searched and can't quite get this nailed - how can
A basic question on MPC55xx microcontroller: I configured the reset boot vector (4 bytes
Pretty basic question here. How come I get a 404 error when I try

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.