zoukankan      html  css  js  c++  java
  • 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;
        }
      }
    }

  • 相关阅读:
    google的几道面试题
    轮盘赌算法
    基于packstack的openstack单节点安装
    攻克python3-字典(第四篇)
    攻克python3-列表与元组(第三篇)
    攻克python3-字符串(第二篇)
    攻克python3(第一篇)
    二维数组
    小白对c语言指针的基础总结
    小白对c语言数组的基础总结
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/7911281.html
Copyright © 2011-2022 走看看