一个在SDL2, c++中旋转纹理的方法
A method to rotate a texture in SDL2, C++
我有一个Texture类,它允许我加载图像并渲染它们。它有以下属性:
private:
SDL_Texture *m_texture; ///The actual texture
int m_width; ///Its width
int m_height; ///Its height
我想创建一个方法来旋转一个角度的纹理。下面是我所做的:
void Texture::rotation( SDL_Renderer *renderer, float angle )
{
//Target texture to render to
SDL_Texture *target = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_RGBA4444, SDL_TEXTUREACCESS_TARGET, m_width, m_height );
//Set the target texture for rendering
SDL_SetRenderTarget( renderer, target );
//Render m_texture to the target texture with an angle
SDL_RenderCopyEx( renderer, m_texture, NULL, NULL, angle, NULL, SDL_FLIP_NONE );
//Detach the target texture
SDL_SetRenderTarget( renderer, NULL );
//Save texture
SDL_DestroyTexture( m_texture );
m_texture = target;
}
然而,它并不完全工作:
- 透明度丢失(变成黑色)。
- 只保留纹理的一部分,因为转换后的纹理的尺寸不应该是初始的m_width和m_height。
我不能简单地旋转纹理渲染时,因为很多原因,如碰撞检测和效率。那么,我该怎么做呢?
对于1),你需要在源纹理上设置alpha混合,在目标纹理上设置(IIRC)。
对于2),考虑源矩形的中心点为原点和指向四个顶点的四个向量。这四个点总是离中心较远的点,所以如果你在旋转后确保它们在纹理内部,就可以了。您可以使用以下公式将矢量绕z轴旋转角度a(以弧度为单位):
x' = cos(a) * x - sin(a) * y
y' = sin(a) * x + cos(a) * y
,其中(x;y)为原向量,(x';y')为旋转后的向量。将此转换应用于所有四个向量,然后选择最小和最大的x和y值,然后您就有了目标纹理的边界。
实际上,因为有两对平行的向量,你可以进一步简化计算:你只需要两个不平行的向量,用下面的公式得到宽度和高度:
w = 2*max(abs(x1', x2'))
h = 2*max(abs(y1', y2'))
这个方法的唯一问题是,如果你现在进一步旋转这个纹理,你会得到一个不断增长的框架。为了避免这种情况,您可以记住原始纹理的尺寸,并在随后的任何转换中使用它。
来源:lipk
相关文章:
- 与互斥锁相比,旋转锁可以保证上下文切换
- 绘制旋转的三角形
- 旋转模型矩阵时的形状失真
- 四边形的 2D 旋转
- 垂直方向的 Gtk3+ 旋转按钮 (c/c++)
- 发布旋转矩阵(openGL/glm)
- 顺时针迭代旋转 3 位数字
- 形状对象的旋转和缩放不正确C++
- 如何在 OpenSceneGraph 中缩放/旋转/移动资产
- 如何有效地计算将单位立方体映射到自身的反射和旋转?
- 子轴围绕父轴而不是他自己的轴旋转
- 将使用太多的纹理插值器 - 带旋转的着色器
- SFML 向下移动时如何围绕屏幕中心旋转?
- 如何在OpenGL中正确旋转和缩放对象?
- 使用 C++在 OpenGL 中移动自动旋转的 3D 多边形
- 按时间自动旋转形状,同时在 OpenGL 中使用C++移动它们
- 如何使用增强::几何计算多边形的旋转固体体积?
- 使用 GDI+ 旋转位图,然后转换为 HDC
- 如何绕任意轴旋转点?
- 程序仅正确旋转方形图像