Exemple #1
0
//---------------------------
//
//---------------------------
void Matrix44::SetView( const Vector3& vPos, const Vector3& vDir_, const Vector3& vUp_ )
{
	Vector3 vDir;
	Vector3 vUp;
	Vector3 vCross;

	vDir = vDir_.Normal();
	vCross = vUp_.CrossProduct( vDir );
	vCross.Normalize();
	vUp = vDir.CrossProduct( vCross );

	_11 = vCross.x;
	_12 = vUp.x;
	_13 = vDir.x;
	_14 = 0.0F;
	_21 = vCross.y;
	_22 = vUp.y;
	_23 = vDir.y;
	_24 = 0.0F;
	_31 = vCross.z;
	_32 = vUp.z;
	_33 = vDir.z;
	_34 = 0.0F;
	_41 = -vPos.DotProduct( vCross );
	_42 = -vPos.DotProduct( vUp );
	_43 = -vPos.DotProduct( vDir );
	_44 = 1.0F;
} //Matrix44::SetView
Exemple #2
0
		void ComputeLefUpForward(Vector3& o_left, Vector3& o_up, Vector3& o_forward, const Vector3& i_position, const Vector3& i_target)
		{
			// compute the forward vector
			o_forward = i_target - i_position;
			o_forward.Normalize();

			// compute temporal up vector based on the forward vector
			// watch out when look up/down at 90 degree
			// for example, forward vector is on the Y axis
			if (fabs(o_forward[0]) < Math::EPSILON && fabs(o_forward[2]) < Math::EPSILON)
			{
				// forward vector is pointing +Y axis
				if (o_forward[1] > 0)
					o_up = Vector3 { 0, 0, -1 };
				// forward vector is pointing -Y axis
				else
					o_up = Vector3 { 0, 0, 1 };
			}
			// in general, up vector is straight up
			else
			{
				o_up = Vector3 { 0, 1, 0 };
			}

			// compute the left vector
			o_left = o_up.CrossProduct(o_forward);  // cross product
			o_left.Normalize();

			// re-calculate the orthonormal up vector
			o_up = o_forward.CrossProduct(o_left);  // cross product
			o_up.Normalize();
		}
Exemple #3
0
Matrix4 Matrix4::CreateLookAtRightHanded(Vector3& position, Vector3& lookAt, Vector3& upVector)
{
    Matrix4 perspective = Matrix4::Zero();

    Vector3 forward = (lookAt - position).Normalize();
    Vector3 right = upVector.CrossProduct(forward).Normalize();
    Vector3 up = forward.CrossProduct(right).Normalize();

    perspective[0][0] = -right.GetX();
    perspective[1][0] = -right.GetY();
    perspective[2][0] = -right.GetZ();
    perspective[3][0] = right.ScalarProduct(position);
    perspective[0][1] = up.GetX();
    perspective[1][1] = up.GetY();
    perspective[2][1] = up.GetZ();
    perspective[3][1] = -up.ScalarProduct(position);
    perspective[0][2] = -forward.GetX();
    perspective[1][2] = -forward.GetY();
    perspective[2][2] = -forward.GetZ();
    perspective[3][2] = forward.ScalarProduct(position);
    perspective[0][3] = 0.0f;
    perspective[1][3] = 0.0f;
    perspective[2][3] = 0.0f;
    perspective[3][3] = 1.0f;

    return perspective;
}
bool Quaternion::FromLookRotation(const Vector3& direction, const Vector3& upDirection)
{
    Quaternion ret;
    Vector3 forward = direction.Normalized();

    Vector3 v = forward.CrossProduct(upDirection);
    // If direction & upDirection are parallel and crossproduct becomes zero, use FromRotationTo() fallback
    if (v.LengthSquared() >= M_EPSILON)
    {
        v.Normalize();
        Vector3 up = v.CrossProduct(forward);
        Vector3 right = up.CrossProduct(forward);
        ret.FromAxes(right, up, forward);
    }
    else
        ret.FromRotationTo(Vector3::FORWARD, forward);

    if (!ret.IsNaN())
    {
        (*this) = ret;
        return true;
    }
    else
        return false;
}
Exemple #5
0
void Matrix44::SetView( const Vector3& pos, const Vector3& dir0, const Vector3& up0 )
{
	Vector3 vDir;
	Vector3 vUp;
	Vector3 vCross;

	vDir = dir0.Normal();
	vCross = up0.CrossProduct( vDir );
	vCross.Normalize();
	vUp = vDir.CrossProduct( vCross );

	_11 = vCross.x;
	_12 = vUp.x;
	_13 = vDir.x;
	_14 = 0.0f;
	_21 = vCross.y;
	_22 = vUp.y;
	_23 = vDir.y;
	_24 = 0.0f;
	_31 = vCross.z;
	_32 = vUp.z;
	_33 = vDir.z;
	_34 = 0.0f;
	_41 = -pos.DotProduct( vCross );
	_42 = -pos.DotProduct( vUp );
	_43 = -pos.DotProduct( vDir );
	_44 = 1.0f;
}
Exemple #6
0
void Node::LookAt(const Vector3& target, const Vector3& upAxis)
{
    Vector3 targetZ = (target - GetWorldPosition()).Normalized();
    Vector3 targetX = upAxis.CrossProduct(targetZ).Normalized();
    Vector3 targetY = targetZ.CrossProduct(targetX).Normalized();
    
    Quaternion rotation(targetX, targetY, targetZ);
    SetRotation(parent_ ? parent_->GetWorldRotation().Inverse() * rotation : rotation);
}
	//! rotates a vector
	Vector3 Quaternion::Rotate(const Vector3& v) const
    {
		// Irrlicht / nVidia SDK implementation
		Vector3 qvec = V;
		Vector3 uv = qvec.CrossProduct(v);
		Vector3 uuv = qvec.CrossProduct(uv);
		uv *= (2.0f * W);
		uuv *= 2.0f;
		return v + uv + uuv;
    }
