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

  • 相关阅读:
    Cordova各个插件使用介绍系列(四)—canvas2ImagePlugin保存二维码到手机本地
    如何实现一个简单的MVVM框架
    2015年总结
    基于setTimeout制作滚动广告板
    (Frontend Newbie)JavaScript基础之函数
    (Frontend Newbie)JavaScript基础之常见数据类型
    (Frontend Newbie)Web三要素(三)
    (Frontend Newbie)Web三要素(二)
    (Frontend Newbie) Web三要素(一)
    (Frontend Newbie)Web简史
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/7910638.html
Copyright © 2020-2023  润新知