c++ - 如何旋转粒子系统使其不绕世界轴旋转

标签 c++ graphics directx particle-system

我在尝试旋转我的粒子系统时遇到问题。首先,我并不是要旋转每个粒子来尝试增强效果,我想做的是旋转发射器位置。因此,例如,如果我有一个位于 0,0,0 的发射器位置,并且我在 x 轴和 z 轴上发射粒子 -5 和 5,我最终将以与世界轴一致的方形方式发射粒子。我想要的是旋转它,使其发射完全相同但围绕发射点旋转。

 // Each particle point is converted to a quad (two triangles)
[maxvertexcount(4)]  
void mainGSDraw
(
point VS_VertIn                  inParticle[1], // One particle in, as a point
inout TriangleStream<GS_VertOut> outStrip       // Triangle stream output, a quad containing two triangles
)
{
// Camera-space offsets for the four corners of the quad from the particle centre
// Will be scaled depending on distance of the particle
const float3 Corners[4] =
{
    float3(-1,  1, 0),
    float3( 1,  1, 0),
    float3(-1, -1, 0),
    float3( 1, -1, 0),
};

// Texture coordinates for the four corners of the generated quad
const float2 UVs[4] = 
{ 
    float2(0,1), 
    float2(1,1),
    float2(0,0),
    float2(1,0),
};

const float3x3 RotY = 
{ 

    cos(rotationAngle),0,sin(rotationAngle), 
    0,1,0,
    -sin(rotationAngle),0,cos(rotationAngle)
};
GS_VertOut outVert; // Used to build output vertices

// Output the four corner vertices of a quad centred on the particle position
[unroll]
for (int i = 0; i < 4; ++i)
{
    // Use the corners defined above and the inverse camera matrix to calculate each world
    // space corner of the quad

    float3 corner = Corners[i] * inParticle[0].Scale;   // scale first
    float3 worldPosition;


worldPosition = mul(inParticle[0].Position + mul( corner, (float3x3)InvViewMatrix), RotY) ;   // face the camera
    // Transform to 2D position and output along with an appropriate UV
    outVert.ViewportPosition = mul( float4(worldPosition, 1.0f), ViewProjMatrix );
    outVert.TexCoord = UVs[i];
    outStrip.Append( outVert );
}
outStrip.RestartStrip();

} 上面是我的几何着色器绘制函数的代码,一个粒子作为一个点进入并扩展为四边形,缩放然后计算世界位置。如果粒子系统在原点,旋转计算是错误的,它会完全按照我想要的方式旋转系统,但是一旦我移动它,它就会围绕原点旋转,所以我的问题是如何围绕发射器的原点进行旋转不是零世界?代码已被删减,因此无需回复此代码可能无效的问题。

最佳答案

您可以将粒子从发射器原点的平移乘以旋转,然后添加发射器从世界原点的平移。

关于c++ - 如何旋转粒子系统使其不绕世界轴旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10434635/

相关文章:

java - 如何使用FontMetrics?

c# - 两个圆的切线

c++ - 神秘的 HRESULT 错误

c++ - 在直接 x 中旋转 3D 网格

c++ - 在 C++ 中为我自己的基于指针的数组分配内存的正确方法

javascript - 如何使用 Javascript 创建 jpg 图像?

c++ - 如何解决C++中的异步任务错误

c++ - 我应该在使用 CreateTexture2D 后在 ID3D10Device 接口(interface)上调用 Release 吗?为什么?

c++ - buffer = "\0"的目的是什么

c++ - extern "C"不允许 C header 使用 C++ 保留字