2012-10-24 12 views
7

元の質問

私は私のカメラとスクリーンを操作するための 'gluPerspectiveなので'、 'glViewport' と 'gluLookAt' を使用したい

gluPerspectiveで、glViewport、gluLookAtとGL_PROJECTIONとGL_MODELVIEW Matricies

どのマトリックスモードに適用するのですか?そして、私はそれらをどのような順番で使うべきですか?ただ、(しかし、それが機能していません!)私は、オンラインドキュメントのために周りを見回している

glMatrixMode(GL_PROJECTION) // Apply following to projection matrix - is this correct? 
glLoadIdentity(); // Reset first 
glPerspective(45.0, (double)w/(double)h, 1.0, 200.0); // Set perspective 
glViewport(0, 0, w, h); // Set viewport 

glMatrixMode(GL_MODELVIEW); // Apply following to modelview - should glViewport come under here? 
glLoadIdentity(); // Reset first 
gluLookAt(px, py, pz, cx, cy, cz, ux, uy, uz); // Set position, centre and then up vectors 
// This surely comes after calling GL_MODELVIEW? 

、と私は機能を理解:たとえば

は、私はこのように私の画面とカメラをアップ設定しようとしていますどこに行くべきか、どんな順番で行くべきではありません!

しばらく...

それは数ヶ月後に、今だと私は、私は、OpenGLで物事をレンダリングするために使用するシステムを表示する簡単な編集を追加してい。これは将来この質問を見る他の人を助けるためです。

私は主に2つの方法を使用します。

方法1:一緒に

このメソッドグループのすべて。

// Firstly, the window may have been resized so re-create the viewing area 
glViewport(0, 0, width_of_window_rendering_area, height_of_window_rendering area); 

これは、ウィンドウの内側の領域全体にレンダリングするためのビューポートを再作成します。 SFMLでは、あなたは...あなたは(供給過剰、GLFW、SDLなど)を使用しているウィンドウのツールキットに依存

// The second step is to add a projection matrix. There are three main ones I like to use 
// Uncomment the one you want 
glMatrixMode(GL_PROJECTION); // Tell OpenGL to manipulate the correct matrix stack 
glLoadIdentity(); // Reset the projection matrix, or bad things happen after multiple calls to below functions! 
// glOrtho(...) // Uncomment to use 2D rendering 
// gluPerspective(...) // Uncomment to use (easy) 3D rendering 
glFrustrum(...) // Uncomment to use (harder/less intuitive?) 3D rendering 
glMatrixMode(GL_MODELVIEW); // I always prefer to leave OpenGL in the modelview manipulation mode. 
    // I consider it the "default" and safest mode to leave OpenGL in, as any rogue 
    // calls to functions changing its contents is likely to mess up your geometry 
    // which should be visible as a problem on the screen, which tells you you need 
    // to fix something! Manipulating the other matrix stacks may not show obvious 
    // problems. 

をwindow.widthのようなもの()またはwindow.height()、または類似した何かをするだろうあなたは意志'glOrtho'、 'gluPerspective'、 'glFrustrum'のうち3つのうちの1つを選択する必要があります。 「gluOrtho2D」もありますが、注意して使用してください! (私はnearとfarのプレーンを 'glOrtho'で自分自身で指定する方が好きです。)あなたは '...'を関数の引数に置き換える必要があります。

// The third step is to clear the screen and set your camera/geometry position 
glClear(GL_COLOR_BUFFER_BIT); // use bitwise OR ('||') with 'GL_DEPTH_BUFFER_BIT' and 'GL_STENCIL_BUFFER_BIT' if required 
gluLookAt(...); 
// I like to use gluLookAt, or at least I _italic_used to! Now I implement my own camera... 
// That is another fun thing you should try if you are comfortable with 3D geometry and hard math! 

// The fourth step is to draw your scene. You may want to put lighting stuff first or in with the drawing 
glutWireTeapot(...); 

方法2:

第二の方法は、上記と同様であるが、別の関数に最初のステップを移動させます。次に、ウィンドウのサイズ変更イベントを検出し、この関数を呼び出す必要があります。 glutを使うと、コールバックを指定することができます。 SFMLを使用すると、ウィンドウのサイズが変更されたときにイベントを検出できます。私はSDLの仕組みを忘れていますが、それは似ています。私はまだglfwの仕組みを学んでいない。

うまくいけば助けになると思います。

一部のOpenGL初心者(おそらく自分自身が一度に含まれています)は、投影行列上でカメラ翻訳を指定しようとします。 Do not do this - 私はそれが照明とおそらく他のものを混乱させてしまう。一度初めに

コールglViewport(...)

+0

あなたの所在地は私にとって適切です。おそらくあなたのエラーは他の場所にあります。 – Tim

+0

シーンがglPerspectiveで指定したニアプレーンとファープレーンの間にあることを確認してください。 – Max

+0

関数を理解していれば、どこに行くのか分かります。たとえば、 'glViewport'が行列を作成すると考えられます。それはしません。 –

答えて

3

は、私は私のようリシェイプコールバック関数を定義します。

そして、単位行列と射影行列をリロードします。次に

glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 

:あなたのカメラの位置を変更する必要がある場合

glPerspective(...) 
glMatrixMode(GL_MODELVIEW); 

gluLookAt(...)は後でいつでも呼び出すことができます。

私の単純な目的のために働きます。

+0

ありがとう、これは私が探していた答えです。 gluLookAtを2回呼び出すと、カメラが再び移動しますか?同様に、glRotatefを2回呼び出すとカメラ/シーンが2回回転しますが、gluLookAtは同じことをしますか? – user3728501

+0

申し訳ありません、もう一つの質問:glViewPortは行列を編集しますか? glViewPortへの呼び出しの前にglMatrixMode(GL_PROJECTION)を呼び出す必要がありますか? – user3728501

+0

はい、シーン全体を2回回転します。しかし、それを表示コールバック関数に入れて、表示関数が繰り返し呼び出されることを覚えている場合は、ディスプレイ・コールバック関数の先頭にモデル行列を単位行列でロードする必要があります。 – Max

関連する問題