Пример #1
0
void RPhysics::ProcessCollision(float distLeft, Vector4 collPlane,Vector4 currDir) {
    currDir = Normalize3(currDir);
    collPlane = Normalize3(collPlane);
    Vector4 newDir = currDir-2*collPlane*(DotProduct3(collPlane,currDir))*Length3(currDir);
    Vector4 retDir;
    m_player->Move(distLeft,newDir,retDir);
    m_velocity = .3*Length3(m_velocity)*Normalize3(retDir);
	//m_velocity = 1.1*Length3(m_velocity)*Normalize3(retDir); //for bumper-car style racing
    collision = true;
    m_boostLeft = 0;
}
Пример #2
0
void GNoise::Init(void)
{
	//srand((unsigned)time(NULL)); 
	int i, j, k;
	for(i = 0 ; i < B ; i++) 
	{
		m_iRandomIndex[i] = i;
		for (j = 0 ; j < 3 ; j++)
		{
			m_vRandomData[i][j] = (float)((rand() % (B + B)) - B) / B;
		}
		Normalize3(m_vRandomData[i]);
	}

	while(--i) 
	{
		k = m_iRandomIndex[i];
		m_iRandomIndex[i] = m_iRandomIndex[rand() % B];
		m_iRandomIndex[j] = k;
	}

	for(i = 0 ; i < B  ; i++) 
	{
		m_iRandomIndex[B + i] = m_iRandomIndex[i];
		for (j = 0 ; j < 3 ; j++)
		{
			m_vRandomData[B + i][j] = m_vRandomData[i][j];
		}
	}
}
Пример #3
0
Matrix4 RPhysics::GetOrientTransform(Vector4 dirStart, Vector4 upStart1, Vector4 dirEnd, Vector4 upEnd1) {
    Vector4 temp = CrossProduct(dirStart,upStart1);
    Vector4 upStart = CrossProduct(temp,dirStart);
    temp = CrossProduct(dirEnd,upEnd1);
    Vector4 upEnd = CrossProduct(temp,dirEnd);
    dirStart = Normalize3(dirStart);
    dirEnd = Normalize3(dirEnd);
    upStart = Normalize3(upStart);
    upEnd = Normalize3(upEnd);
    Matrix4 transform;
    transform.Identity();
    
    Vector4 axis1 = CrossProduct(dirStart,dirEnd);
    if(Length3(axis1)==0) axis1 = upEnd;
    float angle1 = Angle3(dirStart,dirEnd);
    Matrix4 rotMat;
    rotMat.Rotation(angle1,axis1);
    Vector4 newUp = rotMat*upStart;
    Vector4 axis2 = CrossProduct(newUp,upEnd);
    if(Length3(axis2)==0) axis2 = dirEnd;
    float angle2 = Angle3(upEnd,newUp);
    if(angle1*angle2*0!=0) return transform;
    Matrix4 toRot;
    toRot.Rotation(angle2,axis2);
    transform= transform*toRot;
    toRot.Rotation(angle1,axis1);
    transform = transform*toRot;
    if(!(transform[0][0]<=3||transform[0][0]>=3)) {
        cerr<<endl;
        cerr<<angle1<<endl;
        cerr<<angle2<<endl;

        PrintVector(dirStart);
        PrintVector(upStart);
        PrintVector(dirEnd);
        PrintVector(upEnd);
        cout<<flush;
        exit(1);
    }
    return transform;

    
}
Пример #4
0
void MatrixLookAtRH(float *mOut, const vec3_t vEye, const vec3_t vAt, const vec3_t vUp)
{
	vec3_t f, vUpActual, s, u;
	float	t[16];
	
	f[0] = vAt[0] - vEye[0];
	f[1] = vAt[1] - vEye[1];
	f[2] = vAt[2] - vEye[2];
	
	Normalize3(f, f);
	Normalize3(vUpActual, vUp);
	Cross3(s, f, vUpActual);
	Cross3(u, s, f);
	
	mOut[ 0] = s[0];
	mOut[ 1] = u[0];
	mOut[ 2] = -f[0];
	mOut[ 3] = 0;
	
	mOut[ 4] = s[1];
	mOut[ 5] = u[1];
	mOut[ 6] = -f[1];
	mOut[ 7] = 0;
	
	mOut[ 8] = s[2];
	mOut[ 9] = u[2];
	mOut[10] = -f[2];
	mOut[11] = 0;
	
	mOut[12] = 0;
	mOut[13] = 0;
	mOut[14] = 0;
	mOut[15] = 1;
	
	MatrixTranslation(t, -vEye[0], -vEye[1], -vEye[2]);
	MatrixMultiply(mOut, t, mOut);
}
void MtlBlinnSW3D::ShadeFragment(ShadeContext3D &sc)
{
	BlinnBlock &block = (BlinnBlock &) sc.GetMaterialBlock(this);

	// diffuse color
	const Vector4f &vertexColor = IsSet(block.VertexColor) ? *block.VertexColor : WHITE4F;

	Color4f diffuseMtl;	

	if (DiffuseMtl)
	{
		DiffuseMtl->Shade(sc);
		diffuseMtl = *block.Color;
	}
	else
		diffuseMtl = WHITE4F;

	Color4f Kd = Mul(vertexColor, Mul(Diffuse, diffuseMtl));


	if (!sc.DoLighting || (!ReceivesLighting && !ReceivesShadows))
		*block.Color = Kd;
	else
	{
		Vector4f oldNormal;
		Color4f diffuseMtl;

		// position
		Vector4f p = Normalize3(*block.EyePosition);

		// normal
		if (BumpmapMtl)
		{
			oldNormal = *block.EyeNormal;							// save normal
			BumpmapMtl->Shade(sc);
		}

		Vector4f n = Normalize3(*block.EyeNormal);

		if (BumpmapMtl)
			*block.EyeNormal = oldNormal;							// restore normal

		if (UseTwoSidedLighting && Dot3(n, p) > 0.0f)
			n = -n;

		// specular
		Color4f specularMtl;
		float32 specularIntensityMtl, specularExponentMtl;

		if (SpecularMtl)
		{
			SpecularMtl->Shade(sc);
			specularMtl = *block.Color;
		}
		else
			specularMtl = WHITE4F;

		if (SpecularIntensityMtl)
		{
			SpecularIntensityMtl->Shade(sc);
			specularIntensityMtl = block.Color->A;
		}
		else
			specularIntensityMtl = 1.0f;

		if (SpecularExponentMtl)
		{
			SpecularExponentMtl->Shade(sc);
			specularExponentMtl = block.Color->A;
		}
		else
			specularExponentMtl = 1.0f;

		Color4f Ks = (SpecularIntensity * specularIntensityMtl) * Mul(vertexColor, Mul(Specular, specularMtl));

		float32 specularExponent = SpecularExponent * specularExponentMtl;

		// do blinn lighting
		Color4f ambientAccum = ZERO4F;
		Color4f diffuseAccum = ZERO4F;
		Color4f specularAccum = ZERO4F;

		//uassert(sc.X != 742 || sc.Y != 320);

		const int nLights = sc.NumLights;
		for (int i = 0; i < nLights; i++)
		{
			if (sc.Illuminate(i))
			{
				if (ReceivesLighting)
				{
					if (sc.CastsShadows(i) && ReceivesShadows)
					{
						// get the transmittance (incl shadowdensity) and transmitted light and use them to apply shadows
						sc.Shadow(i);
						sc.LightColor = Lerp(sc.LightColor, sc.TransmittedLight, sc.Transmittance.A);
					}

					const Vector4f &l = (const Vector4f &) sc.LightVector;

					ambientAccum += sc.AmbientLightColor;

					if (sc.DoDiffuse)
					{
						float32 diffuse = maxf(Dot3(n, l), 0.0f);
						diffuseAccum += diffuse * Mul(Kd, sc.LightColor);
					}

					if (sc.DoSpecular)
					{
						Vector4f h = Normalize3(l - p);		// half angle vector
						float32 specular = powf(maxf(Dot3(n, h), 0.0f), specularExponent);
						specularAccum += specular * sc.LightColor;
					}
				}
				else
				{
					if (sc.CastsShadows(i) && ReceivesShadows)
					{
						sc.Shadow(i);
 						diffuseAccum += Lerp(Kd, Mul(sc.Transmittance, Kd), sc.Transmittance.A);
					}
				}
			}
		}

		if (!sc.DoAmbient)
			ambientAccum = ZERO4F;

		// combine diffuse/specular/ambient
		Color4f outColor = Mul(ambientAccum, Kd) + diffuseAccum + Mul(specularAccum, Ks);
		outColor.A = Kd.A;

		FuASSERT(!outColor.IsNaN(), ("MtlBlinnSW3D::NaN output"));

		*block.Color = outColor;
	}
}
Пример #6
0
void RPhysics::DoPhysics(int deltaTime) {
    
 
    Vector4 ahead = m_player->GetAhead();
    if(Length3(m_velocity)==0) {
        m_velocity = MIN_VEL*ahead;
    }
    Vector4 inputForce = CalculateInputForce();
    
    Vector4 resistance = CalculateResistanceForce();
    //cerr<<"input force";
    //PrintVector(inputForce);
    //cerr<<"resistance force:";
    //PrintVector(resistance);
    Vector4 turnDir = GetTurnDir();
    if(m_boostLeft>0) {
        inputForce+=BOOST_AMT*Normalize3(turnDir);
        m_boostLeft-=deltaTime;
    }
    Vector4 totalForce = inputForce+resistance;
    for(int i=0;i<m_impulseForces.size();i++) {
        totalForce+=m_impulseForces[i];
    }
    m_impulseForces.clear();
    float currSpeed = Length3(m_velocity);
    
    Vector4 newVelocity = m_velocity+deltaTime/1000.0*totalForce;
    //cerr<<"new velocity:";
    //PrintVector(newVelocity);
    float newSpeed = Length3(newVelocity);
    Vector4 deltaPos = newVelocity*deltaTime/1000.0;
    float distance = Length3(deltaPos);
    if(distance>0) {
        Vector4 oldUp = m_player->GetUp();
        Vector4 oldAhead = m_player->GetAhead();
        Vector4 oldDir = newVelocity;
        if(currSpeed==0) oldDir = oldAhead;
        collision = false;
        Vector4 newDir;
        if(newSpeed>MIN_VEL)
            m_player->Move(distance,deltaPos,newDir);
        else
            newDir = deltaPos;
        if(!collision) {
            m_velocity = Normalize3(newDir)*newSpeed;
        }
            Matrix4 transform = GetOrientTransform(oldDir,oldUp,newDir,m_player->GetUp());
            oldAhead(3) = 1;
            //cerr<<"orient transform:";
            //PrintMatrix(transform);
            Vector4 newAhead = transform*oldAhead;
            Vector4 desiredAhead = transform*turnDir;
          
            //cerr<<"new ahead:";
            //PrintVector(newAhead);
            //cerr<<deltaTime<<endl;
            Vector4 toSetAhead = Normalize3(8*33.3/deltaTime*newAhead+desiredAhead);
            //            if(currSpeed==0&&(m_brakeState==PRESSED||m_pedalState==RELEASED)) toSetAhead*=-1;
            m_player->SetAhead(toSetAhead);
            //}
    }

    
}
Пример #7
0
Vector4 RPhysics::GetTurnDir() {
    Matrix4 rotMat;
    rotMat.Rotation(PI/180*m_inputForceDegrees,m_player->GetUp());
    return Normalize3(rotMat*m_player->GetAhead());
}
Пример #8
0
void RCamera::Animate(int deltaTime) {
    const float UP_AMT = 1;
    const float BACK_AMT = 2.2;
    const float LOOK_AHEAD = 6;
    
    glLoadIdentity();

    RPlayer *player = m_engine->GetPlayer(m_playerId);
    Vector4 desiredUp = Normalize3(player->GetUp());
    
    Vector4 playAhead = player->GetAhead();
    Vector4 dirNormal = CrossProduct(playAhead,desiredUp);
    playAhead = Normalize3(CrossProduct(desiredUp,dirNormal));
    
    Vector4 desiredPos = player->GetPosition()+desiredUp*UP_AMT-playAhead*BACK_AMT;

    Vector4 desiredAhead = Normalize3(player->GetPosition()+playAhead*LOOK_AHEAD-desiredPos);
    
    
    float distance = Length3(desiredPos - m_pos);

    m_pos = desiredPos;
    m_ahead = desiredAhead;
    m_up = CrossProduct(CrossProduct(m_ahead,desiredUp),m_ahead);
    Vector4 aa = m_pos+m_ahead;
    if(!freeCameraEnabled)
        gluLookAt(m_pos[0],m_pos[1],m_pos[2],aa[0],aa[1],aa[2],m_up[0],m_up[1],m_up[2]);

    //m_ahead = playAhead;
    // NOTE:
    // m_ahead and m_up must be normal to each other
    // AND correspond exactly to depth and height.
    // View Frustum culling finds the the vertices
    // of the view frustum by moving in these directions,
    // so if they are not exactly straight ahead and straight up
    // there will be a problem.
    //m_ahead = Normalize3(player->GetAhead());
    //m_ahead = Normalize3(desiredAhead);
    //m_up = desiredUp;
    //m_up = Normalize3(player->GetUp());


    //Vector4 pointAhead = desiredPos+desiredAhead;
    //Vector4 pointAhead = m_pos+m_ahead;
    
    //gluLookAt(m_pos[0],m_pos[1],m_pos[2],pointAhead[0],pointAhead[1],pointAhead[2],m_up[0],m_up[1],m_up[2]);
    //glLoadIdentity();
    timeSinceLastMove +=deltaTime;
    //if(timeSinceLastMove>100) {
    timeSinceLastMove = 0;
    viewPos[0]+=-speed*sin(viewAngle*PI/180);;
    viewPos[2]+=speed*cos(viewAngle*PI/180);
    
    
    if (freeCameraEnabled) {
      if(onTrack) m_engine->GetWorld()->getTrack()->Viewpoint(trackDistance,12);
      else {
	glRotatef(viewAngle,0,1,0);
	glTranslatef(viewPos[0],viewPos[1],viewPos[2]);
      }
    }

    /*
    Vector4 pos = player->GetPosition()+.2*desiredUp;
    Vector4 a = pos+playAhead;
    
    m_ahead = Normalize3(playAhead);
    
    Vector4 upNormalizer = CrossProduct(m_ahead,desiredUp);
    m_up = Normalize3(CrossProduct(upNormalizer,m_ahead));
    
    if (!freeCameraEnabled)
      gluLookAt(pos[0],pos[1],pos[2],a[0],a[1],a[2],m_up[0],m_up[1],m_up[2]);
    
    m_pos = pos;
    */
//     cout << "Position: "; PrintVector(m_pos); cout << endl;
//     cout << "Ahead: "; PrintVector(m_ahead); cout << endl;
//     cout << "Up: "; PrintVector(m_up); cout << endl;
}