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.08 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)
  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 v = acos(s.normal[1]) / PI;//s.point[1] / height; // wspolrzedna y
  56. double u = acos(s.normal[0] / sin(PI*v)) / (2 * PI); // wsp. x
  57.  
  58. //v = frac(v); // tekstura powtarzalna
  59. return Vector(u, v, 0);//fabs(v), 0);
  60.  
  61. }
  62. };
  63.  
  64. /*
  65. Mapowanie cylindryczne uzywane jest praktycznie tylko do walcow
  66. oraz stozkow.
  67. */
  68.  
  69. class Cylindrical : public Mapping
  70. {
  71. private:
  72. double height; // wysokosc tekstury
  73.  
  74. public:
  75. Cylindrical(double a) : height(a) {}
  76.  
  77. virtual Vector GetUV(const ShInfo &s)
  78. {
  79. double v = s.point[1] / height; // wspolrzedna y
  80. double u = acos(s.normal[0]) / PI; // wsp. x
  81.  
  82. v = frac(v); // tekstura powtarzalna
  83. return Vector(u, fabs(v), 0);
  84. }
  85. };
  86.  
  87. /*
  88. Mapowanie plaszczyznowe sluzy do oblozenia tekstura obiektow plaskich
  89. (czyli np. plaszczyzn). Tekstura jest jakby rzucana z rzutnika przezroczy
  90. na dany obiekt.
  91. */
  92.  
  93. class Planar : public Mapping
  94. {
  95. private:
  96. double x, y; // rozmiary tekstury
  97.  
  98. public:
  99. Planar(double x1, double y1) : x(x1), y(y1) {}
  100.  
  101. virtual Vector GetUV(const ShInfo &a)
  102. {
  103. double max = fabs(a.normal[0]);
  104. double u, v;
  105.  
  106. if (fabs(a.normal[1]) > max)
  107. {
  108. max = fabs(a.normal[1]);
  109. if (fabs(a.normal[2]) > max)
  110. {
  111. u = a.point[0];
  112. v = a.point[2];
  113. }
  114. else
  115. {
  116. u = a.point[0];
  117. v = a.point[2];
  118. }
  119. }
  120. else
  121. {
  122. if (fabs(a.normal[2]) > max)
  123. {
  124. u = a.point[0];
  125. v = a.point[1];
  126. }
  127. else
  128. {
  129. u = a.point[1];
  130. v = a.point[2];
  131. }
  132. }
  133.  
  134. u /= x;
  135. u -= floor(u);
  136. v /= y;
  137. v -= floor(v);
  138.  
  139. return Vector(fabs(u), fabs(v), 0);
  140. }
  141.  
  142. void SetXY(double x1, double y1) // ustalenie nowych rozmiarow tekstury
  143. {
  144. x = x1;
  145. y = y1;
  146. }
  147. };
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement