• 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;
        }
      }
    }

  • 相关阅读:
    一月5日
    一月5日
    面试java工程师的自我介绍(模拟篇)
    Spring Boot Jpa 介绍
    spring+redis做缓存使用
    Spring Boot Web 开发详解
    thymeleaf+spring的简单搭建
    如何搭建 Spring boot
    通用baseDao
    pagehelper的使用
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/7911281.html
Copyright © 2020-2023  润新知