# Mouse in 3D scene

how can i place mouse in 3d in opengl ? How kind of function i ought to use ? I want to use mouse cursor ( x,y,z ) . Have i define space ?

I read , that i ought to use gluProject/gluUnProject , how i ought to use ? Give me any example , or literature part .

I think that i could use 2 d scene in 3 d scene ? How put into it ? i have not good ideas ? help me .

that theme ought to be name : mapping of the position of the cursor in 3 D space .

Hi for this you need to know how to obtain the world space position from the clicked point on screen. To obtain that, the following steps are usually done.
Steps:

1. Get the depth value from the depth buffer at the clicked location. This gives you the z component for a given window/screen position.
2. Unproject the window/screen coordinates to obtain the world space coordinates.
3. Place object at the world space coordinates.
How to do it:
In you mouse click function, you have the x and y coordinates passed in, use those (just flip the y value) to get the window coordinate x,y value. Also get the current modelview and projection matrices and the current viewport like this,
``````
void OnMouseDown(int button, int s, int x, int y)
{
if (s == GLUT_DOWN)
{
GLint viewport[4];
GLdouble MV[16];
GLdouble P[16];

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, MV);
glGetDoublev(GL_PROJECTION_MATRIX, P);

int height   = viewport[3];
int winX = x ;
int winY = (height - y);

``````

Next, to read the depth value call the glReadPixels function passing it the window coords like this,

``````
float winZ=0;
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

``````

Next call the gluUnProject function and give it the window coords, the modelview, projection matrices and the current viewport and it will give you back the world coordinates like this,

``````
double objX=0, objY=0, objZ=0;
gluUnProject(winX,winY, winZ,  MV,  P, viewport, &objX, &objY, &objZ);
}
}

``````

Now place the object at objX,objY, objZ and it should be there in the 3D world. See if this helps.

thank You .

I think that i need x,y view and x,z or y,z view except main view x,y,z . these 2 2D gave me view on 3D . how translate 2D view to 3D view ?

Its hard for me to understand what u mean here but if I am correct, you want to have an orthographic view from three planes XZ, YZ and XY. If so you just need to place the camera (use gluLookAt) at a distance from origin lets say 5 units so your camera pos. will be (0,0,5) for XY plane, (0,5,0) for XZ plane and (5,0,0) for YZ plane and then you generate an orthographics projection for the volume you need.

You should take care of the camera up vector though esp for XZ plane since you might need to add a little offset in the x and z values to see something.

gluLookAt is not translate x,y z,x to x,y,z it is observer place . How can i translate from x,y and z,x ( z,y ) to x,y,z ?
I want place plane x,y and z,x (z,y) and x,y,z in three windows and connect it .

HI,
Yes i know u r trying to make something like the 4 viewports of 3dsmax and other modelling applications. You can create 4 viewports and then move the camera as I suggested in the last reply.
Check this http://stackoverflow.com/questions/726379/multiple-viewports-in-opengl
http://coreenginedev.blogspot.com/2009/12/editor-basics-viewports.html
http://www.sonycsl.co.jp/person/nielsen/visualcomputing/chapters/allchapters.html (check chapter 3 openglviewports.cpp)

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.