gluUnproject

Started by
9 comments, last by lory 18 years, 3 months ago
Hi, I'm building an Earth 3d simulation using Qt and OpenGl. I have a problem with the function gluUnproject because I want convert screen cursor coordinates to world coordinates:in my application the Depth Range isn't [0,1] but it is variable (there is a zoom function). I red that gluUnproject works well only if winz is in [0,1] range. Then I want test if the point clicked by cursor is on the sphere. How can I do this? Thanks
Advertisement
You could do the following:

Do gluUnproject twice - once with winz being 0 and the other time with 1.

i.e.
   float nx,ny,nz,fx,fy,fz;   gluUnproject(mousex,mousey,0,modelview,projection,viewport,&nx,&ny,&nz); //at nearplane   gluUnproject(mousex,mousey,1,modelview,projection,viewport,&fx,&fy,&fz); //at farplane


Now having nx,ny,nz (World position at near plane) and fx,fy,fz (world position at farplane), you can create a ray from near plane position to far plane position.
Then you do a simple Ray/Triangle collision detection with the triangles of your sphere.

This should be the most precise solution, as far as I know

Edit:
If you really only need to know IF the mouse is on the sphere, and not WHERE it is on the sphere, you could alternatively do a Ray/Box collision test (with a bounding box around your sphere). This saves some performance
Thanks Hydrael!
I have the following doubt : Can I do what you said also if my near and far plane have winz depending on the distance from the sphere?

Also, I use DirectX and I'm new in OpenGl, so I don't know how do a Ray/Triangle collision detection with the triangles of my sphere.
To build my sphere I have used gluSphere.
Can you help me?

If it can be of help here there is the code which I want porting from DirectX to OpenGl:

D3DVIEWPORT9 Viewport;
pD3DDevice->GetViewport( &Viewport );

Viewport.Width = m_pos.m_windowWidth;
Viewport.Height = m_pos.m_windowHeight;

D3DXVECTOR3 p1( screenX, screenY, Viewport.MinZ );
D3DXVec3Unproject( &p1, &p1, &Viewport, &m_mProj, &m_mView, &m_mWorld );

D3DXVECTOR3 p2( screenX, screenY, Viewport.MaxZ );
D3DXVec3Unproject( &p2, &p2, &Viewport, &m_mProj, &m_mView, &m_mWorld );

float a = (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y) + (p2.z - p1.z) * (p2.z - p1.z);
float b = 2*((p2.x - p1.x)*(p1.x) + (p2.y - p1.y)*(p1.y) + (p2.z - p1.z)*(p1.z));
float c = (p1.x*p1.x + p1.y*p1.y + p1.z*p1.z - m_fWorldRadius * m_fWorldRadius);

float discriminant = (float)(b*b - 4 * a * c);

if(discriminant <= 0)
return false;

float t1 = ((-1.0f) * b - sqrt(b*b - 4 * a * c)) / (2*a);

v.x = p1.x + t1 *(p2.x - p1.x);
v.y = p1.y + t1 *(p2.y - p1.y);
v.z = p1.z + t1 *(p2.z - p1.z);

return true;


Thanks!!!!
Quote:Original post by lory
Thanks Hydrael!
I have the following doubt : Can I do what you said also if my near and far plane have winz depending on the distance from the sphere?


I don't know if I got you right, but I think you misunderstood one thing.
The first gluUnproject (with winz being 0) determines the world position of your mousecursor, if the depth position of the mouse would be right in front of your nose (near plane).
The other one (winz=1) does the same, except for the fact, that it assumes your mousecursor's depth position to be all the way at the back of your scene (far plane).

What I want to say: winz does not represent a third axis for 2D->3D conversion (meaning, it does stand for "z") - it tells where the 2 Dimensional point (mousepos) is, within your depth range, which is view dependant. And this depth range reaches from 0.0 (near plane) to 1.0 (far plane).

Regarding the collision detection:
I haven't worked with gluSphere yet, that's why I can't help you here, sorry :/
But there are plenty of sources on the net, which explain Ray/Triangle, Ray/Box or maybe Ray/Sphere collision detection routines

I don't have access to my sourcecodes right now, but when I get home from work, I could post a Ray/Sphere function

Greets

Chris


Edit again:
Looking at your DX code...you only need to know IF the mousecursor is on the sphere, not WHERE, right?
If so, the function I will post when I get home will be exactly what you need ;)
Thanks Hydrael!!!

