Exemplo n.º 1
0
void Ray::intersectOctree(const Octree* tree, Fragment &inter) const
{
	double t;
	if(intersectSimpleBbox(tree->bBox, t))
	{
		// intersection the objects in current tree node
		for (int i = 0; i < tree->objects.size(); i++)
		{
			MeshObject* obj = tree->objects[i]->parent;
			if(obj->type == SPHERE)
			{
				intersectObject(obj, inter);
			}
			else if (intersectTriangle(*tree->objects[i], inter))
			{
				inter.id = tree->objects[i]->parent;
			}
		}

		// recusive intersects its children nodes
		for(int i = 0; i < tree->nodes.size(); i++)
		{
			intersectOctree(tree->nodes[i], inter);
		}
		
	}

}
Exemplo n.º 2
0
int Ray::intersectRectangle(Ray ray, vector <Vertex> rectangle, Point * I, Point * coords){
    vector <Vertex> testTri1;
    testTri1.push_back(rectangle[0]);
    testTri1.push_back(rectangle[1]);
    testTri1.push_back(rectangle[2]);
	
    if (intersectTriangle(ray, testTri1, I, coords)) { return 2; }
	
    vector <Vertex> testTri2;
    testTri2.push_back(rectangle[2]);
    testTri2.push_back(rectangle[3]);
    testTri2.push_back(rectangle[0]);	
	
    if (intersectTriangle(ray, testTri2, I, coords)) { return 3; }
    return 0;
}
Exemplo n.º 3
0
void
_SoNurbsPickV4SurfaceMap::point( float *v )

