I am creating a 2D map editor in C ++ with GLFW. I want move and drag entities with the mouse, but I have a problem, the mouse coords are in the window size range (1200, 720) and the entity is in the GLSL range (100x100 I think), I compare the mouse coord with the 4 entity edges. How can I do this?

PD: Sorry for bad english.


There is the code for set position and size to entities:

model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(translate, 0.0f));
model = glm::scale(model, glm::vec3(scale, 1.0f));

And this is the GLSL code:

#version 330

layout (location = 0) in vec2 position;
layout (location = 1) in vec2 textureCoord;

out vec2 TexCoord;

uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;

void main() {
    TexCoord = textureCoord;

    gl_Position = projection * view * model * vec4(position, 1.0, 1.0);
  • \$\begingroup\$ There isn't really such a thing as GLSL range. Do you mean normalized device coordinates (-1...1)? If not, I'm not sure what coordinate system you're working in. Want to show us how you've configured your window, viewport, and view/projection matrices? \$\endgroup\$
    – DMGregory
    Commented Jul 9, 2020 at 1:40
  • \$\begingroup\$ I use a model matrix, with glm :: translate to set the position of the entity. Yes, I mean to [-1,1], I have been told that I must normalize the mouse coordinates first, but after that I don't have much idea of what to do. \$\endgroup\$
    – Steback
    Commented Jul 9, 2020 at 3:22
  • \$\begingroup\$ What values do you provide to your view and projection uniforms? \$\endgroup\$
    – DMGregory
    Commented Jul 9, 2020 at 13:19
  • 1
    \$\begingroup\$ Here is a link to another question on the Computer Graphics SE. Look into the answer there. It should give you all the information you need. Additionally, you find a working python3 script at the end that you can run on your computer to compare your results. However, if you are doing a simple 2d game you do not need the projection matrix. Depending on your game, all you need to do is coordinate shifting and scaling. \$\endgroup\$
    – wychmaster
    Commented Jul 9, 2020 at 13:29


You must log in to answer this question.

Browse other questions tagged .