坐标变换和投影问题 [英] Coordinate transformations and projection issues

查看:273
本文介绍了坐标变换和投影问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试了一段时间,让我的鼠标坐标转换成三维空间坐标中的OpenGL场景。

I've been trying for a while to get my mouse coordinates converted into 3D space coordinates in an OpenGL scene.

目前,我的预测是一个烂摊子(我认为)一点点,它似乎并没有充分考虑我的摄像头到时候我走动的场景。要检查这,我画一条线。

Currently, my projections are a little bit of a mess (I think), and it doesn't seem to fully take my "camera" into account when I move around a scene. To check this, I draw a line.

我的大小调整功能:

    void oglWidget::resizeGL(int width, int height)
    {
        if (height == 0) {
            height = 1;
        }
        pMatrix.setToIdentity();
        pMatrix.perspective(fov, (float) width / (float) height, -1, 1);
        glViewport(0, 0, width, height);
    }

我的渲染功能(paintGl())的情况如下:

My rendering function (paintGl()) goes as follows:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
QMatrix4x4 mMatrix;
QMatrix4x4 vMatrix;

QMatrix4x4 cameraTransformation;
cameraTransformation.rotate(alpha, 0, 1, 0);
cameraTransformation.rotate(beta, 1, 0, 0);

QVector3D cameraPosition = cameraTransformation * QVector3D(camX, camY, distance);
QVector3D cameraUpDirection = cameraTransformation * QVector3D(0, 1, 0);
vMatrix.lookAt(cameraPosition, QVector3D(camX, camY, 0), cameraUpDirection);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();    
gluLookAt(cameraPosition.x(), cameraPosition.y(), cameraPosition.z(), camX, camY, 0, cameraUpDirection.x(), cameraUpDirection.y(), cameraUpDirection.z());

shaderProgram.bind();
shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
shaderProgram.setUniformValue("texture", 0);


for (int x = 0; x < tileCount; x++)
{
    shaderProgram.setAttributeArray("vertex", tiles[x]->vertices.constData());
    shaderProgram.enableAttributeArray("vertex");
    shaderProgram.setAttributeArray("textureCoordinate", textureCoordinates.constData());
    shaderProgram.enableAttributeArray("textureCoordinate");
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tiles[x]->image.width(), tiles[x]->image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tiles[x]->image.bits());
    glDrawArrays(GL_TRIANGLES, 0, tiles[x]->vertices.size());
}
shaderProgram.release();

和创建我的光芒:

GLdouble modelViewMatrix[16];
GLdouble projectionMatrix[16];
GLint viewport[4];
GLfloat winX, winY, winZ;
glGetDoublev(GL_MODELVIEW_MATRIX, modelViewMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);

winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

GLdouble nearPlaneLocation[3];
gluUnProject(winX, winY, 0, modelViewMatrix, projectionMatrix,
             viewport, &nearPlaneLocation[0], &nearPlaneLocation[1],
        &nearPlaneLocation[2]);

GLdouble farPlaneLocation[3];
gluUnProject(winX, winY, 1, modelViewMatrix, projectionMatrix,
             viewport, &farPlaneLocation[0], &farPlaneLocation[1],
        &farPlaneLocation[2]);

 QVector3D nearP = QVector3D(nearPlaneLocation[0], nearPlaneLocation[1],
            nearPlaneLocation[2]);
 QVector3D farP = QVector3D(farPlaneLocation[0], farPlaneLocation[1],
            farPlaneLocation[2]);

我觉得我用冲突的系统或什么的。

I feel like I'm using conflicting systems or something.

我应该使用不同的变量来管理我的相机?我看到的谈话投影视图,模型视图等,但我不明白我怎么会用了,还可以使用着色器程序。我还是新手,当涉及到的OpenGL。

Should I be using different variables to manage my camera? I see talk of projection view, model view, etc, but I don't see how I would use that and also use the shader program. I'm still novice when it comes to OpenGL.

