0

I have some Vertex Shader, I post follow.
It receives angle, scaling and width and height of destination texture, that might include rotated source texture.
The problem is, that while saling for some angles image go outside of destination texture.
Follow I have result of two rotations.
For 10 grads and for -20 grads.

Vertex Shader

cbuffer VS_ROTATE : register (b0)
{
   float fxAngle;
   float fyAngle;
   float fzAngle;
   float fScale;
   float fWidthSrc;
   float fHeightSrc;
   float offset;
};
struct VS_INPUT
{
    float4 Pos : POSITION;
    float2 Tex : TEXCOORD;
    uint   TexIdx : TEXINDEX;
};

struct VS_OUTPUT
{
    float4 Pos : SV_POSITION;
    float2 Tex : TEXCOORD;
    uint   TexIdx : TEXINDEX;
};

VS_OUTPUT VS(VS_INPUT input)
{
   VS_OUTPUT output;
   output.Pos = input.Pos;
    output.Tex = input.Tex;
    output.TexIdx = input.TexIdx;
   if (fxAngle == 0.0 && fyAngle == 0.0 && fzAngle == 0.0)
        return output;
    float fxRadAngle = radians(fxAngle);
    float fyRadAngle = radians(fyAngle);
    float fzRadAngle = radians(fzAngle);

    float2 ptPos = input.Pos.xy;
    float2 ptOrigin = ptPos;
    ptOrigin.x = (ptOrigin.x - 0.5) * fWidthSrc;
    ptOrigin.y = (ptOrigin.y - 0.5) * fHeightSrc;
    ptPos.x = ptOrigin.x * cos(fzRadAngle) - ptOrigin.y * sin(fzRadAngle);
    ptPos.y = ptOrigin.x * sin(fzRadAngle) + ptOrigin.y * cos(fzRadAngle);
    ptPos.x = ptPos.x / fWidthSrc;
    ptPos.y = ptPos.y / fHeightSrc;
    ptPos.x = ptPos.x * fScale + 0.5;
    ptPos.y = ptPos.y * fScale + 0.5;

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

10 grads

10 grads

-20 grdas

-20 grads

Olga Pshenichnikova
  • 1,151
  • 2
  • 14
  • 31
  • 1
    Without digging into details, you have to consider the rotation center. Have a look at this: [SO: Rotate an image in C++ without using OpenCV functions](https://stackoverflow.com/a/56985104/7478597) to get an idea of what I mean. – Scheff's Cat Oct 30 '19 at 13:01
  • 1
    the `-0.5` doesn't look quite right to me. – apple apple Oct 30 '19 at 13:09

0 Answers0