Esempio n. 1
0
double SphereRayIntersection(Sphere* s, Ray* r) {
	double a, b, c, d, t1, t2;
    
    a = dot((r->direction), (r->direction));
    b = dot(subtractPoints((r->origin), (s->center)),(r->direction));
    c = dot(subtractPoints((r->origin), (s->center)), subtractPoints((r->origin), (s->center)))
            - (s->radius * s->radius);
    d = (b * b) - (a * c);
    

    if (d >= 0) {
		//fprintf(stderr, "a = %lf, b = %lf, c = %lf\n", a, b, c);
        
		t1 = (-1 * b - sqrt(d)) / a;
		t2 = (-1 * b + sqrt(d)) / a;
	
		if (t2 > t1 && t2 > 0) {
			//fprintf(stderr, "d = %lf, t2 = %lf\n\n", d, t2);
			return t2;
		} else if (t1 > 0) {
			//fprintf(stderr, "d = %lf, t1 = %lf\n\n", d, t1);
			return t1;
		}
	}
	return d;
}
Esempio n. 2
0
TestPoint NelderMead::reflectOrExpand(std::vector<double> centroid, double scale)
{
    TestPoint *maxPoint = worstTestPoint();
    
    std::vector<double> diffVec = centroid;
    subtractPoints(&diffVec, maxPoint->first);
    scalePoint(&diffVec, scale);
    std::vector<double> reflectedVec = centroid;
    addPoints(&reflectedVec, diffVec);
    
    TestPoint reflection = std::make_pair(reflectedVec, 0);
    evaluateTestPoint(&reflection);
    
    return reflection;
}
Esempio n. 3
0
void NelderMead::reduction()
{
    TestPoint bestPoint = testPoints[0];
    
    for (int i = 1; i < testPoints.size(); i++)
    {
        TestPoint point = testPoints[i];
        
        std::vector<double> diffVec = point.first;
        subtractPoints(&diffVec, bestPoint.first);
        scalePoint(&diffVec, sigma);
        std::vector<double> finalVec = bestPoint.first;
        addPoints(&finalVec, diffVec);
        
        TestPoint contractPoint = std::make_pair(finalVec, 0);
        evaluateTestPoint(&contractPoint);
        
        testPoints[i] = contractPoint;
    }
}
Esempio n. 4
0
color_t SphereShading(int sNdx, Ray* r, Point p, Sphere* sphereList, PointLight* l) {
	color_t a, d, s, total;
	double reflectTemp, NdotL, RdotV;
	Point viewVector, lightVector, reflectVector, normalVector;

   //printf("r->%lf g->%lf b->%lf\n", l->ambient->r, l->ambient->g, l->ambient->b);
   //printf("r->%lf g->%lf b->%lf\n", l->diffuse->r, l->diffuse->g, l->diffuse->b);
   //printf("r->%lf g->%lf b->%lf\n\n", l->specular->r, l->specular->g, l->specular->b);

	viewVector = normalize(subtractPoints((r->origin), p));
	lightVector = normalize(subtractPoints(p, (l->position)));
	normalVector = normalize(subtractPoints(p, (sphereList[sNdx].center)));
	reflectVector = subtractPoints(normalVector, lightVector);

   NdotL = dot(lightVector, normalVector);

	reflectTemp = 2 * NdotL;
	reflectVector.x *= reflectTemp;
	reflectVector.y *= reflectTemp;
	reflectVector.z *= reflectTemp;
	
	a.r = l->ambient.r * sphereList[sNdx].ambient.r;
	a.g = l->ambient.g * sphereList[sNdx].ambient.g;
	a.b = l->ambient.b * sphereList[sNdx].ambient.b;

   if (NdotL > 0. ) {

      //printf("%lf\n", NdotL);
      //printf("%lf %lf %lf\n", sphereList[sNdx].diffuse->r, sphereList[sNdx].diffuse->g, sphereList[sNdx].diffuse->b);
      //printf("%lf %lf %lf\n", l->diffuse->r, l->diffuse->g, l->diffuse->b);

      // Diffuse
      d.r = NdotL * l->diffuse.r * sphereList[sNdx].diffuse.r;
      d.g = NdotL * l->diffuse.g * sphereList[sNdx].diffuse.g;
      d.b = NdotL * l->diffuse.b * sphereList[sNdx].diffuse.b;

      // Specular
      RdotV = pow(dot(reflectVector, viewVector), 2.0);
      s.r = RdotV * l->specular.r * sphereList[sNdx].specular.r;
      s.g = RdotV * l->specular.g * sphereList[sNdx].specular.g;
      s.b = RdotV * l->specular.b * sphereList[sNdx].specular.b;

      //printf("%lf %lf %lf\n\n", d.r, d.g, d.b);
	} else {
      d.r = 0;
      d.g = 0;
      d.b = 0;

      s.r = 0;
      s.g = 0;
      s.b = 0;
   }
   
	total.r = glm::min(1.f, a.r + d.r + s.r);
	total.g = glm::min(1.f,a.g + d.g + s.g);
	total.b = glm::min(1.f,a.b + d.b + s.b);

	//fprintf(stderr, "LIGHT A  r = %lf, g = %lf, b = %lf\n", l->ambient->r, l->ambient->g, l->ambient->b);
	//fprintf(stderr, "LIGHT D  r = %lf, g = %lf, b = %lf\n", l->diffuse->r, l->diffuse->g, l->diffuse->b);
	//fprintf(stderr, "LIGHT S  r = %lf, g = %lf, b = %lf\n", l->specular->r, l->specular->g, l->specular->b);

	//fprintf(stderr, "SPHERE r = %lf, g = %lf, b = %lf\n", total.r, total.g, total.b);	
	//fprintf(stderr, "PHONG  r = %lf, g = %lf, b = %lf\n\n", total.r, total.g, total.b);
	
	return total;
}