Exemple #8
0
void Quaternion::FromLookRotation(const Vector3& direction, const Vector3& upDirection)
{
    Vector3 forward = direction.Normalized();
    Vector3 v = forward.CrossProduct(upDirection).Normalized(); 
    Vector3 up = v.CrossProduct(forward);
    Vector3 right = up.CrossProduct(forward);

    Quaternion ret;
    ret.w_ = sqrtf(1.0f + right.x_ + up.y_ + forward.z_) * 0.5f;
    float w4Recip = 1.0f / (4.0f * ret.w_);
    ret.x_ = (up.z_ - forward.y_) * w4Recip;
    ret.y_ = (forward.x_ - right.z_) * w4Recip;
    ret.z_ = (right.y_ - up.x_) * w4Recip;

    (*this) = ret;
}
void ParticleLayer3D::CalcLong(Particle* current,
							   Vector3& topLeft,
							   Vector3& topRight,
							   Vector3& botLeft,
							   Vector3& botRight)
{

	Vector3 currDirection;
	Particle* parent = emitter->GetParentParticle();		
	if ((NULL != parent)&&inheritPosition)
	{		
		currDirection = current->speed*current->velocityOverLife + parent->speed*parent->velocityOverLife;		
	}else
	{
		currDirection = current->speed;
	}
	currDirection.Normalize();

	Vector3 vecShort = currDirection.CrossProduct(direction);
	vecShort.Normalize();			
	//Vector3 vecLong = vecShort.CrossProduct(direction);
	Vector3 vecLong = -currDirection*(scaleVelocityBase+scaleVelocityFactor*current->speed);
	vecShort /= 2.f;	

	float32 widthDiv2 = sprite->GetWidth()*current->size.x*current->sizeOverLife.x/2;
	float32 heightDiv2 = sprite->GetHeight()*current->size.y*current->sizeOverLife.y/2;

	// Apply offset to the current position according to the emitter position.
	UpdateCurrentParticlePosition(current);

	topRight = currentParticlePosition + widthDiv2*vecShort - heightDiv2/2*vecLong;
	topLeft = currentParticlePosition - widthDiv2*vecShort - heightDiv2/2*vecLong;
	botRight = topRight + heightDiv2*vecLong;
	botLeft = topLeft + heightDiv2*vecLong;
}
Exemple #10
0
void Node::LookAt(const Vector3& target, const Vector3& upAxis, bool worldSpace)
{
    Vector3 targetZ;
    if (worldSpace)
        targetZ = (target - GetWorldPosition()).Normalized();
    else
        targetZ = (target - position_).Normalized();
    
    Vector3 targetX = upAxis.CrossProduct(targetZ).Normalized();
    Vector3 targetY = targetZ.CrossProduct(targetX).Normalized();
    
    if (!worldSpace || !parent_)
        SetRotation(Quaternion(targetX, targetY, targetZ));
    else
        SetRotation(parent_->GetWorldRotation().Inverse() * Quaternion(targetX, targetY, targetZ));
}
Exemple #11
0
void Quaternion::FromRotationTo(const Vector3& start, const Vector3& end)
{
    Vector3 normStart = start.Normalized();
    Vector3 normEnd = end.Normalized();
    float d = normStart.DotProduct(normEnd);
    
    if (d > -1.0f + M_EPSILON)
    {
        Vector3 c = normStart.CrossProduct(normEnd);
        float s = sqrtf((1.0f + d) * 2.0f);
        float invS = 1.0f / s;
        
        x_ = c.x_ * invS;
        y_ = c.y_ * invS;
        z_ = c.z_ * invS;
        w_ = 0.5f * s;
    }
    else
    {
        Vector3 axis = Vector3::RIGHT.CrossProduct(normStart);
        if (axis.Length() < M_EPSILON)
            axis = Vector3::UP.CrossProduct(normStart);
        
        FromAngleAxis(180.f, axis);
    }
}
Exemple #12
0
	Matrix4& Matrix4::LookAtLH( const Vector3& eye, const Vector3& at, const Vector3& up )
	{
		Vector3 zaxis = at - eye;
		zaxis.Normalize();

		Vector3 nup = up;
		nup.Normalize();

		Vector3 xaxis = zaxis.CrossProduct( nup );
		Vector3 yaxis = xaxis.CrossProduct( zaxis );

		A[0][0] = xaxis.X;	A[1][0] = xaxis.Y;	A[2][0] = xaxis.Z;	A[3][0] = 0.0f;
		A[0][1] = yaxis.X;	A[1][1] = yaxis.Y;	A[2][1] = yaxis.Z;	A[3][1] = 0.0f;
		A[0][2] = zaxis.X;	A[1][2] = zaxis.Y;	A[2][2] = zaxis.Z;	A[3][2] = 0.0f;
		A[0][3] = -xaxis.DotProduct( eye );		A[1][3] = -yaxis.DotProduct( eye );		A[2][3] = -zaxis.DotProduct( eye );		A[3][3] = 1.0f;

		return *this;
	}
