コード例 #1
0
ファイル: app.cpp プロジェクト: bcrowell/planetfinder_ios
void updateOrientation() {
	if ( orientationDirty == false ) {
		return;
	}
	orientationDirty = false;
	
	float decay = 0.9f;
	
	float hyst = cos( ToRadians( app_orientationHysteresis.GetVal() ) );

	if ( GotCompassUpdate ) {
		if ( headingSmooth.Dot( heading ) < hyst ) {
			headingSmooth *= decay;
			headingSmooth += (1.0f - decay) * heading;
		}
		headingSmooth.Normalize();
	} 		

	if ( accelSmooth.Dot( accel ) < hyst ) {
		accelSmooth *= decay;
		accelSmooth += (1.0f - decay) * accel;		
	}
	accelSmooth.Normalize();		
	
	Vec3f up = -accelSmooth;
	up.Normalize();
	Vec3f north = headingSmooth;
	north -= up * up.Dot( north );
	north.Normalize();

	Matrix3f toTrueNorth = Rotationf( up, ToRadians( trueHeadingDiff ) ).GetMatrix3();
	north = toTrueNorth * north;
	
	Vec3f east = north.Cross( up );

	Matrix4f o;
	o.SetRow( 0, Vec4f(  east.x,  east.y,  east.z, 0.0f ) );
	o.SetRow( 1, Vec4f( north.x, north.y, north.z, 0.0f ) );
	o.SetRow( 2, Vec4f(    up.x,    up.y,    up.z, 0.0f ) );
	o.SetRow( 3, Vec4f(     0.0,     0.0,     0.0, 1.0f ) );
	
	platformOrientation = o.Transpose();
}
コード例 #2
0
ファイル: TurtleShell.cpp プロジェクト: philhey/scroller
static void
HandleCollision (Entity *ent, Entity *ent2, Vec3f norm)
{

	if (ent2 && fabs(norm.x) > .8)
	{
		ent->vel.x += ent2->vel.x;
	}
	if (norm.Dot (ent->vel) < 0) // moving torwards entity
	{
		// velocity
		if (fabs(norm.y) > 0.5) // slide along floors
			ent->vel -= norm.Project(ent->vel)*1.5;
		else // bounce off walls
			ent->vel -= norm.Project(ent->vel)*1.2;
		if (ent2 && (ent2->type & ET_PLAYER) && fabs(norm.x) > .8)
		{
			ent->vel.x += ent2->vel.x*1.2;
		}

		ent->vel.z = 0.;
	}

}
コード例 #3
0
void RayTracer::rayTracing(Ray &ray, Vec3f &shadeColor, int depth)
{
	float curDist = MAX_DIST;
	float newDist;
	int pi = -1;

	for(int k=0; k<numPrim; k++)
	{	
		if(prims[k]->isEnabled() == false) continue;

		if(prims[k]->intercect(ray, newDist) == 1)
		{
			if(newDist < curDist)
			{
				curDist = newDist;
				pi = k;
			}
		}
	}

	if(pi != -1)
	{
		if(prims[pi]->isLightEnabled() == true)
		{
			//light source
			shadeColor = shadeColor + prims[pi]->getMaterial().getColor();
		}
		else
		{
			Vec3f pos = ray.origin+ray.direction*curDist;
			Vec3f N = prims[pi]->getNormal(pos);
			Vec3f R = ray.direction - N * ray.direction.Dot(N) * 2.0f;
			R = Vec3f(0.0f, 0.0f, 0.0f) - R;

			for(int l=0; l<numPrim; l++)
			{
				if(prims[l]->isLightEnabled() == false)
				{
					continue;
				}

				float viewLightDist = (((Sphere *)prims[l])->getCenter() - pos).Length();
				Vec3f L = (((Sphere *)prims[l])->getCenter() - pos).genNormal();

				//shadow light
				float shade = 1.0f;
				Ray rayShadow;
				rayShadow.direction = L;
				rayShadow.origin = pos;
				for(int s=0; s<numPrim; s++)
				{
					if(prims[s]->isLightEnabled() == true) continue;

					float tmpDist;
					if(prims[s]->intercect(rayShadow, tmpDist) == 1 && s != pi && viewLightDist > tmpDist)
					{
						shade = 1.0f;
						break;
					}
				}

				//diffusion light
				float dot = N.Dot(L);
				if(dot > 0)
				{
					float diff = dot * prims[pi]->getMaterial().getDiffRate();
					shadeColor= shadeColor + prims[pi]->getMaterial().getColor() * prims[l]->getMaterial().getColor() * diff * 1.0f;//shade;
				}

				//spec Light
				Vec3f V = ray.direction.genNormal();
				Vec3f LR = L - N * L.Dot(N) * 2.0f;
				dot = V.Dot(LR);
				if (dot > 0)
				{
					float spec = pow(dot, 20) * prims[pi]->getMaterial().getSpecRate();
					shadeColor = shadeColor + prims[l]->getMaterial().getColor() * spec;
				}
			}

			//reflection light
			float refl = prims[pi]->getMaterial().getReflecRate();
			if(depth < RAY_TRACE_DEPTH) 
			{
				Vec3f recColor(0.0f, 0.0f, 0.0f);
				Ray recRay;
				recRay.direction=R;
				recRay.origin=R * 0.00001f + pos;
				rayTracing(recRay, recColor, depth+1);
				shadeColor = shadeColor + recColor * prims[pi]->getMaterial().getColor() * refl;
			}

			//refraction light
			/*float rindex = prims[pi]->getMaterial().getRafracIndex();
			if(prims[pi]->getMaterial().getRefracRate() > 0.0f &&  depth < RAY_TRACE_DEPTH)
			{
				float n = -1.0f;
				Vec3f N = prims[pi]->getNormal(pos);
				float cosI = 0.0f - N.Dot(ray.direction);
				float cosT2 = 1.0f - n * n * (1.0f - cosI * cosI);
				if (cosT2 > 0.0f)
				{
					Vec3f T = (ray.direction * n) + N * (n * cosI - sqrtf( cosT2 ));
					Vec3f rcol(0.0f, 0.0f, 0.0f);
					Ray recRay;
					recRay.direction=T;
					recRay.origin=T + pos;
					rayTracing(recRay, rcol, depth+1);
					shadeColor = shadeColor + rcol * 5.0f;
				}
			}*/
		}

		if(shadeColor.x > 1.0f) shadeColor.x = 1.0f;
		if(shadeColor.y > 1.0f) shadeColor.y = 1.0f;
		if(shadeColor.z > 1.0f) shadeColor.z = 1.0f;
	}
	else
	{
		//nothing
	}
}
コード例 #4
0
//---------------------------------------
Intersection::CollisionInfo Intersection::Find( float t, const Spheref& A, const Vec3f& velA, const Spheref& B, const Vec3f& velB )
{
	// Get the relative velocity
	Vec3f vRel = velB - velA;
	float a = vRel.LengthSqr();

	// Get the minkowski sum of the spheres
	Vec3f center = B.Center - A.Center;
	float c = center.LengthSqr();
	float r = A.Radius + B.Radius;
	float r2 = r * r;

	CollisionInfo cInfo;

	if ( a > 0 )
	{
		float b = center.Dot( vRel );
		if ( b <= 0 )
		{
			if ( -t*a <= b || t*(t*a+2*b) + c <= r2 )
			{
				float deltaC = c - r2;
				float d = b*b - a*deltaC;

				if ( d >= 0 )
				{
					// Sphere start in intersection
					if ( deltaC <= 0 )
					{
						// Contact point is midpoint
						cInfo.ContactPoint = 0.5f * ( A.Center + B.Center );
						cInfo.ContactTime = 0.0f;
					}
					else
					{
						cInfo.ContactTime = -( b + std::sqrtf( d ) ) / a;

						if ( cInfo.ContactTime < 0 )
						{
							cInfo.ContactTime = 0.0f;
						}
						else if ( cInfo.ContactTime > t )
						{
							cInfo.ContactTime = t;
						}

						cInfo.ContactPoint = A.Center + cInfo.ContactTime * velA +
							( A.Radius / r ) * ( center + cInfo.ContactTime * vRel );
					}
					cInfo.Collision = true;
					return cInfo;
				}
			}
			cInfo.Collision = false;
			return cInfo;
		}
	}

	// Sphere start in intersection
	if ( c <= r2 )
	{
		// Contact point is midpoint
		cInfo.ContactPoint = 0.5f * ( A.Center + B.Center );
		cInfo.ContactTime = 0.0f;
		cInfo.Collision = true;
		return cInfo;
	}

	cInfo.Collision = false;
	return cInfo;
}
コード例 #5
0
void R004PseudoTexture::Render()
{
	m_viewport->Clear();
	MatrixStack mstackvertices;
	MatrixStack mstacknormals;

	// TODO: sort meshes

	mstackvertices.PushMatrix(m_camera->m_projectionmatrix);
	mstackvertices.PushMatrix(m_camera->m_viewmatrix);
	mstacknormals.PushMatrix(m_camera->m_viewmatrix);

	for (int i=0 ; i<m_scene->m_meshCount ; i++)
	{
		Mesh *mesh = m_scene->m_meshes[i];
		//mstackvertices.PushMatrix(mesh->m_worldmatrix);
		//mstacknormals.PushMatrix(mesh->m_worldmatrix);

		// Transforming vertices into screen coords
		Vec3f *vertices = new Vec3f[mesh->m_vertexcount];
		for (unsigned int v=0 ; v<mesh->m_vertexcount ; v++)
		{
			Vec3f vec = mesh->m_vertexbuffer[v];			
			vec = vec * mstackvertices.GetTopMatrix();
			vec.x = vec.x / ( (vec.z>0) ? (vec.z) : (-vec.z) );
			vec.y = vec.y / ( (vec.z>0) ? (vec.z) : (-vec.z) );
			vec = vec * m_viewport->m_viewportmatrix;
			vertices[v] = vec;
		}

		// Reading index buffer and drawing lines
		for (unsigned int s=0 ; s<mesh->m_stripcount ; s++)
		{
			Strip *strip = mesh->m_strips[s];
			Vec3f v0,v1,v2;
			v0 = vertices[strip->m_indexbuffer[0]];
			v1 = vertices[strip->m_indexbuffer[1]];
			for (unsigned i=2 ; i<strip->m_indexcount ; i++)
			{
				v2 = vertices[strip->m_indexbuffer[i]];
	
				// backface culling
				Vec3f cullingnormal = ((v1-v0).Cross((v2-v0)));
				if (cullingnormal.z < 0) // TODO dotproduct with camera ?
				{
					// Calculate flat shading
					Vec3f normal = (mesh->m_vertexbuffer[strip->m_indexbuffer[i]]-mesh->m_vertexbuffer[strip->m_indexbuffer[0]])
									.Cross(mesh->m_vertexbuffer[strip->m_indexbuffer[i-1]]-mesh->m_vertexbuffer[strip->m_indexbuffer[0]]);
					normal = normal.Mul3x3(mstacknormals.GetTopMatrix());
					normal.Normalize();
					Vec3f frontvec;
					frontvec.z = 1;
					float normaldotproduct = normal.Dot(frontvec);
					if (normaldotproduct > 0) normaldotproduct = 0;
					unsigned char shade = (unsigned char) ((normaldotproduct * (-200.0f)) + 55.0f);

					// Fill poly
					FillPoly(v0, v1, v2, shade);
				}

				v1 = v2;
			}
		}
		//mstack.PopMatrix(); // Remove world matrix (the mesh one)
		delete vertices;
	}
}