There is a thing that I don't understand: winz =0 and winz=1 are the near and far plane but 0 and 1 are the distances from the viewer?
I have a near and far plane different from 0 and 1 (if you see my code Viewport.MinZ and Viewport.MaxZ depend on a zoom factor and their values are always >> 1 ), can I pass their values as winz gluUnproject parameter?

Thanks for the Ray/Sphere function...I'm waiting for it!

Greets,
Lory
This is the Ray/Sphere collision function.

RayOrigin=Array of 3 floats (x,y,z), representing the origin of the ray (nx,ny,nz in the previously posted code snippet)
RayDir=Array of normalized 3 floats, representing ray direction (calculated with nx,ny,nz,fx,fy,fz)
Center=Array of 3 floats (x,y,z), representing the center coordinate of your sphere
Radius=Radius of your sphere

returns TRUE, if the ray hits the sphere (meaning: mousecursor is on the sphere)

BOOL intersectRaySphere(float *RayOrigin, float *RayDir, float *Center, float Radius) {	float dx,dy,dz,x1,y1,z1,x0,y0,z0,cx,cy,cz,R;	x0=RayOrigin[0];	y0=RayOrigin[1];	z0=RayOrigin[2];	x1=x0+FarPlane*RayDir[0];	y1=y0+FarPlane*RayDir[1];	z1=z0+FarPlane*RayDir[2];	cx=Center[0];	cy=Center[1];	cz=Center[2];	R=Radius;	dx = x1 - x0;        dy = y1 - y0;        dz = z1 -  z0;	float a = dx*dx + dy*dy + dz*dz;        float b = 2*dx*(x0-cx) +  2*dy*(y0-cy) +  2*dz*(z0-cz);        float c = cx*cx + cy*cy + cz*cz + x0*x0 + y0*y0 + z0*z0 + -2*(cx*x0 + cy*y0 + cz*z0) - R*R;	if(b*b-(4*a*c)>=0)		return TRUE;	return FALSE;}
Hydrael is correct, but you don't need to unproject at both the near and far planes. You can use the camera position as the ray origin and then use that and the unprojected mouse position (at any depth) to calculate the ray direction.
MousePos = gluUnproject with mouse coordinates at any depthRayOrigin = CameraPosRayDir = normalize(MousePos - CameraPos)
This way you only need to call gluUnproject once. It's not a very "heavy" function however so you won't notice a speed difference.
As "at any depth" you mean that I can use :
glReadPixels(wx,wy,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&wz);
to calculate wz and then I can pass it to gluUnproject?
And what do you mean with "normalize(MousePos - CameraPos)"?

Thanks very much!!!
That's one possibility - but glReadPixels is very unprecise.

Normalizing a vector means: bringing all three components (x,y,z) into a 0-1 region

void Normalize(float *v) {	float d;		d = (float)sqrt((v[0]*v[0]) + (v[1]*v[1]) + (v[2]*v[2]));	if(d==0)		return;	v[0] = v[0] / d;	v[1] = v[1] / d;	v[2] = v[2] / d;}
Quote:Original post by lory
As "at any depth" you mean that I can use :
glReadPixels(wx,wy,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&wz);
to calculate wz and then I can pass it to gluUnproject?
And what do you mean with "normalize(MousePos - CameraPos)"?

Thanks very much!!!
By "any depth" I mean you can use any valid depth value you want, it won't make a difference to the ray direction since the points at the mouse position at any depth are in the same ray. So there's no point in reading the depth value from the framebuffer, just pass any valid depth value as winz. I say "valid depth value" only because I'm not sure if gluUnproject expects it inside the depth range ([0,1] most likely) and I don't feel like checking right now [grin]. I think it technically could work with ANY value (as long as it isn't a depth value that would be behind the camera, otherwise the ray direction would be the opposite of what you want), but you might as well just use 0 (corresponding to the near plane) to be safe.

(MousePos - CameraPos) will give you a vector from the camera's world-space position to the mouse's world-space position.
Quote:Original post by Hydrael
Normalizing a vector means: bringing all three components (x,y,z) into a 0-1 region
Your normalization function is correct but your description is slightly off. I notice you're from Germany so it's probably just from English not being your native language.

A "normalized vector" is a vector that has unit length (length = 1). You get this by dividing each component in the vector by that vector's magnitude (magnitude == length). This is exactly what Hydrael's function does. For a more mathematical explanation with links to other terms see here.

This topic is closed to new replies.

Advertisement