PDA

View Full Version : How to get the OpenGL Co-Ordinates ?



jerin3D
01-08-2002, 09:25 PM
Hi,

My doubt is how to get the XYZ opengl Co-Ordinates ? I want to get the xyz opengl co-ordinates not screen co-ordinates by clicking on the object using mouse.I try to implement using glReadPixels() and gluUnProject() functions. But i can't got actual value for co-ordinate.

Can you suggest the proper way or code to get the coordinates.

Thanx in advance,
rgrds,
Jerry

[This message has been edited by jerin3D (edited 01-09-2002).]

jide
01-08-2002, 11:28 PM
gluUnProject do that for you.

jerin3D
01-09-2002, 01:30 AM
Originally posted by jide:
gluUnProject do that for you.

Hi

My current problem is that How to get the z screen co-ordinate for the clicked point on the object ? for giving it as a input for the gluUnproject() function.The following shows my implementaion.I know that it is a simple problem but am not expert in OpenGL. pls help me

glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
////For reading z value////
GLfloat*zValue=new float[viewport[2]*viewport[3]];
glFinish();
glPixelStorei(GL_PACK_ALIGNMENT,4);
glPixelStorei(GL_PACK_ROW_LENGTH,0);
glPixelStorei(GL_PACK_SKIP_ROWS,0);
glPixelStorei(GL_PACK_SKIP_PIXELS,0);
glReadPixels(0,0,point.x,point.y,GL_DEPTH_COMPONEN T,GL_FLOAT,zValue);
/////////////////////////////////////////
float zCoordinate = zValue[(point.y*viewport[2])+point.x]; //// Taking z value from the array
CRect rect;
GetClientRect(&rect);
int y = rect.Height() - point.y;
if(GL_TRUE==gluUnProject(point.x, y,fHit, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))

Waiting for ur replay...

rgrds,

Jerry

ScottManDeath
01-09-2002, 04:31 AM
// glut callback
// generates ogl word coordiantes based upon mouse click, i assume a glortho projection so setting the z value to -1 or something other does not change result. with gluperspective you get object coords as if the mouse would be in the plane( here: z=-1)
perhaps you can use your gluperspective znear plane value
void MouseClick(int button, int state, int x, int y)
{
GLdouble modelview[16],projection[16];
GLint viewport[4];
GLdouble objx,objy,objz;

if((button==BUTTON_LEFT )&&(state==GLUT_DOWN))
{
// get matrices
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
// get viewport
glGetIntegerv(GL_VIEWPORT,viewport);

// convert mouse coordinates into world coordinates
gluUnProject(x,y,-1,modelview,projection,viewport,&objx,&objy,&objz);
// now we have the mouse position in objx, objy, objz
}
}

jide
01-09-2002, 04:32 AM
sorry, i've used it only once some times ago.
So i don't remember and i don't have it here.

Try to see one of the Mesa libraries demo: there are 3 linear cubes that can be moved by the mouse.
But if you're under Windows, it will be difficult, i convain. That's why i want appologies.
sorry

JD

Overmind
01-09-2002, 12:53 PM
Here is a part of a project I am working on where I use gluUnProject to pick objects with the mouse:



// draw objects
board->Draw(true);

// get matrices
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);

// convert from windows coordinate system to ogl coordinate system
y = viewport[3] - y;

// convert x,y coordinates to GLfloat
ix = (GLfloat)x; iy = (GLfloat)y;

// read z coordinate
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &iz);

// unproject
gluUnProject(ix, iy, iz, modelview, projection, viewport, &ox, &oy, &oz);


I hope that helps
Overmind