当我在某个位置有一个 QUAD 时,如何旋转它以使其法线指向给定点?想象一下彩色块只是矩形四边形,那么这张图片就有点我的意思了。四边形都以指向球体中心的方式定向。
替代文字 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();
}
更新 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();
}
正如你所看到的,我快到了,虽然四边形的旋转仍然有点“奇怪”。我看到下面带有彩色四边形的图像,您可以清楚地看到旋转的差异。如何旋转四边形以得到与下面的彩色球体相同的结果?