2

I try to draw the camera frustum for debugging purpose. I can draw it using the NDC space with the following code

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

But when i try to draw it using the inverse view, the frustum is drawn behind to the camera.

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

As you can see in the sceenshots, when I look at the center of the scene the frustum is drawn behind the camera.

view from camera 1

view from camera 2

The code to compute the view

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

And the lookAt function

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

Does anyone know what is wrong with my code ? Let me know if there is a lack of information.

The code is also on this github in the branch shadow_map (github.com/jbalestr42/GraphicsEngine)

genpfault
  • 51,148
  • 11
  • 85
  • 139
jbalestr42
  • 74
  • 1
  • 6

1 Answers1

3

Classic OpenGL view and projection matrices are set up so that view space is right-handed, with the camera looking into -z direction, and the glOrtho and glFrustum functions interpreting near and far as distances along the view direction, hence the view space z coordinates of the near and far planes are

z_near = -near
z_far = -far

while your code:

{
    // 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},
};

just draws the frustum in+z direction, which is to be expected to end up behind the camera.

derhass
  • 43,833
  • 2
  • 57
  • 78