2016-12-03 6 views
0

ここで私が達成したいことは、以下のコードでswitch_2D_3Dというフラグがあり、それが真であれば2Dモード、そうでなければ3Dに切り替わります。スイッチfron 2Dから3Dへ

void reshape(GLsizei width, GLsizei height) 
{ 
    if (switch_2D_3D) 
    { 
     // GLsizei for non-negative integer 
     // Compute aspect ratio of the new window 
     if (height == 0) 
      height = 1;    // To prevent divide by 0 

     GLfloat aspect = (GLfloat)width/(GLfloat)height; 

     // Reset transformations 
     glLoadIdentity(); 

     // Set the aspect ratio of the clipping area to match the viewport 
     glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix 

     // Set the viewport to cover the new window 
     glViewport(0, 0, width, height); 

     if (width >= height) 
     { 
      // aspect >= 1, set the height from -1 to 1, with larger width 
      gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0); 
     } 
     else 
     { 
      // aspect < 1, set the width to -1 to 1, with larger height 
      gluOrtho2D(-1.0, 1.0, -1.0/aspect, 1.0/aspect); 
     } 

     winWidth = width; 
     winHeight = height; 
    } // 2D mode 
    else 
    { 
     // Prevent a divide by zero, when window is too short 
     // (you cant make a window of zero width). 
     if (height == 0) 
      height = 1; 

     float ratio = width * 1.0/height; 

     // Use the Projection Matrix 
     glMatrixMode(GL_PROJECTION); 

     // Reset Matrix 
     glLoadIdentity(); 

     // Set the viewport to be the entire window 
     glViewport(0, 0, width, height); 

     // Set the correct perspective. 
     gluPerspective(45.0f, ratio, 0.1f, 100.0f); 

     // Get Back to the Modelview 
     glMatrixMode(GL_MODELVIEW); 

     winWidth = width; 
     winHeight = height; 
    }// 3D mode 
} 

のみ、2Dモードで描画するときにすべてが完璧に動作しますが、私は3Dモードに切り替えるためのフラグを変更すると、ここで問題

が、私は、ウィンドウのサイズを変更するたびに来て、私が描くもの

2次元モードに戻っても、2次元モードのすべてが問題なく機能しますが、問題は3Dモードです。

また、I staフラグがfalseに設定されているプログラムでは、キューブが表示され、毎回ウィンドウのサイズを変更するにつれてサイズが小さくなります。

どうしてですか?

+0

「2D対3D」の考え方をやめてください。その区別は意味をなさない、言われるべき真実。ここで切り替えているのは投影ですが、もちろん3Dシーンに対しても正射投影を使用できます。 – datenwolf

答えて

0

あなたのglLoadIdentity()/glMatrixMode()の対話を見てください。 2Dで

は今、次の2つの異なる動作を持っているあなたは、あなたが「積み重ね」に gluOrtho2D呼び出しを引き起こし機能、おそらく GL_MODELVIEWを入力したときに有効であるものは何でものためにあなたの行列をリセットしています。

3D:投影行列を常にリセットしていますが、これはもっと正しいと思われます。

glLoadIdentityglMatrixModeの呼び出しを、最初のパス(2D)でのみスワップしてみてください。

実際に変更する前に、変更する行列を常に明示的に設定することをお勧めします。

+0

答えをありがとう。私はこれを試しましたが、今は2Dシーンをもう見ることができず、3Dシーンのキューブはまだ小さくなっています。 – RushSykes

+0

まあ、回転をどのように扱うかによって、前と同じように、モデルビューマトリックスもリセットしたいかもしれません。それは良い考えではありませんが、それはあなたがコールオーダーを交換したときに変更されたものです – ltjax

+0

ああ私はそれを得ました、私は両方のシーンのための投影モードマトリックスを使用するので、再びモデルビューに行列を設定すべきではありません.... – RushSykes

関連する問題