ウィンドウの高さと幅から外れたいくつかの形状 (円など) を描画しています。ウィンドウは常に特定のサイズで開始されるため、正しく描画されますが、ウィンドウのサイズを変更すると、縦横比が乱れます。
ウィンドウのサイズに関係なく、形状を適切に描画するにはどうすればよいですか?
オブジェクトのサイズを明示的にウィンドウ サイズに依存させたくはありません。
genpfault で既に提案されているように、ウィンドウ サイズが変更されるたびに射影行列を調整します。
ウィンドウのサイズ変更でやるべきこと:
ビューポートを調整
glViewport(0, 0, width, height)
はさみの長方形を調整します(有効にしている場合のみGL_SCISSOR_TEST
)
glScissor(0, 0, width, height)
射影行列の調整
レガシー (固定関数パイプライン) OpenGL の場合、次の方法で実行できます。
glFrustum(left * ratio, right * ratio, bottom, top, nearClip,farClip)
また
glOrtho(left * ratio, right * ratio, bottom, top, nearClip,farClip)
また
gluOrtho2D(left * ratio, right * ratio, bottom, top)
left, right, bottom
(とtop
がすべて等しいと仮定するとratio=width/height
)
gluPerspective()のようなものを使用している場合は、ウィンドウの幅と高さの比率を使用してください。
gluPerspective(60, (double)width/(double)height, 1, 256);
OpenGL ウィンドウのサイズが変更されるたびに呼び出されるある種のウィンドウ ハンドラ関数をセットアップする必要があります。アスペクト比 > 1 の場合とアスペクト比 <= 1 の場合は別々に処理する必要があります。そうしないと、画面のサイズ変更後にジオメトリが画面からはみ出す可能性があります。
void windowResizeHandler(int windowWidth, int windowHeight){
const float aspectRatio = ((float)windowWidth) / windowHeight;
float xSpan = 1; // Feel free to change this to any xSpan you need.
float ySpan = 1; // Feel free to change this to any ySpan you need.
if (aspectRatio > 1){
// Width > Height, so scale xSpan accordinly.
xSpan *= aspectRatio;
}
else{
// Height >= Width, so scale ySpan accordingly.
ySpan = xSpan / aspectRatio;
}
glOrhto2D(-1*xSpan, xSpan, -1*ySpan, ySpan, -1, 1);
// Use the entire window for rendering.
glViewport(0, 0, windowWidth, windowHeight);
}