实现图片满屏显示,最常用的用途是把一张图片作为显示窗体的背景图片。我这里有总两种方法:
绘制背景图片时必须关闭深度测试,否则会导致后面渲染的3d场景不能显示。
在绘制3d场景时开启深度测试是为了保证3D模型绘制的正确性,主要指彼此的遮挡关系。
示例代码:
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3f (1.0, 1.0, 1.0);
glDisable(GL_DEPTH_TEST);
// 切换到正交模式
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, g_fWidth, 0, g_fHeight, 0, g_fDepth);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, g_textureID );
glInterleavedArrays( GL_T2F_V3F, 0, g_bgVertices );
glDrawArrays( GL_QUADS, 0, 4 ); // 绘制背景图片
glEnable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
// 切换到三维场景
glMatrixMode (GL_PROJECTION); //回复原有的设置
glLoadIdentity ();
gluPerspective(30, g_fWidth/g_fHeight, 0.001, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
gluLookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
{
glTranslatef(0, 0, 0);
glRotatef(g_xAngle, 1, 0, 0);
glRotatef(g_yAngle, 0, 1, 0);
glCallList(g_modelId); // 绘制三维图形
} glPopMatrix();
glFlush();
// glutPostRedisplay();
}
void display(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor3f (1.0, 1.0, 1.0);
ComputeBackgroundQuad();
glDisable(GL_DEPTH_TEST);
// 绘制背景图片
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, g_textureID );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glInterleavedArrays( GL_T2F_V3F, 0, g_bgVertices );
glDrawArrays( GL_QUADS, 0, 4 );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
glDisable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
{
glTranslatef(0, 0, g_fOffset);
glRotatef(g_xAngle, 1, 0, 0);
glRotatef(g_yAngle, 0, 1, 0);
glCallList(g_modelId);
} glPopMatrix();
glFlush();
// glutPostRedisplay();
}
void ComputeBackgroundQuad()
{
GLint viewport[4];
GLdouble projMatrix[16];
GLdouble modelMatrix[16];
// glGetDoublev(GL_PROJECTION, projMatrix); 缺少matrix但是编译不报错~
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
//
double objX, objY, objZ;
const double winZ = 0; // 0 / 1
gluUnProject(0, 0, winZ, modelMatrix, projMatrix, viewport, &objX, &objY, &objZ);
g_bgVertices[0].x = objX;
g_bgVertices[0].y = objY;
g_bgVertices[0].z = objZ;
gluUnProject(g_fWidth, 0, winZ, modelMatrix, projMatrix, viewport, &objX, &objY, &objZ);
g_bgVertices[1].x = objX;
g_bgVertices[1].y = objY;
g_bgVertices[1].z = objZ;
gluUnProject(g_fWidth, g_fHeight, winZ, modelMatrix, projMatrix, viewport, &objX, &objY, &objZ);
g_bgVertices[2].x = objX;
g_bgVertices[2].y = objY;
g_bgVertices[2].z = objZ;
gluUnProject(0, g_fHeight, winZ, modelMatrix, projMatrix, viewport, &objX, &objY, &objZ);
g_bgVertices[3].x = objX;
g_bgVertices[3].y = objY;
g_bgVertices[3].z = objZ;
}
最终的效果如图: