Exemple #1
0
    int EXPORT_API ProcessSpheresCollisionsVsStaticMesh(IBroadphase* tree, btVector3* positions, Triangle* triangles, btVector3* sphereCentres, float radius, int pointsCount, bool flipNormals, btVector3* colResp, int* colissionCount, bool twoSided)
    {

        int totalCollisions = 0;
        CollisionResult collisionResults[16];

        for (int k = 0; k < pointsCount; k++)
        {

            int colCount = IntersectSphere(tree, positions, triangles, &sphereCentres[k], radius, flipNormals, collisionResults, 16);

            colissionCount[k] += colCount;
            if (colCount > 0)
            {
                btVector3 responseVec(0, 0, 0);
                for (int i = 0; i < colCount; i++)
                {
                    CollisionResult cr = collisionResults[i];

                    //two sided collisions
                    float s = btDot(cr.contactPoint - sphereCentres[k], cr.normal);
                    if (twoSided && s  > 0)
                    {
                        cr.normal = -cr.normal;
                    }

                    responseVec += cr.normal * (radius - cr.distance + 0.00001f);
                    totalCollisions++;
                }
                colResp[k] += responseVec / (btScalar)colCount;
            }

        }
        return totalCollisions;
    }
bool RayTesselatedSpherePrimitive::Intersect(const Ray& ray, Intersection* intersection) {
    Vec3f p;
    float r;
    float rayEpsilon;
    Matrix4d m;
    Matrix4d mi;
    _ResolveSphereAttrib(p,r,m,mi);

    // xform the ray
    Ray xformRay = ray;
    xformRay.Transform(mi);

    float t;
    bool hit = IntersectSphere(p, r, xformRay, &t, &rayEpsilon);

    if(hit) {
        intersection->rayEpsilon = rayEpsilon;
        intersection->t = t;
        intersection->dp.P = xformRay.Eval(t);
        intersection->dp.Ng = (intersection->dp.P - p).GetNormalized();
        intersection->dp.N = intersection->dp.Ng;
        intersection->dp.uv[0] = (atan2f(intersection->dp.N[0], intersection->dp.N[1]) + (float)PI) / (float)(2*PI);
        intersection->dp.uv[1] = acosf(intersection->dp.N[2]) / (float)PI;
        intersection->dp.GenerateTuTv();
        intersection->dp.st = intersection->dp.uv;
        intersection->m = material;
    }

    // xform back
    if(hit) {
        intersection->Transform(m);
    }

    return hit;
}
Exemple #3
0
bool IsShadowed(int currPrimitiveNum, Ray &rayToLight, float distanceToLight, Primitive *primitives, int numPrimitives)
{  
  TraceResult traceResult;
  traceResult.hit=false;
  // check all spheres 
  for(int i=0; i<numPrimitives; i++)
    {
      if( i == currPrimitiveNum ) continue;//no auto
      
      switch(primitives[i].type)
        {
	case SPHERE_TYPE: 
	  traceResult = IntersectSphere(*primitives[i].sphereProperties, rayToLight);
	  break;
	case PLANE_TYPE:
	  traceResult = IntersectPlane(*primitives[i].planeProperties, rayToLight);
	  break;
	case CYLINDER_TYPE:
	  traceResult = IntersectCylinder(*primitives[i].cylinderProperties, rayToLight);
	  break;
	case TRIANGLE_TYPE:
	  traceResult = IntersectTriangle(*primitives[i].triangleProperties, rayToLight);
	  break;
	default:
	  continue; 
	  break;
        }
        
      if( traceResult.hit && (traceResult.distance < distanceToLight) )
	return true;
        
    }
    
  return false;
}
Exemple #4
0
		bool Sphere::intersect(Ray ray, Intersection* result) const {
			if (IntersectSphere(_pos, _radius, ray.origin, ray.direction, &result->d)) {
				result->position = (ray.origin + ray.direction * result->d);
				result->normal = glm::normalize(result->position - _pos);
				return true;
			}
			return false;
		}
bool RayTesselatedSpherePrimitive::Intersect(const Ray& ray) {
    Vec3f p;
    float r;
    Matrix4d m;
    Matrix4d mi;
    _ResolveSphereAttrib(p,r,m,mi);
    Ray xformRay = ray;
    xformRay.Transform(mi);
    float t, rayEpsilon;
    return IntersectSphere(p, r, xformRay, &t, &rayEpsilon);
}
Exemple #6
0
void centerIntersection(AAContext *aaContext, float *intersectionM)
{
	UserData *userData = (UserData*) aaContext->userData;
	float rayDir[4];
	rayDir[0] = userData->sphere->modelT->m[3][0]-userData->eye[0];
	rayDir[1] = userData->sphere->modelT->m[3][1]-userData->eye[1];
	rayDir[2] = userData->sphere->modelT->m[3][2]-userData->eye[2];
	rayDir[3] = 1.0f;

	IntersectSphere(userData->sphere, rayDir, userData->eye, intersectionM);
}
Exemple #7
0
float SceneManager::intersectRaySphere(glm::dmat4 view, glm::dmat4 projection, int mouseXPos, int mouseYPos, int screenwidth, int screenheight)
{
	raypicker.CalculateRays(view, projection, mouseXPos, mouseYPos, screenwidth, screenheight);
	std::map<unsigned int, AnimatedObject>::iterator iter = m_animatedObjects.begin();
	float ret = -1;
	for (iter; iter != m_animatedObjects.end(); ++iter)
	{
		BoundingSphere sph = iter->second.skinnedMesh->bsphere;
		sph.center += iter->second.skinnedMesh->GetPositon();
		if (IntersectSphere(raypicker, sph) != -1)
		{
			selectedAnimObject = iter->second;
			ret = 1;
		}
	}

	return ret;
}
Exemple #8
0
bool Scene::Trace(glm::vec3 pos, glm::vec3 dir, glm::vec3* out_pos, glm::vec3* out_normal, glm::vec3* color, glm::vec3* emit) {
	float distance = 0;
	float best_dist = 999999.0f;
	bool found = false;
	for(int i = 0; i < spheres.size(); ++i) {
		if(IntersectSphere(spheres[i], pos, dir, &distance)) {
			if(best_dist > distance) {
				glm::vec3 inters_point = pos + dir * distance;
				*out_normal = glm::normalize(inters_point - spheres[i].pos);
				*out_pos = inters_point;
				*color = spheres[i].color;
				*emit = spheres[i].emit;
				best_dist = distance;
				found = true;
			}
		}
	}
	return found;
}
Exemple #9
0
bool clickIntersection(AAContext* aaContext, POINT point, float *intersectionM)
{
	UserData *userData = (UserData*) aaContext->userData;
	float rayDir[4];
	float viewDir[4];
	RECT rect;
	MMatrix P;
	MMatrix iP;

	ScreenToClient(aaContext->hWnd, &point);
	GetClientRect(aaContext->hWnd, &rect);

	viewDir[0] = 2.0f*point.x / (float) (rect.right-rect.left)-1.0f;
	viewDir[1] = 2.0f*(1.0f-point.y / (float )(rect.bottom-rect.top))-1.0f;
	viewDir[2] = 1.0f;
	viewDir[3] = 1.0f;

	MViewRotation(&iP, userData->eye);
	MTranspose(&P, &iP);
	MMap(rayDir, &P, viewDir);
	return IntersectSphere(userData->sphere, rayDir, userData->eye, intersectionM); 
}