Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 不正确的注视矩阵_C++_Opengl_Matrix - Fatal编程技术网

C++ 不正确的注视矩阵

C++ 不正确的注视矩阵,c++,opengl,matrix,C++,Opengl,Matrix,我已经开始研究平行光的阴影贴图,为此我需要一个lookAt矩阵,但当我尝试从在线教程的示例构建时,它看起来像这样: gl_Position = projection * view * model * vec4(vertexPosition, 1.0); 目前看起来是这样的: 我尝试了多种构造方法,但没有成功,我检查了规范化、交叉和转换函数是否正确,但事实并非如此。我也尝试过从列主矩阵改为行主矩阵,但没有成功。有人能指出我做错了什么吗 观察矩阵构造: 中心向量=(0,0,0), 上方向向量=

我已经开始研究平行光的阴影贴图,为此我需要一个lookAt矩阵,但当我尝试从在线教程的示例构建时,它看起来像这样:

gl_Position = projection * view * model * vec4(vertexPosition, 1.0);

目前看起来是这样的:

我尝试了多种构造方法,但没有成功,我检查了规范化、交叉和转换函数是否正确,但事实并非如此。我也尝试过从列主矩阵改为行主矩阵,但没有成功。有人能指出我做错了什么吗

观察矩阵构造:

中心向量=(0,0,0), 上方向向量=(0,1,0)

代码学分:

这是我将矩阵传递给着色器的方式:

void Shader::setMat4(const char* name, const math::Matrix4f& matrix){
    glUniformMatrix4fv(getUniformLocation(name), 1, GL_TRUE, matrix.mElements);
}
在计算注视矩阵后,我直接将其传递给顶点着色器到统一:
视图
并计算如下所示的点:

gl_Position = projection * view * model * vec4(vertexPosition, 1.0);
这就是我的矩阵乘法的工作原理:

Matrix4f Matrix4f::multiply(const Matrix4f& other) const {
    Matrix4f out;
    for (int y = 0; y < 4; y++) {
        for (int x = 0; x < 4; x++) {
            fl32 o = 0;
            for (int c = 0; c < 4; c++) {
                o += this->mElements[c + y * 4] * other.mElements[x + c * 4];                   }
            out.mElements[x + y * 4] = o;
        }
    }
    return out;
}
Matrix4f Matrix4f::乘法(常量Matrix4f&other)常量{
矩阵输出;
对于(int y=0;y<4;y++){
对于(int x=0;x<4;x++){
fl32 o=0;
对于(int c=0;c<4;c++){
o+=this->mElements[c+y*4]*其他.mElements[x+c*4];}
out.melents[x+y*4]=o;
}
}
返回;
}
编辑:更新图片
编辑:添加了更详细的说明

在计算u之前,您需要规范化s。我不确定这是否是唯一的问题

如果您的位置(
位置
中心
)和
向上
向量在视口空间中,则视图矩阵的Z轴是反向视线,Y轴是向上向量。请参阅以下代码:


经过数小时的努力,我放弃了构建注视矩阵的方法,取而代之的是,我根据相机的位置和相机应该观察的位置构建了一个注视矩阵,使用三角函数,我能够创建我正在寻找的结果

我目前构建观察矩阵的方法:

Matrix4f Matrix4f::lookAt(const Vector3f& position, const Vector3f& center) {
        Vector3f deltaVector = (position - center).normalize();

        fl32 yaw = (fl32)radToDeg(atan(deltaVector.x / deltaVector.z));
        fl32 pitch = (fl32)radToDeg(acos(Vector2f(deltaVector.x, deltaVector.z).magnitude()));

        if (deltaVector.z > 0)
            yaw = yaw - 180.0f;

        Matrix4f yRotation = Matrix4f::rotation(Vector3f(0.0f, 1.0f, 0.0f), -yaw);
        Matrix4f xRotation = Matrix4f::rotation(Vector3f(1.0f, 0.0f, 0.0f), pitch);

        Matrix4f translation = Matrix4f::translation(position);

        return (translation * (yRotation * xRotation));
    }

GLSL不提供查看功能吗?有什么原因可以让着色器来处理计算而不是手动执行此操作吗?@0x5453可以避免着色器计算太多并试图找出lookat矩阵的工作原理。@0x5453:“GLSL不提供lookat函数吗?”没有。@0x5453 GLSL当然不提供,但有以opengl为中心的库可以提供,作为其中的一员,既然它在github上,我建议您看看如何将Matrix4f传递给OpenGL?矩阵乘法背后的概念是什么?我的意思是,一个点是由
M*p
p*M
转换的吗?如何使用
lookAt
的结果?我的意思是,它是相机的矩阵,还是它的逆矩阵?我们需要知道这些事情来回答这个问题。好的!。但这确实不是唯一的问题。你能更新一下图片吗。目前它有什么问题?变量p和t是什么?它似乎工作得更好,这与我预期的不太一样。我想要的是能够使lookAt矩阵工作(或者它周围的函数可能不工作),这样我就能够构建渲染阴影贴图的环境,而不需要三角函数。而且你可能会招致签名问题。您的第一种方法很好,使用了normalize修复。只是定义了一个行顺序矩阵,而不是列顺序矩阵,这正是OpenGL所使用的。不管怎样,你的照片看起来不错,除了一个三角形,靠近相机,隐藏了其余的部分。同样,是的,
位置
是相机所在的位置,
中心
是相机所看的位置,两者都是在世界坐标系下。
Matrix4f Matrix4f::lookAt(const Vector3f& position, const Vector3f& center) {
        Vector3f deltaVector = (position - center).normalize();

        fl32 yaw = (fl32)radToDeg(atan(deltaVector.x / deltaVector.z));
        fl32 pitch = (fl32)radToDeg(acos(Vector2f(deltaVector.x, deltaVector.z).magnitude()));

        if (deltaVector.z > 0)
            yaw = yaw - 180.0f;

        Matrix4f yRotation = Matrix4f::rotation(Vector3f(0.0f, 1.0f, 0.0f), -yaw);
        Matrix4f xRotation = Matrix4f::rotation(Vector3f(1.0f, 0.0f, 0.0f), pitch);

        Matrix4f translation = Matrix4f::translation(position);

        return (translation * (yRotation * xRotation));
    }