OpenGL第十三节:矩阵变换

Posted yongfengnice

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
  }
}

 

以上是关于OpenGL第十三节:矩阵变换的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL中的坐标变换矩阵变换

浅谈OpenGL变换矩阵

OpenGL 矩阵变换

affine transformation matrix 仿射变换矩阵 与 OpenGL

OpenGL平移变换矩阵

OPENGL之矩阵