OpenGL第十三节:矩阵变换

LTexture.cpp

void LTexture::render( GLfloat x, GLfloat y, LFRect* clip )
{
  if( mTextureID != 0 )
  {
    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;
    }

    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 gRotatingTexture;
GLfloat gAngle = 0.f;

int gTransformationCombo = 0;

void update()
{
  gAngle += 360.f / SCREEN_FPS;

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

void render()
{
  glClear( GL_COLOR_BUFFER_BIT );

  glLoadIdentity();

  switch( gTransformationCombo )
  {
    case 0:
      glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f );
      glRotatef( gAngle, 0.f, 0.f, 1.f );
      glScalef( 2.f, 2.f, 0.f );
      glTranslatef( gRotatingTexture.imageWidth() / -2.f, gRotatingTexture.imageHeight() / -2.f, 0.f );
    break;

    case 1:
      glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f );
      glRotatef( gAngle, 0.f, 0.f, 1.f );
      glTranslatef( gRotatingTexture.imageWidth() / -2.f, gRotatingTexture.imageHeight() / -2.f, 0.f );
      glScalef( 2.f, 2.f, 0.f );
    break;

    case 2:
      glScalef( 2.f, 2.f, 0.f );
      glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f );
      glRotatef( gAngle, 0.f, 0.f, 1.f );
      glTranslatef( gRotatingTexture.imageWidth() / -2.f, gRotatingTexture.imageHeight() / -2.f, 0.f );
    break;

    case 3:
      glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f );
      glRotatef( gAngle, 0.f, 0.f, 1.f );
      glScalef( 2.f, 2.f, 0.f );
    break;

    case 4:
      glRotatef( gAngle, 0.f, 0.f, 1.f );
      glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f );
      glScalef( 2.f, 2.f, 0.f );
      glTranslatef( gRotatingTexture.imageWidth() / -2.f, gRotatingTexture.imageHeight() / -2.f, 0.f );
    break;
  }

  gRotatingTexture.render( 0.f, 0.f );

  glutSwapBuffers();
}

void handleKeys( unsigned char key, int x, int y )
{
  if( key == 'q' )
  {
    gAngle = 0.f;

    gTransformationCombo++;
    if( gTransformationCombo > 4 )
    {
      gTransformationCombo = 0;
    }
  }
}

 

posted @ 2017-11-28 17:57  yongfengnice  阅读(241)  评论(0编辑  收藏  举报