# Sun model matrix

I'm playing with sun implementation in my engine and I correctly built model matrix using below order of matrix operations:

1. Translate to camera position.
2. Calculate x and y axis of rotation matrix using cross product. Z axis is equal to sun direction.

However I'd like to use function which takes into account two unit vectors and based on them build rotation matrix (explanation here). When I replace 2nd step with function call the sun is in completely different position on the sky (but correctly oriented). As a input vectors I set direction vector and [0, 1, 0] vector.

Could someone provide a solution/hint/explanation of that topic?

Code

Working solution:

Matrix matrix; matrix.setPosition(ActiveCamera.getInversedMatrix().pos);

Vector xaxis = Cross(Vector(0,1,0), direction);
xaxis.normalize();

Vector yaxis = Cross(direction, xaxis);
yaxis.normalize();

matrix.x.x = xaxis.x;
matrix.y.x = yaxis.x;
matrix.z.x = direction.x;

matrix.x.y = xaxis.y;
matrix.y.y = yaxis.y;
matrix.z.y = direction.y;

matrix.x.z = xaxis.z;
matrix.y.z = yaxis.z;
matrix.z.z = direction.z;


Not working solution:

Matrix matrix; matrix.setPosition(ActiveCamera.getInversedMatrix().pos);
matrix.setRotation(Vector(0, 1, 0), direction);

Matrix3& Matrix3::setRotation(const Vector &u, const Vector &v) {
Vector cross = Cross(u, v);
float sin = cross.magnitude(); // sine of angle
cross.normalize();
float cos = Dot(u, v); // cosine of angle

this->x.x = cross.x * cross.x * (1 - cos) + cos;
this->x.y = cross.x * cross.y * (1 - cos) + sin * cross.z;
this->x.z = cross.x * cross.z * (1 - cos) - sin * cross.y;

this->y.x = cross.x * cross.y * (1 - cos) - sin * cross.z;
this->y.y = cross.y * cross.y * (1 - cos) + cos;
this->y.z = cross.y * cross.z * (1 - cos) + sin * cross.x;

this->z.x = cross.x * cross.z * (1 - cos) + sin * cross.y;
this->z.y = cross.y * cross.z * (1 - cos) - sin * cross.x;
this->z.z = cross.z * cross.z * (1 - cos) + cos;

return (*this);
}

• Can you post the code you have for this process? It's hard to tell what might be wrong without seeing your code. Aug 3, 2018 at 2:32
• To share a working solution, please add it as an Answer, not as en edit to the question. This makes it easier for users experiencing similar problems to search out answers that have worked for others in the past. Aug 7, 2018 at 18:48