Untitled

                Never    
C++
       
int Sphere::FindInter(const Ray &ray1, double &t)
{

	/* 
	IMPORTANT!!!

	Tasks: 

	This method's purpose are the following steps ONLY:
	1) calculation of intersection equation roots (=> lab manual)
	2) if there is proper solution (visible intersection => lab manual + 
	technical information below), return the corresponding root value
	
	Additional information:
	
	a) observer position can be obtained by calling "Point()" method from ray object
	b) ray direction can be obtained by calling "Direction()" method from ray object
	c) position and radius of the sphere are fields of the Sphere class

	Vector - this class represents vector; it can be accessed with "[]" operators, example:
			X = w[0], Y = w[1], Z = w[2]
			(where "w" is object of class Vector)
	Vector Normalize(const Vector &w) - function which normalizes given vector and returns normalized vector

	Technical information:

	1) All calculations should be made using "double" type
	2) A intersection root is assumed positive when it's greater than 0.001 (not 0.0!) 
		- this is due to loss of precision during calculations
	   This is only true for intersection roots (t, t1, ti etc.), not ANY root (sqrt...)!
	3) Calculated root must be checked against parameter "t" which is passed to this method:
		- if the newly calculated root is greater or equal to current "t" value we discard
			the found root (= no intersection found)
		This is because we're interested only in the closest intersection, and parameter t 
		passed to this method contains recently found closest intersection.
	4) If this method finds intersection, it must assign the root value to parameter t and return "1".
	5) If no intersection is found, this method must return "0" and leave parameter t intact.
	*/

	double	a, b, c; // wsp. rownania
	double	delta;
	double	t1, t2; // pierwiastki
	Vector	temp = ray1.Point() - pos; // wektor pomocniczy
	Vector	dir = ray1.Direction(); // wektor kierunkowy

	a = Dot(dir, dir);
	b = 2 * Dot(dir, temp);
	c = Dot(temp, temp) - rr; // parametry rownania kwadratowego

	delta = b * b - 4.0 * a * c; // rozwiazujemy rownanie kwadratowe by znalezc przeciecie

	if (delta < 0)
		return 0; // brak przeciec
	else
	{
		delta = sqrt(delta);

		t1 = (-b - delta) / (2.0 * a); // obliczenie pierwiastkow...
		if (fabs(t1) < 0.01) t1 = -1.0; //... z zabezpieczeniem na zbyt male wartosci

		t2 = (-b + delta) / (2.0 * a);
		if (fabs(t2) < 0.01) t2 = -1.0;

		if (t1 < 0)
			if (t2 < t)
				if (t2 > 0)
				{
					t = t2;
					return 1; // znaleziono przeciecie
				}
				else
					return 0; // brak przeciec
			else
				return 0; // brak...
		else
			if (t2 < 0)
				if (t1 < t)
					if (t1 > 0)
					{
						t = t1;
						return 1; // przeciecie
					}
					else return 0; // brak
				else
					return 0; // brak
			else
			{
				t1 = t1 < t2 ? t1 : t2; // znajdz mniejszy
				if (t1 < t)
				{
					t = t1;
					return 1; // okay
				}
				else
					return 0; // brak
			}
	}

	return 0;
}



virtual Vector GetUV(const ShInfo &s)
	{
		/*
		  IMPORTANT: 
		  - Normal to the surface in given point is found in ShInfo structure and can be obtained from object "s" (s.normal)
		  - There is "PI" constant defined
		  - There is "acos()" function defined
		  - Calculated u,v value is (for technical reasons) returned as vector of [u, v, 0]
		  - Values x, y, z, R are UNKNOWN and must be eliminated from equestions in manual mathematically before applying those equations in code!
		*/
		double u;
		double v;
		v = acos(s.normal[1]) / PI;
		u = acos(s.normal[0] / sin(PI*v)) / (2 * PI);
		return Vector(u, v, 0);

	}


int Plane::FindInter(const Ray &ray, double &t)
{
	// Uwaga: parametry A, B, C plaszczyzny to po prostu skladowe jej wektora normalnego
	double x0 = ray.Point()[0];
	double y0 = ray.Point()[1];
	double z0 = ray.Point()[2];

	double xd = ray.Direction()[0];
	double yd = ray.Direction()[1];
	double zd = ray.Direction()[2];

	double A = normal[0];
	double B = normal[1];
	double C = normal[2];
	double D = d;

	double created_t;
	created_t = -((A*x0 + B*y0 + C*z0 + D) / (A*xd + B*yd + C*zd));
	if (created_t < 0.01) {
		return 0;
	}
	if (created_t <t) {
		t = created_t;
		return 1;
	}

		return 0;

	
}

Raw Text