OpenGL第十一节:拉伸和过滤

LTexture.h
void render( GLfloat x, GLfloat y, LFRect* clip = NULL, LFRect* stretch = NULL );

 

LTexture.cpp

void LTexture::render( GLfloat x, GLfloat y, LFRect* clip, LFRect* stretch )
{
  if( mTextureID != 0 )
  {
    glLoadIdentity();//移除之前的变换

    GLfloat texTop = 0.f;//纹理坐标
    GLfloat texBottom = (GLfloat)mImageHeight / (GLfloat)mTextureHeight;
    GLfloat texLeft = 0.f;
    GLfloat texRight = (GLfloat)mImageWidth / (GLfloat)mTextureWidth;

    GLfloat quadWidth = mImageWidth;
    GLfloat quadHeight = mImageHeight;

    if( clip != NULL )//如果裁剪
    {
      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;
    }

    if( stretch != NULL )//如果拉伸
    {
      quadWidth = stretch->w;
      quadHeight = stretch->h;
    }

    glTranslatef( x, y, 0.f );//移动

    glBindTexture( GL_TEXTURE_2D, mTextureID );//绑定

    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 gStretchedTexture;
LFRect gStretchRect = { 0.f, 0.f, SCREEN_WIDTH, SCREEN_HEIGHT };
GLenum gFiltering = GL_LINEAR;//纹理过滤

bool loadMedia()
{
  if( !gStretchedTexture.loadTextureFromFile( "mini_opengl.png" ) )
  {
    printf( "Unable to load mini texture!\n" );
    return false;
  }

  return true;
}

void render()
{
  glClear( GL_COLOR_BUFFER_BIT );

  gStretchedTexture.render( 0.f, 0.f, NULL, &gStretchRect );

  glutSwapBuffers();
}

void handleKeys( unsigned char key, int x, int y )
{
  if( key == 'q' )
  {
    glBindTexture( GL_TEXTURE_2D, gStretchedTexture.getTextureID() );//绑定,然后修改

    if( gFiltering != GL_LINEAR )
    {
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );//线性拉伸
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );

      gFiltering = GL_LINEAR;
    }
    else
    {
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );//附近拉伸
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );

      gFiltering = GL_NEAREST;
    }

    glBindTexture( GL_TEXTURE_2D, NULL );//解除绑定
    }
}

 

转载于:https://www.cnblogs.com/yongfengnice/p/7910453.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值