OpenGL下实现图片满屏显示
转自:http://blog.csdn.net/dizuo/article/details/7596242
实现图片满屏显示,最常用的用途是把一张图片作为显示窗体的背景图片。我这里有总两种方法:
一、 正交模式显示背景图片
- 关闭深度测试
- 正交投影
- 设置四边形的尺寸为窗口四个角,将图片贴到Quad上
- 切换到透视模式下
- 开启深度测试
- 显示三维场景
绘制背景图片时必须关闭深度测试,否则会导致后面渲染的3d场景不能显示。
在绘制3d场景时开启深度测试是为了保证3D模型绘制的正确性,主要指彼此的遮挡关系。
示例代码:
1 void display(void) 2 { 3 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 4 glColor3f (1.0, 1.0, 1.0); 5 6 glDisable(GL_DEPTH_TEST); 7 8 // 切换到正交模式 9 glMatrixMode(GL_PROJECTION); 10 glLoadIdentity(); 11 glOrtho(0, g_fWidth, 0, g_fHeight, 0, g_fDepth); 12 glMatrixMode(GL_MODELVIEW); 13 glLoadIdentity(); 14 15 glDisable(GL_LIGHTING); 16 glEnable(GL_TEXTURE_2D); 17 glBindTexture( GL_TEXTURE_2D, g_textureID ); 18 glInterleavedArrays( GL_T2F_V3F, 0, g_bgVertices ); 19 glDrawArrays( GL_QUADS, 0, 4 ); // 绘制背景图片 20 glEnable(GL_LIGHTING); 21 glDisable(GL_TEXTURE_2D); 22 23 // 切换到三维场景 24 glMatrixMode (GL_PROJECTION); //回复原有的设置 25 glLoadIdentity (); 26 gluPerspective(30, g_fWidth/g_fHeight, 0.001, 100.0); 27 glMatrixMode(GL_MODELVIEW); 28 glLoadIdentity (); 29 gluLookAt(0, 5, 5, 0, 0, 0, 0, 1, 0); 30 31 glEnable(GL_DEPTH_TEST); 32 33 glPushMatrix(); 34 { 35 glTranslatef(0, 0, 0); 36 37 glRotatef(g_xAngle, 1, 0, 0); 38 glRotatef(g_yAngle, 0, 1, 0); 39 40 glCallList(g_modelId); // 绘制三维图形 41 } glPopMatrix(); 42 43 glFlush(); 44 // glutPostRedisplay(); 45 }
二、 透视模式显示背景图片
背景图片和3d场景都在透视模式下显示,此时为了精确实现图片满屏,需要将窗口四个角的坐标投影到3d场景中,计算出对应的四边形四个角坐标,然后图片贴上去。步骤如下:
- 窗口四角 投影到3d场景中,使用gluUnProject函数 计算出新的quad
- 关闭深度测试
- 绘制quad,贴图
- 开启深度测试
- 绘制3d场景
示例代码:
1 void display(void) 2 { 3 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 4 glColor3f (1.0, 1.0, 1.0); 5 6 ComputeBackgroundQuad(); 7 8 glDisable(GL_DEPTH_TEST); 9 10 // 绘制背景图片 11 glDisable(GL_LIGHTING); 12 glEnable(GL_TEXTURE_2D); 13 glBindTexture( GL_TEXTURE_2D, g_textureID ); 14 15 glEnableClientState( GL_VERTEX_ARRAY ); 16 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 17 glInterleavedArrays( GL_T2F_V3F, 0, g_bgVertices ); 18 glDrawArrays( GL_QUADS, 0, 4 ); 19 glDisableClientState( GL_VERTEX_ARRAY ); 20 glDisableClientState( GL_TEXTURE_COORD_ARRAY ); 21 22 glDisable(GL_TEXTURE_2D); 23 24 glEnable(GL_LIGHTING); 25 26 glEnable(GL_DEPTH_TEST); 27 28 glPushMatrix(); 29 { 30 glTranslatef(0, 0, g_fOffset); 31 32 glRotatef(g_xAngle, 1, 0, 0); 33 glRotatef(g_yAngle, 0, 1, 0); 34 35 glCallList(g_modelId); 36 } glPopMatrix(); 37 38 glFlush(); 39 // glutPostRedisplay(); 40 }
1 void ComputeBackgroundQuad() 2 { 3 GLint viewport[4]; 4 GLdouble projMatrix[16]; 5 GLdouble modelMatrix[16]; 6 7 // glGetDoublev(GL_PROJECTION, projMatrix); 缺少matrix但是编译不报错~ 8 glGetDoublev(GL_PROJECTION_MATRIX, projMatrix); 9 glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix); 10 glGetIntegerv(GL_VIEWPORT, viewport); 11 12 // 13 double objX, objY, objZ; 14 const double winZ = 0; // 0 / 1 15 gluUnProject(0, 0, winZ, modelMatrix, projMatrix, viewport, &objX, &objY, &objZ); 16 g_bgVertices[0].x = objX; 17 g_bgVertices[0].y = objY; 18 g_bgVertices[0].z = objZ; 19 20 gluUnProject(g_fWidth, 0, winZ, modelMatrix, projMatrix, viewport, &objX, &objY, &objZ); 21 g_bgVertices[1].x = objX; 22 g_bgVertices[1].y = objY; 23 g_bgVertices[1].z = objZ; 24 25 gluUnProject(g_fWidth, g_fHeight, winZ, modelMatrix, projMatrix, viewport, &objX, &objY, &objZ); 26 g_bgVertices[2].x = objX; 27 g_bgVertices[2].y = objY; 28 g_bgVertices[2].z = objZ; 29 30 gluUnProject(0, g_fHeight, winZ, modelMatrix, projMatrix, viewport, &objX, &objY, &objZ); 31 g_bgVertices[3].x = objX; 32 g_bgVertices[3].y = objY; 33 g_bgVertices[3].z = objZ; 34 }
最终的效果如图: