0
\$\begingroup\$

I'm trying to implement an editor like placing mod with opengl. When you click somewhere on the screen an object get placed in that position.

So far this is my code

void RayCastMouse(double posx, double posy)
{
    glm::fvec2 NDCCoords = glm::fvec2( (2.0*posx)/ float(SCR_WIDTH)-1.f, ((-2.0*posy) / float(SCR_HEIGHT)+1.f) );
    glm::mat4 viewProjectionInverse = glm::inverse(projection * camera.GetViewMatrix());
    glm::vec4 worldSpacePosition(NDCCoords.x, NDCCoords.y, 0.0f, 1.0f);
    glm::vec4 worldRay = viewProjectionInverse*worldSpacePosition;
    printf("X=%f / y=%f / z=%f\n", worldRay.x, worldRay.y, worldRay.z);
    m_Boxes.emplace_back(worldRay.x, 0, worldRay.z);
}

The problem is the object isn't placed at the correct position, worldRay vec3 is used to translate the model matrix.

Can anyone one please help with this i will really appreciate it. By the way division the worldRay xyz component by worldRay.w is setting the object at the camera position.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.