2017-07-05 53 views
3

我已经开始在阴影贴图的定向灯的工作和我需要的lookAt矩阵,但是当我试图从例如一个在线教程的构建它看起来是这样的:错误LOOKAT矩阵

enter image description here

目前,它看起来像这样: 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; 
} 

编辑:更新图片 编辑:添加更详细的描述

+0

不GLSL提供'lookAt'功能?您是否有理由手动完成此操作,而不是让着色器处理计算? – 0x5453

+0

@ 0x5453为了节省着色器的计算量,并试图弄清楚lookat矩阵是如何工作的。 –

+6

@ 0x5453:“* GLSL不提供lookAt函数吗?*”不,它不。 –

回答

1

后试图让LOOKAT矩阵小时的工作我已经放弃了构建LOOKAT矩阵的方式,而不是我构建了一个基于摄像头的位置LOOKAT矩阵和位置相机应该看,使用三角函数我能够创造我正在寻找的结果。

我的构建LOOKAT矩阵的电流方式:

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

不需要三角。你可能会遇到签署问题。你的第一个方法很好,用normalize-fix。只是你定义了一个行顺序矩阵,而不是一个col-order,这是OpenGL使用的。总之,你的照片看起来不错,除了可能是一个三角形,靠近相机,隐藏其余部分。另外,是的,“位置”是摄像机的位置,“中心”是摄像机在世界坐标系中的位置。 – Ripi2

3

您需要在计算u之前对s进行归一化。我不知道这是否是唯一的问题

+0

好赶上!但确实不是唯一的问题。 –

+0

你可以请更新图片。它目前有什么问题? – geza

+0

我更新了图片:) –

2

如果你的位置(positioncenter)和up载体是视空间,然后观察矩阵的Z轴的视线和Y逆线Axis是向上的向量。请看下面的代码:

Matrix4f Matrix4f::lookAt(const Vector3f& position, const Vector3f& center, const Vector3f& up) 
{ 
    Matrix4f out(1.0f); // I suppose this initilizes a 4*4 identity matrix 

    // Z-Axis is the line of sight 
    Vector3f z = position.substract(center).normalize(); // inverse line of sight 

    // Y-Axis is the up vector 
    Vector3f y = up; 

    // X-Axis is the cross product of Y-Axis and Z-Axis 
    Vector3f x = y.cross(z).normalize(); 

    // orthonormalize the Y-Axis 
    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))); 
} 
+0

什么是变量p和t? –

+0

@DaanMeijer复制粘贴问题 – Rabbid76

+0

它似乎在更好地工作 https://media.giphy.com/media/QrMnqBBJZuATu/giphy.gif –