Exemple #13
0
    void WorldNode::SetDirection(const Vector3& Direction, const Mezzanine::TransformSpace& TS, const Vector3& LocalAxis)
    {
        static const Vector3 Zero(0,0,0);
        if(Direction == Zero)
            return;

        Vector3 NormalizedDir = Direction.GetNormal();
        switch(TS)
        {
            default:
            case Mezzanine::TS_World:
            {
                // Do nothing
                break;
            }
            case Mezzanine::TS_Local:
            {
                NormalizedDir = GetOrientation() * NormalizedDir;
                break;
            }
            case Mezzanine::TS_Parent:
            {
                if(Parent) NormalizedDir = Parent->GetOrientation() * NormalizedDir;
                else return;  /// @todo May want to change this to an exception, maybe.
                break;
            }
        }

        Quaternion FinalOrientation;
        if(FixedYaw)
        {
            Vector3 XVec = FixedYawAxis.CrossProduct(NormalizedDir);
            XVec.Normalize();
            Vector3 YVec = NormalizedDir.CrossProduct(XVec);
            YVec.Normalize();
            Quaternion ZToTarget(XVec,YVec,NormalizedDir);

            if(LocalAxis == Vector3::Neg_Unit_Z())
            {
                FinalOrientation.SetValues(-ZToTarget.Y,-ZToTarget.Z,ZToTarget.W,ZToTarget.X);
            }else{
                FinalOrientation = ZToTarget * (LocalAxis.GetRotationToAxis(Vector3::Unit_Z()));
            }
        }else{
            Quaternion CurrOri = GetOrientation();
            Vector3 CurrDir = CurrOri * LocalAxis;
            if( (CurrDir+NormalizedDir).SquaredLength() < 0.00005 )
            {
                FinalOrientation.SetValues(-CurrOri.Y,-CurrOri.Z,CurrOri.W,CurrOri.X);
            }else{
                FinalOrientation = (CurrDir.GetRotationToAxis(NormalizedDir)) * CurrOri;
            }
        }

        SetOrientation(FinalOrientation);
    }
Exemple #14
0
Vector3 Vector3::Reflect(const Vector3& n) const
{
	Vector3 result;
	
	//TODO: Calculate the reflection of this vector given the input normal n
	//Store the result in result

	// DONE
	return n.CrossProduct(*this).CrossProduct(n) * 2.f - *this;
}
Exemple #15
0
	void RigidBody::AddForceAtPoint(const Vector3& force, const Point3& point)
	{
		Vector3 pointCrossForce;
		Vector3 posAsVec = m_Pos.ToVector3();
		Vector3 pointRelative = point.ToVector3().Subtract(posAsVec);

		m_ForceAccum = m_ForceAccum.Add(force);
		pointCrossForce = pointRelative.CrossProduct(force);
		m_TorqueAccum = m_TorqueAccum.Add(pointCrossForce);
	}
Exemple #16
0
void SoundSystem::SetListenerOrientation(const Vector3 & at, const Vector3 & left)
{
	Vector3 atNorm = at;
	atNorm.Normalize();
	Vector3 upNorm = at.CrossProduct(left);
	upNorm.Normalize();

	FMOD_VECTOR fmodAt = {atNorm.x, atNorm.y, atNorm.z};
	FMOD_VECTOR fmodUp = {upNorm.x, upNorm.y, upNorm.z};
	FMOD_VERIFY(fmodEventSystem->set3DListenerAttributes(0, 0, 0, &fmodAt, &fmodUp));
}
Exemple #17
0
void Camera::LookAt(const Vector3& eye, const Vector3& center, const Vector3& up)
{
    mPosition = eye;
    mCenter = center;
    mUp = up;

    // http://www.opengl.org/wiki/GluLookAt_code

    const Vector3 forward = (center - eye).GetNormalized();
    const Vector3 side = (forward.CrossProduct(up)).GetNormalized();
    const Vector3 newUp = side.CrossProduct(forward).GetNormalized();

    mViewMatrix = Matrix4x4(side.X, newUp.X, -forward.X, 0.0f,
                            side.Y, newUp.Y, -forward.Y, 0.0f,
                            side.Z, newUp.Z, -forward.Z, 0.0f,
                            0.0f, 0.0f, 0.0f, 1.0f);
    mViewMatrix.Translate(eye * -1.0f);

    mViewFrustum.LookAt(eye, center, up);
}
Exemple #18
0
/**
*  @brief
*    Calculate the intersection of a ray and a triangle
*/
dFloat BodyTerrain::RayCastTriangle(const Vector3 &p0, const Vector3 &dp, const Vector3 &origin, const Vector3 &e1, const Vector3 &e2)
{
	dFloat t;
	dFloat b0;
	dFloat b1;
	dFloat b00;
	dFloat b11;
	dFloat a00;
	dFloat a10;
	dFloat a11;
	dFloat det;
	dFloat dot;
	dFloat tol;

	// Clip line again first triangle
	Vector3 normal(e2.CrossProduct(e1));

	dot = normal.DotProduct(dp);
	if (dot <= 1.0e-6f) {
		t = ((origin - p0).DotProduct(normal)) / dot;
		if (t > 0.0f) {
			if (t < 1.0f) {
				Vector3 q = p0 + dp*t;
				a00 = e1.DotProduct(e1);
				a11 = e2.DotProduct(e2);
				a10 = e1.DotProduct(e2);
				det = a00*a11 - a10*a10;
				// det must be positive and different than zero
//				_ASSERTE(det > 0.0f);
				
				Vector3 q0p0 = q - origin;
				b0 = q0p0.DotProduct(e1);
				b1 = q0p0.DotProduct(e2);

				tol = -det*1.0e-3f;
				b00 = b0*a11 - b1*a10;
				if (b00 >= tol) {
					b11 = b1*a00 - b0*a10;
					if (b11 >= tol) {
						if ((b00 + b11) <= (det*1.001f)) {
							// Found a hit return this value
							return t;
						}
					}
				}
			}
		}
	}

	// If it come here the there no intersection
	return 1.2f;
}
	//! constructor
	Frustum::Frustum(f32 fFov, f32 fRatio, f32 fNear, f32 fFar, const Vector3& vPosition, const Vector3& vLookAt, const Vector3& vUp)
	{
		f32 Hnear = Math::Tan(fFov * Math::DegToRadFactor / 2) * fNear;
		f32 Wnear = Hnear * fRatio;
		f32 Hfar = Math::Tan(fFov * Math::DegToRadFactor / 2) * fFar;
		f32 Wfar = Hfar * fRatio;
		Vector3 vDirection = (vLookAt-vPosition).Normalize();
		Vector3 vRight = vDirection.CrossProduct(vUp).Normalize();
		Vector3 vLocalUp = vRight.CrossProduct(vDirection).Normalize();

		Vector3 FCenter = vPosition + vDirection * fFar;
		Vector3 FTopLeft = FCenter + (vLocalUp * Hfar) - (vRight * Wfar);
		Vector3 FTopRight = FCenter + (vLocalUp * Hfar) + (vRight * Wfar);
		Vector3 FBottomLeft = FCenter - (vLocalUp * Hfar) - (vRight * Wfar);
		Vector3 FBottomRight = FCenter - (vLocalUp * Hfar) + (vRight * Wfar);
		Vector3 NCenter = vPosition + vDirection * fNear;
		Vector3 NTopLeft = NCenter + (vLocalUp * Hnear) - (vRight * Wnear);
		Vector3 NTopRight = NCenter + (vLocalUp * Hnear) + (vRight * Wnear);
		Vector3 NBottomLeft = NCenter- (vLocalUp * Hnear) - (vRight * Wnear);
		Vector3 NBottomRight = NCenter - (vLocalUp * Hnear) + (vRight * Wnear);

		m_Planes[P_Top] = Plane(NTopLeft, FTopLeft, FTopRight);
		m_Planes[P_Bottom] = Plane(NBottomRight, FBottomRight, FBottomLeft);
		m_Planes[P_Left] = Plane(FBottomLeft, FTopLeft, NTopLeft);
		m_Planes[P_Right] = Plane(NBottomRight, NTopRight, FTopRight);
		m_Planes[P_Near] = Plane(NBottomLeft, NTopLeft, NTopRight);
		m_Planes[P_Far] = Plane(FBottomRight, FTopRight, FTopLeft);

#ifdef SHOOT_EDITOR
		m_FTopLeft = FTopLeft;
		m_FTopRight = FTopRight;
		m_FBottomLeft = FBottomLeft;
		m_FBottomRight = FBottomRight;
		m_NTopLeft = NTopLeft;
		m_NTopRight = NTopRight;
		m_NBottomLeft = NBottomLeft;
		m_NBottomRight = NBottomRight;
#endif // SHOOT_EDITOR
	}
