OpenGL 旋转 2D 纹理 [英] OpenGL rotating a 2D texture

查看:42
本文介绍了OpenGL 旋转 2D 纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

见底部更新.

我在互联网上看了很多遍,我找到了一些教程来解释我想要实现的目标,但我无法让它工作,要么教程不完整,要么不适用于我的代码.

I've been looking alot around the internet and I have found a few tutorials that explain what I'm trying to achieve but I can't get it to work, either the tutorial is incomplete or not applicable on my code.

我正在尝试一些简单的方法,例如围绕原点(中心)旋转 2D 图像.

I'm trying something as simple as rotating a 2D image around its origin (center).

我使用 xStart、xEnd、yStart 和 yEnd 来翻转 0 或 1 的纹理.

I use xStart, xEnd, yStart and yEnd to flip the texture which are either 0 or 1.

这是代码的样子

GameRectangle dest = destination;
Vector2 position = dest.getPosition();

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, this->image);

//If the rotation isn't 0 we'll rotate it
if (rotation != 0)
{
    glMatrixMode(GL_TEXTURE);

    glLoadIdentity();
    glTranslatef(0.5, 0.5, 0);
    glRotatef(rotation, 0, 0, 1);

    glMatrixMode(GL_PROJECTION);
}

glBegin(GL_QUADS);
glTexCoord2d(xStart,yStart);
glVertex2f(position.x, position.y);

glTexCoord2d(xEnd,yStart);
glVertex2f(position.x + this->bounds.getWidth(), position.y);

glTexCoord2d(xEnd,yEnd); 
glVertex2f(position.x + this->bounds.getWidth(), position.y + this->bounds.getHeight());

glTexCoord2d(xStart,yEnd);
glVertex2f(position.x, position.y + this->bounds.getHeight());

glEnd();

glDisable(GL_TEXTURE_2D);

//Reset the rotation so next object won't be rotated
glMatrixMode(GL_TEXTURE);

glLoadIdentity();
glRotatef(0, 0, 0, 1);

glMatrixMode(GL_PROJECTION);

此代码将以原始大小绘制图像并旋转它,但它会从左上角旋转它,从而对图像进行了大量裁剪.通过调用 GameRectangle.getOrigin() 我可以很容易地得到矩形的中心,但我不知道在哪里使用它.

This code will draw the image in it's original size and it will rotate it, but it will rotate it from the top left corner which crops the image a lot. By calling GameRectangle.getOrigin() I can easily get the center of the rectangle, but I don't know where to use it.

位如果放置:

 glTranslatef(-0.5, -0.5, 0);

在我打电话之后:

 glRotatef(0.5, 0.5, 0);

它会从中心旋转,但如果不是完美的 90 度旋转,它会拉伸图像.

It will rotate from the center, but it will strech the image if it's not a perfect 90 degrees rotation.

更新

在尝试了几乎所有可能的方法之后,我得到了想要的结果.

After trying pretty much everything possible, I got the result I was looking for.

但我不确定这是否是最好的方法.如果我的代码有问题,请告诉我.

But I'm not sure if this is the best approach. Please tell me if there's something wrong with my code.

正如我在上面的评论中提到的,我多次使用相同的图像并使用不同的值绘制它,所以我无法将任何内容保存到实际图像中.所以每次渲染后我都必须重置这些值.

As I mentioned in a comment above, I use the same image multiple times and draw it with different values, so I can't save anything to the actual image. So I must reset the values everytime after I have rendered it.

我将代码更改为:

//Store the position temporary
GameRectangle dest = destination;
Vector2 position = dest.getPosition();

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, this->image);

glTranslatef(dest.getOrigin().x, dest.getOrigin().y, 0);
glRotatef(rotation, 0, 0, 1);

glBegin(GL_QUADS);
glTexCoord2d(xStart,yStart);
glVertex2f(-dest.getWidth()/2, -dest.getHeight()/2);

glTexCoord2d(xEnd,yStart);
glVertex2f(dest.getWidth()/2, -dest.getHeight()/2);

glTexCoord2d(xEnd,yEnd); 
glVertex2f(dest.getWidth()/2, dest.getHeight()/2);

glTexCoord2d(xStart,yEnd);
glVertex2f(-dest.getWidth()/2, dest.getHeight()/2);

glEnd();

//Reset the rotation and translation
glRotatef(-rotation,0,0,1);
glTranslatef(-dest.getOrigin().x, -dest.getOrigin().y, 0);

glDisable(GL_TEXTURE_2D);

这会将纹理与其绘制的四边形一起旋转,它不会拉伸或裁剪.但是,如果图像填充为正方形,边缘会有些锯齿状,但我想如果不使用抗锯齿我就无法避免这种情况.

This rotates the texture together with the quad it's drawn in, it doesn't strech or crop. However the edges are a bit jagged if the image is filled square but I guess I can't avoid that with out antialiasing.

推荐答案

你想要的是这个:

glPushMatrix(); //Save the current matrix.
//Change the current matrix.
glTranslatef(dest.getOrigin().x, dest.getOrigin().y, 0);
glRotatef(rotation, 0, 0, 1);

glBegin(GL_QUADS);
glTexCoord2d(xStart,yStart);
glVertex2f(-dest.getWidth()/2, -dest.getHeight()/2);

glTexCoord2d(xEnd,yStart);
glVertex2f(dest.getWidth()/2, -dest.getHeight()/2);

glTexCoord2d(xEnd,yEnd); 
glVertex2f(dest.getWidth()/2, dest.getHeight()/2);

glTexCoord2d(xStart,yEnd);
glVertex2f(-dest.getWidth()/2, dest.getHeight()/2);

glEnd();

//Reset the current matrix to the one that was saved.
glPopMatrix();

这篇关于OpenGL 旋转 2D 纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