OpenGL万向节锁 [英] OpenGL gimbal lock
问题描述
我找到了有关如何避免云台锁定的示例程序:
http://www.mfwweb.com/OpenGL/Special_Rotations/Source.c
我的问题是,如果在向量或列表中放置多个对象,函数void Render_Scene(void)
的外观应该如何?我在程序中使用了这段代码的片段,但是旋转不起作用.我知道矩阵存在一些问题.这是我的paintGL()函数(我正在使用qt):
I found example program about how to avoid gimbal lock:
http://www.mfwweb.com/OpenGL/Special_Rotations/Source.c
My question is how should function void Render_Scene(void)
looks like if we have more than one object placed in vector or list? I used fragment of this code in my program but rotation doesn't work. I know there is some problem with matrixs. Here is my paintGL() function (I'm using qt):
void GLBox::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
double *trans;
double *rot;
double *matrix;
double ang;
double **vertexes;
//camera
glTranslated(xTrans, yTrans, zTrans);
glRotated(xRot, 1, 0, 0);
glRotated(yRot, 0, 1, 0);
glRotated(zRot, 0, 0, 1);
for(unsigned int i = 0; i < vector_objects.size(); i++)
{
glPushMatrix();
trans = (*vector_objects[i]).getTranslation();
rot = (*vector_objects[i]).getRotation();
matrix = (*vector_objects[i]).getMatrixRotation();
vertexes = (*vector_objects[i]).getVertexes_coordinates();
ang = (*vector_objects[i]).getAngle();
glTranslated(trans[0], trans[1], trans[2]);
if (ang != 0.0)
{
//glLoadIdentity ();
glRotatef (ang, rot[0], rot[1], rot[2]);
glMultMatrixd(matrix);
glGetDoublev(GL_MODELVIEW_MATRIX, matrix);
}
glMultMatrixd(matrix);
//drawing
for(int j = 0; j < (*vector_objects[i]).getNumber_of_vertexes(); j += 3)
{
glBegin(GL_TRIANGLES);
glVertex3dv( vertexes[j]);
glVertex3dv( vertexes[j + 1]);
glVertex3dv( vertexes[j + 2]);
glEnd();
}
glPopMatrix();
}
glFlush();
}
您也许知道如何解决此问题?谢谢您的帮助
Do you maybe know how to fix this problem? Thanks for you help
推荐答案
您确实要为此使用四元数.周围有很多样本.我已经使用了SGI的一个(由加文·贝尔(Gavin Bell)撰写)非常成功.还有一些库(例如, GLM )包括处理/使用它们的例程.几乎所有有关计算机图形学的正派书籍都将至少有一到两章专门讨论基于四元数的旋转.
You really want to use quaternions for this. There are quite a few samples around. I've used one from SGI (written by Gavin Bell) quite successfully. There are also libraries (e.g., GLM) that include routines to manipulate/use them. Almost any decent book on the basics of computer graphics will have at least a chapter or two devoted to quaternion-based rotations as well.
这篇关于OpenGL万向节锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!