I need some help with a graphics application I am building.
I am trying to create a object dynamically i.e. sphere, teapot. I am doing this by right clicking the mouse and selecting the object I want to create. A passive mouse function then picks up the coordinates of the mouse and I tty to get the Z value by using glReadPixels. The problem is that the object is not being created in the correct place.
Please use [ code]/[ /code] (without space after ‘[’) around code snippets to make them more readable.
The problem is that the object is not being created in the correct place.
Can you describe what is wrong in more detail? Have you tried printing the calculated position, for the case where your camera is in a known location to see if those positions make sense. For example if your camera is in the world coordinate system origin looking down negative z, clicking a couple of points from left to right should start with negative x values that increase and become positive in the right half of the screen.
If the calculated position makes sense the problem must be elsewhere in your code. What happens if you create a couple of objects at known positions, do they show up where you expect them to be?
I guess you are facing the problem of not getting desired depth,
z coordinate. You want to render a sphere at exact location(that you obtain from your mouse click coords. - 2D) and depth. Is that true?
As you can see I use glReadPixels and gluUnProject. is that right??
That gives you world coordinates provided your modelview matrix is really just a view matrix (i.e. the transformation from world to camera coordinates). If the modelview also contains a transformation from an object’s object coordinate system to world coordinates (in addition to the camera transform) what you get is object coordinates. (If you look at the gluUnproject man page you’ll see that all it does is convert the given window coordinates to NDC and apply the inverse of (PR * MV), where PR is the projection and MV the modelview matrix).
I’d suggest double checking the contents of the modelview matrix to make sure there is no unwanted transformation multiplied in.
Another (probably minor) caveat is that if you click somewhere on the background (i.e where nothing but the clear color is seen) you’ll get a position at the far clipping plane.
It seems to me that the modelview matrix is ok. I have noticed that no matter where I click, the object appears at the same depth. I think you are right in saying that it is being created on the far clipping plane.
One work around, to assign depth to your object, can be:
Get the mouse position and search for z-coord. for nearest point in your scene. i.e find least L2 norm in an imaginary frustum with mouse position as center. (Take coords x and y only to find the norm )
If there exist a point in your virtual frustum that has least L2 norm, assign the z-coord. of the nearest point to your object (sphere) that is to be drawn. Thus, your sphere will be created at a different position but with same depth as that of point belonging to some other object.
If in step 1, no such point is found then the z-coord ( depth) of your sphere’s center will be same as the far clipping plane of your virtual ( smaller) frustum as explained in step 1.
Step 2 will be close to where you expect your sphere (object ) to be drawn.
The steps emulate selection and picking but with a different purpose.