如何在OpenGL / GLSL上使用查找矩阵解决问题

时间:2019-02-13 10:34:33

标签: c++ opengl graphics glsl

对于我自己的查找矩阵,我有以下代码(矩阵的乘积和向量的叉积很好用,我检查了它):

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);
}

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

1 个答案:

答案 0 :(得分:1)

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

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

在“透视投影”中,投影矩阵描述了从针孔相机中看到的世界3D点到视口的2D点的映射。
摄像机视锥中的眼睛空间坐标(截断的金字塔)映射到立方体(规范化的设备坐标)。
在透视投影时,视图空间(体积)由平截头体(截顶的金字塔)定义,金字塔的顶部是查看者的位置。 视线的方向(视线)以及近距和远距定义了将金字塔截成圆锥台的平面(视线是该平面的法向矢量)。
这意味着到近平面的距离和到远平面的距离这两个值都必须为正值:

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);

视图空间是本地系统,由场景上的视点定义。 视图的位置,视线和视图的向上方向定义了相对于世界坐标系的坐标系。
视图矩阵必须从世界空间转换为视图空间,因此视图矩阵是视图坐标系的逆矩阵。
如果视图空间的坐标系是Right-handed系统,其中X轴指向左侧,Y轴指向上方,则Z轴指向视图外(右手注意系统Z轴是X轴和Y轴的叉积。

z轴的视线是从视点eye到支架center的矢量:

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是指从视点中心到视锥面在近平面上的距离。 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
    };
}