How to change the default QGLViewer's near and far clipping distance in QT and QGLViewer programs?

StackOverflow https://stackoverflow.com/questions/20624582

  •  02-09-2022
  •  | 
  •  

Question

I got a fairly large model that need to be displayed in a QT UI program that uses QGLViewer. So the model got cut because of the default near and far clipping distance is too narrow. My question is how to change the default near and far clipping range.

For example my problem could look like this one enter image description here

I tried to use something like,

::glMatrixMode(GL_PROJECTION) ;
::glLoadIdentity() ;

::glClearColor(1.0f,1.0f,1.0f,0.0f);
::glFrustum(-0.5,0.5,-0.5,0.5,-100000000.0,100000000.0) ;

::glMatrixMode(GL_MODELVIEW) ;
::glLoadIdentity() ;

This doesn't work at all, and it breaks the mouse interaction in the QGLViewer too. Since I'm using QT and QGLViewer, there's no glu functions that I can use.

So I'm asking for anyone knows how to make the QGLViewer change its default clipping range.

I found some examples QGLViewer provided like clipping plane example, standard camera example, but I still don't have a clue how to change the default viewer.

Was it helpful?

Solution

I think I worked out this by myself. From the documentation here. I just used this code to initialise the viewer,

void Viewer::initializeGL()
{
  QGLViewer::initializeGL();
  this->setSceneRadius(10000.0);
}

But this sets the default scene camera too, if the radius is high, the default perspective's position is changed too, so this setSceneRadius is not only changing the near/far clipping plane.

Actually, there are different methods from the documentation here.

So this one maybe better. The formula to calculate the real near and far is in the documentation of the last link. Smaller near coef and larger Clipping coef means larger range of the viewing area.

void Viewer::initializeGL()
{
  QGLViewer::initializeGL();
  this->camera()->setZNearCoefficient(0.00001);
  this->camera()->setZClippingCoefficient(1000.0);
}

Of course you can override your own version of near and far definition.

class myCamera :: public qglviewer::Camera
{
    virtual float Camera::zNear() const { return 0.001; };
    virtual float Camera::zFar() const { return 100.0; };
}

And construct your QGLViewer object with this customised camera.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top