case WM_MOUSEMOVE:
{
// Get the mouse position
int mouseX = GET_X_LPARAM(lParam);
int mouseY = GET_Y_LPARAM(lParam);
// Convert mouse coordinates to NDC
float NDC_X = (2.0f * static_cast<float>(mouseX)) / window_width - 1.0f;
float NDC_Y = 1.0f - (2.0f * static_cast<float>(mouseY)) / window_height;
// Convert NDC to orthographic coordinates
glm::vec4 orthoCoords = glm::inverse(projection) * glm::vec4(NDC_X, NDC_Y, 0.0f, 1.0f);
float glY = orthoCoords.x;
float glX = orthoCoords.y;
// Update the vertices of the two lines
std::vector<Vertex> line1_vertices = {
{ static_cast<float>(-window_width) / 2.0f, glY, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
{ static_cast<float>(window_width) / 2.0f, glY, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f }
};
std::vector<Vertex> line2_vertices = {
{ glX, static_cast<float>(window_height) / 2.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
{ glX, static_cast<float>(-window_height) / 2.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f }
};
// Assuming the first two drawables are the two lines
Line* line1 = dynamic_cast<Line*>(drawables[0].get());
Line* line2 = dynamic_cast<Line*>(drawables[1].get());
if (line1 && line2) {
line1->updateVertices(line1_vertices);
line2->updateVertices(line2_vertices);
}
break;
}
How is it possible to convert the mouse co-ordinates from this win32 API example to the orthographic plane?
So far the orthographic projection I have set up has the center of the client area as x=0,y=0 and left is -window_width/2 and right is window_width/2 and top is window_height/2 and bottom is -window_height/2.
So far what I have here is not working yet. Is there a known formula or method to perform such a conversion?