1
votes

I'm using GLFW and I want to get world space coordinates by mouse coordinates on window to drawing objects while clicking in selected place. Firstly I was trying to get this coordinates using only matrices transformations but as a result i did not get anything with sense.

glfwGetCursorPos(window, &xPos, &yPos);
mat4 inverse_projection = glm::inverse(projection);
mat4 inverse_view = glm::inverse(view);

GLdouble normalized_coord2D_x = (2.0 * xPos / -1.0),
    normalized_coord2D_y = (2.0 * yPos / -1.0);

vec4 worldCoordNear;
vec4 worldCoordFar;

vec4 normalizedCoordNear = vec4(normalized_coord2D_x, normalized_coord2D_y, -1.0, 1.0);
vec4 clipCoordNear = normalizedCoordNear;
vec4 eyeCoordNear = inverse_projection * clipCoordNear;
worldCoordNear /= worldCoordNear.w;

vec4 normalizedCoordFar = vec4(normalized_coord2D_x, normalized_coord2D_y, 1.0, 1.0);
vec4 clipCoordFar = normalizedCoordFar;
vec4 eyeCoordFar = inverse_projection * clipCoordFar;
worldCoordFar = inverse_view * eyeCoordFar;
worldCoordFar /= worldCoordFar.w;

Now I gave up from that solution and trying to solve the problem with gluUnProject on simple 2D scene, my function looks like:

void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
{
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
    std::cout << "Left button pressed" << std::endl;

    GLdouble xPos, yPos;
    glfwGetCursorPos(window, &xPos, &yPos);

    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;
    GLdouble posX, posY, posZ;

    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);

    winX = (float)xPos;
    winY = (float)viewport[3] - (float)yPos;
    glReadPixels(xPos, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

    gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
    }
}

And I am getting error:

LNK2019 unresolved external symbol _gluUnProject@48 referenced in function "void __cdecl mouseButtonCallback(struct GLFWwindow*,int,int,int)" (?mouseButtonCallback@@YAXPAUGLFWwindow@@HHH@Z)

I am working on VS2015 and that's my includes:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <math.h>
#include <iostream>
#include <GL/GLU.h>
#include <Windows.h>

It is worth pointing out that the glu library I downloaded with NuGet Packages

I would be very grateful for all the help and an explanation on how to solve the problem.

1
Just pointing out the obvious. But since it's a linker problem, are you sure you're linking and providing the correct path to the lib file?vallentin
where can i find glu32.lib then? cause i cant find it and i saw that gluUnProject need Glu32.lib, I only linked opengl32.lib, glew32.lib, glfw3.libdevi
Ok, i solved the problem, but the question now is if I use perspective, will gluUnProject return good values?devi

1 Answers

0
votes

Basically your projection matrix will take vertices from camera/eye space to clip space then it does perspective divide to get normalised device coordinates.Then what ever viewport you have mentions using that window transform will be applied on it to get screen coordinates. gluUnProject is doing this in reverse order for you. So rest assured it will give to good values.