14

ウィンドウの高さと幅から外れたいくつかの形状 (円など) を描画しています。ウィンドウは常に特定のサイズで開始されるため、正しく描画されますが、ウィンドウのサイズを変更すると、縦横比が乱れます。

ウィンドウのサイズに関係なく、形状を適切に描画するにはどうすればよいですか?

4

5 に答える 5

21

オブジェクトのサイズを明示的にウィンドウ サイズに依存させたくはありません。

genpfault で既に提案されているように、ウィンドウ サイズが変更されるたびに射影行列を調整します。

ウィンドウのサイズ変更でやるべきこと:

  1. ビューポートを調整

    glViewport(0, 0, width, height)
    
  2. はさみの長方形を調整します(有効にしている場合のみGL_SCISSOR_TEST)

    glScissor(0, 0, width, height)
    
  3. 射影行列の調整

    レガシー (固定関数パイプライン) 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)

于 2010-07-17T08:11:51.773 に答える
6

gluPerspective()のようなものを使用している場合は、ウィンドウの幅と高さの比率を使用してください。

gluPerspective(60, (double)width/(double)height, 1, 256);
于 2010-07-16T19:19:40.067 に答える
4

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);
}
于 2013-04-03T05:35:06.353 に答える