Don't transform/translate vectors with an invalid listener orientation

This commit is contained in:
Chris Robinson 2011-10-30 05:47:14 -07:00
parent a5c02556e6
commit 50913948a0

View File

@ -57,18 +57,19 @@ static __inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *i
inVector1[2]*inVector2[2]; inVector1[2]*inVector2[2];
} }
static __inline ALvoid aluNormalize(ALfloat *inVector) static __inline ALfloat aluNormalize(ALfloat *inVector)
{ {
ALfloat length, inverse_length; ALfloat length, inverse_length;
length = aluSqrt(aluDotproduct(inVector, inVector)); length = aluSqrt(aluDotproduct(inVector, inVector));
if(length != 0.0f) if(length > 0.0f)
{ {
inverse_length = 1.0f/length; inverse_length = 1.0f/length;
inVector[0] *= inverse_length; inVector[0] *= inverse_length;
inVector[1] *= inverse_length; inVector[1] *= inverse_length;
inVector[2] *= inverse_length; inVector[2] *= inverse_length;
} }
return length;
} }
static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat w,ALfloat matrix[4][4]) static __inline ALvoid aluMatrixVector(ALfloat *vector,ALfloat w,ALfloat matrix[4][4])
@ -462,29 +463,32 @@ ALvoid CalcSourceParams(ALsource *ALSource, const ALCcontext *ALContext)
N[0] = ALContext->Listener.Forward[0]; // At-vector N[0] = ALContext->Listener.Forward[0]; // At-vector
N[1] = ALContext->Listener.Forward[1]; N[1] = ALContext->Listener.Forward[1];
N[2] = ALContext->Listener.Forward[2]; N[2] = ALContext->Listener.Forward[2];
aluNormalize(N); // Normalized At-vector
V[0] = ALContext->Listener.Up[0]; // Up-vector V[0] = ALContext->Listener.Up[0]; // Up-vector
V[1] = ALContext->Listener.Up[1]; V[1] = ALContext->Listener.Up[1];
V[2] = ALContext->Listener.Up[2]; V[2] = ALContext->Listener.Up[2];
aluNormalize(V); // Normalized Up-vector if(aluNormalize(N) > 0.0f && aluNormalize(V) > 0.0f)
aluCrossproduct(N, V, U); // Right-vector {
aluNormalize(U); // Normalized Right-vector /* Build and normalize right-vector */
Matrix[0][0] = U[0]; Matrix[0][1] = V[0]; Matrix[0][2] = -N[0]; Matrix[0][3] = 0.0f; aluCrossproduct(N, V, U);
Matrix[1][0] = U[1]; Matrix[1][1] = V[1]; Matrix[1][2] = -N[1]; Matrix[1][3] = 0.0f; if(aluNormalize(U) > 0.0f)
Matrix[2][0] = U[2]; Matrix[2][1] = V[2]; Matrix[2][2] = -N[2]; Matrix[2][3] = 0.0f; {
Matrix[3][0] = 0.0f; Matrix[3][1] = 0.0f; Matrix[3][2] = 0.0f; Matrix[3][3] = 1.0f; Matrix[0][0]=U[0]; Matrix[0][1]=V[0]; Matrix[0][2]=-N[0]; Matrix[0][3]=0.0f;
Matrix[1][0]=U[1]; Matrix[1][1]=V[1]; Matrix[1][2]=-N[1]; Matrix[1][3]=0.0f;
Matrix[2][0]=U[2]; Matrix[2][1]=V[2]; Matrix[2][2]=-N[2]; Matrix[2][3]=0.0f;
Matrix[3][0]=0.0f; Matrix[3][1]=0.0f; Matrix[3][2]= 0.0f; Matrix[3][3]=1.0f;
// Translate position /* Translate position */
Position[0] -= ALContext->Listener.Position[0]; Position[0] -= ALContext->Listener.Position[0];
Position[1] -= ALContext->Listener.Position[1]; Position[1] -= ALContext->Listener.Position[1];
Position[2] -= ALContext->Listener.Position[2]; Position[2] -= ALContext->Listener.Position[2];
// Transform source position and direction into listener space /* Transform source vectors into listener space */
aluMatrixVector(Position, 1.0f, Matrix); aluMatrixVector(Position, 1.0f, Matrix);
aluMatrixVector(Direction, 0.0f, Matrix); aluMatrixVector(Direction, 0.0f, Matrix);
// Transform source and listener velocity into listener space aluMatrixVector(Velocity, 0.0f, Matrix);
aluMatrixVector(Velocity, 0.0f, Matrix); aluMatrixVector(ListenerVel, 0.0f, Matrix);
aluMatrixVector(ListenerVel, 0.0f, Matrix); }
}
} }
else else
ListenerVel[0] = ListenerVel[1] = ListenerVel[2] = 0.0f; ListenerVel[0] = ListenerVel[1] = ListenerVel[2] = 0.0f;