Exemple #20
0
Matrix4 Matrix4::GenerateOrthogonalBaseFromAxis(const Vector3 &aAxis)
{
	Vector3 FirstVector = aAxis.NormalisedCopy();
	Vector3 SecondVector;
	Vector3 ThirdVector;
	if(aAxis != Vector3::UnitX)
	{
		SecondVector = Vector3::UnitX;
	}
	else
	{
		SecondVector = Vector3::UnitY;
	}

	ThirdVector = FirstVector.CrossProduct(SecondVector).NormalisedCopy();
	SecondVector = FirstVector.CrossProduct(ThirdVector).NormalisedCopy();

	return Matrix4(		SecondVector.X(),	SecondVector.Y(),	SecondVector.Z(),	0,
						FirstVector.X(),	FirstVector.Y(),	FirstVector.Z(),	0,
						ThirdVector.X(),	ThirdVector.Y(),	ThirdVector.Z(),	0,
						0,					0,					0,					1);
}
Exemple #21
0
void Triangle::SetTriangle(Vector3 v0, Vector3 v1, Vector3 v2)
{
	m_vertices[0] = v0;
	m_vertices[1] = v1;
	m_vertices[2] = v2;

	//Calculate Normal
	Vector3 NormalA = m_vertices[1] - m_vertices[0];
	Vector3 NormalB = m_vertices[2] - m_vertices[0];
	Vector3 Norm = NormalA.CrossProduct(NormalB);
	Norm.Normalise();
	m_normal = Norm;
}
// ----------------------------------------------------------------------------
Vector3 Face::Intersect(const Vector3& origin, const Vector3& direction) const
{
#define NO_INTERSECTION Vector3(-1, -1, -1)
#define DO_CULL 0

    // Algorithm taken from:
    // "Fast, Minimum Storage Ray/Triangle Intersection"
    // See doc/research/
    Vector3 edge1 = vertex_[1]->position_ - vertex_[0]->position_;
    Vector3 edge2 = vertex_[2]->position_ - vertex_[0]->position_;

    Vector3 p = direction.CrossProduct(edge2);
    float determinant = p.DotProduct(edge1);
#if DO_CULL
    if(determinant == 0.0f)
        return NO_INTERSECTION;
#endif

    Vector3 ray = origin - vertex_[0]->position_;
    float u = p.DotProduct(ray);
#if DO_CULL
    if(u < 0.0f || u > determinant)
        return NO_INTERSECTION;
#endif

    Vector3 q = ray.CrossProduct(edge1);
    float v = q.DotProduct(direction);
#if DO_CULL
    if(v < 0.0f || v > determinant)
        return NO_INTERSECTION;
#endif

    determinant = 1.0f / determinant;
    u *= determinant;
    v *= determinant;

    return Vector3(1.0f - u - v, u, v);
}
Exemple #23
0
//---------------------------
//
//---------------------------
void Matrix44::SetWorld( const Vector3& vPos, const Vector3& vINDir, const Vector3& vINUp )
{
	Vector3		vDir	= vINDir.Normal();
	Vector3		vCross	= vINUp.CrossProduct( vDir ).Normal();
	Vector3		vUp		= vDir.CrossProduct( vCross );

	_11 = vCross.x;
	_12 = vCross.y;
	_13 = vCross.z;
	_14 = 0.0F;
	_21 = vUp.x;
	_22 = vUp.y;
	_23 = vUp.z;
	_24 = 0.0F;
	_31 = vDir.x;
	_32 = vDir.y;
	_33 = vDir.z;
	_34 = 0.0F;
	_41 = vPos.x;
	_42 = vPos.y;
	_43 = vPos.z;
	_44 = 1.0F;
} //Matrix44::SetWorld
Exemple #24
0
//////////////////////////////////////////////////////////////////////////
///  Rotate the camera.
///
///  @param [in]      yaw   movement delta on X
///  @param [in]      pitch  movement delta on Y
///
///  @remark this version of rotation rotate using floats.
///
///  This function doesn't return a value
//////////////////////////////////////////////////////////////////////////
void Camera::Rotate(VCNFloat yaw, VCNFloat pitch)
{
  Vector3 vAxis = mFocus - mPosition;
  vAxis = vAxis.CrossProduct(mUp).Normalized();

  RotateView(pitch, vAxis.x, vAxis.y, vAxis.z);

  // Prevent head and feet angles
  if ( VCN::Abs(mDir.DotProduct(mUp)) > BLOCK_ANGLE )
  {
    RotateView(-pitch, vAxis.x, vAxis.y, vAxis.z);
  }

  RotateView(yaw, mUp.x, mUp.y, mUp.z);
}
void ModelLoader::GenerateNormals()
{
    mVertReferences.resize(mVertices.size());
    for(unsigned int i = 0; i < mCombinedVertices.size(); ++i)
    {
        //Assign every vertice to their respective triangle(s).
        mVertReferences[mCombinedVertices[i].PtId1].push_back(i);
        mVertReferences[mCombinedVertices[i].PtId2].push_back(i);
        mVertReferences[mCombinedVertices[i].PtId3].push_back(i);
    }
    
    vector<Vector3> FacesNormals(mCombinedVertices.size());
    
    for(unsigned int i = 0; i < mCombinedVertices.size(); ++i)
    {
        Vector3 VecL =  mVertices[mCombinedVertices[i].PtId1] - 
                        mVertices[mCombinedVertices[i].PtId2];
        Vector3 VecR =  mVertices[mCombinedVertices[i].PtId3] - 
                        mVertices[mCombinedVertices[i].PtId2];
        
        FacesNormals[i] = VecR.CrossProduct(VecL).NormalisedCopy();	
    }
    
    mNormals.resize(mVertices.size());
    mCombinedNormals.resize(mCombinedVertices.size());
    for(unsigned int i = 1; i < mVertices.size(); ++i)
    {
        Vector3 NormalSum(0,0,0);
        for(unsigned int j = 0; j < mVertReferences[i].size(); ++j)
        {
            NormalSum += FacesNormals[mVertReferences[i][j]];
            
            mCombinedNormals[mVertReferences[i][j]].PtId1 = 
                mCombinedVertices[mVertReferences[i][j]].PtId1;
            
            mCombinedNormals[mVertReferences[i][j]].PtId2 = 
                mCombinedVertices[mVertReferences[i][j]].PtId2;
            
            mCombinedNormals[mVertReferences[i][j]].PtId3 = 
                mCombinedVertices[mVertReferences[i][j]].PtId3;
        }
        
        NormalSum /= float(mVertReferences[i].size());
        mNormals[i] = NormalSum.NormalisedCopy();
    }
}
Exemple #26
0
//--------------------------------
//
//--------------------------------
void Quaternion::SetRotationArc( const Vector3& v0, const Vector3& v1 )
{
    Vector3 vCross = v0.CrossProduct( v1 );

    float fDot = v0.DotProduct( v1 );
    float s = (float)sqrt( ( 1.0F + fDot ) * 2.0F );
    if( 0.1f >  s )
    {
        x = 0;
        y = 1;
        z = 0;
        w = 0;
        return;
    }

    x = vCross.x / s;
    y = vCross.y / s;
    z = vCross.z / s;
    w = s * 0.5F;
} //Quaternion::SetRotationArc
Exemple #27
0
void Quaternion::SetRotationArc(const Vector3& v0, const Vector3& v1, const Vector3 &norm)
{
	Vector3 vCross = v0.CrossProduct(v1);
	const float len = vCross.Length();
	if (len <= 0.01f)
	{
		// v0 - v1 벡터가 정확히 반대 방향이거나, 정확히 같은 방향을 가르킬때,
		// 두 벡터에 직교하는 벡터 norm 에서 180도 회전하거나, 회전하지 않거나
		// 결정한다.
		*this = Quaternion(norm, v0.DotProduct(v1) > 0 ? 0 : MATH_PI);
		return;
	}

	float fDot = v0.DotProduct(v1);
	float s = (float)sqrtf((1.0f + fDot) * 2.0f);

	x = vCross.x / s;
	y = vCross.y / s;
	z = vCross.z / s;
	w = s * 0.5f;
} //Quaternion::SetRotationArc
Exemple #28
0
//--------------------------------
//
//--------------------------------
void Quaternion::SetRotationArc( const Vector3& v0, const Vector3& v1 )
{
	Vector3 vCross = v0.CrossProduct(v1);
	const float len = vCross.Length();
	if (len <= 0.01f)
	{
		x = 0; y = 0; z = 0; w = 1;
		return;
	}

	float fDot = v0.DotProduct( v1 );
	float s = (float)sqrtf((1.0f + fDot) * 2.0f);
	//if (0.1f >  s)
	//{
	//	x = 0; y = 1; z = 0; w = 0;
	//	return;
	//}

	x = vCross.x / s;
	y = vCross.y / s;
	z = vCross.z / s;
	w = s * 0.5f;
} //Quaternion::SetRotationArc
Exemple #29
0
int main()
{
    cout << "---------------------------Matrix 3--------------------------------" << endl;

    Matrix3 TranslationXY;
    TranslationXY = Mat3.m_TranslationXY(2, 2);
    cout << TranslationXY;
    cout << endl;

    cout << "--------------------------MATRIX 4 --------------------------------" << endl;

    Matrix4 RotationX;
    RotationX = Mat4.m_RotationX(3);
    cout << RotationX;
    cout << endl;

    Matrix4 RotationY;
    RotationY = Mat4.m_RotationY(2);
    cout << RotationY;
    cout << endl;

    Matrix4 RotationZ;
    RotationZ = Mat4.m_RotationZ(2);
    cout << RotationZ;
    cout << endl;

    Matrix4 TranslationXYZ;
    TranslationXYZ = Mat4.m_TranslationXYZ(2, 2, 2);
    cout << TranslationXYZ;
    cout << endl;

    Matrix4 mat;
    mat = Mat4.m_OrthoProjection(2,2,2,2,2,2);
    cout << mat;
    cout << endl;

    Matrix4 Identity;
    mat = Mat4.m_CreateIdentity();
    cout << Identity;
    cout <<endl;

    cout << "---------------------------COMMON MATH--------------------------------" << endl;

    cout << ComMath.Pow2(2, 2)<< endl;
    cout << ComMath.m_RadianConvert(360)<< endl;
    cout << ComMath.m_degreeConvert(6) << endl;
    Vect3.x = 2;
    Vect3.y = 2;
    Vect3.z =2;
    Vect33.x = 4;
    Vect33.y = 4;
    Vect33.z = 4;
    cout << ComMath.m_Lerp(Vect3, Vect33, 2) << endl;

    cout << "---------------------------VECTOR 3--------------------------------" << endl;
    Vect3.x = 2;
    Vect3.y =2;
    Vect3.z =2;
    cout << Vect3.Magnitude()<<endl;

    Vect3.x = 2;
    Vect3.y =2;
    Vect3.z =2;
    cout << Vect33.Normalise(Vect3)<<endl;

    Vect3.x = 2;
    Vect3.y =2;
    Vect3.z = 2;
    cout << Vect33.GetNormal(Vect3) << endl;

    Vect3.x = 2;
    Vect3.y = 2;
    Vect3.z =2;
    cout <<Vect33.DotProduct(Vect3) << endl;

    Vect3.x = 4;
    Vect3.y = 4;
    Vect3.z = 4;
    Vect33.x =4;
    Vect33.y = 4;
    Vect3.z = 4;
    cout << Vect3.EulerAngle(Vect3, Vect33)<<endl;

    Vect3.x = 2;
    Vect3.y = 2;
    Vect3.z = 2;
    Vect33.x = 2;
    Vect33.y =2;
    Vect33.z = 2;
    cout << Vect3.CrossProduct(Vect3, Vect33)<<endl;

    Matrix3 Transform;
    Transform = Mat3;
    Vect3.x =2;
    Vect3.y = 2;
    Vect3.z = 2;
    cout << Vect3.m_TransformVector3(Mat3)<<endl;

    Matrix3 tempM;
    tempM = Mat3;
    Vect3.x = 2;
    Vect3.y = 2;
    Vect3.z = 2;
    cout << Vect3.Scale(Mat3);
    cout << endl;

    cout << "---------------------------VECTOR 4--------------------------------" << endl;
    Vect4.x = 2;
    Vect4.y =2;
    Vect4.z =2;
    Vect4.w = 2;
    cout << Vect4.m_Magnitude()<<endl;

    Vect4.x = 2;
    Vect4.y =2;
    Vect4.z =2;
    Vect4.w = 2;
    cout << Vect4.m_GetNormal(Vect4)<<endl;

    Vect4.x = 2;
    Vect4.y =2;
    Vect4.z =2;
    Vect4.w = 2;
    cout << Vect4.m_Normalise(Vect4) <<endl;

    Vect4.x = 2;
    Vect4.y =2;
    Vect4.z =2;
    Vect4.w = 2;
    cout << Vect4.m_DotProduct(Vect4) << endl;

    cout << Vect4.m_RGBconverter(0xFFFFFFFF)<<endl;

    Matrix4 Transform2;
    Transform2 = Mat4;
    Vect4.x = 2;
    Vect4.y =2;
    Vect4.z =2;
    Vect4.w = 2;
    cout << Vect4.m_TransformPoint(Mat4) << endl;

    Matrix4 Transform3;
    Transform3 = Mat4;
    Vect4.x = 2;
    Vect4.y =2;
    Vect4.z =2;
    Vect4.w = 2;
    cout << Vect4.m_TransformVector4(Vect4, Mat4);
    cout << endl;

    Matrix4 mat4;
    mat4 = Mat4;
    Vect4.x = 2;
    Vect4.y = 2;
    Vect4.z = 2;
    Vect4.w = 2;
    cout << Vect4.Scale(Mat4);
    cout << endl;

    cout << "---------------------------VECTOR 2--------------------------------" << endl;
    Vectors Point;
    Point.x = 2;
    Point.y =2;
    cout << V2.pointSubtract(Point, 2) << endl;

    Vectors Point2;
    Point2.x = 2;
    Point2.y =2;
    cout << V2.pointAdd(Point2, 2)<<endl;

    Vectors Point3;
    Point3.x = 2;
    Point3.y =2;
    cout << V2.multiplyScalar(Point3, 2) << endl;

    Vectors Point4;
    Point4.x = 2;
    Point4.y =2;
    cout << V2.getMagnitude(Point4)<<endl;

    Vectors Point5;
    Point5.x = 2;
    Point5.y =2;
    cout<<V2.getNormal(Point5)<<endl;

    getchar();
    return 0;
}
Exemple #30
0
/**
*  @brief
*    Returns the currently picked texture coordinate (can also be outside the 0..1 interval)
*/
bool PickingResult::GetTextureCoordinate(Vector2 &vTexCoord, uint32 nTexCoordChannel) const
{
	// Is anything picked?
	if (m_pSceneNode) {
		// Get the mesh handler
		MeshHandler *pMeshHandler = m_pSceneNode->GetMeshHandler();
		if (pMeshHandler) {
			// Get the mesh
			Mesh *pMesh = pMeshHandler->GetResource();
			if (pMesh) {
				// Get the first LOD level of the mesh
				MeshLODLevel *pLODLevel = pMesh->GetLODLevel(0);
				if (pLODLevel) {
					// Get the vertex indices of the picked triangle
					uint32 nVertex0, nVertex1, nVertex2;
					if (pLODLevel->GetTriangle(m_nGeometry, m_nTriangle, nVertex0, nVertex1, nVertex2)) {
						// Get and lock the vertex buffer
						VertexBuffer *pVertexBuffer = pMeshHandler->GetVertexBuffer();
						if (pVertexBuffer && pVertexBuffer->GetNumOfElements() && pVertexBuffer->Lock(Lock::ReadOnly)) {
							// Get triangle vertex position
							const Vector3 vA = static_cast<const float*>(pVertexBuffer->GetData(nVertex0, VertexBuffer::Position));
							const Vector3 vB = static_cast<const float*>(pVertexBuffer->GetData(nVertex1, VertexBuffer::Position));
							const Vector3 vC = static_cast<const float*>(pVertexBuffer->GetData(nVertex2, VertexBuffer::Position));

							// Get triangle texture coordinates
							const float *pfTexCoordA = static_cast<const float*>(pVertexBuffer->GetData(nVertex0, VertexBuffer::TexCoord, nTexCoordChannel));
							const float *pfTexCoordB = static_cast<const float*>(pVertexBuffer->GetData(nVertex1, VertexBuffer::TexCoord, nTexCoordChannel));
							const float *pfTexCoordC = static_cast<const float*>(pVertexBuffer->GetData(nVertex2, VertexBuffer::TexCoord, nTexCoordChannel));

							// Unlock the vertex buffer
							pVertexBuffer->Unlock();

							// Calculate picked texture coordinate
							if (pfTexCoordA && pfTexCoordB && pfTexCoordC) {
								const Vector2 vTexCoordA = pfTexCoordA;
								const Vector2 vTexCoordB = pfTexCoordB;
								const Vector2 vTexCoordC = pfTexCoordC;

								// We want to have the texture coordinate for 'm_vPoint'...
								// ... we have all triangle data we need to calculate our ...
								// ... 3 vertex positions, 3 texture coordinates and the point on the triangle we want to calculate
								// the texture coordinate for. We solve the problem by using "Barycentric coordinates".
								// http://www.gamedev.net/community/forums/topic.asp?topic_id=451357

								// Compute the normal of the triangle
								Vector3 vN;
								vN.CrossProduct(vB-vA, vC-vA);
								vN.Normalize();

								// Compute twice area of triangle ABC
								const float fAreaABC = vN.DotProduct(Vector3().CrossProduct(vB-vA, vC-vA));

								// Compute a
								const float fAreaPBC = vN.DotProduct(Vector3().CrossProduct(vB-m_vPoint, vC-m_vPoint));
								const float fA = fAreaPBC / fAreaABC;

								// Compute b
								const float fAreaPCA = vN.DotProduct(Vector3().CrossProduct(vC-m_vPoint, vA-m_vPoint));
								const float fB = fAreaPCA / fAreaABC;

								// Compute c
								const float fC = 1.0f - fA - fB;

								// Finally, calculate the texture coordinate
								vTexCoord = vTexCoordA*fA + vTexCoordB*fB + vTexCoordC*fC;

								// Done
								return true;
							}
						}
					}
				}
			}
		}
	}

	// Set to null on error
	vTexCoord = Vector2::Zero;

	// Error!
	return false;
}