• Create Account

### #Actualdoesnotcompute

Posted 30 December 2011 - 02:10 AM

This is my LookAt function. I think I started from the Mesa implementation originally. My matrix class is row-major so that's why everything is transposed from you. I have to pass transpose=true to glUniformMatrix4fv because of this.

INLINE static void CreateLookAt(Vector3 const & position, Vector3 const & target, Vector3 const & upVector, Matrix & result)
{
Vector3 forward;
Vector3::Subtract(target, position, forward);
forward.Normalize();

Vector3 right;
Vector3::Cross(forward, upVector, right);
right.Normalize();

Vector3 up;
Vector3::Cross(right, forward, up);
up.Normalize();

result.elements[0] = right.X;
result.elements[1] = right.Y;
result.elements[2] = right.Z;

result.elements[4] = up.X;
result.elements[5] = up.Y;
result.elements[6] = up.Z;

result.elements[8] = -forward.X;
result.elements[9] = -forward.Y;
result.elements[10] = -forward.Z;

result.elements[12] = 0.0f;
result.elements[13] = 0.0f;
result.elements[14] = 0.0f;

result.elements[3] = -Vector3::Dot(right,position);
result.elements[7] = -Vector3::Dot(up,position);
result.elements[11] = Vector3::Dot(forward,position);
result.elements[15] = 1.0f;
}


### #2doesnotcompute

Posted 30 December 2011 - 02:07 AM

This is my LookAt function. I think I started from the Mesa implementation originally. My matrix class is row-major so that's why everything is transposed from you. I have to pass transpose=true to glUniformMatrix4fv because of this.

Anyway, I think you need to be setting your translation to the three dot products like in my example, not to the eye position or whatever you're doing.

INLINE static void CreateLookAt(Vector3 const & position, Vector3 const & target, Vector3 const & upVector, Matrix & result)
{
Vector3 forward;
Vector3::Subtract(target, position, forward);
forward.Normalize();

Vector3 right;
Vector3::Cross(forward, upVector, right);
right.Normalize();

Vector3 up;
Vector3::Cross(right, forward, up);
up.Normalize();

result.elements[0] = right.X;
result.elements[1] = right.Y;
result.elements[2] = right.Z;

result.elements[4] = up.X;
result.elements[5] = up.Y;
result.elements[6] = up.Z;

result.elements[8] = -forward.X;
result.elements[9] = -forward.Y;
result.elements[10] = -forward.Z;

result.elements[12] = 0.0f;
result.elements[13] = 0.0f;
result.elements[14] = 0.0f;

result.elements[3] = -Vector3::Dot(right,position);
result.elements[7] = -Vector3::Dot(up,position);
result.elements[11] = Vector3::Dot(forward,position);
result.elements[15] = 1.0f;
}


### #1doesnotcompute

Posted 30 December 2011 - 02:06 AM

This is my LookAt function. I think I started from the Mesa implementation originally. My matrix class is row-major so that's why everything is transposed from you. I have to pass transpose=true to glUniformMatrix4fv because of this.

Anyway, I think you need to be setting your translation to the three dot products like in my example, not to the eye position or whatever you're doing.

INLINE static void CreateLookAt(Vector3 const & position, Vector3 const & target, Vector3 const & upVector, Matrix & result)
{
Vector3 forward;
Vector3::Subtract(target, position, forward);
forward.Normalize();
Vector3 right;
Vector3::Cross(forward, upVector, right);
right.Normalize();
Vector3 up;
Vector3::Cross(right, forward, up);
up.Normalize();
result.elements[0] = right.X;
result.elements[1] = right.Y;
result.elements[2] = right.Z;
result.elements[4] = up.X;
result.elements[5] = up.Y;
result.elements[6] = up.Z;
result.elements[8] = -forward.X;
result.elements[9] = -forward.Y;
result.elements[10] = -forward.Z;
result.elements[12] = 0.0f;
result.elements[13] = 0.0f;
result.elements[14] = 0.0f;

result.elements[3] = -Vector3::Dot(right,position);
result.elements[7] = -Vector3::Dot(up,position);
result.elements[11] = Vector3::Dot(forward,position);
result.elements[15] = 1.0f;
}


PARTNERS