简体   繁体   English

如何沿任意轴缩放对象(在3D空间中)

[英]How To Scale Object Along Any Axis (In 3D Space)

I'm trying to write a physics simulator in c++ with opengl and I need to be able to scale objects (mostly cubes right now) along the axis that the camera is facing. 我正在尝试使用opengl用c ++写一个物理模拟器,我需要能够沿着相机所面对的轴缩放对象(现在主要是立方体)。

I can draw objects perfectly fine using this as my Model Matrix: 我可以使用它作为模型矩阵完美地绘制对象:

mat4 Model = Translate(cube.Position - float3(gl.Position.x, gl.Position.y, -gl.Position.z)) * Rotate(cube.Rotation) * Scale(cube.Scale);

Where gl.Position is the cam's position, float3 is a vec3 like class I wrote, etc... gl.Position是凸轮的位置,float3是vec3,就像我写的类一样,等等...

So i tried to modify that line to include a scaling factor before all else (where stuff at the end is applied first): 因此,我尝试修改该行以在所有其他内容之前添加缩放比例(首先应用最后的内容):

mat4 Model = Translate(cube.Position - float3(gl.Position.x, gl.Position.y, -gl.Position.z)) * Rotate(cube.Rotation) * Scale(cube.Scale) * (Rotate(float3(gl.Rotation.x, gl.Rotation.y, gl.Rotation.z)) * Scale(float3(1.0f, 1.0f, sqrt(1 - (velocity * velocity)))) * Rotate(float3(tau - gl.Rotation.x, tau - gl.Rotation.y, tau - gl.Rotation.z)));

It's the last part that is important, where I rotate the object, scale it, then rotate it back.. the sqrt(1 - (velocity * velocity)) is the physics part (Lorentz contraction) and gl.Rotation is a vec3 where each axis holds the pitch, yaw, and roll in radians of the cam. 这是很重要的最后一部分,我在其中旋转对象,缩放它然后向后旋转。.sqrt(1-(速度*速度))是物理部分(洛伦兹收缩),而gl.Rotation是一个vec3,其中每个轴保持凸轮的俯仰,偏航和滚动弧度。 My translate, rotate, etc. functions work properly, but I need help with the theory of creating a matrix to scale. 我的平移,旋转等功能正常运行,但是在创建按比例缩放矩阵的理论方面需要帮助。

Scaling matrices are of the form: 缩放矩阵的形式为:

{{s_x, 0, 0, 0},
 {0, s_y, 0, 0},
 {0, 0, s_z, 0},
 {0,  0,  0, 1}}

Assuming uniform coordinates. 假设坐标统一。

To apply them, you need to scale, then rotate, then translate. 要应用它们,您需要缩放,旋转,然后平移。 I recommend using gl functions to do this. 我建议使用gl函数来做到这一点。 Assume you want your object at position x, y, z, and its rotation is in a quaternion {theta, r_x, r_y, r_z}. 假设您希望对象位于x,y,z位置,并且其旋转方向是四元数{theta,r_x,r_y,r_z}。 Scaling and rotation need to occur in the model coordinate frame. 在模型坐标系中需要进行缩放和旋转。 GL applies the transformations in an appropriate order, so its code would look like this: GL以适当的顺序应用转换,因此其代码如下所示:

glTranslatef(x, y, z);
glRotatef(theta, r_x, r_y, r_z);
glScalef(s_x, s_y, s_z);

//draw my model

I solved it by writing this function that is based off of pippin1289's solution 我通过编写基于pippin1289解决方案的函数解决了该问题

mat4 ScaleOnAxis(float3 a)
{
    a = Norm3f(a);

    if(a == x || (a.x == -1.0f && a.y == 0.0f && a.z == 0.0f))
        return Scale(0.2f, 1.0f, 1.0f);

    float3 axis = Cross(a, x);
    float theta = acos(Dot(a, x));

    if(theta > pi / 2)
    {
        axis = axis * -1.0f;
        theta = pi - theta;
    }

    Quaternion ToRotation(axis.x, axis.y, axis.z, theta);
    Quaternion FromRotation(axis.x, axis.y, axis.z, tau - theta);

    return mat4(FromRotation) * (Scale(float3(0.2f, 1.0f, 1.0f)) * mat4(ToRotation));
}

It returns a matrix that will scale by 0.2 on a axis 它返回一个将在轴上按0.2缩放的矩阵

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM