我已经开始在阴影贴图的定向灯的工作和我需要的lookAt矩阵,但是当我试图从例如一个在线教程的构建它看起来是这样的:错误LOOKAT矩阵
目前,它看起来像这样: https://media.giphy.com/media/QrMnqBBJZuATu/giphy.gif
我试图构建它的多种方法,但没有成功,我检查,如果常态化,交叉和转换功能是不正确的,但事实并非如此。我也尝试从列主矩阵更改为行主矩阵,但没有运气。有人能够指出我做错了什么吗?
LOOKAT基质构建:
中心矢量=(0,0,0), 向上矢量=(0,1,0)
Matrix4f Matrix4f::lookAt(const Vector3f& position, const Vector3f& center, const Vector3f& up) {
Matrix4f out(1.0f);
Vector3f z = position.substract(center).normalize();
Vector3f y = up;
Vector3f x = y.cross(z).normalize();
y = z.cross(x);
out.mElements[0 * 4 + 0] = x.x;
out.mElements[0 * 4 + 1] = x.y;
out.mElements[0 * 4 + 2] = x.z;
out.mElements[1 * 4 + 0] = y.x;
out.mElements[1 * 4 + 1] = y.y;
out.mElements[1 * 4 + 2] = y.z;
out.mElements[2 * 4 + 0] = z.x;
out.mElements[2 * 4 + 1] = z.y;
out.mElements[2 * 4 + 2] = z.z;
return (out * Matrix4f::translation(Vector3f(-position.x, -position.y, -position.z)));
}
}
信用代码:https://stackoverflow.com/users/5577765/rabbid76
这是我如何将一个矩阵传递给着色器:
void Shader::setMat4(const char* name, const math::Matrix4f& matrix){
glUniformMatrix4fv(getUniformLocation(name), 1, GL_TRUE, matrix.mElements);
}
A我计算的lookAt矩阵压脚提升我直接把它传递给顶点着色均匀:view
,并计算这样一个观点:
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;
}
编辑:更新图片 编辑:添加更详细的描述
不GLSL提供'lookAt'功能?您是否有理由手动完成此操作,而不是让着色器处理计算? – 0x5453
@ 0x5453为了节省着色器的计算量,并试图弄清楚lookat矩阵是如何工作的。 –
@ 0x5453:“* GLSL不提供lookAt函数吗?*”不,它不。 –