gpt4 book ai didi

c++ - glRotate 和我的矩阵::旋转

转载 作者:行者123 更新时间:2023-11-30 04:10:38 25 4
gpt4 key购买 nike

我想获得鼠标效果。

省略鼠标坐标采集等,问题的要点是:

使用固定的 opengl 函数,这可以做到

glRotate(angley,0,1,0);
glRotate(anglex,1,0,0);

使用我的矩阵类,结果不是如上:

mat4 cammtx;
cammtx.rotate(angley,0,1,0);
cammtx.rotate(anglex,1,0,0);

因为相机还没有保持 y 轴,也就是说,z 轴似乎也在旋转......

如何使用我的 matrix::rotate 实现实现相同的 glRotate 行为?

struct mat4
{
float m[16];

mat4(){identity();}
.
.
void rotate(float angle,float x,float y,float z){float res[16];matrix::rotate(res,m,a,x,y,z);memcpy(m,res,sizeof(float)*16);}

};

这是我的旋转函数(输入 vector 未归一化,我知道,但我只传递单位 vector )

void rotate(float* mr,float* m,float angle,float x,float y,float z)
{
float a=angle*PI_OVER_180;
float m2[16] = {0};

float c=cos(a);
float s=sin(a);

float xx=x*x,
yy=y*y,
zz=z*z;


m2[0] = xx+(1.0f-xx)*c;
m2[4] = (1.0f-c)*x*y-s*z;
m2[8] = (1.0f-c)*x*z+s*y;
m2[3] = 0.0f;

m2[1] = (1.0f-c)*y*x+s*z;
m2[5] = yy+(1.0f-yy)*c;
m2[9] = (1.0f-c)*y*z-s*x;
m2[7] = 0.0f;

m2[2] = (1.0f-c)*z*x-s*y;
m2[6] = (1.0f-c)*z*y+s*x;
m2[10] = zz+(1.0f-zz)*c;
m2[11] = 0.0f;

m2[12] = 0;
m2[13] = 0;
m2[14] = 0;
m2[15] = 1.0f;

multiply(mr,m2,m);
}

这是乘法函数

float* multiply(float* c,float* aa,float* bb)
{
for(int i = 0; i < 4; i++)
{
c[i*4] = bb[i*4] * aa[0] + bb[i*4+1] * aa[4] + bb[i*4+2] * aa[8] + bb[i*4+3] * aa[12];
c[i*4+1] = bb[i*4] * aa[1] + bb[i*4+1] * aa[5] + bb[i*4+2] * aa[9] + bb[i*4+3] * aa[13];
c[i*4+2] = bb[i*4] * aa[2] + bb[i*4+1] * aa[6] + bb[i*4+2] * aa[10] + bb[i*4+3] * aa[14];
c[i*4+3] = bb[i*4] * aa[3] + bb[i*4+1] * aa[7] + bb[i*4+2] * aa[11] + bb[i*4+3] * aa[15];
}
return c;
}

最佳答案

使用 multiply (...) 的编写方式,您当前正在预乘 m2m。因为 OpenGL 使用(更重要的是,因为 your rotation code 正在生成)列主矩阵,为了以正确的顺序执行一系列矩阵乘法,您需要发布- 将操作数相乘。

这可以简单地通过将 rotate (...) 函数的末尾更改为以下内容来实现:

multiply(mr,m,m2); // Result = Matrix * Rot

请记住,您需要为您决定自己实现的所有矩阵运算执行此操作。平移、缩放等。假设您坚持使用列优先表示法,则需要进行后乘。

关于c++ - glRotate 和我的矩阵::旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20574564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com