//
////////////////////////////////////////////////////////////////////////
{
    //
    // Store the incoming point and calculate the normal at that point
    //
    P.p[0] = v[0];
    P.p[1] = v[1];
    P.p[2] = v[2];
    P.p[3] = v[3];
    computeFirstPartials ();
    computeNormal ();

    if (!cacheReady) {

        // The cache of mesh vertices is still filling, so a triangle
        // isn't ready to be intersected yet.  Just fill this point
        // into the cache and update the cache vertices.
        SP[cacheIndices[curCacheIndex]].p[0] = P.p[0]/P.p[3];
        SP[cacheIndices[curCacheIndex]].p[1] = P.p[1]/P.p[3];
        SP[cacheIndices[curCacheIndex]].p[2] = P.p[2]/P.p[3];
        SP[cacheIndices[curCacheIndex]].norm[0] = P.norm[0];
        SP[cacheIndices[curCacheIndex]].norm[1] = P.norm[1];
        SP[cacheIndices[curCacheIndex]].norm[2] = P.norm[2];
        TP[cacheIndices[curCacheIndex]][0] = tmpTexPt[0];
        TP[cacheIndices[curCacheIndex]][1] = tmpTexPt[1];
        if (curCacheIndex == 1) {
            cacheReady = 1;
        }
        curCacheIndex = 1 - curCacheIndex;
        return;
    }

    // The cache is full now, so triangles are now forming with each
    // vertex.  Take the two cached vertices and the current vertex
    // to form a triangle and intersect it.
    SP[curPrimIndex].p[0] = P.p[0]/P.p[3];
    SP[curPrimIndex].p[1] = P.p[1]/P.p[3];
    SP[curPrimIndex].p[2] = P.p[2]/P.p[3];
    SP[curPrimIndex].norm[0] = P.norm[0];
    SP[curPrimIndex].norm[1] = P.norm[1];
    SP[curPrimIndex].norm[2] = P.norm[2];
    TP[curPrimIndex][0] = tmpTexPt[0];
    TP[curPrimIndex][1] = tmpTexPt[1];
    intersectTriangle();

    // Update the cache indices
    int tmp = curPrimIndex;
    curPrimIndex = cacheIndices[curCacheIndex];
    cacheIndices[curCacheIndex] = tmp; 
    curCacheIndex = 1 - curCacheIndex;
}
Exemplo n.º 4
0
bool Ray::intersectMesh(const Mesh &mesh, Fragment &inter) const
{
	bool result = false;
	for (int i = 0; i < mesh.size(); i++)
	{
		if(intersectTriangle(mesh[i], inter))
		{			
			result = true;
		}
	}
	return result;
}
Exemplo n.º 5
0
int HitDetection::intersectModel(Ray* r, Model* m, Coordinate* c, Hit* hit, float time){
	if(m == NULL)
		return FALSE;
	int test;
	for(size_t i = 0; i < m->getShapeCount(); i++){
		for(size_t j = 0; j < m->getShape(i)->getTriangleCount(); j++){		
			test = intersectTriangle(r, m->getShape(i)->getTriangle(j), c, hit, time);
			if(test != FALSE)
				return test;
		}
	}
	return FALSE;
}
Exemplo n.º 6
0
bool insideSurfaceCrossing(const Point3D &pTest, const Surface &s, const SpacialHash &faceHash){
  //  check against bounding box
  if (pTest.x < s.pMin.x || pTest.x > s.pMax.x ||
      pTest.y < s.pMin.y || pTest.y > s.pMax.y ||
      pTest.z < s.pMin.z || pTest.z > s.pMax.z)
    return false;

  //  generate the random ray
  Vector3D testRay;
  testRay.x = rand()/(float)RAND_MAX;
  testRay.y = rand()/(float)RAND_MAX;
  testRay.z = rand()/(float)RAND_MAX;
  testRay.norm();

  //  setup SEADS grid stepper
  SpacialHash::Stepper stepper;
  stepper.sh = &faceHash;
  Array<int> *cell = stepper.discreteSetup(pTest, testRay);

  //  flags for which triangles have been tested
  int numTri = s.triangles.getSize();
  Array<bool> done;
  done.resize(numTri);
  done.clear();

  //  start walking and testing
  int count = 0;
  while(cell) {
    //  test the triangles
    int numInCell = cell->getSize();
    for (int l = 0; l < numInCell; l++){
      int triN = cell->index(l);
      if (!done.index(triN)){
        done.index(triN) = true;

        const Surface::Triangle *tri = &s.triangles.index(triN);
        const Point3D *p0 = &s.vertices.index(tri->v[0]).p;
        const Point3D *p1 = &s.vertices.index(tri->v[1]).p;
        const Point3D *p2 = &s.vertices.index(tri->v[2]).p;

        float t = -1;
        if (intersectTriangle(pTest, testRay, &t, *p2, *p1, *p0) && t >= 0)
          count++;
        }
      }

    cell = stepper.discreteStep();
    }

  return (count%2) != 0;
}
Exemplo n.º 7
0
/*
    finding the closest point on a triangle
*/
double distanceToTriangle(ClosestPointInfo *inf, const Point3D &pTest, const Point3D P[3]){
  //  normal to plane
  Vector3D v1, v2, vC;
  v1.difference(P[1], P[0]);
  v2.difference(P[2], P[0]);
  vC.cross(v1, v2);

  //  transforms to get into 2D
  double minD = REAL_MAX;

  //  test against triangle
  float t;
  if (intersectTriangle(pTest, vC, &t, P[2], P[1], P[0])){
    inf->type = FACE;
    vC.add(&inf->pClose, pTest, t);
    minD = inf->pClose.distanceSQR(pTest);
    }
  else{
    //  project p1 onto each edge
    for (int i = 0; i < 3; i++){
      Point3D pt;
      if (project3D(&pt, pTest, P[i], P[(i+1)%3])){
        double d = pt.distanceSQR(pTest);
        if (d < minD){
          minD = d;
          inf->num = i;
          inf->type = EDGE;
          inf->pClose = pt;
          }
        }
      }

    //  test against each vertex
    for (int i = 0; i < 3; i++){
      double d = P[i].distanceSQR(pTest);
      if (d < minD){
        inf->num = i;
        inf->type = VERTEX;
        inf->pClose = P[i];
        minD = d;
        }
      }
    }

  return sqrt(minD);
}
Exemplo n.º 8
0
Primitive::IntRet LWObject::Face::intersect(const Ray& _ray, float _previousBestDistance) const
{
	IntRet ret;

	float4 inter = 
		intersectTriangle(
			m_lwObject->vertices[vert1], m_lwObject->vertices[vert2], m_lwObject->vertices[vert3], _ray
		);

	ret.distance = inter.w;

	if(inter.w < _previousBestDistance)
	{
		SmartPtr<ExtHitPoint> hit = new ExtHitPoint;
		ret.hitInfo = hit;
		hit->intResult = inter;
	}

	return ret;
}
Exemplo n.º 9
0
double distanceToTriangleSPECIAL(ClosestPointInfo *inf, double minDS, const Point3D &pTest, const Vector3D &nTri, const Point3D P[3], const int pI[3]){
  //  transforms to get into 2D
  double minD = minDS;

  //  test against triangle
  float t;
  if (intersectTriangle(pTest, nTri, &t, P[2], P[1], P[0])){
    float d = t*t;               //  distance squared
    if (d < minD){
      inf->type = FACE;
      nTri.add(&inf->pClose, pTest, t);
      minD = d;
      }
    }
  else{
    //  project p1 onto each edge
    for (int i = 0; i < 3; i++){
      int i1 = (i+1)%3;

      if (pI[i] < pI[i1]){            //    only need to consider each point once
        Point3D pt;
        if (project3D(&pt, pTest, P[i], P[i1])){
          double d = pt.distanceSQR(pTest);
          if (d < minD){
            minD = d;
            inf->num = i;
            inf->type = EDGE;
            inf->pClose = pt;
            }
          }
        }
      }

    //  vertices are checked outside
    }

  return minD;
}
Exemplo n.º 10
0
//Loop through spheres and triangles to calculate the color and shading.
bool calRays(Ray &ray, Point &color){

  bool f = false;
  double distance = 1000000;
  // loops spheres
  for(int i = 0; i < num_spheres; i++){
    double t;
    Point normal;
    if(intersectCircle(ray, spheres[i], t, normal)){
      if(t < distance){
        f = true;
        distance = t;
        Point intersection;
        intersection.x = ray.p.x + t * ray.d.x;
        intersection.y = ray.p.y + t * ray.d.y;
        intersection.z = ray.p.z + t * ray.d.z;
        Point tempColor = calShading(ray, normal, spheres[i].color_specular, spheres[i].color_diffuse, spheres[i].shininess, intersection);

        color.x = tempColor.x;
        color.y = tempColor.y;
        color.z = tempColor.z;

      }

    }

  }
  //loop triangles
  for(int m = 0; m < num_triangles; m++){
    double u, v, t;
    Point normal;

    if(intersectTriangle(ray, triangles[m], t, u, v, normal)){
      if(t < distance){
        distance = t;
        f = true;
        Point intersection;
        intersection.x = ray.p.x + t * ray.d.x;
        intersection.y = ray.p.y + t * ray.d.y;
        intersection.z = ray.p.z + t * ray.d.z;
        double kd[3];
        double ks[3];
        double s = 1 - u - v;
        kd[0] = s * triangles[m].v[0].color_diffuse[0] + u * triangles[m].v[1].color_diffuse[0] + v * triangles[m].v[2].color_diffuse[0];
        kd[1] = s * triangles[m].v[0].color_diffuse[1] + u * triangles[m].v[1].color_diffuse[1] + v * triangles[m].v[2].color_diffuse[1];
        kd[2] = s * triangles[m].v[0].color_diffuse[2] + u * triangles[m].v[1].color_diffuse[2] + v * triangles[m].v[2].color_diffuse[2];
        ks[0] = s * triangles[m].v[0].color_specular[0] + u * triangles[m].v[1].color_specular[0] + v * triangles[m].v[2].color_specular[0];
        ks[1] = s * triangles[m].v[0].color_specular[1] + u * triangles[m].v[1].color_specular[1] + v * triangles[m].v[2].color_specular[1];
        ks[2] = s * triangles[m].v[0].color_specular[2] + u * triangles[m].v[1].color_specular[2] + v * triangles[m].v[2].color_specular[2];
        double sh;
        sh = s * triangles[m].v[0].shininess + u * triangles[m].v[1].shininess + v * triangles[m].v[2].shininess;
        Point temp = calShading(ray, normal, ks, kd, sh, intersection);
        color.x = temp.x;
        color.y = temp.y;
        color.z = temp.z;
      }
    }
  }

  if(f){ // add ambient light at the end
    color.x += ambient_light[0];
    color.y += ambient_light[1];
    color.z += ambient_light[2];
    return true;
  }

  color.x = 0.0;
  color.y = 0.0;
  color.z = 0.0;
  return false;
}
Exemplo n.º 11
0
// Calculate the color.
Point calShading(Ray &ray, Point &normal, double *ks, double *kd, double sh, Point &intersection){
  Point color = {0, 0, 0};
  for( int light_num = 0; light_num < num_lights; light_num++){
    bool inShadow = false;
    Ray shadow;
    shadow.p.x = intersection.x;
    shadow.p.y = intersection.y;
    shadow.p.z = intersection.z;
    shadow.d.x = lights[light_num].position[0] - intersection.x;
    shadow.d.y = lights[light_num].position[1] - intersection.y;
    shadow.d.z = lights[light_num].position[2] - intersection.z;
    normalize(shadow.d);
    Point temp;
    temp.x = lights[light_num].position[0] - intersection.x;
    temp.y = lights[light_num].position[1] - intersection.y;
    temp.z = lights[light_num].position[2] - intersection.z;
    double tempT;
    PointMultiply(temp, temp, tempT);

    for(int i = 0; i < num_spheres; i++){
      double t;
      Point n;
      if(intersectCircle(shadow, spheres[i], t, n)){
        if(t  <= tempT){
          inShadow = true;
        }
      }
    }
                                                                            //shadow rays
    for(int i = 0; i < num_triangles; i++){
      double t, u, v;
      Point n;
      if(intersectTriangle(shadow, triangles[i], t, u, v, n)){
        if(t <= tempT){
          inShadow = true;
        }
      }
    }

    if(!inShadow){
      double dotN;
      PointMultiply(shadow.d, normal, dotN);
      Point r;
      r.x = 2 * dotN * normal.x - shadow.d.x;
      r.y = 2 * dotN * normal.y - shadow.d.y;
      r.z = 2 * dotN * normal.z - shadow.d.z;
      normalize(r);
      if(dotN < 0)
        dotN = 0.0;
      double dotR;
      Point v;
      v.x = - ray.d.x;
      v.y = - ray.d.y;
      v.z = - ray.d.z;
      PointMultiply(r, v, dotR);
      if(dotR < 0)
        dotR = 0.0;
      color.x += lights[light_num].color[0] * (kd[0] * dotN + ks[0] * pow(dotR, sh));
      color.y += lights[light_num].color[1] * (kd[1] * dotN + ks[1] * pow(dotR, sh));
      color.z += lights[light_num].color[2] * (kd[2] * dotN + ks[2] * pow(dotR, sh));
    }
  }
  return color;
}
Exemplo n.º 12
0
hpreal Ray::intersect(Triangle& triangle){
    hpvec3 hit;
    if( !intersectTriangle(triangle, hit) )
        return std::numeric_limits<hpreal>::max();
    return glm::distance(m_origin, hit);
}