c++ - 如何解决 OpenGL/GLSL 上的观察矩阵问题

标签 c++ opengl graphics glsl

我有以下用于我自己的观察矩阵的代码(矩阵的乘法和 vector 的叉积工作完美,我检查过):

template<typename Type>
void setLookAt(Matrix4x4<Type>& matrix, const Vector3<Type> eye, const Vector3<Type> center, const Vector3<Type> up) noexcept
{
    Math::Vector3f right = Math::cross(center, up).normalize();
    Matrix4x4f lookAt({
        right.getX(), right.getY(), right.getZ(), 0.0,
        up.getX(), up.getY(), up.getZ(), 0.0,
        center.getX(), center.getY(), center.getZ(), 0.0,
        0.0, 0.0, 0.0, 1.0
    });
    Matrix4x4f additionalMatrix({
        0.0, 0.0, 0.0, -(eye.getX()),
        0.0, 0.0, 0.0, -(eye.getY()),
        0.0, 0.0, 0.0, -(eye.getZ()),
        0.0, 0.0, 0.0, 1.0
    });
    lookAt.mul(additionalMatrix);
    matrix = lookAt;
}

template<typename Type>
void setPerspectiveMatrix(Matrix4x4<Type>& matrix, Type fov, Type aspect, Type znear, Type zfar) noexcept
{
    const Type yScale = static_cast<Type>(1.0 / tan(RADIANS_PER_DEGREE * fov / 2));
    const Type xScale = yScale / aspect;
    const Type difference = znear - zfar;
    matrix  = {
        xScale, 0, 0, 0,
        0, yScale, 0, 0,
        0, 0, (zfar + znear) / difference, 2 * zfar * znear / difference,
        0, 0, -1, 0
    };
}

矩阵乘法实现:

// static const std::uint8_t ROW_SIZE = 4;
// static const std::uint8_t MATRIX_SIZE = ROW_SIZE * ROW_SIZE;
// static const std::uint8_t FIRST_ROW = 0;
// static const std::uint8_t SECOND_ROW = ROW_SIZE;
// static const std::uint8_t THIRD_ROW = ROW_SIZE + ROW_SIZE;
// static const std::uint8_t FOURTH_ROW = ROW_SIZE + ROW_SIZE + ROW_SIZE;

template<class Type>
void Matrix4x4<Type>::mul(const Matrix4x4& anotherMatrix) noexcept
{
    Type currentElements[MATRIX_SIZE];
    std::copy(std::begin(mElements), std::end(mElements), currentElements);
    const Type* otherElements = anotherMatrix.mElements;
    for (std::uint8_t i = 0; i < MATRIX_SIZE; i += ROW_SIZE) 
    {
        mElements[i] = currentElements[i] * otherElements[FIRST_ROW] +
            currentElements[i + 1] * otherElements[SECOND_ROW] +
            currentElements[i + 2] * otherElements[THIRD_ROW] +
            currentElements[i + 3] * otherElements[FOURTH_ROW];
        mElements[i + 1] = currentElements[i] * otherElements[FIRST_ROW + 1] +
            currentElements[i + 1] * otherElements[SECOND_ROW + 1] +
            currentElements[i + 2] * otherElements[THIRD_ROW + 1] +
            currentElements[i + 3] * otherElements[FOURTH_ROW + 1];
        mElements[i + 2] = currentElements[i] * otherElements[FIRST_ROW + 2] +
            currentElements[i + 1] * otherElements[SECOND_ROW + 2] +
            currentElements[i + 2] * otherElements[THIRD_ROW + 2] +
            currentElements[i + 3] * otherElements[FOURTH_ROW + 2];
        mElements[i + 3] = currentElements[i] * otherElements[FIRST_ROW + 3] +
            currentElements[i + 1] * otherElements[SECOND_ROW + 3] +
            currentElements[i + 2] * otherElements[THIRD_ROW + 3] +
            currentElements[i + 3] * otherElements[FOURTH_ROW + 3];
    }
}

跨产品实现:

