Skip to content

Instantly share code, notes, and snippets.

@danielbierwirth
Last active June 13, 2022 00:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielbierwirth/e2ade76be3a571c9f1fd to your computer and use it in GitHub Desktop.
Save danielbierwirth/e2ade76be3a571c9f1fd to your computer and use it in GitHub Desktop.
OpenTK pick matrix calculation. Transforms viewmatrix according to pick location and picking region. Modified matrix can be used for object pick determination.
/// <summary>
/// OpenTK/OpenGl pick matrix calculation. Takes pick location,pick region to
// modify the current viewport matrix.
/// </summary>
/// <param name="x">The x pick location.</param>
/// <param name="y">The y pick location.</param>
/// <param name="deltax">The pick region x bounds.</param>
/// <param name="deltay">The pick region y bounds.</param>
/// <param name="viewport">The viewport matrix.</param>
public void PickMatrix(double x, double y, double deltax, double deltay, int[] viewport)
{
if (deltax <= 0 || deltay <= 0) {
return;
}
// Translate and scale the picked region to cover the
// canvas
GL.Translate((viewport[2] - 2 * (x - viewport[0])) / deltax,
(viewport[3] - 2 * (y - viewport[1])) / deltay, 0);
GL.Scale(viewport[2] / deltax, viewport[3] / deltay, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment