3

I'm trying to set the far clipping plane to a higher value, but I'm a bit lost.

I'm looking at a complex object using a QMatrix4x4 camera persepective like that:

glMatrixMode(GL_MODELVIEW);
m_view = QMatrix4x4();
m_view.lookAt(QVector3D(17.f, 36.f, 36.f),  // eye
              QVector3D(17.f,  0.f, 19.f),  // center
              QVector3D( 0.f,  0.f, -1.f)); // up
glLoadMatrixf(m_view.data());

Now, looking from this perspective, my object is being clipped by the far plane.

far plane clip

I've tried to increase the far plane distance by using a frustum matrix operation with a huge value. All I want is to modify the back clipping plane:

m_view.frustum(1.f, 1.f, 1.f, 1.f, 1.f, 200000.f);

But this does not show any difference, modifying the farPlane parameter does not change anything. The same applies to ortho:

m_view.ortho(1.f, 1.f, 1.f, 1.f, 1.f, 200000.f);

Or perspective:

m_view.perspective(0.f, 1.f, 1.f, 200000.f);

What's wrong with my approach? How to modify the far clipping plane?


Edit

I've extended the code snippets above as the problem was not visible. Obviously my mistake was modifying the GL_MODELVIEW instead of the GL_PROJECTION.

Working code should look like that:

// Projection
m_projection = QMatrix4x4();
m_projection.perspective(20.f, m_width / m_height, 0.01f, 2000.f); // note the far plane
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(m_projection.data());
// Model view
m_view = QMatrix4x4();
m_view.lookAt(QVector3D(17.f, 36.f, 36.f),  // eye
              QVector3D(17.f,  0.f, 19.f),  // center
              QVector3D( 0.f,  0.f, -1.f)); // up
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(m_view.data());
q9f
  • 11,293
  • 8
  • 57
  • 96

1 Answers1

2

2 issues:

  1. You are mixing modelview (lookat) and projection (frustum) operations on the same matrix. You need 2 different matrices.

  2. You define the left and right values as both 1.f in ::frustum(). It cannot work, they need to be different. Same for the top and bottom values.

Here is an extract of the definition of QMatrix4x4::frustum():

if (left == right || bottom == top || nearPlane == farPlane)    
    return *this;

So your call to frustum does not do anything.

Once you have good values for top/left/top/bottom, try playing with the near plane distance too, as it might be it that clips your object and not the far plane.

Yohan Danvin
  • 885
  • 6
  • 13
  • No effect with both zero and negative values. :( – q9f Jun 30 '13 at 10:40
  • up using a negative value for the z axis might be a problem, try using a negative value for the far clipping plane too. Also, make sure this is what you want. up usually is (0,1,0) (y axis points up). – Yohan Danvin Jun 30 '13 at 10:41
  • Yeah, did that, too. Modifying near and far in any direction does not show any effect. – q9f Jun 30 '13 at 11:05
  • 1
    Hmm. Are you sure m_view is used afterwards? If you change it and it has no effect, maybe it's because it's not in effect at all and you're changing a variable which is otherwise unused. – Yohan Danvin Jun 30 '13 at 11:10
  • Yes. See at the code in my question, `m_view.lookAt()` works as desired, but `m_view.frustum()` is not doing anything with the near and far planes at all. Modifying the left,right,top,bottom parameters of frustum is indeed working somehow. Just the near and far planes are not. – q9f Jun 30 '13 at 12:05
  • I just realized: your left and right values are the same. And top and bottom too. All 1.f. It cannot work. Editing answer. – Yohan Danvin Jun 30 '13 at 12:16
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32637/discussion-between-donschoe-and-yohan-danvin) – q9f Jun 30 '13 at 12:33