Example #1
0
void Transform::look_at(const Vector3 & eye, const Vector3 & center,
                        const Vector3 & up, Matrix4 * m)
{
	Vector3 forward;
	forward.set(center);
	forward.sub(eye);
	forward.normalize();

	Vector3 side;
	side.cross(forward, up);
	side.normalize();

	Vector3 upvec;
	upvec.cross(side, forward);

	Matrix4 xform;
	xform.identity();
	xform.set_row(0, side);
	xform.set_row(1, upvec);

	forward.negate();
	xform.set_row(2, forward);

	m->mul(xform);

	Vector3 neg_eye;
	neg_eye.set(eye);
	neg_eye.negate();

	Matrix4 xlate;
	xlate.translate(neg_eye);
	m->mul(xlate);
}
void Camera::lookAt (const Point3 &eyePos, const Point3 &centerOfView, const Vector3 &_up)
{
	viewMatrix = Matrix4::Identity();
	Vector3 up = _up;

	Vector3 direction;
	direction.x() = centerOfView.x() - eyePos.x();
	direction.y() = centerOfView.y() - eyePos.y();
	direction.z() = centerOfView.z() - eyePos.z();
	direction.normalize();

	up.normalize();
	Vector3 side = direction.cross(up);
	Vector3 Utrue = side.cross (direction);

	viewMatrix (0, 0) = side.x();
	viewMatrix (0, 1) = side.y();
	viewMatrix (0, 2) = side.z();
	viewMatrix (1, 0) = Utrue.x();
	viewMatrix (1, 1) = Utrue.y();
	viewMatrix (1, 2) = Utrue.z();
	viewMatrix (2, 0) = -direction.x();
	viewMatrix (2, 1) = -direction.y();
	viewMatrix (2, 2) = -direction.z();

	viewMatrix (0, 3) = -eyePos.x();
	viewMatrix (1, 3) = -eyePos.y();
	viewMatrix (2, 3) = -eyePos.z();

	std::cout << viewMatrix << std::endl;
}
Example #3
0
void CoordinateFrame::lookAt(
    const Vector3&  target,
    Vector3         up) {

    up = up.direction();

    Vector3 look = (target - translation).direction();
    if (fabs(look.dot(up)) > .99f) {
        up = Vector3::unitX();
        if (fabs(look.dot(up)) > .99f) {
            up = Vector3::unitY();
        }
    }

    up -= look * look.dot(up);
    up.unitize();

    Vector3 z = -look;
    Vector3 x = -z.cross(up);
    x.unitize();

    Vector3 y = z.cross(x);

    rotation.setColumn(0, x);
    rotation.setColumn(1, y);
    rotation.setColumn(2, z);
}
Example #4
0
Matrix4x3 Transform::lookAt( const Vector3& lookAtVector, const Vector3& upVector )
{
	const Vector3& eye = position;

	Vector3 zaxis = (eye - lookAtVector).normalize();
	Vector3 xaxis = upVector.cross(zaxis).normalize();
	Vector3 yaxis = zaxis.cross(xaxis);

	Matrix4x3 mat;

	mat.m11 = xaxis.x;
	mat.m12 = yaxis.x;
	mat.m13 = zaxis.x;

	mat.m21 = xaxis.y;
	mat.m22 = yaxis.y;
	mat.m23 = zaxis.y;

	mat.m31 = xaxis.z;
	mat.m32 = yaxis.z;
	mat.m33 = zaxis.z;

	mat.tx = -xaxis.dot(eye);
	mat.ty = -yaxis.dot(eye);
	mat.tz = -zaxis.dot(eye);

	return mat;
}
Example #5
0
Matrix4& Camera::getMatrix() {

	Vector3 z = Vector3();
	z.sub( e, d );
	z.normalize();
	
	Vector3 x = Vector3();
	x.cross( up, z );
	x.normalize();
	
	Vector3 y = Vector3();
	y.cross( z, x );
	
	camera.set(0, 0, x.getX() );
	camera.set(0, 1, x.getY() );
	camera.set(0, 2, x.getZ() );
	
	camera.set(1, 0, y.getX() );
	camera.set(1, 1, y.getY() );
	camera.set(1, 2, y.getZ() );
	
	camera.set(2, 0, z.getX() );
	camera.set(2, 1, z.getY() );
	camera.set(2, 2, z.getZ() );
	
	camera.set(3, 3, 1 );
	
	return camera;
}
void HermiteSpline::calculateFrenetFrame(float u, HermiteSplineControlPoint* c0, HermiteSplineControlPoint* c1, Vector3 &tangent, Vector3 &binormal, Vector3 &normal){
	//********************************************************************************
	//hier wird der Frenet-Frame berechnet und in den �bergebenen Funktionsargumenten gespeichert
	//Sie brauchen noch die zweite Ableitung einer Bezier-Kurve...

	vector<Vector3> bez;

	bez.push_back(c0->getPosition());
	bez.push_back(c0->getPosition() + ONE_THIRD * c0->getDirection());
	bez.push_back(c1->getPosition() - ONE_THIRD * c1->getDirection());
	bez.push_back(c1->getPosition());
	
	Vector3 point;

	deCasteljau(u, bez, point, tangent);

	Vector3 secDer =   (6.0f - 6.0f * u) * bez[0] +
					 (18.0f * u - 12.0f) * bez[1] +
					  (6.0f - 18.0f * u) * bez[2] +
								6.0f * u * bez[3];

	binormal = secDer.cross(tangent);
	binormal.normalize();

	normal = binormal.cross(tangent);

}
Example #7
0
ZBWidget::Matrix4 ZBWidget::lookAt(float eye_x, float eye_y, float eye_z,
                                   float center_x, float center_y, float center_z,
                                   float up_x, float up_y, float up_z)
{
  Vector3 eye(eye_x, eye_y, eye_z);
  Vector3 center(center_x, center_y, center_z);
  Vector3 up(up_x, up_y, up_z);
  Vector3 f = (center-eye).normalized();
  Vector3 s = f.cross(up).normalized();
  Vector3 u = s.cross(f).normalized();

  Matrix4 result(Matrix4::Identity());
  result(0, 0) = s.x();
  result(0, 1) = s.y();
  result(0, 2) = s.z();
  result(1, 0) = u.x();
  result(1, 1) = u.y();
  result(1, 2) = u.z();
  result(2, 0) = -f.x();
  result(2, 1) = -f.y();
  result(2, 2) = -f.z();
  result(0, 3) = -s.dot(eye);
  result(1, 3) = -u.dot(eye);
  result(2, 3) = f.dot(eye);
  return result;
}
Example #8
0
void Matrix44::setUpAndOrthonormalize(Vector3 up)
{
	up.normalize();

	//put the up vector in the matrix
	m[4] = up.x;
	m[5] = up.y;
	m[6] = up.z;

	//orthonormalize
	Vector3 right,front;
	right = rightVector();

	if ( abs(right.dot( up )) < 0.99998 )
	{
		right = up.cross( frontVector() );
		front = right.cross( up );
	}
	else
	{
		front = Vector3(right).cross( up );
		right = up.cross( front );
	}

	right.normalize();
	front.normalize();

	m[8] = front.x;
	m[9] = front.y;
	m[10] = front.z;

	m[0] = right.x;
	m[1] = right.y;
	m[2] = right.z;
}
Vector3 UIGeom::segmentNormal(const LineSegment& seg, const Vector3& eye) {
    Vector3 E = eye - seg.point(0);
    Vector3 V = seg.point(1) - seg.point(0);
    Vector3 U = E.cross(V);
    Vector3 N = V.cross(U).direction();
    return N;
}
Example #10
0
void Frustum::prepare()
{
	//the camera coordinate system
	Vector3 zaxis = camera->parent->location - camera->parent->front;
	zaxis.normalize();
	Vector3 xaxis = camera->parent->up.cross(zaxis);
	xaxis.normalize();
	Vector3 yaxis = zaxis.cross(xaxis);
	yaxis.normalize();

	//centers
	Point nc = camera->parent->location - zaxis * nearp;
	Point fc = camera->parent->location - zaxis * farp;

	nearplane.init(nc,zaxis);
	farplane.init(fc,zaxis * -1);

	Vector3 aux,normal;

	(aux = (nc + yaxis * nh) - camera->parent->location).normalize();
	normal = aux.cross(xaxis);
	top.init(nc + yaxis * nh,normal);

	(aux = (nc - yaxis * nh) - camera->parent->location).normalize();
	normal = xaxis.cross(aux);
	bottom.init(nc - yaxis * nh,normal);
	
	(aux = (nc - xaxis * nw) - camera->parent->location).normalize();
	normal = aux.cross(yaxis);
	left.init(nc - xaxis * nw,normal);

	(aux = (nc + xaxis * nw) - camera->parent->location).normalize();
	normal = yaxis.cross(aux);
	right.init(nc + xaxis * nw,normal);
}
Example #11
0
Vector3 Vector3::cosRandom(const Vector3& normal)
{
    double e1 = G3D::random(0, 1);
    double e2 = G3D::random(0, 1);

    // Angle from normal
    double theta = acos(sqrt(e1));

    // Angle about normal
    double phi   = 2 * G3D_PI * e2;

    // Make a coordinate system
    Vector3 U = normal.direction();
    Vector3 V = Vector3::unitX();

    if (abs(U.dot(V)) > .9)
    {
        V = Vector3::unitY();
    }

    Vector3 W = U.cross(V).direction();
    V = W.cross(U);

    // Convert to rectangular form
    return cos(theta) * U + sin(theta) * (cos(phi) * V + sin(phi) * W);
}
Example #12
0
//Needs some debugging to figure out why the camera isn't
//changing based on this function
void Camera::moveCam(CamMoveDir dir){
	switch(dir){
		case dirForward:{
			Vector3* dif = camTarg->subtract(camPos);
			dif->normalize();
			dif->multiply(CAMERA_MOVE_SPEED);
			camPos->add(dif);				
		} break;
		case dirBack:{
			Vector3* dif = camTarg->subtract(camPos);
			dif->normalize();
			dif->multiply(CAMERA_MOVE_SPEED);
			camPos->subtract(dif);				
		} break;
		case dirLeft:{
			Vector3* dif = camTarg->subtract(camPos);
			dif->multiply(-1);
			dif->cross(camUP);
			dif->normalize();
			dif->multiply(CAMERA_MOVE_SPEED);
			camPos->add(dif);				
		} break;
		case dirRight:{
			Vector3* dif = camTarg->subtract(camPos);
			dif->cross(camUP);
			dif->normalize();
			dif->multiply(CAMERA_MOVE_SPEED);
			camPos->add(dif);
		} break;
	}
}
Example #13
0
	Matrix4 lookAt(const Vector3& fromPosition, const Vector3& toPosition, const Vector3& upDirection)
	{
		const Vector3 zAxis = (fromPosition - toPosition).normal();	// Forward Vector
		const Vector3 xAxis = upDirection.cross(zAxis).normal();	// Right Vector
		const Vector3 yAxis = zAxis.cross(xAxis);					// Up Vector

		//optimized
		Matrix4 viewMatrix
		(
			Vector4( xAxis.x,				   yAxis.x,					 zAxis.x,				   0),
			Vector4( xAxis.y,				   yAxis.y,					 zAxis.y,				   0),
			Vector4( xAxis.z,			       yAxis.z,					 zAxis.z,				   0),
			Vector4( -xAxis.dot(fromPosition), -yAxis.dot(fromPosition), -zAxis.dot(fromPosition), 1)
		); 

		return viewMatrix;
		/*Matrix4 orientation 
		(
			Vector4( xAxis.x, yAxis.x, zAxis.x, 0),
			Vector4( xAxis.y, yAxis.y, zAxis.y, 0),
			Vector4( xAxis.z, yAxis.z, zAxis.z, 0),
			Vector4( 0,		  0,	   0,		1)
		);
		
		Matrix4 translation
		(
			Vector4(			  1,			   0,				0,	    0),
			Vector4(			  0,			   1,				0,		0),
			Vector4(			  0,			   0,			    1,		0),
			Vector4(-fromPosition.x, -fromPosition.y, -fromPosition.z,		1)
		);

		return (orientation * translation);*/	
	}
