C++/openGL:使用四元数将QUAD旋转到一个点

pol*_*lux 10 c++ opengl rotation quaternions

当我在某个位置有一个QUAD时,我怎么能以这样的方式旋转它以使其法线指向一个给定的点?想象一下,彩色块只是矩形四边形,然后这个图像显示了我的意思.四边形都以这样的方式定向,它们指向球体的中心.

alt text http://emrahgunduz.com/wp-content/uploads/2009/01/material_id_gui-600x364.jpg

也许这第二张图片显示了我正在尝试的更多内容: alt text http://img689.imageshack.us/img689/3130/screenshot20100708at555.png

我正在使用openGL/C++(以及Eigen lib).我有这个代码来绘制一个简单的四边形:

#include "ofMain.h"
#include "Quad.h"
Quad::Quad(Vector3f oPosition):position(oPosition) {
}

void Quad::update() {
}

void Quad::draw() {
    float size = 1.3;
    glColor3f(1.0f, 0.0f, 0.6f);
    glPushMatrix();
        glTranslatef(position.x(), position.y(), position.z());
        glScalef(size, size,size);
        glBegin(GL_QUADS);
            glVertex3f(0,0,0);
            glVertex3f(1,0,0);
            glVertex3f(1,1,0);
            glVertex3f(0,1,0);
        glEnd();
    glPopMatrix();
}
Run Code Online (Sandbox Code Playgroud)

更新17-07 亲爱的读者,

旋转四边形进一步了解一下.我随机定位了几个四边形,然后look_at使用此代码使用以下回复中的描述将它们旋转到vector3f:

void Quad::draw() {
    float size = 0.5;
    glColor3f(1.0f, 0.0f, 0.6f);
    glPushMatrix();
        Vector3f center = look_at - position;
        Vector3f center_norm = center.normalized();
        float r_angle   = acos(center_norm.dot(normal));
        Vector3f axis = normal.normalized().cross(center_norm);

        glPointSize(8);
        glLineWidth(4.0f);

        // draw the center point
        glColor3f(1.0f, 0.0f, 0.0f);
        glBegin(GL_POINTS); 
            glVertex3fv(look_at.data());
        glEnd();

        // draw the quad
        glColor4f(0.0f, 0.0f, 0.0f, 0.85f); 
        glTranslatef(position.x(), position.y(), position.z());
        glRotatef(r_angle * RAD_TO_DEG, axis.x(), axis.y(), axis.z());
        glScalef(size, size,size);
        glBegin(GL_QUADS);
            glVertex3f(-0.5,-0.5,0);
            glVertex3f(0.5,-0.5,0);
            glVertex3f(0.5,0.5,0);
            glVertex3f(-0.5,0.5,0);
        glEnd();

    glPopMatrix();
}
Run Code Online (Sandbox Code Playgroud)

结果如下所示: alt text http://img688.imageshack.us/img688/711/3drotatedquads.png

你可以看到我几乎就在那里,虽然四边形的旋转仍然有点"奇怪".我看到下面带有彩色四边形的图像,你可以清楚地看到旋转的差异.如何以这样的方式旋转四边形我得到与下面的彩色球体相同的结果?

Sig*_*erm 2

旋转轴 = Normalize(crossproduct(currentNormal,desiredNormal))

旋转角度 = acos(dotproduct(normalize(currentNormal), normalize(desiredNormal)).

您可以从轴和角度构建旋转矩阵或四元数。确切的公式可以在任何有关四元数的资源中找到。

您可能需要翻转角度或轴,具体取决于您是绕其底部还是绕其尖端旋转法线。

此外,该资源似乎有足够的关于四元数、旋转和 3d 空间的信息。