Opengl 我的2D旋转矩阵有什么问题?
我有一个绕Z旋转的旋转矩阵:Opengl 我的2D旋转矩阵有什么问题?,opengl,graphics,2d,linear-algebra,Opengl,Graphics,2d,Linear Algebra,我有一个绕Z旋转的旋转矩阵: function rotation4x4(angle: number, output: number[], origin_x: number = 0.0, origin_y: number = 0.0) { let x = origin_x; let y = origin_y; let r00 = cos(angle); let r01 = -sin(angle); let r10 = sin(angle); let
function rotation4x4(angle: number, output: number[], origin_x: number = 0.0, origin_y: number = 0.0)
{
let x = origin_x;
let y = origin_y;
let r00 = cos(angle);
let r01 = -sin(angle);
let r10 = sin(angle);
let r11 = cos(angle);
mat4x4_set(output, 0, 0, r00);
mat4x4_set(output, 0, 1, r01);
mat4x4_set(output, 0, 2, x-r00*x-r01*y);
mat4x4_set(output, 0, 3, y-r10*x-r11*y);
mat4x4_set(output, 1, 0, r10);
mat4x4_set(output, 1, 1, r11);
mat4x4_set(output, 1, 2, 0);
mat4x4_set(output, 1, 3, 0);
mat4x4_set(output, 2, 0, 0);
mat4x4_set(output, 2, 1, 0);
mat4x4_set(output, 2, 2, 1);
mat4x4_set(output, 2, 3, 0);
mat4x4_set(output, 3, 0, 0);
mat4x4_set(output, 3, 1, 0);
mat4x4_set(output, 3, 2, 0);
mat4x4_set(output, 3, 3, 1);
}
然后使用该矩阵乘以渲染的所有顶点:
x = mat4x4_get(m, 0, 0) * x + mat4x4_get(m, 0, 1) * y + mat4x4_get(m, 0, 2) * z + mat4x4_get(m, 0, 3) * w;
y = mat4x4_get(m, 1, 0) * x + mat4x4_get(m, 1, 1) * y + mat4x4_get(m, 1, 2) * z + mat4x4_get(m, 1, 3) * w;
z = mat4x4_get(m, 2, 0) * x + mat4x4_get(m, 2, 1) * y + mat4x4_get(m, 2, 2) * z + mat4x4_get(m, 2, 3) * w;
w = mat4x4_get(m, 3, 0) * x + mat4x4_get(m, 3, 1) * y + mat4x4_get(m, 3, 2) * z + mat4x4_get(m, 3, 3) * w;
但当我尝试渲染围绕点旋转的矩形圆时,我得到以下结果:
作为圆心的点在这个形状的中间。
预期结果(但更平滑): 在这里,我为每个象限指定了一种颜色:似乎只有y不正确?对不起,伙计们。我很笨。请看以下代码:
x = mat4x4_get(m, 0, 0) * x + mat4x4_get(m, 0, 1) * y + mat4x4_get(m, 0, 2) * z + mat4x4_get(m, 0, 3) * w;
y = mat4x4_get(m, 1, 0) * x + mat4x4_get(m, 1, 1) * y + mat4x4_get(m, 1, 2) * z + mat4x4_get(m, 1, 3) * w;
z = mat4x4_get(m, 2, 0) * x + mat4x4_get(m, 2, 1) * y + mat4x4_get(m, 2, 2) * z + mat4x4_get(m, 2, 3) * w;
w = mat4x4_get(m, 3, 0) * x + mat4x4_get(m, 3, 1) * y + mat4x4_get(m, 3, 2) * z + mat4x4_get(m, 3, 3) * w;
我使用新的x来计算y坐标。现在它可以工作了:
尽管“渲染围绕点旋转的矩形圆”非常具有描述性,但如果您至少添加了预期结果,可能会有所帮助。我不确定您的原点应该是什么以及这些原点应该如何工作,但我在使用它的代码中看不到任何逻辑。假设它们为零,您可以尝试
mat4x4\u集(输出,0,0,r00);mat4x4_组(输出,0,1,-r01);mat4x4_集(输出,0,2,0);mat4x4_集(输出,0,3,0);mat4x4_集(输出,1,0,-r10);mat4x4_组(输出,1,1,r11)代码>并查看是否得到有意义的结果。我已将原点x和原点y都设置为零。当我将它们设置为其他值时,由于某种原因,当我调整窗口大小时,整个形状将缩放。是的,我试着去掉它们,然后像你说的那样放上零——结果是一样的。原点应该是你旋转的点。在上面的测试中,我将它们设置为零,然后转换所有内容。但是您是否尝试过我在前面的评论中所做的所有修复/更改?还有更多,只有几个零。哈哈,*命中发生了