不使用OpenGL调用获取点y = 0的飞机从鼠标坐标鼠标、坐标、飞机、OpenGL

2023-09-08 00:02:44 作者:各自不回头

我有:

在屏幕坐标x,y(0,0是屏幕中间,1,1是左上角) 屏幕尺寸 在摄像头位置矢量 在摄像头的外观载体 投影矩阵 模型视图矩阵 Y = 0平面正常的(0,1,0,0) 的Y = 0平面位置(0,0,0,0)

和我希望得到在y = 0平面的,其中我点击我的窗口中的位置(x,0,Z)(应该是一条线 - 面相交,但考虑到相机性能)。

烦人,我没有访问GLU要求unprojecting等。只是基本向量和矩阵计算。我并不真的需要确切的code因为如此,只是技术 - 作为一个线平面交叉点是很容易做到的。找到,它从眼通过屏幕上的点线是困难的部分。

我还以为是只使用由摄像机位置投射了一缕相机的外观载体,但是这并没有考虑到鼠标的坐标。所以,我需要考虑到相机的视野呢?

解决方案

  //获得的视线,通过鼠标光标线

闪烁视[4];
GLdouble mvmatrix [16],projmatrix [16];
闪烁真的; / * OpenGL的y坐标位置* /
GLdouble WX,WY,WZ; / *返回世界X,Y,Z COORDS * /
GLdouble WX2,WY2,WZ2; / *返回世界X,Y,Z COORDS * /
glGetIntegerv(GL_VIEWPORT,视口);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);
/ *注视[3]是在像素窗口的高度* /
真的=视[3]  - (闪烁)point.y  -  1;
gluUnProject((GLdouble)point.x,(GLdouble)真的,0.0,
    mvmatrix,projmatrix,视口,和放大器; WX,和放大器; WY,和放大器; WZ);
//的printf(设为z世界COORDS = 0.0是(%F,%F,%F)\ N,
// WX,WY,WZ);
gluUnProject((GLdouble)point.x,(GLdouble)真的,1.0,
    mvmatrix,projmatrix,视口,和放大器; WX2,和放大器; WY2,和放大器; WZ2);
//的printf(设为z世界COORDS = 1.0是(%F,%F,%F)\ N,
// WX,WY,WZ);

//视线交汇与Y = 0平面线

双F = WY /(WY2  -  WY);
双X2D = WX  -  F *(WX2  -  WX);
双z2d = WZ  -  F *(WZ2  -  WZ);
 

point.x,point.y是鼠标屏幕共同ords,0.0是左上角。

在code假设Y = 0平面填充视口。 (你是看不起世界,从一个狭窄的飞机端口,看不到任何的天空。)如果Y = 0平面不填写的视口,你要测试的x,y位置是天上。(WY2 - WY<小的值),​​并做适当的事情为你的应用

OpenGL3.3 通过物体坐标系对物体实现鼠标移动

I have:

screen coordinates x,y (0,0 being the middle of the screen, 1,1 being top left) screen dimensions camera location vector camera look vector projection matrix ModelView matrix y=0 Plane normal of (0,1,0,0) y=0 plane location of (0,0,0,0)

And am looking to get the location (x,0,z) on the y=0 plane of where i click on my window (should be a line - plane intersection, but has to take into account the camera properties).

Annoyingly, I don't have the access to the GLU calls for unprojecting and the like. Just basic vector and matrix calculations. I don't really need the exact code as such, but just the technique - as a line plane intersection is easy enough to do. Finding the line that goes from the eye through the point on the screen is the hard part.

I thought it was just using the camera look vector for a ray projected from the camera location, but this doesn't take into account the mouse co-ordinates. So do i need to take into account the camera FOV too?

解决方案

// get line of sight through mouse cursor

GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy;  /*  OpenGL y coordinate position  */
GLdouble wx, wy, wz;  /*  returned world x, y, z coords  */
GLdouble wx2, wy2, wz2;  /*  returned world x, y, z coords  */
glGetIntegerv (GL_VIEWPORT, viewport);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
/*  note viewport[3] is height of window in pixels  */
realy = viewport[3] - (GLint) point.y - 1;
gluUnProject ((GLdouble) point.x, (GLdouble) realy, 0.0,
    mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
//printf ("World coords at z=0.0 are (%f, %f, %f)\n",
//  wx, wy, wz);
gluUnProject ((GLdouble) point.x, (GLdouble) realy, 1.0,
    mvmatrix, projmatrix, viewport, &wx2, &wy2, &wz2);
//printf ("World coords at z=1.0 are (%f, %f, %f)\n",     
//  wx, wy, wz);

// line of sight intersection with y = 0 plane

double f = wy / ( wy2 - wy );
double x2d = wx - f * (wx2 - wx );
double z2d = wz - f * (wz2 - wz );

point.x, point.y are the mouse screen co-ords, 0.0 being top left.

The code assumes that the y = 0 plane fills the viewport. ( You are looking down on the world from a narrow aircraft port and cannot see any of the sky. ) If the y=0 plane does NOT fill the view port, you have to test for the x,y location being 'in the sky' ( wy2 - wy < small value ) and do something appropriate for your application.