OpenGL旋轉 (OpenGL Rotation)


問題描述

OpenGL 旋轉 (OpenGL Rotation)

我正在嘗試在 OpenGL 中進行簡單的旋轉,但一定沒有抓住重點。我不是在尋找一個具體的修復,而是一個更一般地解釋 OpenGL 旋轉的快速解釋或鏈接。

目前我有這樣的代碼:

glPushMatrix();
  glRotatef(90.0, 0.0, 1.0, 0.0);
  glBegin(GL_TRIANGLES);        
    glVertex3f( 1.0, 1.0, 0.0 );        
    glVertex3f( 3.0, 2.0, 0.0 );        
    glVertex3f( 3.0, 1.0, 0.0 );        
  glEnd();
glPopMatrix();

但是結果不是一個旋轉 90 度的三角形。

編輯 嗯,感謝 Mike Haboustak ‑ 看來我的代碼正在調用使用 glOrtho 的 SetCamera 函數。我對 OpenGL 太陌生了,不知道這意味著什麼,但是禁用它並在 Z 軸上旋轉會產生所需的結果。


參考解法

方法 1:

Ensure that you're modifying the modelview matrix by putting the following before the glRotatef call:

glMatrixMode(GL_MODELVIEW);

Otherwise, you may be modifying either the projection or a texture matrix instead.

方法 2:

Do you get a 1 unit straight line? It seems that 90deg rot. around Y is going to have you looking at the side of a triangle with no depth.

You should try rotating around the Z axis instead and see if you get something that makes more sense.

OpenGL has two matrices related to the display of geometry, the ModelView and the Projection. Both are applied to coordinates before the data becomes visible on the screen. First the ModelView matrix is applied, transforming the data from model space into view space. Then the Projection matrix is applied with transforms the data from view space for "projection" on your 2D monitor.

ModelView is used to position multiple objects to their locations in the "world", Projection is used to position the objects onto the screen.

Your code seems fine, so I assume from reading the documentation you know what the nature of functions like glPushMatrix() is. If rotating around Z still doesn't make sense, verify that you're editing the ModelView matrix by calling glMatrixMode.

方法 3:

The "accepted answer" is not fully correct ‑ rotating around the Z will not help you see this triangle unless you've done some strange things prior to this code. Removing a glOrtho(...) call might have corrected the problem in this case, but you still have a couple of other issues.

Two major problems with the code as written:

  • Have you positioned the camera previously? In OpenGL, the camera is located at the origin, looking down the Z axis, with positive Y as up. In this case, the triangle is being drawn in the same plane as your eye, but up and to the right. Unless you have a very strange projection matrix, you won't see it. gluLookat() is the easiest command to do this, but any command that moves the current matrix (which should be MODELVIEW) can be made to work.

  • You are drawing the triangle in a left handed, or clockwise method, whereas the default for OpenGL is a right handed, or counterclockwise coordinate system. This means that, if you are culling backfaces (which you are probably not, but will likely move onto as you get more advanced), you would not see the triangle as expected. To see the problem, put your right hand in front of your face and, imagining it is in the X‑Y plane, move your fingers in the order you draw the vertices (1,1) to (3,2) to (3,1). When you do this, your thumb is facing away from your face, meaning you are looking at the back side of the triangle. You need to get into the habit of drawing faces in a right handed method, since that is the common way it is done in OpenGL.

The best thing I can recommend is to use the NeHe tutorials ‑ http://nehe.gamedev.net/. They begin by showing you how to set up OpenGL in several systems, move onto drawing triangles, and continue slowly and surely to more advanced topics. They are very easy to follow.

方法 4:

When I had a first look at OpenGL, the NeHe tutorials (see the left menu) were invaluable.

方法 5:

I'd like to recommend a book:

3D Computer Graphics : A Mathematical Introduction with OpenGL by Samuel R. Buss

It provides very clear explanations, and the mathematics are widely applicable to non‑graphics domains. You'll also find a thorough description of orthographic projections vs. perspective transformations.

(by Matt MitchellspateMike HaboustakPerryAntAdam Hollidge)

參考文件

  1. OpenGL Rotation (CC BY‑SA 2.5/3.0/4.0)

#opengl #glut #C++






相關問題

為什麼我們需要維護自己的矩陣來轉換遊戲對象? (Why we need to maintain our own matrices to transform Game objects?)

如何從我的 Windows 機器運行安裝在 linux 機器上的 OpenGL 應用程序? (How can I run an OpenGL application installed on a linux machine from my windows machine?)

OpenGL等圖形庫的目的是什麼? (What is the purpose of a graphics library such as OpenGL?)

如何在 OpenGL/C++ 中繪製居中的字符串? (How can I draw a centered string in OpenGL/C++?)

Vẽ bản đồ nhỏ (trò chơi) (Drawing a minimap (game))

為什麼OpenGL不重載它的函數? (Why OpenGL does not overload its functions?)

DX11 OpenGL 互操作:無法複製緩衝區 (DX11 OpenGL interop: can't copy buffer)

GLSL 著色器中的翻譯 (translation in GLSL shader)

停靠欄下的窗口化opengl遊戲中的雙光標 (Double cursor in windowed opengl game under dock)

GLSL / HLSL 著色器中的星球大戰全息效果 (Star Wars holographic effect in GLSL / HLSL shader)

在 GLSL 中混合多個紋理 (Blending multiple textures in GLSL)

在winform應用程序中關閉表單時如何修復“System.ObjectDisposedException” (How to fix "System.ObjectDisposedException" when closing a form in winform application)







留言討論