c++ OpenGL旋转和计算
在一个 OpenGL 项目中,我有一个旋转模型的函数:
In an OpenGL project, I have a function that rotates the model:
glRotatef(M_PI/4, 0, 0, 1);
//旋转 PI/4 弧度,即 45 度
glRotatef(M_PI/4, 0, 0, 1);
//rotate by PI/4 radians i.e 45 degrees
现在,当我相对于旋转绘制点 (0, 1)
时.我将在点 (1/sqrt(2), 1/sqrt(2)) 这只是单位圆几何.然而,这些点将使用可变坐标 x1, x2, y1, y2
绘制.另外,我想在我的近似值足够接近后停止绘图.
Now when I draw the point (0, 1)
with respect to the rotation. I will be at the point (1/sqrt(2), 1/sqrt(2)) this is just unit circle geometry. However the points will be drawn with variable coordinates x1, x2, y1, y2
. Also, I would like to stop drawing after my approximation is close enough.
问题是我的停止标准仍然是全局坐标,而不是旋转.例如,考虑 y2 = y1,其中方程尊重变换.因此,在全局坐标中,这将是 y2=y1*sin(pi/4),因此 y2 将位于 y1 的下方和右侧.但是,y2 的值??只是设置为 y1,这在我的停止标准 y2-y1 中产生了一个问题,该标准随后将为零.
The problem is my stopping criteria is still in terms of global coordinates and not with respect to the rotation. For example, consider y2 = y1 where the equation respects the transformation. Thus in the global coordinates this would just be y2=y1*sin(pi/4) and so y2 will be lower and to the right of y1. However the value of y2 is just set to y1 and this creates a problem in my stopping criteria y2-y1 which would then be zero.
如何从 OpenGL 中获取 y2 的旋转值?
推荐答案
如果你使用 OpenGL 中的 glRotatef
,不要忘记旋转角度的单位是度而不是弧度!!!
if you use glRotatef
from OpenGL do not forget that rotation angle is in Degrees not in radians !!!
对于转换和混淆部分 user1118321 是绝对正确的.
for the transformation and confusion part user1118321 is absolutely right.
为了您的目的,您只需要应用 GL_MODELVIEW_MATRIX
I thing for your purposes you need only to apply GL_MODELVIEW_MATRIX
如果您需要模型空间中的坐标,则必须将逆模型视图矩阵乘以全局坐标向量.
if you need coordinates in model space you must multiply inverse modelview matrix by global coordinates vector.
如果您需要全局空间中的坐标,则必须将模型视图矩阵乘以模型坐标向量.
If you need coordinates in global space than you must multiply modelview matrix by model coordinates vector.
顶点发送的坐标在模型空间中您可以使用 gl 函数 glGetFlotav/glGetDoublev
coordinates send by vertex are in model space
you can obtain actual modelview matrix with gl functions glGetFlotav/glGetDoublev
double m[16];
glGetDoublev(GL_MODELVIEW_MATRIX,m);
OpenGL 中不存在逆矩阵和矩阵 x 向量乘法,因此您必须自己编写代码或使用一些库.不要忘记 OpenGL 中的矩阵是面向列而不是面向行的,并且坐标向量是同质的,所以 x,y,z,w
where w=1
为您的目的.这是我用于 OpenGL 子计算的代码,所有向量都是 double[4]
并且矩阵是 double[16]
inverse matrix and matrix x vector multiplication is not present in OpenGL so you must code it yourself or use some lib. Do not forget that matrices in OpenGL are column oriented not row oriented and coordinate vectors are homogenuous so x,y,z,w
where w=1
for your purposes. Here is code I use for my OpenGL sub-calculations, all vectors are double[4]
and matrices are double[16]
void matrix_mul_vector(double *c,double *a,double *b)
{
double q[3];
q[0]=(a[ 0]*b[0])+(a[ 4]*b[1])+(a[ 8]*b[2])+(a[12]);
q[1]=(a[ 1]*b[0])+(a[ 5]*b[1])+(a[ 9]*b[2])+(a[13]);
q[2]=(a[ 2]*b[0])+(a[ 6]*b[1])+(a[10]*b[2])+(a[14]);
for(int i=0;i<3;i++) c[i]=q[i];
}
void matrix_subdet (double *c,double *a)
{
double q[16];
int i,j;
for (i=0;i<4;i++)
for (j=0;j<4;j++)
q[j+(i<<2)]=matrix_subdet(a,i,j);
for (i=0;i<16;i++) c[i]=q[i];
}
double matrix_subdet ( double *a,int r,int s)
{
double c,q[9];
int i,j,k;
k=0; // q = sub matrix
for (j=0;j<4;j++)
if (j!=s)
for (i=0;i<4;i++)
if (i!=r)
{
q[k]=a[i+(j<<2)];
k++;
}
c=0;
c+=q[0]*q[4]*q[8];
c+=q[1]*q[5]*q[6];
c+=q[2]*q[3]*q[7];
c-=q[0]*q[5]*q[7];
c-=q[1]*q[3]*q[8];
c-=q[2]*q[4]*q[6];
if (int((r+s)&1)) c=-c; // add signum
return c;
}
double matrix_det ( double *a)
{
double c=0;
c+=a[ 0]*matrix_subdet(a,0,0);
c+=a[ 4]*matrix_subdet(a,0,1);
c+=a[ 8]*matrix_subdet(a,0,2);
c+=a[12]*matrix_subdet(a,0,3);
return c;
}
double matrix_det ( double *a,double *b)
{
double c=0;
c+=a[ 0]*b[ 0];
c+=a[ 4]*b[ 1];
c+=a[ 8]*b[ 2];
c+=a[12]*b[ 3];
return c;
}
void matrix_inv (double *c,double *a)
{
double d[16],D;
matrix_subdet(d,a);
D=matrix_det(a,d);
if (D) D=1.0/D;
for (int i=0;i<16;i++) c[i]=d[i]*D;
}
更多信息见:
- 了解 4x4 齐次变换矩阵
相关文章