OpenGL第十二节:旋转

LTexture.cpp

void LTexture::render( GLfloat x, GLfloat y, LFRect* clip, LFRect* stretch, GLfloat degrees )
{
  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 + quadWidth / 2.f, y + quadHeight / 2.f, 0.f );//移到绘制区域中心点

    glRotatef( degrees, 0.f, 0.f, 1.f );//旋转

    glBindTexture( GL_TEXTURE_2D, mTextureID );

    glBegin( GL_QUADS );
      glTexCoord2f( texLeft, texTop ); glVertex2f( -quadWidth / 2.f, -quadHeight / 2.f );
      glTexCoord2f( texRight, texTop ); glVertex2f( quadWidth / 2.f, -quadHeight / 2.f );
      glTexCoord2f( texRight, texBottom ); glVertex2f( quadWidth / 2.f, quadHeight / 2.f );
      glTexCoord2f( texLeft, texBottom ); glVertex2f( -quadWidth / 2.f, quadHeight / 2.f );
    glEnd();
  }
}

 

LUtil.cpp

void update()
{
  gAngle += 360.f / SCREEN_FPS;//旋转角度

  if( gAngle > 360.f )
  {
    gAngle -= 360.f;
  }
}

void render()
{
  glClear( GL_COLOR_BUFFER_BIT );

  gRotatingTexture.render( ( SCREEN_WIDTH - gRotatingTexture.imageWidth() ) / 2.f, ( SCREEN_HEIGHT - gRotatingTexture.imageHeight() ) / 2.f, NULL, NULL, gAngle );

  glutSwapBuffers();
}

posted @ 2017-11-28 16:22  yongfengnice  阅读(178)  评论(0编辑  收藏  举报