template<typename Type>
Math::Vector3<Type> cross(Vector3<Type> vector, Vector3<Type> anotherVector) noexcept
{
    const Type x = vector.getY()*anotherVector.getZ() - vector.getZ()*anotherVector.getY();
    const Type y = -(vector.getX()*anotherVector.getZ() - vector.getZ()*anotherVector.getX());
    const Type z = vector.getX()*anotherVector.getY() - vector.getY()*anotherVector.getX();
    return { x, y, z };
}

使用它:

// OpenGL

glUseProgram(mProgramID);
Matrix4x4f lookAt;
setLookAt(lookAt, { 0.0f, 0.0f, 3.0f }, { 0.0f, 0.0f, -1.0f }, { 0.0f, 1.0f, 0.0f });
glUniformMatrix4fv(glGetAttribLocation(mProgramID, "viewMatrix"), 1, GL_TRUE, lookAt);
Matrix4x4f projection;
setPerspectiveMatrix(projection, 45.0f, width / height, -0.1, 100.0f);
glUniformMatrix4fv(glGetAttribLocation(mProgramID, "projectionMatrix "), 1, GL_TRUE, projection);
// GLSL

layout (location = 0) in vec3 position;

uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

void main()
{
    gl_Position = projectionMatrix * viewMatrix * vec4(position, 1.0f);
}

使用此代码后,我得到一个空白屏幕,尽管我必须绘制一个立方体。问题出在矩阵本身,所以其他矩阵工作正常(偏移、旋转、...),但我可以准确地理解在哪里。你能告诉我可能是什么问题吗?

最佳答案

"projectionMatrix""viewMatrix" 是统一变量。统一位置可以通过glGetUniformLocation获取而不是 glGetAttribLocation ,这将返回事件属性的属性索引:

GLint projLoc = glGetUniformLocation( mProgramID, "projectionMatrix" );
GLint viewLoc = glGetUniformLocation( mProgramID, "viewMatrix" );

在透视投影中,投影矩阵描述了从针孔相机看到的世界中的 3D 点到视口(viewport)的 2D 点的映射。
视锥体(截棱锥)中的眼空间坐标映射到立方体(归一化设备坐标)。
在透视投影中, View 空间(体积)由平截头锥体(截头金字塔)定义,其中金字塔的顶部是观察者的位置。 视线方向(视线)和远近距离定义了将金字塔截成截锥体的平面(视线方向是该平面的法 vector )。
这意味着到近平面的距离和到远平面的距离这两个值都必须是正值:

Matrix4x4f lookAt;
setLookAt(lookAt, { 0.0f, 0.0f, 3.0f }, { 0.0f, 0.0f, -1.0f }, { 0.0f, 1.0f, 0.0f });
glUniformMatrix4fv(viewLoc, 1, GL_TRUE, lookAt);

Matrix4x4f projection;
setPerspectiveMatrix(projection, 45.0f, width / height, 0.1f, 100.0f); // 0.1f instead of -0.1f
glUniformMatrix4fv(projLoc, 1, GL_TRUE, projection);

View 空间是由场景中的视点定义的局部系统。 View 的位置,视线和 View 的向上方向,定义一个相对于世界坐标系的坐标系。
View 矩阵必须从世界空间变换到 View 空间,因此 View 矩阵是 View 坐标系的逆矩阵。
如果 View 空间的坐标系是Right-handed系统,其中 X 轴指向左侧,Y 轴指向上方,然后 Z 轴指向 View 外(请注意,在右手系统中,Z 轴是 X 轴和Y 轴)。

z轴视线是从视点eye到目标center的 vector :

template<typename Type>
void setLookAt(Matrix4x4<Type>& matrix, const Vector3<Type> eye, const Vector3<Type> center, const Vector3<Type> up) noexcept
{
    Vector3f mz( { eye.getX()-center.getX(), eye.getY()-center.getY(), eye.getZ()-center.getZ() } );
    mz = mz.normalize();
    Vector3f my = up.normalize();
    Vector3f mx = cross(my, mz).normalize();

    Type tx = dot( mx, eye );
    Type ty = dot( my, eye );
    Type tz = -dot( mz, eye );

    matrix = {
        mx.getX(), mx.getY(), mx.getZ(), tx,
        my.getX(), my.getY(), my.getZ(), ty,
        mz.getX(), mz.getY(), mz.getZ(), tz,
        0.0,       0.0,       0.0,       1.0
    };
}

