1

我尝试使用许多人似乎找到的好方法,我用不同的 z 值调用 gluUnproject 2 次,然后尝试从这两个向量计算射线的方向向量。

我阅读了这个问题并尝试将那里的结构用于我自己的代码:

        glGetFloat(GL_MODELVIEW_MATRIX, modelBuffer);
        glGetFloat(GL_PROJECTION_MATRIX, projBuffer);
        glGetInteger(GL_VIEWPORT, viewBuffer);

        gluUnProject(mouseX, mouseY, 0.0f, modelBuffer, projBuffer, viewBuffer, startBuffer);
        gluUnProject(mouseX, mouseY, 1.0f, modelBuffer, projBuffer, viewBuffer, endBuffer);

        start = vecmath.vector(startBuffer.get(0), startBuffer.get(1), startBuffer.get(2));
        end = vecmath.vector(endBuffer.get(0), endBuffer.get(1), endBuffer.get(2));

        direction = vecmath.vector(end.x()-start.x(), end.y()-start.y(), end.z()-start.z());

但这只会返回同质剪辑坐标(我相信),因为它们在每个轴上的范围仅从 -1 到 1。

如何实际获取可以创建射线的坐标?

编辑:这就是我构造矩阵的方式:

Matrix projectionMatrix = vecmath.perspectiveMatrix(60f, aspect, 0.1f,
            100f);
//The matrix of the camera = viewMatrix
setTransformation(vecmath.lookatMatrix(eye, center, up));
//And every object sets a ModelMatrix in it's display method  
Matrix modelMatrix = parentMatrix.mult(vecmath
                    .translationMatrix(translation));
modelMatrix = modelMatrix.mult(vecmath.rotationMatrix(1, 0, 1, angle));

编辑2:

这是函数现在的样子:

private void calcMouseInWorldPosition(float mouseX, float mouseY, Matrix proj, Matrix view) {
    Vector start = vecmath.vector(0, 0, 0);
    Vector end = vecmath.vector(0, 0, 0);

    FloatBuffer modelBuffer = BufferUtils.createFloatBuffer(16);
    modelBuffer.put(view.asArray());
    modelBuffer.rewind();
    FloatBuffer projBuffer = BufferUtils.createFloatBuffer(16);
    projBuffer.put(proj.asArray());
    projBuffer.rewind();

    FloatBuffer startBuffer = BufferUtils.createFloatBuffer(16);
    FloatBuffer endBuffer = BufferUtils.createFloatBuffer(16);
    IntBuffer viewBuffer = BufferUtils.createIntBuffer(16);

     //The two calls for projection and modelView matrix are disabled here,   
       as I use my own matrices in this case
//  glGetFloat(GL_MODELVIEW_MATRIX, modelBuffer);
//  glGetFloat(GL_PROJECTION_MATRIX, projBuffer);
    glGetInteger(GL_VIEWPORT, viewBuffer);

    //I know this is really ugly and bad, but I know that the height and width is always 600  
    // and this is just for testing purposes 
    mouseY = 600 - mouseY;

    gluUnProject(mouseX, mouseY, 0.0f, modelBuffer, projBuffer, viewBuffer, startBuffer);
    gluUnProject(mouseX, mouseY, 1.0f, modelBuffer, projBuffer, viewBuffer, endBuffer);

    start = vecmath.vector(startBuffer.get(0), startBuffer.get(1), startBuffer.get(2));
    end = vecmath.vector(endBuffer.get(0), endBuffer.get(1), endBuffer.get(2));

    direction = vecmath.vector(end.x()-start.x(), end.y()-start.y(), end.z()-start.z());

}

我正在尝试使用我自己的投影和视图矩阵,但这似乎只会给出更奇怪的结果。
使用 GlGet... 的东西,我在右上角点击一下:
开始:(0.97333336,-0.98,-1.0)
结束:(0.97333336,-0.98,1.0)

当我使用自己的东西时,我会得到相同的位置:
开始:(-2.4399707,-0.55425626,-14.202201)
结束:(-2.4399707,-0.55425626,-16.198204)

现在我实际上需要一个模型视图矩阵而不仅仅是视图矩阵,但我不知道我应该如何获得它,因为它在每个对象的每个显示调用中都被更改并重新创建。
但这真的是问题吗?在教程中,他说“通常,要从眼睛空间进入剪辑空间,我们将向量乘以投影矩阵。我们可以通过乘以该矩阵的逆矩阵来向后退。” 在下一步中,他再次乘以视图矩阵的倒数,所以我认为这就是我应该做的?

编辑 3:
在这里我尝试了 user42813 的建议:

    Matrix view = cam.getTransformation();
    view = view.invertRigid();

    mouseY = height - mouseY - 1;

    //Here I only these values, because the Z and W values would be 0  
    //following your suggestion, so no use adding them here
    float tempX = view.get(0, 0) * mouseX + view.get(1, 0) * mouseY;
    float tempY = view.get(0, 1) * mouseX + view.get(1, 1) * mouseY;
    float tempZ = view.get(0, 2) * mouseX + view.get(1, 2) * mouseY;

    origin = vecmath.vector(tempX, tempY, tempZ);
    direction = cam.getDirection();

但是现在方向和原点值始终相同:
原点:(-0.04557252,-0.0020000197,-0.9989586)
方向:(-0.04557252,-0.0020000197,-0.9989586)

4

2 回答 2

1

好的,我终于设法解决了这个问题,也许这会对某人有所帮助。
我为此找到了一些公式,并使用我得到的坐标进行了此操作,范围从 -1 到 1:

    float tempX = (float) (start.x() * 0.1f * Math.tan(Math.PI * 60f / 360));
    float tempY = (float) (start.y() * 0.1f * Math.tan(Math.PI * 60f / 360) * height / width);
    float tempZ = -0.1f;

    direction = vecmath.vector(tempX, tempY, tempZ); //create new vector with these x,y,z
    direction = view.transformDirection(direction); 
    //multiply this new vector with the INVERSED viewMatrix
    origin = view.getPosition(); //set the origin to the position values of the matrix (the right column)
于 2014-09-26T19:09:17.513 回答
0

我并没有真正使用过时的opengl,但我会分享我的想法,首先,如果您向我们展示如何构建视图矩阵,这将很有帮助,其次,您拥有的视图矩阵位于相机的本地空间中,现在通常您会乘以您的mouseX 和 (ScreenHeight - mouseY - 1) 由 View 矩阵(我认为该矩阵的倒数对不起,不确定!)然后您将在相机空间中拥有鼠标坐标,然后您将前向向量添加到由创建的向量鼠标,然后你就会拥有它,它看起来像这样:

float mouseCoord[] = { mouseX, screen_heihgt - mouseY - 1, 0, 0 }; /* 0, 0 because we multipling by a matrix 4.*/
mouseCoord = multiply( ViewMatrix /*Or: inverse(ViewMatrix)*/, mouseCoord );
float ray[] = add( mouseCoord, forwardVector );
于 2014-09-19T18:00:34.210 回答