Beispiel #1
0
void Player::DoInput() {
	static double minPitch = -PI_2;
	static double maxPitch =  PI_2;

	yaw += Input::DeltaMx() / 300.0;
	pitch -= Input::DeltaMy() / 300.0 * (Config::Controls::InvertMouse ? -1 : 1);

	if (pitch < minPitch) {
		pitch = minPitch;
	}
	if (pitch > maxPitch) {
		pitch = maxPitch;
	}

	dir = Vector3D(sin(yaw) * cos(pitch), cos(yaw) * cos(pitch), sin(pitch)).Normalize();

	Vector3D kdir = Vector3D(sin(yaw), cos(yaw), 0).Normalize();

	Vector3D right = kdir.Cross(Vector3D::AxisZ).Normalize();

	cameraUp = right.Cross(dir);

	kvec = kdir * (Input::KeyPressed(Config::Key::Forward) - Input::KeyPressed(Config::Key::Backward))
		   + right * (Input::KeyPressed(Config::Key::Right) - Input::KeyPressed(Config::Key::Left));

	if (noclip) {
		kvec += Vector3D::AxisZ * ((Input::KeyPressed(Config::Key::Up) || Input::KeyPressed(Config::Key::Jump)) - Input::KeyPressed(Config::Key::Down));
	}

	kvec = kvec.Normalize(1.0 / (Input::KeyPressed(Config::Key::Walk) ? (noclip ? 300.0 : 200.0) : (Input::KeyPressed(Config::Key::Run) ? (noclip ? 5.0 : 25.0) : (noclip ? 35.0 : 50.0))));
}
Beispiel #2
0
bool Triangle2::rayIntersect(const Vector3D &origin, const Vector3D &direction, float &u, float &v, Vector3D &intersect)
{
    // TODO: Make it work for polygon (tri/quad).
    
    Vector3D v0 = vertex(0)->data().position;
    Vector3D v1 = vertex(1)->data().position;
    Vector3D v2 = vertex(2)->data().position;
    
    Vector3D e1 = v1 - v0;
    Vector3D e2 = v2 - v0;
    Vector3D p = direction.Cross(e2);
    float a = e1.Dot(p);
    
    if (fabsf(a) < FLOAT_EPS)
        return false;
    
    float f = 1.0f / a;
    
    Vector3D s = origin - v0;
    u = f * s.Dot(p);
    if (u < 0.0f || u > 1.0f)
        return false;
    
    Vector3D q = s.Cross(e1);
    v = f * direction.Dot(q);
    if (v < 0.0f || u + v > 1.0f)
        return false;
    
    float t = f * e2.Dot(q);
    if (t >= 0.0f)
    {
        intersect = v0 + e1 * u + e2 * v;
        return true;
    }
    return false;
}
void SphericEmitter::computeMatrix()
{
	tDirection.Normalize();
	if( (tDirection.x == 0.0f) && (tDirection.y == 0.0f) )
	{
			
		matrix[0] = tDirection.z;
		matrix[1] = 0.0f;
		matrix[2] = 0.0f;
		matrix[3] = 0.0f;
		matrix[4] = tDirection.z;
		matrix[5] = 0.0f;
		matrix[6] = 0.0f;
		matrix[7] = 0.0f;
		matrix[8] = tDirection.z;
	}
	else
	{
		Vector3D axis;
		axis.Cross( tDirection, Vector3D(0.0f,0.0f,1.0f) );

		float cosA = tDirection.z;
		float sinA = -axis.GetLength();
		axis /= -sinA;

		float x = axis.x;
		float y = axis.y;
		float z = axis.z;

		matrix[0] = x * x + cosA * (1.0f - x * x);
		matrix[1] = x * y * (1.0f - cosA) - z * sinA;
		matrix[2] = tDirection.x;
		matrix[3] = x * y * (1.0f - cosA) + z * sinA;
		matrix[4] = y * y + cosA * (1.0f - y * y);
		matrix[5] = tDirection.y;
		matrix[6] = x * z * (1.0f - cosA) - y * sinA;
		matrix[7] = y * z * (1.0f - cosA) + x * sinA;
		matrix[8] = tDirection.z;
	}
}
Beispiel #4
0
//
//#############################################################################
//#############################################################################
//
UnitQuaternion&
	UnitQuaternion::Subtract(
		const UnitVector3D &end,
		const UnitVector3D &start
	)
{
	Check_Pointer(this);
	Check_Object(&start);
	Check_Object(&end);

	Vector3D
		axis;
	SinCosPair
		delta;
	delta.cosine = start*end;

	//
	//----------------------------------------------------------------------
	// See if the vectors point in the same direction.  If so, return a null
	// rotation
	//----------------------------------------------------------------------
	//
	if (Close_Enough(delta.cosine, 1.0f))
	{
		x = 0.0f;
		y = 0.0f;
		z = 0.0f;
		w = 1.0f;
	}

	//
	//-------------------------------------------------------------------------
	// See if the vectors directly oppose each other.  If so, pick the smallest
	// axis coordinate and generate a vector along it.  Project this onto the
	// base vector and subtract it out, leaving a perpendicular projection.
	// Extend that out to unit length, then set the angle to PI
	//-------------------------------------------------------------------------
	//
	else if (Close_Enough(delta.cosine, -1.0f))
	{
		//
		//---------------------------
		// Pick out the smallest axis
		//---------------------------
		//
		int
			smallest=0;
		Scalar
			value=2.0f;
		for (int i=X_Axis; i<=Z_Axis; ++i)
		{
			if (Abs(start[i]) < value)
			{
				smallest = i;
				value = Abs(start[i]);
			}
		}

		//
		//----------------------------------------
		// Set up a vector along the selected axis
		//----------------------------------------
		//
		axis.x = 0.0f;
		axis.y = 0.0f;
		axis.z = 0.0f;
		axis[smallest] = 1.0f;

		//
		//-------------------------------------------------------------------
		// If the value on that axis wasn't zero, subtract out the projection
		//-------------------------------------------------------------------
		//
		if (!Small_Enough(value))
		{
			Vector3D t;
			t.Multiply(start, start*axis);
			axis.Subtract(axis, t);
			axis.Normalize(axis);
		}

		//
		//----------------------
		// Convert to quaternion
		//----------------------
		//
		x = axis.x;
		y = axis.y;
		z = axis.z;
		w = 0.0f;
	}

	//
	//--------------------------------------------------
	// Otherwise, generate the cross product and unitize
	//--------------------------------------------------
	//
	else
	{
		axis.Cross(start, end);
		delta.sine = axis.GetLength();
		axis /= delta.sine;

		//
		//---------------------------------------------------------------
		// Now compute sine and cosine of half the angle and generate the
		// quaternion
		//---------------------------------------------------------------
		//
		delta.sine = Sqrt((1.0f - delta.cosine)*0.5f);
		x = axis.x * delta.sine;
		y = axis.y * delta.sine;
		z = axis.z * delta.sine;
		w = Sqrt((1.0f + delta.cosine)*0.5f);
	}
	return *this;
}
inline bool IsLeft(const Point3D& pt, const Point3D& lp, const Vector3D v, const Vector3D& pn)
{
	return v.Cross(pt - lp) * pn > 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	LinearMatrix4D::AlignLocalAxisToWorldVector(
		const Vector3D &world_target,
		int pointing_axis,
		int rotating_axis,
		int minor_axis
	)
{
	Check_Object(this);
	Check_Object(&world_target);
	Verify(static_cast<unsigned>(pointing_axis) <= Z_Axis);
	Verify(static_cast<unsigned>(rotating_axis) <= Z_Axis);
	Verify(rotating_axis != pointing_axis);

	//
	//------------------------------------------------------------------
	// These are the variables that the alignment algorithm must fill in
	//------------------------------------------------------------------
	//
	UnitVector3D
		rotation_vector,
		pointing_vector,
		minor_vector;

	//
	//------------------------------------------------------------------
	// Extract the current target axis direction, then cross it with the
	// plane target to find the minor axis direction (unsigned)
	//------------------------------------------------------------------
	//
	if (Small_Enough(world_target.GetLengthSquared()))
		return;
	rotation_vector.x = (*this)(rotating_axis, X_Axis);
	rotation_vector.y = (*this)(rotating_axis, Y_Axis);
	rotation_vector.z = (*this)(rotating_axis, Z_Axis);
	Check_Object(&rotation_vector);
	Vector3D temp;
	temp.Cross(rotation_vector, world_target);

	//
	//----------------------------------------------------------------------
	// First check to see if we are rotating around a frozen axis.  If so,
	// if the axes specified are in the right-handed configuration, simply
	// generate the new pointing axis values, otherwise negate the minor
	// axis and generate the pointing vector appropriately
	//----------------------------------------------------------------------
	//
	if (minor_axis == -1)
	{
		minor_axis = 3 - pointing_axis - rotating_axis;
		minor_vector.Normalize(temp);
		if ((rotating_axis+1)%3 == pointing_axis)
			pointing_vector.Vector3D::Cross(minor_vector, rotation_vector);
		else
		{
			minor_vector.Vector3D::Negate(minor_vector);
			pointing_vector.Vector3D::Cross(rotation_vector, minor_vector);
		}
		Check_Object(&pointing_vector);
	}

	//
	//------------------------------------------------------------------------
	// The next case to check is non-frozen rotation.  In this case, maximum
	// effort is taken to preserve the rotating matrix, but it will be rotated
	// around the minor axis so that the pointing axis is exactly aligned with
	// the target vector
	//------------------------------------------------------------------------
	//
	else
	{

		//
		//--------------------------------------------------------------------
		// If the resultant vector is zero, it means the rotating axis is
		// parallel to the target vector, and thus a correct orthogonal set of
		// axis vectors can already be found in the matrix
		//--------------------------------------------------------------------
		//
		Verify(minor_axis == 3 - pointing_axis - rotating_axis);
		if (Small_Enough(temp.GetLengthSquared()))
		{
			if (world_target*rotation_vector > 0.0f)
			{
				pointing_vector.x = (*this)(rotating_axis, X_Axis);
				pointing_vector.y = (*this)(rotating_axis, Y_Axis);
				pointing_vector.z = (*this)(rotating_axis, Z_Axis);
				rotation_vector.x = -(*this)(pointing_axis, X_Axis);
				rotation_vector.y = -(*this)(pointing_axis, Y_Axis);
				rotation_vector.z = -(*this)(pointing_axis, Z_Axis);
			}
			else
			{
				pointing_vector.x = -(*this)(rotating_axis, X_Axis);
				pointing_vector.y = -(*this)(rotating_axis, Y_Axis);
				pointing_vector.z = -(*this)(rotating_axis, Z_Axis);
				rotation_vector.x = (*this)(pointing_axis, X_Axis);
				rotation_vector.y = (*this)(pointing_axis, Y_Axis);
				rotation_vector.z = (*this)(pointing_axis, Z_Axis);
			}
			minor_vector.x = (*this)(minor_axis, X_Axis);
			minor_vector.y = (*this)(minor_axis, Y_Axis);
			minor_vector.z = (*this)(minor_axis, Z_Axis);
		}

		//
		//---------------------------------------------------------------------
		// We have a non-trivial minor vector, so use it to generate the real
		// minor axis, then calculate the new rotation axis.  If the axes
		// specified are in the right-handed configuration, simply generate the
		// new pointing axis values, otherwise negate the minor axis and
		// generate the pointing vector appropriately
		//---------------------------------------------------------------------
		//
		else
		{
			pointing_vector.Normalize(world_target);
			minor_vector.Normalize(temp);
			if ((rotating_axis+1)%3 == pointing_axis)
				rotation_vector.Vector3D::Cross(pointing_vector, minor_vector);
			else
			{
				minor_vector.Vector3D::Negate(minor_vector);
				rotation_vector.Vector3D::Cross(minor_vector, pointing_vector);
			}
			Check_Object(&rotation_vector);
		}
	}

	//
	//------------------------------------------------
	// Now stuff the unit vectors back into the matrix
	//------------------------------------------------
	//
	Check_Object(&pointing_vector);
	(*this)(pointing_axis, X_Axis) = pointing_vector.x;
	(*this)(pointing_axis, Y_Axis) = pointing_vector.y;
	(*this)(pointing_axis, Z_Axis) = pointing_vector.z;

	Check_Object(&rotation_vector);
	(*this)(rotating_axis, X_Axis) = rotation_vector.x;
	(*this)(rotating_axis, Y_Axis) = rotation_vector.y;
	(*this)(rotating_axis, Z_Axis) = rotation_vector.z;

	Check_Object(&minor_vector);
	(*this)(minor_axis, X_Axis) = minor_vector.x;
	(*this)(minor_axis, Y_Axis) = minor_vector.y;
	(*this)(minor_axis, Z_Axis) = minor_vector.z;

	Check_Object(this);
}
void main()
{
	printf_s("\nDefault Constructor");
	printf_s("\nthisVector -> ");
	Vector3D vectorDefaultConstructor = Vector3D();
	vectorDefaultConstructor.Print();

	printf_s("\nConstructor with floats");
	printf_s("\nthisVector -> ");
	Vector3D thisVector = Vector3D(1, 2, 3);
	thisVector.Print();

	printf_s("\nConstructor with floats");
	printf_s("\notherVector -> ");
	Vector3D otherVector = Vector3D(2, 3, 4);
	otherVector.Print();

	printf_s("\nOperator +=");
	printf_s("\nthisVector += otherVector -> ");
	thisVector += otherVector;
	thisVector.Print();

	printf_s("\nOperator -=");
	printf_s("\nthisVector -= otherVector -> ");
	thisVector -= otherVector;
	thisVector.Print();

	printf_s("\nReset thisVector");
	printf_s("\nthisVector -> ");
	thisVector = Vector3D(1, 2, 3);
	thisVector.Print();
	printf_s("otherVector -> ");
	otherVector.Print();

	printf_s("\nOperator *=");
	printf_s("\nthisVector *= 3 -> ");
	thisVector *= 3;
	thisVector.Print();

	printf_s("\nOperator /=");
	printf_s("\nthisVector /= 3 -> ");
	thisVector /= 3;
	thisVector.Print();

	printf_s("\nOperator =");
	printf_s("\nthisVector = otherVector -> ");
	thisVector = otherVector;
	thisVector.Print();

	printf_s("\nOperator unary -");
	printf_s("\n-thisVector -> ");
	(-thisVector).Print();

	printf_s("\nReset thisVector");
	printf_s("\nthisVector -> ");
	thisVector = Vector3D(1, 2, 3);
	thisVector.Print();
	printf_s("otherVector -> ");
	otherVector.Print();

	printf_s("\nOperator +");
	printf_s("\nthisVector + otherVector -> ");
	(thisVector + otherVector).Print();

	printf_s("\nOperator -");
	printf_s("\nthisVector - otherVector -> ");
	(thisVector - otherVector).Print();

	printf_s("\nOperator *");
	printf_s("\nthisVector * 3 -> ");
	(thisVector * 3).Print();

	printf_s("\nOperator /");
	printf_s("\nthisVector / 3 -> ");
	(thisVector / 3).Print();

	printf_s("\nReset thisVector");
	printf_s("\nthisVector -> ");
	thisVector = Vector3D(1, 2, 3);
	thisVector.Print();
	printf_s("otherVector -> ");
	otherVector.Print();

	printf_s("\nOperator ==");
	printf_s("\nthisVector == otherVector -> ");
	
	if (thisVector == otherVector)
	{
		printf_s("True\n");
	}

	else
	{
		printf_s("False\n");
	}

	printf_s("\nOperator !=");
	printf_s("\nthisVector != otherVector -> ");

	if (thisVector != otherVector)
	{
		printf_s("True\n");
	}

	else
	{
		printf_s("False\n");
	}

	printf_s("\nNormalize");
	printf_s("\nthisVector -> ");
	thisVector.Normalize();
	thisVector.Print();

	printf_s("\nDot Product");
	printf_s("\nthisVector(dot)otherVector -> %.2f\n", thisVector.Dot(otherVector));

	printf_s("\nCross Product");
	printf_s("\nthisVector(cross)otherVector -> ");
	(thisVector.Cross(otherVector)).Print();

	getchar();
}
void COpenGLES2GrassRenderer::render(CSGPGrass* pGrass)
{	
	SGPVertex_GRASS_GLES2 tempData;
	if( m_GrassClusterInstanceArray.size() > 0 )
	{
		COpenGLES2GrassRenderer::Sorter CompareGrassInstance(m_pRenderDevice);
		m_GrassClusterInstanceArray.sort(CompareGrassInstance);
		
		uint32 nGrassClusterNum = jmin(m_GrassClusterInstanceArray.size(), INIT_GRASSCLUSTERINSTANCE_NUM);
		uint32 nSizeData = sizeof(SGPVertex_GRASS_GLES2) * nGrassClusterNum * (4*3);
		uint32 nVBOffset = (m_GrassClusterInstanceArray.size() <= INIT_GRASSCLUSTERINSTANCE_NUM) ? 0 : m_GrassClusterInstanceArray.size() - INIT_GRASSCLUSTERINSTANCE_NUM;
		SGPVertex_GRASS_Cluster* pGrassClusterSrc = m_GrassClusterInstanceArray.getRawDataPointer() + nVBOffset;
		
		// update Dynamic Grass Instance Buffer
		glBindBuffer(GL_ARRAY_BUFFER, m_nGrassClusterVBOID);
		glBufferData(GL_ARRAY_BUFFER, nSizeData, NULL, GL_STREAM_DRAW);
		SGPVertex_GRASS_GLES2* pGrassVertex = (SGPVertex_GRASS_GLES2*)m_pRenderDevice->extGlMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);

		for( uint32 i=0; i<nGrassClusterNum; i++ )
		{
			tempData.vNormal[0] = ( pGrassClusterSrc[i].vPackedNormal[0] / 255.0f - 0.5f ) * 2.0f;
			tempData.vNormal[1] = ( pGrassClusterSrc[i].vPackedNormal[1] / 255.0f - 0.5f ) * 2.0f;
			tempData.vNormal[2] = ( pGrassClusterSrc[i].vPackedNormal[2] / 255.0f - 0.5f ) * 2.0f;
			tempData.vNormal[3] = pGrassClusterSrc[i].vPackedNormal[3] / 255.0f;
			tempData.vColor[0] = pGrassClusterSrc[i].vColor[0];
			tempData.vColor[1] = pGrassClusterSrc[i].vColor[1];
			tempData.vColor[2] = pGrassClusterSrc[i].vColor[2];
			tempData.vColor[3] = pGrassClusterSrc[i].vColor[3];
			tempData.LMtu = pGrassClusterSrc[i].vPosition[0] / m_pRenderDevice->GetWorldSystemManager()->getTerrain()->GetTerrainWidth();
			tempData.LMtv = 1.0f - pGrassClusterSrc[i].vPosition[2] / m_pRenderDevice->GetWorldSystemManager()->getTerrain()->GetTerrainWidth();


			Matrix4x4 RotMatrix;
			RotMatrix.Identity();
			if( pGrassClusterSrc[i].vPackedNormal[1] < 250 )
			{
				Vector3D vTerrainNormal(tempData.vNormal[0], tempData.vNormal[1], tempData.vNormal[2]);
				vTerrainNormal.Normalize();

				float cost = tempData.vNormal[1];
				Vector3D v;
				v.Cross( Vector3D(0,1,0), vTerrainNormal );
				v.Normalize();
				float sint = std::sqrt(1-cost*cost);
				float one_sub_cost = 1 - cost;

				RotMatrix._11 = v.x * v.x * one_sub_cost + cost;
				RotMatrix._12 = v.x * v.y * one_sub_cost + v.z * sint;
				RotMatrix._13 = v.x * v.z * one_sub_cost - v.y * sint;
				RotMatrix._21 = v.x * v.y * one_sub_cost - v.z * sint;
				RotMatrix._22 = v.y * v.y * one_sub_cost + cost;
				RotMatrix._23 = v.y * v.z * one_sub_cost + v.x * sint;
				RotMatrix._31 = v.x * v.z * one_sub_cost + v.y * sint;
				RotMatrix._32 = v.y * v.z * one_sub_cost - v.x * sint;
				RotMatrix._33 = v.z * v.z * one_sub_cost + cost;
			}


			for( uint32 j=0; j<4*3; j++ )
			{
				tempData.tu = m_grassVertex[j].tu;
				tempData.tv = m_grassVertex[j].tv;

				Vector3D modelPos = Vector3D(m_grassVertex[j].x, m_grassVertex[j].y, m_grassVertex[j].z) * tempData.vNormal[3] * RotMatrix;
				modelPos += Vector3D(pGrassClusterSrc[i].vPosition[0], pGrassClusterSrc[i].vPosition[1], pGrassClusterSrc[i].vPosition[2]);

				float windAngle = m_vTimeParams.x * m_vTimeParams.y * Vector3D(pGrassClusterSrc[i].vPosition[0], pGrassClusterSrc[i].vPosition[1], pGrassClusterSrc[i].vPosition[2]).GetLength();
				Vector3D vCosSin = Vector3D( std::cos(windAngle), 0, std::sin(windAngle) );
				Vector3D vOffset(	pGrassClusterSrc[i].vWindParams[0] * m_vWindDirForce.x * m_vWindDirForce.w,
									pGrassClusterSrc[i].vWindParams[1] * m_vWindDirForce.y * m_vWindDirForce.w,
									pGrassClusterSrc[i].vWindParams[2] * m_vWindDirForce.z * m_vWindDirForce.w );

				vOffset.x += vCosSin.x * pGrassClusterSrc[i].vWindParams[0] * m_vWindDirForce.w;
				vOffset.y += vCosSin.y * pGrassClusterSrc[i].vWindParams[1] * m_vWindDirForce.w;
				vOffset.z += vCosSin.z * pGrassClusterSrc[i].vWindParams[2] * m_vWindDirForce.w;

				Vector3D vv = Vector3D(tempData.vNormal[0], tempData.vNormal[1], tempData.vNormal[2]) + vOffset;
				vv.Normalize();
				modelPos += vv * tempData.vNormal[3] * (1.0f - tempData.tv);	

				tempData.vPosition[0] = modelPos.x;
				tempData.vPosition[1] = modelPos.y;
				tempData.vPosition[2] = modelPos.z;
				tempData.vPosition[3] = pGrassClusterSrc[i].vPosition[3];

				(*pGrassVertex) = tempData;

				pGrassVertex++;
			}
		}

		m_pRenderDevice->extGlUnmapBuffer(GL_ARRAY_BUFFER);

	}
	
	m_vTextureAtlas.Set( pGrass->m_fTextureAtlasNbX, pGrass->m_fTextureAtlasNbY, pGrass->m_fTextureAtlasW, pGrass->m_fTextureAtlasH );
}