當OpenGL窗口的大小發生改變時,OpenGL窗口會變形需要重新設定以適應新的窗口大小.
- 收到WM_SIZE消息後獲取窗口大小,高度為HIWORD(lParam),寬度為LOWORD(lparam)
- 重置視區尺寸glViewport(0,0,width,height);
- 設定投影矩陣glMatrixMode(GL_PROJECTION);
- 載入單位矩陣glLoadIdentity();
- 計算窗口尺寸比例gluPerspective();
- 設定模型視圖矩陣glMatrixMode(GL_MODELVIEW);
- 載入單位矩陣glLoadIdentity();
給出重設窗口大小完整代碼
void Set_WindowSize_OpenGL(int width,int height)
{
if(height == 0)
height = 1;// 确保分母不为0
// 重置视区尺寸
::glViewport(0,0,width,height);
// 设定投影矩阵
::glMatrixMode(GL_PROJECTION);
// 载入单位矩阵
::glLoadIdentity();
// 计算窗口尺寸比例
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);
// 设定模型视图矩阵
::glMatrixMode(GL_MODELVIEW);
// 载入单位矩阵
::glLoadIdentity();
}