Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1.  
  2. #include "shape.h"
  3.  
  4. constantMaterial shape::defaultMaterial;
  5.  
  6. // The default constructor sets default colors and builds all the transforms
  7. shape::shape()
  8. {
  9. surfaceColor = rgb::white;
  10. specularColor = rgb::white;
  11. shininess = 10.0;
  12.  
  13. translation = point(0, 0, 0);
  14. scale = vector(1, 1, 1);
  15. rotX = rotY = rotZ = 0.0;
  16.  
  17. totalTransform = matrix::Identity(4);
  18. inverseTransform = matrix::Identity(4);
  19. inverseTranspose = matrix::Identity(4);
  20. canShadow = true;
  21. canBeShadowed = true;
  22.  
  23. objectMat = &defaultMaterial;
  24. }
  25.  
  26. // CalculateIntersection transforms the ray and then calls "Intersect" to
  27. // get the intersection with the untransformed object. Then it must
  28. // transform the results. Look in the derived classes for the definition
  29. // of the "Intersect" method
  30. bool shape::CalculateIntersection(const ray &R, intersection &I)
  31. {
  32. // Hit the ray with inv(M)
  33. ray RTrans = TransformRay(R);
  34.  
  35. // Calculate intersection with object, and if there is one,
  36. // process it
  37. if (Intersect(RTrans, I))
  38. {
  39. // Transform the result to get the true intersection
  40. I.p = R.PointOnRay(I.t);
  41. I.n = TransformNormal(I.n);
  42. I.n.Normalize();
  43. I.v = -R.v;
  44. I.v.Normalize();
  45. return true;
  46. }
  47. else
  48. return false;
  49. }
  50.  
  51. vector shape::TransformNormal(const vector &n)
  52. {
  53. // To Do
  54. //
  55. // Returned the normal transformed by this object's transform.
  56. // Hint: how do you transform a normal?
  57.  
  58. //To get actual intersection data.
  59. // p = Mp'
  60. // n = (M^-1)^t * n'
  61. // t = t'
  62. vector returnN;
  63.  
  64. returnN = this->inverseTranspose * n;
  65.  
  66. return returnN;
  67. }
  68.  
  69. ray shape::TransformRay(const ray &r)
  70. {
  71. // To Do
  72. //
  73. // Return the ray transformed by this object's transform.
  74. // Hint: how do you transform a ray?
  75. ray result;
  76.  
  77. result.p = this->inverseTransform * r.p;
  78. result.v = this->inverseTransform * r.v;
  79.  
  80. //intersect r' with original untransformed object to get p',
  81. // t', n'.
  82.  
  83. return result;
  84. }
  85.  
  86. void shape::SetTranslation(const point &p)
  87. {
  88. translation = p;
  89. RecalculateMatrices();
  90. }
  91.  
  92. void shape::SetXRotation(double angle)
  93. {
  94. rotX = angle;
  95. RecalculateMatrices();
  96. }
  97.  
  98. void shape::SetYRotation(double angle)
  99. {
  100. rotY = angle;
  101. RecalculateMatrices();
  102. }
  103.  
  104. void shape::SetZRotation(double angle)
  105. {
  106. rotZ = angle;
  107. RecalculateMatrices();
  108. }
  109.  
  110. void shape::SetRotation(double x, double y, double z)
  111. {
  112. rotX = x;
  113. rotY = y;
  114. rotZ = z;
  115. RecalculateMatrices();
  116. }
  117.  
  118. void shape::SetScale(const vector &s)
  119. {
  120. scale = s;
  121. RecalculateMatrices();
  122. }
  123.  
  124. void shape::RecalculateMatrices(void)
  125. {
  126. // These are members of the shape class
  127. totalTransform = matrix(4, 4, 1.0);
  128. inverseTransform = matrix(4, 4, 1.0);
  129. inverseTranspose = matrix(4, 4, 1.0);
  130.  
  131. matrix S, Rx, Ry, Rz, T;
  132. matrix S_Inv, Rx_Inv, Ry_Inv, Rz_Inv, T_Inv;
  133.  
  134. // To Do
  135. //
  136. // Compute the totalTransform as the product of the scale, rotation
  137. // and translation. Remember, we want the matrix to apply the
  138. // Scale first then the x-Rotation, y-Rotation and z-Rotation and
  139. // then finally the Translation.
  140. //
  141. // Then compute the inverse by computing the inverse of the individual
  142. // matrices, and multiply them together in the proper order to
  143. // compute the inverseTransform.
  144. //
  145. // Finally, set the inverseTranspose to the transpose of the
  146. // inverseTransform
  147. //
  148.  
  149. S.MakeScale(this->scale[0],this->scale[1],this->scale[2]);
  150. Rx.MakeRotationX(this->rotX);
  151. Ry.MakeRotationY(this->rotY);
  152. Rz.MakeRotationZ(this->rotZ);
  153. T.MakeTranslation(this->translation[0],this->translation[1],this->translation[2]);
  154.  
  155. S_Inv.MakeScale((1/this->scale[0]),(1/this->scale[1]),(1/this->scale[2]));
  156. Rx_Inv.MakeRotationX(-this->rotX);
  157. Ry_Inv.MakeRotationY(-this->rotY);
  158. Rz_Inv.MakeRotationZ(-this->rotZ);
  159. T_Inv.MakeTranslation(-this->translation[0],-this->translation[1],-this->translation[2]);
  160.  
  161. totalTransform = (T * (Rz * (Ry * (Rx * (S * totalTransform)))));
  162. /*totalTransform = T * Rz * Ry * Rx * S;
  163. inverseTransform = S_Inv * Rx_Inv * Ry_Inv * Rz_Inv * T_Inv;*/
  164. inverseTransform = (S_Inv * (Rx_Inv * (Ry_Inv * (Rz_Inv * (T_Inv * inverseTransform)))));
  165. inverseTranspose = inverseTransform.Transpose();
  166. }
  167.  
  168. rgb shape::ApplyMaterial(intersection &inter, LinkedList<light *> &lights, LinkedList<shape *> &shapes)
  169. {
  170. return objectMat->ReflectLights(inter, lights, shapes);
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement