OpenGL ES 1.1 图像从中心旋转

发布于 2024-09-28 15:33:05 字数 1594 浏览 3 评论 0原文

我正在尝试使用 OGL ES 旋转 2D 图像。加载后,我可以将其移动到屏幕上,但是当尝试通过其中心旋转图像时,它有一个奇怪的行为,因为旋转中心是屏幕的左下角,而不是图像本身的中心。

谷歌搜索我读到我可以推送当前矩阵,更改我需要的任何内容(翻译坐标,旋转图像等),然后弹出矩阵回到之前的矩阵状态......我做到了,但仍然没有按照我正在寻找的方式工作(但至少现在看来,旋转的原始坐标不是左下角......)

有什么想法吗?有人能发现我的问题出在哪里吗?

任何帮助将不胜感激!谢谢!!

void drawImage(Image *img)
{

   GLfloat fX = (GLfloat)img->x;

   GLfloat fY = (GLfloat)(flipY(img->m_height+img->y));

   GLfloat   coordinates[] = { 0, img->m_textureHeight, img->m_textureWidth, img->m_textureHeight, 0, 0, img->m_textureWidth, 0 };
   GLfloat      vertices[] = 
   { 
      fX, fY, 0.0,
      img->m_width+fX, fY, 0.0,
      fX, img->m_height+fY, 0.0,
      img->m_width+fX, img->m_height+fY, 0.0 
   };   

   //Push and change de matrix, translate coords, rotate and scale image and then pop the matrix
   glPushMatrix(); //push texture matrix
   glTranslatef((int)fX, (int)fY, 0.0);  //translate texture matrix

   // rotate
   if (img->rotation != 0.0f )
      glRotatef( -img->rotation, 0.0f, 0.0f, 1.0f );

   // scale
   if (img->scaleX != 1.0f || img->scaleY != 1.0f)
      glScalef( img->scaleX, img->scaleY, 1.0f );

   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

   glEnableClientState(GL_TEXTURE_COORD_ARRAY);

   glColor4f(1.0, 0.0, 0.0, 1.0);
   glBindTexture(GL_TEXTURE_2D, img->m_name);
   glVertexPointer(3, GL_FLOAT, 0, vertices);
   glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

   glDisableClientState(GL_TEXTURE_COORD_ARRAY);

   glPopMatrix();
}

I'm trying to rotate a 2D image using OGL ES. After load it I can move it through the screen but when trying to rotate the image through its center, it has an odd behavior as the rotation center is the lower-left screen corner, not the center of the image itself.

Googling around I've read that I could push the current matrix, change whatever I need (translate the coords, rotate the image, etc) and then pop the matrix coming back to the previous matrix status... I did it but still not working as I'm looking for (but at least now seems that the original coords where it does the rotation are not the lower-left corner...)

Any thoughts? Anyone could spot where my problem is?

Any help would be much appreciated! Thanks!!

void drawImage(Image *img)
{

   GLfloat fX = (GLfloat)img->x;

   GLfloat fY = (GLfloat)(flipY(img->m_height+img->y));

   GLfloat   coordinates[] = { 0, img->m_textureHeight, img->m_textureWidth, img->m_textureHeight, 0, 0, img->m_textureWidth, 0 };
   GLfloat      vertices[] = 
   { 
      fX, fY, 0.0,
      img->m_width+fX, fY, 0.0,
      fX, img->m_height+fY, 0.0,
      img->m_width+fX, img->m_height+fY, 0.0 
   };   

   //Push and change de matrix, translate coords, rotate and scale image and then pop the matrix
   glPushMatrix(); //push texture matrix
   glTranslatef((int)fX, (int)fY, 0.0);  //translate texture matrix

   // rotate
   if (img->rotation != 0.0f )
      glRotatef( -img->rotation, 0.0f, 0.0f, 1.0f );

   // scale
   if (img->scaleX != 1.0f || img->scaleY != 1.0f)
      glScalef( img->scaleX, img->scaleY, 1.0f );

   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 

   glEnableClientState(GL_TEXTURE_COORD_ARRAY);

   glColor4f(1.0, 0.0, 0.0, 1.0);
   glBindTexture(GL_TEXTURE_2D, img->m_name);
   glVertexPointer(3, GL_FLOAT, 0, vertices);
   glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

   glDisableClientState(GL_TEXTURE_COORD_ARRAY);

   glPopMatrix();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦浮鱼 2024-10-05 15:33:05

最重要的是,您需要了解如何执行此操作。

在进行旋转之前,您必须将自身平移到旋转原点,然后才能应用于旋转。

查看这篇文章很好地解释了这一点。

简单的细分是:

  • 将对象移动到原点。
  • 旋转。
  • 向后移动对象。

Most importantly, you need to understand how to do this operation.

before doing a rotation you have to translate your self in the rotation origin and only then apply to rotation.

Check out this article which explains it well.

The simple breakdown is:

  • move object to origin.
  • Rotate.
  • Move object back.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文