I get the corrected screen coordination (0,0 at Left Bottom):
GLint realy;
realy = winHeight - (GLint) y - 1;
screen.x = (float)x;
screen.y = (float)realy;
Then I get the world at zNear and zFar:
worldNear = glm::unProject(glm::vec3(screen.x, screen.y, 0.0), viewMatrix, projectionMatrix, glm::vec4(0.0f, 0.0f, winWidth, winHeight));
worldFar = glm::unProject(glm::vec3(screen.x, screen.y, 1.0), viewMatrix, projectionMatrix, glm::vec4(0.0f, 0.0f, winWidth, winHeight));
but I setup my Perspective projection like this, so I know the zFar and zNear and I'm not sure if the above code is necessary:
glm::perspective(fovy, 1.0f * winWidth/winHeight, zNear, zFar); (we need to get zNear and zFar by X, Y, Z -- so we need to unproject near and far)
Also, detect Zdepth buffer like this:
GLfloat depth;
glReadPixels(screen.x, screen.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
and pixel colors like this:
GLubyte pixel[3];
glReadBuffer( GL_BACK );
glReadPixels(screen.x, screen.y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, (void *)pixel);
Problem is here: I can get the World X,Y,Z, but I need to normalize coordination, so I can move objects smoothly by mouse, How?
glm::vec4 normalizePoint = ** ??? **
world.x = normalizePoint.x;
world.y = normalizePoint.y;
world.z = normalizePoint.z;
(currently my world coord is either too far or too near, that's the problem)
Edit: I find the answer in the first question here:
iPhone OpenGL : Using gluUnProject (port) and detecting clicking on an object
I need ray-object collision, it works now!!