0

I am trying to implement a point click functionality on a world map using Opengl (glfw and glew). My map is loaded as a texture and displayed on the screen using the coordinate vertices

GLfloat vertices[] = 
        {
            -180.0f, -90.0f, 0.0f, 0.0f,
            180.0f, -90.0f, 1.0f, 0.0f,
            180.0f,  90.0f, 1.0f, 1.0f,
            -180.0f,  90.0f, 0.0f, 1.0f
        };

it ranges from -180 to +180 longitudes and -90 to +90 latitudes as defined in the VAO. While implementing the click functionality, I want the exact coordinate of the world where I click the mouse on. I am using glm::unProject but it is not giving the exact world coordinates.

if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
    mouseClicked = true;
    firstMouse = true;
    int pixel_w, pixel_h, screen_w, screen_h;
    glm::mat4 modelview = view * model;
    glfwGetWindowSize(window, &screen_w, &screen_h);
    glfwGetFramebufferSize(window, &pixel_w, &pixel_h);
    glfwGetCursorPos(window, &xPos, &yPos);
    glm::vec2 screen_pos = glm::vec2(xPos, yPos);
    glm::vec2 pixel_pos = screen_pos * glm::vec2(pixel_w, pixel_h) / glm::vec2(screen_w, screen_h);
    glm::vec3 win = glm::vec3(pixel_pos.x, pixel_h - pixel_pos.y, 0.0f);
    glm::vec3 h_hat = glm::unProject(win, modelview, projection, glm::vec4(0, 0, screen_w, screen_h)); // convert the screen coordinate to world coordinate
    float worldX = h_hat.x;
    float worldY = h_hat.y;
    std::cout<<worldX<<":"<<worldY<<std::endl;

The projection matrix is as follows:

projection = glm::perspective(camera.GetZoom(), (GLfloat)screenWidth/(GLfloat)screenHeight, 1.0f, 100.0f);
glm::vec3 position = glm::vec3(0.0f,0.0f, 30.0f);
glm::vec3 up = glm::vec3( 0.0f, 1.0f, 0.0f );
glm::vec3 front=glm::vec3( 0.0f, 0.0f, -1.0f);
view = glm::lookAt(position, position + front, up );
view = glm::translate(view, glm::vec3(-80.0f, -20.0f, 0.0f)); //for India Region
glm::vec3 model(0.1f);
genpfault
  • 49,394
  • 10
  • 79
  • 128

0 Answers0