Question

I'm programming a billiards game in C++ with Qt and OpenGL (more specific, the QGLWidget). I know my way around OpenGL somewhat, but not too much of a professional.

What I did: create a Qt app window with Qt widgets (buttons,...) and a QGLWidget (my own implementation) in it as well.

The problem: I can draw a ball in this window and rotate it,... etc but the aspect ratio of the view is not right + I can't seem to use the full space.

Wrong aspect ratio The white rectangle is the playing field (measurements: 1.4 x 2.7 in float coordinates)

I can transform the model but then it seems cut off: Model cut off

As soon as I transform the view (check below for functions) a part of my model seems to be cut off.

This is the rectangle (balls are drawn correctly within these coordinates):

glBegin(GL_LINE_LOOP);
    glColor3f(255.0, 255.0, 255.0);
    glVertex3f(0.0, 0.0, 0.0);
    glVertex3f(1.4, 0.0, 0.0);

    glVertex3f(1.4, 0.0, 0.0);
    glVertex3f(1.4, 2.7, 0.0);

    glVertex3f(1.4, 2.7, 0.0);
    glVertex3f(0.0, 2.7, 0.0);

    glVertex3f(0.0, 2.7, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
glEnd();

OpenGL resize function:

void GLWidget::resizeGL( int width, int height )
{
    int side = qMin( width, height );
    glViewport( (width - side) / 2, (height - side) / 2, side, side );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( -0.05, +1.45, -0.05, +2.75, 0.0, 50.0 );
    //glFrustum( -0.05, +1.45, 0.0, 0.0, 0.0, 50.0 );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();                                                     
}

And at last, a part from the render function related to the view:

 gluLookAt(0.0, 0.0, 1.0, 0.0, -0.70, 0.0, 0.0, 1.0, 0.0 ); // This is the cut of part
 //gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ); // Normal but weird rectangle
 glScalef( 1.0, 1.0, 1.0);
Was it helpful?

Solution

Your problem lies here:

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -0.05, +1.45, -0.05, +2.75, 0.0, 50.0 );

The left/right or top/bottom limits of the projection should match the aspect of your viewport. Those hardcoded values will not adapt to changes in the window geometry.

Also make sure that the near and far plane don't cut into the visible scene range.

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