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

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

问题描述

更新

有关更新见底部。

我一直在找了很多在互联网附近,我已经找到了几个教程,解释我想实现,但我无法得到它的工作,无论是教程不完整或不适用我的code。

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.

这是什么code看起来像

This is what the code looks like

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);

这code将图像绘制在它的原始大小,它会旋转,但它会从左上角其裁剪图像很多旋转。通过调用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);

这将旋转的中心,但它会STRETCH时的图像,如果它不是一个完美的90度旋转。

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

更新

在试图pretty的太多一切可能,我得到了我一直在寻找的结果。

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

但我不知道这是最好的办法。请告诉我,如果有什么问题我的code。

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.

我改变了我的code到这一点:

I changed my code to this:

//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);

此旋转纹理连同它的吸入,它不STRETCH时或作物的四边形。然而,边缘有点参差不齐如果图像填充的正方形,但我想我不能避免与出的平滑处理。

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天全站免登陆