因此,为了澄清:我试图将我的鼠标坐标到三维空间坐标。到目前为止,它似乎半的工作,它只是不考虑相机的旋转。我证实了我的问题,是因为有两种射线的创作,或坐标unprojection,不符合我的实际射线采摘逻辑。

So to clarify: I'm attempting to convert my mouse coordinates into 3D space coordinates. So far, it appears to semi-work, it just doesn't take into account camera rotation. I confirmed that my problem has to do with either the ray creation, or with the unprojection of coordinates, not with my actual ray picking logic.

推荐答案

在这里,我想我有一系列的问题。

Here, I think I had a series of problems.

  1. 我应该用-1我靠近飞机,而不是0
  2. 在我不同的数据类型为我的矩阵(QT那些具体,),使我无法直接将其插入unproject功能。

我通过以下操作解决了这些:

I solved these by doing the following:

  1. 在我安装了 GLM库规范化我的数据类型
  2. 在我执行的矩阵计算自己
    • 所以我把矩阵到我的射线创建函数,然后由逆模型矩阵相乘的逆视图矩阵,然后通过逆投影矩阵。
    • 此值将被在2个不同的画面向量坐标乘以(这必须是在NDC空间)。一个为-1 AZ,其他的1对应的窗口空间的近平面和远平面。最后,这些载体也必须具有1瓦特值,以使所得矩阵*矢量的计算可以通过其自身的最后点被划分。
  1. I installed the GLM library to normalize my data types
  2. I performed the matrix calculations myself
    • So I pulled the matrices into my ray creation function, then multiplied the inverse view matrix by the inverse model matrix, then by the inverse projection matrix.
    • This value would then be multiplied in 2 different vectors of the screen coordinate (which had to be in NDC space). One with a z of -1, the other of +1 corresponding to the near and far planes of window space. Lastly, those vectors also had to have a "w" value of 1, so that the resulting calculation of matrix * vector could be divided by the last spot of itself.

我有一系列的在这里打开,因为光线是我最初的问题采摘导致揭示了一系列的错误,在我的code的问题。 如果有人碰到在此处发布的问题,它可能是值得一试我,我打开了其他问题,如从投影问题我所有的问题都干:

I had a series of questions opened here because my initial problem of ray picking lead to revealing a whole series of errors in my code. If anyone has come across the problem posted here, it may be worth checking out my other questions that I opened, as all of my problems all stem from projection issues:

  • 在这里,我才知道,其实我需要一些计算为使光线创作形式。
  • 在这里,我才知道,unProject不工作因为我想拉使用OpenGL函数模型和视图矩阵,但我从来没有真正将它们摆在首位,因为我建立了矩阵的手。我解决了这个问题的2倍:我做手工的数学,和我做了相同的数据类型的所有矩阵(它们混合数据类型早些时候,导致的问题也一样)
  • 最后,在这里,我才知道,操作我的订单是稍微偏离(需要由一个矢量,而不是相反乘以矩阵),我的靠近平面需要是-1,不为0,并且该载体,其将与矩阵相乘的最后的值(值瓦特 )需要为1。
  • In here, I learned that I actually need some form of calculations in order for ray creation.
  • In here, I learned that unProject wasn't working because I was trying to pull the model and view matrices using OpenGL functions, but I never actually set them in the first place, because I built the matrices by hand. I solved that problem in 2 fold: I did the math manually, and I made all the matrices of the same data type (they were mixed data types earlier, leading to issues as well).
  • And lastly, in here, I learned that my order of operations was slightly off (need to multiply matrices by a vector, not the reverse), that my near plane needs to be -1, not 0, and that the last value of the vector which would be multiplied with the matrices (value "w") needed to be 1.

由于我的问题,延长了时间相当大篇幅,并采取了几个问题,我欠贷款少数几个人谁帮我一路上:

Since my problem extended over quite a great length of time, and took up several questions, I owe credit to a few individuals who helped me along the way:

  • srobins of facepunch, in this thread
  • derhass from here, in this question, and this discussion

这篇关于坐标变换和投影问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