When I have a QUAD at a certain position, how can I rotate it in such a way that its normal points toward a given point? Imagine the colored blocks are just rectangular quads, then this image shows a bit what I mean. The quads are all oriented in such a way they point toward the center of the sphere.
Maybe this second image shows a bit more what I'm trying to do:
I'm using openGL / C++ (and the Eigen lib). And I have this code to draw a simple quad:
#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();
}
Update 17-07 Dear reader,
Just got a little bit further with rotating the quads. I'm positioning a couple of quads randomly and then I rotate them towards a look_at
vector3f using this code using the descriptions from the replies below:
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();
}
The result looks like this:
As you can see I'm almost there, though the rotation of the quads is still a bit "strange". I you see the image below with the colored quads you clearly see the difference in rotation. How can I rotate the quad in such a way I get the same result as the colored sphere below?
Thanks in advance