Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #ifndef __MAPPING_H
  2. #define __MAPPING_H
  3.  
  4. #include <math.h>
  5.  
  6. #include "material.h"
  7.  
  8. // frac bedzie zwracalo czesc ulamkowa liczby:
  9. #define frac(x) ((x)-floor(x))
  10. #define __WP
  11.  
  12. #ifndef __PI
  13. #define __PI
  14.  
  15. const double PI = 3.141592654;
  16.  
  17. #endif
  18.  
  19. /*
  20. Klasa Mapping jest abstrakcyjna. Okresla ona odwzorowanie punktu
  21. z przestrzeni trojwymiarowej na przestrzen dwuwymiarowa. Na podstawie
  22. wspolrzednych w przestrzeni 3d znajdowany jest punkt 2d, odpowiadajacy
  23. barwie obiektu w danym miejscu.
  24.  
  25. Funkcje GetUV zostaly zdefiniowane jako INLINE ze wzgledu na wymagana
  26. szybkosc dzialania (sa wywolywane bardzo czesto), ponadto sa to
  27. jedyne metody w tych klasach, dlatego nie wplywa to na przejrzystosc
  28. */
  29.  
  30. class Mapping
  31. {
  32. public:
  33. virtual Vector GetUV(const ShInfo &a) = 0; // pobranie wspolrzednych 2D
  34. };
  35.  
  36. /*
  37. Spherical mapping class
  38. */
  39.  
  40. class Spherical : public Mapping
  41. {
  42. public:
  43.  
  44. virtual Vector GetUV(const ShInfo &s)
  45. {
  46. /*
  47. IMPORTANT:
  48. - Normal to the surface in given point is found in ShInfo structure and can be obtained from object "s" (s.normal[1])
  49. - There is "PI" constant defined
  50. - There is "acos()" function defined
  51. - Calculated u,v value is (for technical reasons) returned as vector of [u, v, 0]
  52. - Values x, y, z, R are UNKNOWN and must be eliminated from equestions in manual mathematically before applying those equations in code!
  53. */
  54.  
  55. double u = 0;
  56. double v = 0;
  57. v = acos(s.normal[1]) / 3.14;
  58. u = acos(s.normal[0] / sin(3.14*v) / 2 * 6.28);
  59.  
  60. return Vector(u, v, 0);
  61.  
  62. }
  63. };
  64.  
  65. /*
  66. Mapowanie cylindryczne uzywane jest praktycznie tylko do walcow
  67. oraz stozkow.
  68. */
  69.  
  70. class Cylindrical : public Mapping
  71. {
  72. private:
  73. double height; // wysokosc tekstury
  74.  
  75. public:
  76. Cylindrical(double a) : height(a) {}
  77.  
  78. virtual Vector GetUV(const ShInfo &s)
  79. {
  80. double v = s.point[1] / height; // wspolrzedna y
  81. double u = acos(s.normal[0]) / PI; // wsp. x
  82.  
  83. v = frac(v); // tekstura powtarzalna
  84. return Vector(u, fabs(v), 0);
  85. }
  86. };
  87.  
  88. /*
  89. Mapowanie plaszczyznowe sluzy do oblozenia tekstura obiektow plaskich
  90. (czyli np. plaszczyzn). Tekstura jest jakby rzucana z rzutnika przezroczy
  91. na dany obiekt.
  92. */
  93.  
  94. class Planar : public Mapping
  95. {
  96. private:
  97. double x, y; // rozmiary tekstury
  98.  
  99. public:
  100. Planar(double x1, double y1) : x(x1), y(y1) {}
  101.  
  102. virtual Vector GetUV(const ShInfo &a)
  103. {
  104. double max = fabs(a.normal[0]);
  105. double u, v;
  106.  
  107. if (fabs(a.normal[1]) > max)
  108. {
  109. max = fabs(a.normal[1]);
  110. if (fabs(a.normal[2]) > max)
  111. {
  112. u = a.point[0];
  113. v = a.point[2];
  114. }
  115. else
  116. {
  117. u = a.point[0];
  118. v = a.point[2];
  119. }
  120. }
  121. else
  122. {
  123. if (fabs(a.normal[2]) > max)
  124. {
  125. u = a.point[0];
  126. v = a.point[1];
  127. }
  128. else
  129. {
  130. u = a.point[1];
  131. v = a.point[2];
  132. }
  133. }
  134.  
  135. u /= x;
  136. u -= floor(u);
  137. v /= y;
  138. v -= floor(v);
  139.  
  140. return Vector(fabs(u), fabs(v), 0);
  141. }
  142.  
  143. void SetXY(double x1, double y1) // ustalenie nowych rozmiarow tekstury
  144. {
  145. x = x1;
  146. y = y1;
  147. }
  148. };
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement