首页 > 解决方案 > HLSL的所有三角函数都使用弧度吗,如何旋转图像?

问题描述

我在所有可用文档中搜索了该问题的答案:Microsoft、wiki 和手册之外。
没有地方可以回答这个问题:“HLSL 的所有三角函数都使用弧度吗?”
我刚刚在微软发现函数的参数必须是:

每个分量都应该是 -1 到 1 范围内的浮点值。

但它并没有说,如果是弧度或渐变......

例如,我有顶点着色器和坐标。
我需要将图片旋转 10°。
我通过常量缓冲区将 10 作为参数传递:fzAngle= 10。
我通过极坐标系使用变换。
下面的代码是否正确?

顶点着色器

float fzRadAngle = radians(fzAngle);
float2 ptPos = input.Pos.xy;
float polarAngle = atan(ptPos.y / ptPos.x);
float polarRadius = sqrt(pow(ptPos.x, 2) + pow(ptPos.y, 2));
polarAngle += fzRadAngle;
ptPos.x = polarRadius * cos(polarAngle);
ptPos.y = polarRadius * sin(polarAngle);

output.Pos.xy = ptPos.xy;
return output;

UPD那么旋转呢......即使我不旋转,只是在坐标系之间转移,我的图像超出范围(它从目标纹理中消失)

float fzRadAngle = radians(fzAngle);
float2 ptPos = input.Pos.xy;
float x = ptPos.x;
float y = ptPos.y;
float phita = atan(y / x);
float ro = sqrt(x * x + y * y);
x = ro * cos(phita);
y = ro * sin(phita);
ptPos.x = x;
ptPos.y = y;
output.Pos.xy = ptPos.xy;
return output;

正如我所看到的,半径计算中的问题,因此,如果我省略ro,我会按原样返回图像:

float fzRadAngle = radians(fzAngle);
float2 ptPos = input.Pos.xy;
float x = ptPos.x;
float y = ptPos.y;
float phita = atan(y / x);
y = x * tan(phita);
ptPos.x = x;
ptPos.y = y;
output.Pos.xy = ptPos.xy;
return output;

UPD

我发现了一些有趣的事情。

坐标系:

[-1, 1, -1, 1]

在此代码的情况下,图片不会改变:

float2 ptPos = input.Pos.xy;
float x = ptPos.x;
float y = ptPos.y;
float phita = atan(y / x);
float ro = sqrt(pow(x, 2) + pow(y, 2));
y = x * tan(phita);
ptPos.x = x;
ptPos.y = y;
output.Pos.xy = ptPos.xy;
return output;

在这种情况下,它消失了:

float2 ptPos = input.Pos.xy;
float x = ptPos.x;
float y = ptPos.y;
float phita = atan(y / x);
float ro = sqrt(pow(x, 2) + pow(y, 2));
x = ro * cos(phita);
y = ro * sin(phita);
ptPos.x = x;
ptPos.y = y;
output.Pos.xy = ptPos.xy;
return output;

即使我尝试从最终值添加/子结构 1 或 0.5x并且y这不会将图像返回到视图字段。

在这里,我只是尝试从 Decart 系统转换为极地并返回。

标签: c++hlsl

解决方案


推荐阅读