Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __MAPPING_H
- #define __MAPPING_H
- #include <math.h>
- #include "material.h"
- // frac bedzie zwracalo czesc ulamkowa liczby:
- #define frac(x) ((x)-floor(x))
- #define __WP
- #ifndef __PI
- #define __PI
- const double PI = 3.141592654;
- #endif
- /*
- Klasa Mapping jest abstrakcyjna. Okresla ona odwzorowanie punktu
- z przestrzeni trojwymiarowej na przestrzen dwuwymiarowa. Na podstawie
- wspolrzednych w przestrzeni 3d znajdowany jest punkt 2d, odpowiadajacy
- barwie obiektu w danym miejscu.
- Funkcje GetUV zostaly zdefiniowane jako INLINE ze wzgledu na wymagana
- szybkosc dzialania (sa wywolywane bardzo czesto), ponadto sa to
- jedyne metody w tych klasach, dlatego nie wplywa to na przejrzystosc
- */
- class Mapping
- {
- public:
- virtual Vector GetUV(const ShInfo &a) = 0; // pobranie wspolrzednych 2D
- };
- /*
- Spherical mapping class
- */
- class Spherical : public Mapping
- {
- public:
- 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[1])
- - 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 = 0;
- double v = 0;
- v = acos(s.normal[1]) / 3.14;
- u = acos(s.normal[0] / sin(3.14*v) / 2 * 6.28);
- return Vector(u, v, 0);
- }
- };
- /*
- Mapowanie cylindryczne uzywane jest praktycznie tylko do walcow
- oraz stozkow.
- */
- class Cylindrical : public Mapping
- {
- private:
- double height; // wysokosc tekstury
- public:
- Cylindrical(double a) : height(a) {}
- virtual Vector GetUV(const ShInfo &s)
- {
- double v = s.point[1] / height; // wspolrzedna y
- double u = acos(s.normal[0]) / PI; // wsp. x
- v = frac(v); // tekstura powtarzalna
- return Vector(u, fabs(v), 0);
- }
- };
- /*
- Mapowanie plaszczyznowe sluzy do oblozenia tekstura obiektow plaskich
- (czyli np. plaszczyzn). Tekstura jest jakby rzucana z rzutnika przezroczy
- na dany obiekt.
- */
- class Planar : public Mapping
- {
- private:
- double x, y; // rozmiary tekstury
- public:
- Planar(double x1, double y1) : x(x1), y(y1) {}
- virtual Vector GetUV(const ShInfo &a)
- {
- double max = fabs(a.normal[0]);
- double u, v;
- if (fabs(a.normal[1]) > max)
- {
- max = fabs(a.normal[1]);
- if (fabs(a.normal[2]) > max)
- {
- u = a.point[0];
- v = a.point[2];
- }
- else
- {
- u = a.point[0];
- v = a.point[2];
- }
- }
- else
- {
- if (fabs(a.normal[2]) > max)
- {
- u = a.point[0];
- v = a.point[1];
- }
- else
- {
- u = a.point[1];
- v = a.point[2];
- }
- }
- u /= x;
- u -= floor(u);
- v /= y;
- v -= floor(v);
- return Vector(fabs(u), fabs(v), 0);
- }
- void SetXY(double x1, double y1) // ustalenie nowych rozmiarow tekstury
- {
- x = x1;
- y = y1;
- }
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement