I am drawing a 2D mesh imported from CAD. The mesh vertex coordinates are in feet from some arbitrary origin and Z is always 0. I am supplying the coordinates to my shader program and using projection matrix, view matrix and model matrix to display everything. I am also using the view matrix and the model matrix to pan and zoom my 2D scene. I am trying to get the coordinates in feet from my mouse cursor coordinates. Should be a straightforward solution. A matrix multiplied by it's inverse will give you an identity matrix but something is very wrong. Here is my code (don't lough at the vb, I took over an old project):

Private Sub openGLControl1_MouseMove(sender As Object, e As MouseEventArgs) Handles openGLControl1.MouseMove
    Dim UnprojectMat As mat4 = glm.inverse(scene.projectionMatrix * scene.viewMatrix * scene.modelMatrix)
    Dim wCoordsVec As vec4 = UnprojectMat * New vec4(2 * e.X / openGLControl1.Width - 1, -(2 * e.Y / openGLControl1.Height - 1), 0, 1)
    wCoordsVec = wCoordsVec / wCoordsVec.w
    wCoordsLabel.Text = wCoordsVec.x.ToString + ", " + wCoordsVec.y.ToString
End Sub

And this is my vertex shader:

#version 150

in vec3 in_Position;

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

void main(void) {
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);
//pass_Color = in_Color;


The in_Position vector is what I'm trying to get. As you see, I normalized the mouse coordinates for the -1, 1 coordinate system. I don't have a viewport. I'm just using the entire gl control window. Please help me pinpoint what I'm doing wrong.

  • \$\begingroup\$ That is a lot of math doing a lot of nothing. You are not using one dimension z so you can reduce the proj/view/model matrix into a single 3by3 2D matrix to give you rotation scale position and display aspect (aspect is all you need from the projection) invert and multiply for the mouse to model space coords, and in the vertex shader all you have to pass is the 3by3 matrix and multiply the vertices with that.. \$\endgroup\$
    – Blindman67
    Nov 23, 2016 at 3:12

1 Answer 1


The problem was with the .net port of glm (OpengGl Mathematics) library. I patched up the library and the issue was resolved.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .