2021年1月1日星期五

How I can map the position in real pixels of the mouse to a normalized one when using glm::perspective?

I'm drawing a 2D tilemap using OpenGL and I will like to be able to know where the position of the mouse corresponds into my scene. This is what I currently have:

The 2D tilemap I'm drawing

To draw this screen this projection is used

glm::mat4 projection = glm::perspective(    glm::radians(45.0f),    (float)screenWidth / (float)screenHeight,    1.0f,    100.0f  );  

Then this camera is used to move and zoom the tilemap

glm::vec3 camera(0.0f, 0.0f, -1.00f);  

Which then translates into a camera view

glm::mat4 cameraView = glm::translate(state.projection, camera);  

That finally gets passed through a uniform to the vertex shader

#version 330 core    layout(location = 0) in vec2 aPosition;    uniform mat4 uCameraView;    void main() {    gl_Position = uCameraView * vec4(aPosition.x, aPosition.y, 0.0f, 1.0f);  }  

This shader receives a normalized vertex, which it means that I never know how much in pixels a tile is in my screen.

Now I'm trying to somehow calculate where the mouse will be inside of my scene if it was projected like a ray into the tilemap and then hit it. If I managed to get the position of that collision I will be able to know which tile the mouse is hovering.

What will be the best approach to find this coordinate?

https://stackoverflow.com/questions/65535304/how-i-can-map-the-position-in-real-pixels-of-the-mouse-to-a-normalized-one-when January 02, 2021 at 09:33AM

没有评论:

发表评论