Example #14
0
 Matrix4 Matrix4::MakeLookIn(const Vector3 &pos, const Vector3 &dir, const Vector3 &up) {
   Vector3 x = dir.cross(up).normalize();
   Vector3 y = x.cross(dir).normalize();
   Matrix4 r;
   r.setRow(0, Vector4(x, -x.dot(pos)));
   r.setRow(1, Vector4(y, -y.dot(pos)));
   r.setRow(2, Vector4(-dir, dir.dot(pos)));
   return r;
 }
Example #15
0
HingeJointSW::HingeJointSW(BodySW *rbA, BodySW *rbB, const Vector3 &pivotInA, const Vector3 &pivotInB,
		const Vector3 &axisInA, const Vector3 &axisInB) :
		JointSW(_arr, 2) {

	A = rbA;
	B = rbB;

	m_rbAFrame.origin = pivotInA;

	// since no frame is given, assume this to be zero angle and just pick rb transform axis
	Vector3 rbAxisA1 = rbA->get_transform().basis.get_axis(0);

	Vector3 rbAxisA2;
	real_t projection = axisInA.dot(rbAxisA1);
	if (projection >= 1.0f - CMP_EPSILON) {
		rbAxisA1 = -rbA->get_transform().basis.get_axis(2);
		rbAxisA2 = rbA->get_transform().basis.get_axis(1);
	} else if (projection <= -1.0f + CMP_EPSILON) {
		rbAxisA1 = rbA->get_transform().basis.get_axis(2);
		rbAxisA2 = rbA->get_transform().basis.get_axis(1);
	} else {
		rbAxisA2 = axisInA.cross(rbAxisA1);
		rbAxisA1 = rbAxisA2.cross(axisInA);
	}

	m_rbAFrame.basis = Matrix3(rbAxisA1.x, rbAxisA2.x, axisInA.x,
			rbAxisA1.y, rbAxisA2.y, axisInA.y,
			rbAxisA1.z, rbAxisA2.z, axisInA.z);

	Quat rotationArc = Quat(axisInA, axisInB);
	Vector3 rbAxisB1 = rotationArc.xform(rbAxisA1);
	Vector3 rbAxisB2 = axisInB.cross(rbAxisB1);

	m_rbBFrame.origin = pivotInB;
	m_rbBFrame.basis = Matrix3(rbAxisB1.x, rbAxisB2.x, -axisInB.x,
			rbAxisB1.y, rbAxisB2.y, -axisInB.y,
			rbAxisB1.z, rbAxisB2.z, -axisInB.z);

	//start with free
	m_lowerLimit = Math_PI;
	m_upperLimit = -Math_PI;

	m_useLimit = false;
	m_biasFactor = 0.3f;
	m_relaxationFactor = 1.0f;
	m_limitSoftness = 0.9f;
	m_solveLimit = false;

	tau = 0.3;

	m_angularOnly = false;
	m_enableAngularMotor = false;

	A->add_constraint(this, 0);
	B->add_constraint(this, 1);
}
Example #16
0
Affine3 SgCamera::positionLookingFor(const Vector3& eye, const Vector3& direction, const Vector3& up)
{
    Vector3 d = direction.normalized();
    Vector3 c = d.cross(up).normalized();
    Vector3 u = c.cross(d);
    Affine3 C;
    C.linear() << c, u, -d;
    C.translation() = eye;
    return C;
}
Example #17
0
void Camera::createONB(const Vector3& a, const Vector3& b)
{

   // add code to this function
   // Calculate the correspoing w u v vector
	basis.w = (a/a.norm())*-1;
	basis.u = b.cross(basis.w) / (b.cross(basis.w).norm());
	basis.v = basis.w.cross(basis.u);
	
}
void GeometryTerrain::computeNormalsArea(int xs, int zs) {

	/*Vector3** normals = new Vector3*[GetLength()];

	for(int i = 0; i < GetLength(); i++) 
	{
	normals[i] = new Vector3[GetWidth()];
	}
	*/

	for(unsigned int z = zs; z-10 < zs+10; ++z) 
	{
		for(unsigned int x = xs-10; x < xs+10; ++x) 
		{
			Vector3 sum(0.0f, 0.0f, 0.0f);

			Vector3 out;
			if (z > 0) {
				out = Vector3(0.0f, m_pHeight[z - 1][x] - m_pHeight[z][x], -1.0f);
			}
			Vector3 in;
			if (z < GetLength() - 1) {
				in = Vector3(0.0f, m_pHeight[z + 1][x] - m_pHeight[z][x], 1.0f);
			}
			Vector3 left;
			if (x > 0) {
				left = Vector3(-1.0f, m_pHeight[z][x - 1] - m_pHeight[z][x], 0.0f);
			}
			Vector3 right;
			if (x < GetWidth() - 1) {
				right = Vector3(1.0f, m_pHeight[z][x + 1] - m_pHeight[z][x], 0.0f);
			}


			if (x > 0 && z > 0) {
				sum += out.cross(left);
			}
			if (x > 0 && z < GetLength() - 1) {
				sum += left.cross(in);
			}
			if (x < GetWidth() - 1 && z < GetLength() - 1) {
				sum += in.cross(right);
			}
			if (x < GetWidth() - 1 && z > 0) {
				sum += right.cross(out);
			}


			m_pNormals[x+z * GetWidth()].x = sum.x;
			m_pNormals[x+z * GetWidth()].y = sum.y;
			m_pNormals[x+z * GetWidth()].z = sum.z;
		}
	}
	//NormalBuffer.setElementList(m_pNormals, m_nVertexCount);
}
Example #19
0
void Camera::lookAt(const Vector3 &at,const Vector3 &up) {
    Vector3 k = at - _position;
    k.normalize();
    Vector3 i;
    i.cross(k, up);
    i.normalize();
    Vector3 j;
    j.cross(k, i);
    j.normalize();
    _orientation.set(i,j,k);
}
Example #20
0
//Needs some debugging to figure out why the camera isn't
//changing based on this function
void Camera::moveCam(CamMoveDir dir){
	switch(dir){
		case dirForward:{
			Vector3* dif = camTarg->subtract(camPos);
			dif = dif->normalize();
			dif = dif->multiply(CAMERA_MOVE_SPEED);
			camPos = camPos->add(dif);				
		} break;
		case dirBack:{
			Vector3* dif = camTarg->subtract(camPos);
			dif = dif->normalize();
			dif = dif->multiply(CAMERA_MOVE_SPEED);
			camPos = camPos->subtract(dif);				
		} break;
		case dirLeft:{
			//printf("\n[case dirLeft in moveCam]");
			//printf("\ncamPos:");
			//camPos->print();
			//printf("\ncamTarg:");
			//camTarg->print();
			Vector3* dif = camTarg;
			//printf("\ndif:");
			//dif->print();
			dif = dif->subtract(camPos);
			dif = dif->multiply(-1);
			//printf("\ndif, after multiply by -1:");
			//dif->print();
			dif = dif->cross(camUP);
			//printf("\ndif, after cross(camUP):");
			//dif->print();
			dif = dif->normalize();
			//printf("\ndif, after normalize:");
			//dif->print();
			dif = dif->multiply(CAMERA_MOVE_SPEED);
			//print for debug:
			//printf("\ndif, after multiply(CAMERA_MOVE_SPEED):");
			//dif->print();
			camPos = camPos->add(dif);
			//test add stuff to camPos:
			//Vector3* stuff;
			//stuff = new Vector3(2.0f, 0.0f, 0.0f);
			//camPos = camPos->add(stuff);
			//printf("\ncamPos, after add(dif):");
			//camPos->print();
		} break;
		case dirRight:{
			Vector3* dif = camTarg->subtract(camPos);
			dif = dif->cross(camUP);
			dif = dif->normalize();
			dif = dif->multiply(CAMERA_MOVE_SPEED);
			camPos = camPos->add(dif);
		} break;
	}
}
Example #21
0
void Cylinder::getReferenceFrame(CoordinateFrame& cframe) const {
    cframe.translation = center();

    Vector3 Y = (p1 - p2).direction();
    Vector3 X = (abs(Y.dot(Vector3::unitX())) > 0.9) ? Vector3::unitY() : Vector3::unitX();
    Vector3 Z = X.cross(Y).direction();
    X = Y.cross(Z);        
    cframe.rotation.setColumn(0, X);
    cframe.rotation.setColumn(1, Y);
    cframe.rotation.setColumn(2, Z);
}
Example #22
0
 Matrix4 Matrix4::MakeLookAt(const Vector3 &p, const Vector3 &a, const Vector3 &u) {
   Vector3 z = (p - a).normalize();
   Vector3 x = u.cross(z).normalize();
   Vector3 y = z.cross(x).normalize();
   Matrix4 r;
   r.setRow(0, Vector4(x, 0));
   r.setRow(1, Vector4(y, 0));
   r.setRow(2, Vector4(z, 0));
   Matrix4 t = MakeTranslate(-p);
   return (r * t);
 }
Example #23
0
Matrix4 Camera::buildMatrixLookAt(Vector3 eye, Vector3 target, Vector3 up)
{
	const Vector3 zAxis = (eye-target).normalize();
	const Vector3 xAxis = up.cross(zAxis).normalize();
	const Vector3 yAxis = zAxis.cross(xAxis);

	return Matrix4(xAxis.x, xAxis.y, xAxis.z, -xAxis.dot(eye),
				   yAxis.x, yAxis.y, yAxis.z, -yAxis.dot(eye),
				   zAxis.x, zAxis.y, zAxis.z, -zAxis.dot(eye),
				   0,		0,		 0,		   1).transpose();
}
Example #24
0
void Entity::lookAt(const Vector3& target, const Vector3& up) {
    // modified Mesa 9.0 glu implementation
    Quaternion result;
    Vector3 vFwd = (target - m_positionAbs).normalized();
    Vector3 vSide = vFwd.cross(up).normalized();
    Vector3 vUp = vSide.cross(vFwd);
    Matrix3x3 m(vSide.getX(), vUp.getX(), -vFwd.getX(),
                vSide.getY(), vUp.getY(), -vFwd.getY(),
                vSide.getZ(), vUp.getZ(), -vFwd.getZ());
    result = m.getRotationQuat();
    setOrientationAbs(result);
}
Example #25
0
Vector3 Quaternion::rotate(Vector3 rhs)
{
    //nVidia SDK implementation
    Vector3 quatVec = Vector3(x, y, z);
    Vector3 cross1 = quatVec.cross(rhs);
    Vector3 cross2 = quatVec.cross(cross1);

    cross1 *= (2.0f * w);
    cross2 *= 2.0f;

    return rhs + cross1 + cross2;
}
Example #26
0
	int Object::createPlane( Material* mat , float w , float h , float* color , float xTile , float yTile , 
		                       Vector3 const& normal , Vector3 const& alignDir , Vector3 const& offset , bool invYDir )
	{
		Vector3 yLen =  normal.cross( alignDir );
		assert( yLen.length2() > 1e-4 );

		yLen.normalize();
		Vector3 xLen = yLen.cross( normal );

		xLen *= 0.5f * w;
		yLen *= 0.5f * h;

		if ( invYDir )
			yLen = -yLen;

		Vector3 n = normal;
		n.normalize();

		int texLen = 2;
		VertexType type = ( color ) ? CFVT_XYZ_N_CF1 : CFVT_XYZ_N;
		//VertexType type = ( color ) ? CFVT_XYZ_CF1 : CFVT_XYZ;

		MeshBuilder builder = MeshBuilder( type | CFVF_TEX1( 2 ) );

		if ( color )
			builder.setColor( color );

		builder.setNormal( n );

		builder.reserveVexterBuffer( 4 );
		builder.reserveIndexBuffer( 12 );

		builder.setPosition( offset - xLen - yLen );
		builder.setTexCoord( 0 , 0 , 0 );
		builder.addVertex();

		builder.setPosition( offset + xLen - yLen );
		builder.setTexCoord( 0 ,xTile, 0 );
		builder.addVertex();

		builder.setPosition( offset + xLen + yLen );
		builder.setTexCoord( 0 , xTile , yTile );
		builder.addVertex();

		builder.setPosition( offset - xLen + yLen );
		builder.setTexCoord( 0 , 0 , yTile );
		builder.addVertex();

		builder.addQuad( 0 , 1 , 2 , 3 );

		return builder.createIndexTrangle( this , mat );
	}
