如何在openGL中使用逆视图矩阵绘制相机平截头体

时间:2017-05-26 10:08:57

标签: c++ opengl 3d camera frustum

我尝试绘制相机视锥体以进行调试。 我可以使用NDC空间使用以下代码绘制它

Matrix inv = (camera.getViewMatrix() * camera.getProjectionMatrix()).inverse();

Vector4 f[8u] =
{
    // near face
    {1, 1, -1, 1.f},
    {-1, 1, -1, 1.f},
    {1, -1, -1, 1.f},
    {-1, -1, -1, 1.f},

    // far face
    {1, 1, 1, 1.f},
    {-1, 1, 1 , 1.f},
    {1, -1, 1 , 1.f},
    {-1, -1,1, 1.f},
};

Vector3 v[8u];
for (int i = 0; i < 8; i++)
{
    Vector4 ff = inv * f[i];
    v[i].x = ff.x / ff.w;
    v[i].y = ff.y / ff.w;
    v[i].z = ff.z / ff.w;
}

drawLine(v[0], v[1], Color::White);
drawLine(v[0], v[2], Color::White);
drawLine(v[3], v[1], Color::White);
drawLine(v[3], v[2], Color::White);

drawLine(v[4], v[5], Color::White);
drawLine(v[4], v[6], Color::White);
drawLine(v[7], v[5], Color::White);
drawLine(v[7], v[6], Color::White);

drawLine(v[0], v[4], Color::White);
drawLine(v[1], v[5], Color::White);
drawLine(v[3], v[7], Color::White);
drawLine(v[2], v[6], Color::White);

但是当我尝试使用反向视图绘制它时,平截头体被拉到相机后面。

Matrix inv = camera.getViewMatrix().inverse();

float ar = static_cast<float>(800.f / 600.f);
float fov = 60.f;
float near = 0.1f;
float far = 100.f;
float halfHeight = tanf(Deg2Rad * (fov / 2.f));
float halfWidth = halfHeight * ar;

float xn = halfWidth * near;
float xf = halfWidth * far;
float yn = halfHeight * near;
float yf = halfHeight * far;

Vector4 f[8u] =
{
    // near face
    {xn, yn, near, 1.f},
    {-xn, yn, near, 1.f},
    {xn, -yn, near, 1.f},
    {-xn, -yn,near , 1.f},

    // far face
    {xf, yf, far, 1.f},
    {-xf, yf,far , 1.f},
    {xf, -yf,far , 1.f},
    {-xf, -yf,far, 1.f},
};

Vector3 v[8];
for (int i = 0; i < 8; i++)
{
    Vector4 ff = inv * f[i];
    v[i].x = ff.x / ff.w;
    v[i].y = ff.y / ff.w;
    v[i].z = ff.z / ff.w;
}

drawLine(v[0], v[1], Color::White);
drawLine(v[0], v[2], Color::White);
drawLine(v[3], v[1], Color::White);
drawLine(v[3], v[2], Color::White);

drawLine(v[4], v[5], Color::White);
drawLine(v[4], v[6], Color::White);
drawLine(v[7], v[5], Color::White);
drawLine(v[7], v[6], Color::White);

drawLine(v[0], v[4], Color::White);
drawLine(v[1], v[5], Color::White);
drawLine(v[3], v[7], Color::White);
drawLine(v[2], v[6], Color::White);

正如你在sceenshots中看到的那样,当我看到场景的中心时,会在镜头后面画出平截头体。

view from camera 1

view from camera 2

计算视图的代码

static const float speed = frametime * 10000.f; // TODO add speed variable
Vector2 delta = m_mousePosition - Mouse::getPosition();
m_mousePosition = Mouse::getPosition();

Quaternion q;
Transformable::rotateX(-delta.y * frametime * 100.f);
Transformable::rotateY(delta.x * frametime * 100.f);
q.fromEuler(Transformable::getRotation());
m_direction = m_originDirection.rotate(q).normalize();
m_right = m_originUp.cross(m_direction).normalize();
m_up = m_direction.cross(m_right).normalize();

if (Keyboard::isKeyPress(GLFW_KEY_A))
    Transformable::translate(m_right * frametime * speed);
else if (Keyboard::isKeyPress(GLFW_KEY_D))
    Transformable::translate(-m_right * frametime * speed);
if (Keyboard::isKeyPress(GLFW_KEY_W))
    Transformable::translate(m_direction * frametime * speed);
else if (Keyboard::isKeyPress(GLFW_KEY_S))
    Transformable::translate(-m_direction * frametime * speed);
if (Keyboard::isKeyPress(GLFW_KEY_Q))
    Transformable::translate(-m_up * frametime * speed);
else if (Keyboard::isKeyPress(GLFW_KEY_E))
    Transformable::translate(m_up * frametime * speed);

m_view = Matrix::lookAt(Transformable::getPosition(), Transformable::getPosition() + m_direction, m_up);

lookAt函数

Matrix Matrix::lookAt(Vector3 const & eye, Vector3 const & center, Vector3 const & up)
{
    Vector3 f = (center - eye).normalize();
    Vector3 u = up;
    u.normalize();
    Vector3 s = f.cross(u).normalize();
    u = s.cross(f);

    Matrix result(s.x, u.x, -f.x, 0.f,
                s.y, u.y, -f.y, 0.f,
                s.z, u.z, -f.z, 0.f,
                -s.dotProduct(eye), -u.dotProduct(eye), f.dotProduct(eye), 1.f);
    return (result);
}

有谁知道我的代码出了什么问题? 如果缺少信息,请告诉我。

代码也在分支shadow_map中的这个github上 (github.com/jbalestr42/GraphicsEngine)

1 个答案:

答案 0 :(得分:2)

设置经典OpenGL视图和投影矩阵,以便视图空间是右手的,相机调查-z方向,glOrthoglFrustum函数解释{{ 1}}和near沿着视图方向作为距离,因此近距离和远距离平面的视图空间far坐标是

z

代码:

z_near = -near
z_far = -far

只是在{ // near face {xn, yn, near, 1.f}, {-xn, yn, near, 1.f}, {xn, -yn, near, 1.f}, {-xn, -yn,near , 1.f}, // far face {xf, yf, far, 1.f}, {-xf, yf,far , 1.f}, {xf, -yf,far , 1.f}, {-xf, -yf,far, 1.f}, }; 方向画出平截头体,预计最终会落在相机后面。