0

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);
Elliott
  • 1,336
  • 1
  • 13
  • 26
TheDudeAbides
  • 420
  • 1
  • 7
  • 21

1 Answers1

1

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.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • The near and far plane are set up "far" enough, that works AFAIK. What do you suggest I change about them? I just want to show the table (left bottom coord = 0,0, right top coord = 1.4,2.7), maybe it can have some empty space around it, but just the table at first.. Thanks – TheDudeAbides Jun 04 '12 at 13:50
  • Apparently they're not far away enough, because your scene clips. I'd say the near clipping plane is the culprit. In a ortho projection negative values for the near clip plane are perfectly acceptable. – datenwolf Jun 04 '12 at 18:44
  • Wouldn't setup correctly only the viewport (keeping the correct aspect) be much easier? – Luca Jun 04 '12 at 20:19
  • @Luca: The viewport only selects the subset of the window that the NDC coordinates are mapped to. If you had a fixed aspect ratio in your projection, then the aspect ratio of the viewport dimensions would have to be kept the same. Technically the ratio between the viewport aspect and the projection aspect should be the aspect ratio of a pixel. Most displays use square pixels, so viewport aspect == projection aspect. But all of my engines support non square pixels (allows you e.g. to strech 1024x768 to a 16:10 widescreen, yet keeping the geometry undistorted). – datenwolf Jun 04 '12 at 21:21