Пример #1
0
Vec scatteredReflectedRay(Vec const& N, Vec const& I)
{
	Vec O = I - 2*N*dot(I, N*frand(0.5,1.5));
	//O.x += frand(0.0, 0.1);
	// slightly rotate vector?
	return O.normalized();
}
Пример #2
0
int main(int argc, char* argv[]) {
	const int w = 512, h = 512, samps = (argc == 2) ? atoi(argv[1]) : 1 << 13;

	// main rendering process
	const Ray cam(Vec(50, 45, 290), Vec(0, -0.042612, -1).normalized());
	const Vec cx = Vec(w * 0.25 / h), cy = (cx % cam.d).normalized() * 0.25;
	Vec *c = new Vec[w * h];
	#pragma omp parallel for schedule(dynamic, 1)
	for (int y = 0; y < h; y++) {
		fprintf(stderr, "\rRendering %5.2f%%", 100.0 * y / (h - 1));
		for (int x = 0; x < w; x++) {
			const Vec d = cx * ((x + 0.5) / w - 0.5) + cy * (-(y + 0.5) / h + 0.5) + cam.d;
			const int i = x + y * w;
			c[i] = trace(Ray(cam.o + d * 140, d.normalized()), i, samps);
		}
	}

	// save the HDR image
	float *cf = new float[w * h * 3];
	for (int i = 0; i < w * h; i++) {
		for (int j = 0; j < 3; j++) cf[i * 3 + j] = c[i][j];
	}
	FILE *f = fopen("image.pfm", "wb"); 
	fprintf(f, "PF\n%d %d\n%6.6f\n", w, h, -1.0);
	fwrite(cf, w * h * 3, sizeof(float), f);
}
Пример #3
0
inline double bssrdf(const Vec& xi, const Vec& ni, const Vec& wi, const Vec& xo, const Vec& no, const Vec& wo, const int j) {
	// distance
	const Vec xoxi = xo - xi;
	const double r = xoxi.len();

	// modified normal
	const Vec ni_s = (xoxi.normalized()) % ((ni % xoxi).normalized());

	// directions of ray sources
	const double nnt = 1.0 / eta, ddn = -wi.dot(ni);
	const Vec wr = (wi * -nnt - ni * (ddn * nnt + sqrt(1.0 - nnt * nnt * (1.0 - ddn * ddn)))).normalized();
	const Vec wv = wr - ni_s * (2.0 * wr.dot(ni_s));

	// distance to real sources
	const double cos_beta = -sqrt((r * r - xoxi.dot(wr) * xoxi.dot(wr)) / (r * r + de[j] * de[j]));
	double dr;
	const double mu0 = -no.dot(wr);
	if (mu0 > 0.0) {
		dr = sqrt((D[j] * mu0) * ((D[j] * mu0) - de[j] * cos_beta * 2.0) + r * r);
	} else {
		dr = sqrt(1.0 / (3.0 * sigma_t[j] * 3.0 * sigma_t[j]) + r * r);
	}

	// distance to virtual source
	const Vec xoxv = xo - (xi + ni_s * (2.0 * A * de[j]));
	const double dv = xoxv.len();

	// BSSRDF
	const double result = Sp_d(xoxi, wr, dr, no, j) - Sp_d(xoxv, wv, dv, no, j);

	// clamping to zero
	return (result < 0.0) ? 0.0 : result;
}
Пример #4
0
Vec keyVelocity()
{
    using std::make_pair;

    std::pair<sf::Keyboard::Key, Vec> keyDirs[] = {
        make_pair(sf::Keyboard::W, Vec(0, 0, 1)),
        make_pair(sf::Keyboard::A, Vec(-1, 0, 0)),
        make_pair(sf::Keyboard::S, Vec(0, 0, -1)),
        make_pair(sf::Keyboard::D, Vec(1, 0, 0)),
        make_pair(sf::Keyboard::Space, Vec(0, 1, 0)),
        make_pair(sf::Keyboard::LControl, Vec(0, -1, 0)),
    };

    Vec velocity;

    for (int i = 0; i < sizeof(keyDirs) / sizeof(keyDirs[0]); ++i)
    {
        if (sf::Keyboard::isKeyPressed(keyDirs[i].first))
        {
            velocity = velocity + keyDirs[i].second;
        }
    }

    return velocity.normalized();
}
Пример #5
0
Vec uniformRandomRay(Vec const& N)
{
  Vec T, S;
  genOrtho(N, T, S);
  float z = sqrt(frand(0,1));
  float theta = acos(z);
  float phi = frand(0, 2.0f*M_PI);
  //Vec V = Vec(sin(theta)*cos(phi), sin(theta)*sin(phi), z);
  Vec Vt = sin(theta)*cos(phi) * T + sin(theta)*sin(phi) * S + z * N;
  return Vt.normalized();
}
void DynamicShape::rotateOnAxis(Scalar force, Vec axis){

    const Scalar R = prefs.R; // radius of sphere which moves exactly as the user intended
    const Scalar APPLICATION_DISTANCE = prefs.applicationDistance;
    //if > 0: molec manipulated from inside, outside follows
    //if = 0: mol moves rigidly (barring centrifugal forces)
    //if < 0: mol manipuladed from the outside, inside follows

    axis = axis.normalized();
    force *= 0.004;

    for (uint i = 0; i < ball.size(); ++i) {

        Pos& pos = ball[i].currPos;
        Pos& prev = ball[i].prevPos;
        Pos proj = projectOnAxis( pos, axis, barycenter);

        //pos += cross(axis,pos - proj)*force;


        Vec k = pos - proj;

        Scalar nonlin =  powf(dot(k,k)/(R*R), APPLICATION_DISTANCE);

        if(nonlin > 0.001){
            Vec displacement = cross(axis,k)*(force/nonlin);
            pos += displacement;

            // =========================
            // OPTIMIZED FOR trail = 1;
            // =========================
            /*
            Scalar& trail = prefs.trailing;
            if((displacement*(1-trail)).Norm() <= prefs.trailThres){ //se norm <= a soglia
                prev += displacement*trail;
            }
            else{
               Scalar f = (displacement.Norm() - prefs.trailThres)/(displacement*trail).Norm();
               prev += displacement*(trail*f);

            }
            */
            //OPTIMIZED FOR trail = 1
            prev += displacement;

        }
    }
}
Пример #7
0
Vec reflectedRay(Vec const& N, Vec const& I)
{
  Vec O = 2*N*dot(I, N) - I;
  return O.normalized();
}