LFRect.h//定义一个结构体,表示裁剪的区域

#ifndef LFRECT_H
#define LFRECT_H
#include "LOpenGL.h"
struct LFRect
{
  GLfloat x;//x
  GLfloat y;//y
  GLfloat w;//宽
  GLfloat h;//高
};
#endif

LTexture.h

void render( GLfloat x, GLfloat y, LFRect* clip = NULL );//渲染方法添加第三个参数,给它一个NULL的默认值

LTexture.cpp

void LTexture::render( GLfloat x, GLfloat y, LFRect* clip )
{
  if( mTextureID != 0 )
  {
  glLoadIdentity();//重置之前的所以变换操作
  GLfloat texTop = 0.f;//下面四句是纹理的坐标,水平方向0~1表示从左到右,竖直方向0~1表示从上到下
  GLfloat texBottom = 1.f;
  GLfloat texLeft = 0.f;
  GLfloat texRight = 1.f;
  GLfloat quadWidth = mTextureWidth;//四边形的宽
  GLfloat quadHeight = mTextureHeight;//高

  if( clip != NULL )
  {
    //纠正纹理坐标,纹理坐标取值0~1的,需要转为对应的比例值
    texLeft = clip->x / mTextureWidth;
    texRight = ( clip->x + clip->w ) / mTextureWidth;
    texTop = clip->y / mTextureHeight;
    texBottom = ( clip->y + clip->h ) / mTextureHeight;
    //纠正四边形的宽高 
    quadWidth = clip->w;
    quadHeight = clip->h;
  }
  glTranslatef( x, y, 0.f );//移动
  glBindTexture( GL_TEXTURE_2D, mTextureID );//绑定纹理ID
  glBegin( GL_QUADS );//绘制
    glTexCoord2f( texLeft, texTop ); glVertex2f( 0.f, 0.f );
    glTexCoord2f( texRight, texTop ); glVertex2f( quadWidth, 0.f );
    glTexCoord2f( texRight, texBottom ); glVertex2f( quadWidth, quadHeight );
    glTexCoord2f( texLeft, texBottom ); glVertex2f( 0.f, quadHeight );
  glEnd();
  }
}

LUtil.cpp


LTexture gArrowTexture;
LFRect gArrowClips[ 4 ];//定义四个裁剪区域,打算裁剪四个角
bool loadMedia()
{
  gArrowClips[ 0 ].x = 0.f;//左上角
  gArrowClips[ 0 ].y = 0.f;
  gArrowClips[ 0 ].w = 128.f;
  gArrowClips[ 0 ].h = 128.f;
  gArrowClips[ 1 ].x = 128.f;//右上角
  gArrowClips[ 1 ].y = 0.f;
  gArrowClips[ 1 ].w = 128.f;
  gArrowClips[ 1 ].h = 128.f;
  gArrowClips[ 2 ].x = 0.f;//左下角
  gArrowClips[ 2 ].y = 128.f;
  gArrowClips[ 2 ].w = 128.f;
  gArrowClips[ 2 ].h = 128.f;
  gArrowClips[ 3 ].x = 128.f;//右下角
  gArrowClips[ 3 ].y = 128.f;
  gArrowClips[ 3 ].w = 128.f;
  gArrowClips[ 3 ].h = 128.f;
  if( !gArrowTexture.loadTextureFromFile( "arrows.png" ) )
  {
    printf( "Unable to load arrow texture!\n" );
    return false;
  }
  return true;
}
void render()
{
  glClear( GL_COLOR_BUFFER_BIT );
  gArrowTexture.render( 0.f, 0.f, &gArrowClips[ 0 ] );//绘制左上角
  gArrowTexture.render( SCREEN_WIDTH - gArrowClips[ 1 ].w, 0.f, &gArrowClips[ 1 ] );//绘制右上角
  gArrowTexture.render( 0.f, SCREEN_HEIGHT - gArrowClips[ 2 ].h, &gArrowClips[ 2 ] );//绘制左下角
  gArrowTexture.render( SCREEN_WIDTH - gArrowClips[ 3 ].w, SCREEN_HEIGHT - gArrowClips[ 3 ].h, &gArrowClips[ 3 ] );//绘制右下角
  glutSwapBuffers();//更新屏幕
}