Creating objects dynamically

Hi Guys,

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.

See the code below

	smaElement *pE = createNewElement();
	pE->isWireframe = 0;
	strcpy(pE->m_cvType, "sphere");

	pE->m_fvAmb[0] = 0.8f;
	pE->m_fvAmb[1] = 0.1f;
	pE->m_fvAmb[2] = 0.1f;
	pE->m_fvAmb[3] = 1.0f;

	pE->m_fvDif[0] = 0.8f;
	pE->m_fvDif[1] = 0.1f;
	pE->m_fvDif[2] = 0.1f;
	pE->m_fvDif[3] = 1.0f;

	pE->m_fvSpe[0] = 0.1f;
	pE->m_fvSpe[1] = 0.2f;
	pE->m_fvSpe[2] = 0.2f;
	pE->m_fvSpe[3] = 1.0f;

	pE->m_iSize = 2;

	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)iXPos;
	winY = (float)viewport[3] - (float)iYPos;

	glReadPixels( iXPos, (winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
	gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

	pE->m_fvPos[0] = posX;
	pE->m_fvPos[1] = posY;
	pE->m_fvPos[2] = posZ;

	pushHead(pE);
	renderList();

	primitive = 0;

Thanks for your help

Baz

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?

@bazaldo
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?

Hi Guys,

I all ready draw some objects at predefine positions and they appear fine.

say for example I click at about mid point of the screen, the object appears over to the right of where I clicked, it is also below where I clicked.

Also once the object has been created and I navigate the scene the objects shrinks and expands depending on the mouse rotation. That really is unexpectedand out of the ordinary!

Thanks for your help guys I appreciate it

Baz

This may be due to errors in mapping mouse position to your screen coordinates.

Also once the object has been created and I navigate the scene the objects shrinks and expands depending on the mouse rotation.

This is strange. If this behavior occurred during window re-sizing it would be understandable.

Yes that is what it seems like :-\

How would you go about mapping mouse position to a world position??

As you can see I use glReadPixels and gluUnProject. is that right??

Thanks

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.

Hi Carsten,

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.

Is there anyway to resolve this issue??

Thanks

Baz

One work around, to assign depth to your object, can be:

  1. 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 )

  2. 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.

  3. 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.