Example #27
0
bool
lineIntersectsTriangle(Line3 const & line, Vector3 const & v0, Vector3 const & edge01, Vector3 const & edge02, Real * time)
{
  // The code is taken from the ray-triangle intersection test in Dave Eberly's Wild Magic library, v5.3, released under the
  // Boost license: http://www.boost.org/LICENSE_1_0.txt .

  static Real const EPS = 1e-30f;

  Vector3 diff = line.getPoint() - v0;
  Vector3 normal = edge01.cross(edge02);

  // Solve Q + t*D = b1*E1 + b2*E2 (Q = diff, D = line direction, E1 = edge01, E2 = edge02, N = Cross(E1,E2)) by
  //   |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
  //   |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
  //   |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)

  Real DdN = line.getDirection().dot(normal);
  int sign;
  if (DdN > EPS)
    sign = 1;
  else if (DdN < -EPS)
  {
    sign = -1;
    DdN = -DdN;
  }
  else
  {
    // Line and triangle are parallel, call it a "no intersection" even if the line does intersect
    return false;
  }

  Real DdQxE2 = sign * line.getDirection().dot(diff.cross(edge02));
  if (DdQxE2 >= 0)
  {
    Real DdE1xQ = sign * line.getDirection().dot(edge01.cross(diff));
    if (DdE1xQ >= 0)
    {
      if (DdQxE2 + DdE1xQ <= DdN)
      {
        // Line intersects triangle
        Real QdN = -sign * diff.dot(normal);
        if (time) *time = QdN / DdN;
        return true;
      }
      // else: b1 + b2 > 1, no intersection
    }
    // else: b2 < 0, no intersection
  }
  // else: b1 < 0, no intersection

  return false;
}
Example #28
0
inline Vector3 pointOnHemisphere(const Vector3& n, float theta, float phi)
{
	Vector3 ref = (fabs(n.x) < 0.5f) ?
		Vector3(1.0f, 0.0f, 0.0f) : Vector3(0.0f, 1.0f, 0.0f);

	Vector3 u = n.cross(ref).normalize();
	Vector3 v = u.cross(n);

	return
		u * sinf(theta) * cosf(phi) +
		v * sinf(theta) * sinf(phi) +
		n * cosf(theta);
}
Example #29
0
    void SpriteBatcher3D::pushSprite(const Vector3 position, const Vector3 &normal, const Vector2 &size, const TextureRegion *region)
  {
    //画像の上方向を表すベクトル(これがそのまま上方向になる訳ではない)
    Vector3 up;
    //通常はy軸が画像の上, 画像が上か下を向いている時はz軸が画像の上
    if( normal.x == 0 && normal.z == 0)
      up.set(0,0,1);
    else
      up.set(0,1,0);  

    //X軸
    Vector3 axis1 = up.cross(normal);
    axis1.normalize();

    //Y軸
    Vector3 axis2 = normal.cross(axis1);
    axis2.normalize();

    axis1 *= size.x/2;
    axis2 *= size.y/2;
    const Vector3 leftBottom  = position - axis1 - axis2;
    const Vector3 rightBottom = position + axis1 - axis2;
    const Vector3 rightTop    = position + axis1 + axis2;
    const Vector3 leftTop     = position - axis1 + axis2;

    verticesBuffer[bufferIndex++] = leftBottom.x;
    verticesBuffer[bufferIndex++] = leftBottom.y;
    verticesBuffer[bufferIndex++] = leftBottom.z;
    verticesBuffer[bufferIndex++] = region->u1;
    verticesBuffer[bufferIndex++] = region->v2;

    verticesBuffer[bufferIndex++] = rightBottom.x;
    verticesBuffer[bufferIndex++] = rightBottom.y;
    verticesBuffer[bufferIndex++] = rightBottom.z;
    verticesBuffer[bufferIndex++] = region->u2;
    verticesBuffer[bufferIndex++] = region->v2;

    verticesBuffer[bufferIndex++] = rightTop.x;
    verticesBuffer[bufferIndex++] = rightTop.y;
    verticesBuffer[bufferIndex++] = rightTop.z;
    verticesBuffer[bufferIndex++] = region->u2;
    verticesBuffer[bufferIndex++] = region->v1;

    verticesBuffer[bufferIndex++] = leftTop.x;
    verticesBuffer[bufferIndex++] = leftTop.y;
    verticesBuffer[bufferIndex++] = leftTop.z;  
    verticesBuffer[bufferIndex++] = region->u1;
    verticesBuffer[bufferIndex++] = region->v1;
    numSprite++;  
  }
Example #30
0
Hako::Math::Matrix4 Hako::Math::Matrix4::make_lookat(const Vector3 eye, const Vector3 target, const Vector3 up)
{
	Vector3 zaxis = (eye - target).get_normalized();
	Vector3 xaxis = up.cross(zaxis).get_normalized();
	Vector3 yaxis = zaxis.cross(xaxis);

	const float cells[4][4] = {
		{ xaxis.x, yaxis.x, zaxis.x, 0 },
		{ xaxis.y, yaxis.y, zaxis.y, 0 },
		{ xaxis.z, yaxis.z, zaxis.z, 0 },
		{ -xaxis.dot(eye), -yaxis.dot(eye), -zaxis.dot(eye), 1 }
	};

	return Matrix4::make_array(cells);
}