template<typename Type>
Vector3<Type> cross(Vector3<Type> vector, Vector3<Type> anotherVector) noexcept
{
    const Type x = vector.getY()*anotherVector.getZ() - vector.getZ()*anotherVector.getY();
    const Type y = -(vector.getX()*anotherVector.getZ() - vector.getZ()*anotherVector.getX());
    const Type z = vector.getX()*anotherVector.getY() - vector.getY()*anotherVector.getX();
    return { x, y, z };
}

template<typename Type>
Vector3<Type> Vector3<Type>::normalize(void) const
{ 
  Type len = std::sqrt(mV[0]*mV[0] + mV[1]*mV[1] + mV[2]*mV[2]);
  return { mV[0] / len, mV[1] / len, mV[2] / len }; 
}

template<typename Type>
Type dot(Vector3<Type> vector, Vector3<Type> anotherVector) noexcept
{
    Type ax = vector.getX(), ay = vector.getY(), az = vector.getZ();
    Type bx = anotherVector.getX(), by = anotherVector.getY(), bz = anotherVector.getZ();
    return ax*bx + ay*by + az*bz;
}

透视投影矩阵可以由平截头体定义。
leftrightbottomtop 是 View 中心到侧面的距离平截头体的面,在近平面上。 nearfar 指定到平截头体上近平面和远平面的距离。

r = right, l = left, b = bottom, t = top, n = near, f = far

x              y              z                t
2*n/(r-l)      0              (r+l)/(r-l)      0
0              2*n/(t-b)      (t+b)/(t-b)      0
0              0             -(f+n)/(f-n)     -2*f*n/(f-n)
0              0             -1                0

如果投影是对称的,视线是视锥体的对称轴,那么矩阵可以简化:

x              y              z                t
1/(ta*a)       0              0                0
0              1/ta           0                0
0              0             -(f+n)/(f-n)     -2*f*n/(f-n)
0              0             -1                0

哪里:

a = w / h
ta = tan( fov_y / 2 );

2 * n / (r-l) = 1 / (ta * a)
2 * n / (t-b) = 1 / ta

此外,投影矩阵从右手系统切换到左手系统,因为 z 轴发生了转动。

template<typename Type>
void setPerspectiveMatrix(Matrix4x4<Type>& matrix, Type fov, Type aspect, Type znear, Type zfar) noexcept
{
    const Type yScale = static_cast<Type>(1.0 / tan(RADIANS_PER_DEGREE * fov / 2));
    const Type xScale = yScale / aspect;
    const Type difference = zfar - znear;
    matrix  = {
        xScale, 0,       0,                            0,
        0,      yScale,  0,                            0,
        0,      0,      -(zfar + znear) / difference, -2 * zfar * znear / difference,
        0,      0,      -1,                            0
    };
}

关于c++ - 如何解决 OpenGL/GLSL 上的观察矩阵问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54668059/

相关文章:

graphics - 尝试使用简单着色器 + VBO 时出现问题

c++ - 如何在 C++ 中通过内置的 reverse() 函数存储反转的字符串

opengl - 你能计算每个实例而不是每个顶点的东西吗?

java - 实现 Bresenham 的圆形绘制算法

c++ - GLut.h/osfinfo.c/dbgheap.c/mlock.c 中 0x76fe15de 的未处理异常

opengl - OpenGL 是使用 Xlib 来绘制窗口和渲染事物,还是相反?

image - OpenGL 与 GLUT——malloc 错误

c++ - 什么是尼布洛德?

c++ - 在 Eigen C++ 中关心断言失败的最佳方法是什么?

c++ - 在 native c++ 应用程序中注册 Windows 运行时组件 (c++winrt)