Advertisement
Guest User

Untitled

a guest
May 26th, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. class general : public object{
  2. public:
  3. double A,B,C,D,E,F,G,H,I,J,length,height,width;
  4. general(double a,double b,double c,double d,double e,double f,double g,double h,double i,double j,vector3 temp,double len,double hei,double wid)
  5. {
  6. A = a;
  7. B = b;
  8. C = c;
  9. D = d;
  10. E = e;
  11. F = f;
  12. G = g;
  13. H = h;
  14. I = i;
  15. J = j;
  16. reference_point = temp;
  17. length = len;
  18. height = hei;
  19. width = wid;
  20.  
  21. }
  22.  
  23. void draw(){
  24. //write codes for drawing sphere
  25.  
  26.  
  27.  
  28. }
  29.  
  30.  
  31. double * getColorAt(vector3 tempVector){
  32.  
  33. double r[3];
  34.  
  35. for(int i = 0;i<3;i++){
  36. r[i] = color[i];
  37. }
  38.  
  39.  
  40. return r;
  41.  
  42. }
  43.  
  44. vector3 getNormal(vector3 tempInterSectionPoint)
  45. {
  46. double i,j,k;
  47. i = 2*A*tempInterSectionPoint.x + D*tempInterSectionPoint.y + F*tempInterSectionPoint.z + G;
  48. j = 2*B*tempInterSectionPoint.y + D*tempInterSectionPoint.x + E*tempInterSectionPoint.z + H;
  49. k = 2*C*tempInterSectionPoint.z + E*tempInterSectionPoint.y + F*tempInterSectionPoint.x + I;
  50. vector3 finalVector(i,j,k);
  51. return finalVector;
  52.  
  53.  
  54.  
  55. }
  56.  
  57. double getIntersectingT(ray *r)
  58. {
  59.  
  60. double a,b,c,d,t,t1,t2;
  61. vector3 for_t;
  62.  
  63.  
  64. a = A * r->dir.x * r->dir.x + B * r->dir.y * r->dir.y + C * r->dir.z * r->dir.z + D * r->dir.x * r->dir.y + E * r->dir.y * r->dir.z + F * r->dir.x * r->dir.z;
  65. b = 2 * ( r->start.x * r->dir.y * A + r->start.y * r->dir.y * B + r->start.z * r->dir.z * C ) + D * ( r->start.x * r->dir.y + r->dir.x * r->start.y)+ E * ( r->start.y * r->dir.z + r->dir.y * r->start.z ) + F * ( r->start.x * r->dir.z + r->dir.x * r->start.z ) + G * r->dir.x + H * r->dir.y + I * r->dir.z;
  66. c = A * r->start.x * r->start.x + B * r->start.y * r->start.y + C * r->start.z * r->start.z + D * r->start.x * r->start.y + E * r->start.y * r->start.z + F * r->start.x * r->start.z+ G * r->start.x + H * r->start.y + I * r->start.z + J;
  67.  
  68. d = b*b - 4 *a*c;
  69. if(d<0){
  70. return -1;
  71. }
  72. else
  73. {
  74. d = sqrt(d);
  75. t1 = (-b+d)/(2*a);
  76. t2 = (-b-d)/(2*a);
  77. //double t = min(t1,t2);
  78.  
  79.  
  80. }
  81.  
  82. vector3 interSectionPoint1,interSectionPoint2;
  83. interSectionPoint1 = r->start + r->dir*t1;
  84. interSectionPoint2 = r->start + r->dir*t2;
  85.  
  86.  
  87. if(length == 0 && width == 0 && height ==0)
  88. {
  89. t = min(t1,t2);
  90. return t;
  91.  
  92.  
  93. }
  94.  
  95.  
  96. else
  97. {
  98. if(isValid(reference_point,interSectionPoint1,length,0) && isValid(reference_point,interSectionPoint1,width,1) && isValid(reference_point,interSectionPoint1,height,2))
  99. {
  100.  
  101. if(isValid(reference_point,interSectionPoint2,length,0) && isValid(reference_point,interSectionPoint2,width,1) && isValid(reference_point,interSectionPoint2,height,2))
  102. {
  103. t = min(t1,t2);
  104. return t;
  105. }
  106. else
  107. {
  108. return t1;
  109. }
  110.  
  111.  
  112. }
  113. else
  114. {
  115. if(isValid(reference_point,interSectionPoint2,length,0) && isValid(reference_point,interSectionPoint2,width,1) && isValid(reference_point,interSectionPoint2,height,2))
  116. {
  117.  
  118. return t2;
  119. }
  120. else
  121. return -1;
  122.  
  123. }
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. }
  134.  
  135.  
  136.  
  137.  
  138. double intersect(ray *r, double *current_color, int level){
  139.  
  140. double t;
  141. t = getIntersectingT(r);
  142. if(t<=0){
  143.  
  144. return -1;
  145. }
  146. if(level == 0)
  147. {
  148.  
  149. return t;
  150. }
  151.  
  152. vector3 interSectionPoint;
  153. interSectionPoint = r->start + r->dir*t;
  154.  
  155.  
  156. double colorAt[3];
  157.  
  158. //getColor
  159. for(int i = 0;i<3;i++){
  160. colorAt[i] = color[i];
  161. }
  162.  
  163.  
  164.  
  165. //setColor
  166. for(int i = 0;i<3;i++){
  167. current_color[i] = colorAt[i]*co_efficients[0];
  168. }
  169.  
  170. //normalize
  171.  
  172. vector3 normal;
  173. normal = getNormal(interSectionPoint);
  174.  
  175.  
  176.  
  177. double sum_normal = sqrt(pow(normal.x,2)+pow(normal.y,2)+pow(normal.z,2));
  178.  
  179.  
  180. normal.normalize();
  181.  
  182.  
  183.  
  184.  
  185. for(int i = 0;i<lights.size();i++){
  186. vector3 lightDir;
  187. vector3 lightStart;
  188.  
  189. lightDir = lights.at(i) - interSectionPoint;
  190.  
  191.  
  192.  
  193.  
  194.  
  195. double sum = sqrt(pow(lightDir.x,2)+pow(lightDir.y,2)+pow(lightDir.z,2));
  196.  
  197. lightDir.normalize();
  198. lightStart = interSectionPoint + lightDir;
  199.  
  200.  
  201. ray *newL = new ray(lightStart,lightDir);
  202. //ray newL(,tempDir);
  203.  
  204. bool set_flag = false;
  205.  
  206. for(int j = 0;j<objects.size();j++){
  207. double tempT;
  208. tempT = objects.at(j)->getIntersectingT(newL);
  209.  
  210. if(tempT>0 && tempT<sum){
  211. set_flag = true;
  212. break;
  213.  
  214.  
  215. }
  216. }
  217.  
  218. if(set_flag!=true){
  219.  
  220.  
  221. double lambertValue = dot_product(lightDir,normal);
  222. if(lambertValue<0){
  223. lambertValue = 0;
  224.  
  225. }
  226.  
  227. vector3 a;
  228. vector3 tempB;
  229.  
  230. a = r->start - interSectionPoint ;
  231. a.normalize();
  232.  
  233. tempB = (normal*(dot_product(lightDir,normal)*2)) - lightDir;
  234.  
  235. tempB.normalize();
  236.  
  237.  
  238. double phongValue = dot_product(a,tempB);
  239. if(phongValue<0){
  240. phongValue = 0;
  241. }
  242.  
  243.  
  244. //cout<<"Lmabert value is "<<lambertValue<<"phong value is "<<phongValue<<endl;
  245.  
  246.  
  247. for(int p= 0;p<3;p++){
  248. current_color[p]+=lambertValue*co_efficients[1]*colorAt[p];
  249. current_color[p]+= pow(phongValue,Shine)*co_efficients[2]*colorAt[p];
  250.  
  251. }
  252. }
  253.  
  254.  
  255. if(level<recursion_level)
  256. {
  257. vector3 reflection= r->dir - (normal*(dot_product(r->dir,normal)*2));
  258.  
  259. vector3 newLightStart = interSectionPoint + reflection*1 ;
  260. ray *reflectionRay = new ray(newLightStart,reflection);
  261.  
  262. double nearest = -1;
  263.  
  264. double t_min = 999999;
  265. double dummyColor[3];
  266.  
  267. for(int k = 0;k<objects.size();k++){
  268. double t = objects[k] -> intersect(reflectionRay,dummyColor,0);
  269.  
  270.  
  271. if(t<=0)
  272. {
  273. continue;
  274. }
  275.  
  276.  
  277. if(t<t_min){
  278. t_min = t;
  279. nearest = k;
  280. }
  281. }
  282.  
  283. double reflected_color [3];
  284. if(nearest!=-1){
  285.  
  286. t = objects[nearest]->intersect(reflectionRay,reflected_color,level+1);
  287. for(int p = 0;p<3;p++){
  288. current_color[p]+=reflected_color[p]*co_efficients[3];
  289.  
  290. }
  291.  
  292.  
  293. }
  294.  
  295.  
  296.  
  297. for(int h = 0;h<3;h++){
  298. if(current_color[h]>1){
  299. current_color[h] = 1;
  300. }
  301. }
  302.  
  303.  
  304. }
  305.  
  306.  
  307.  
  308.  
  309. }
  310.  
  311.  
  312.  
  313.  
  314.  
  315. return t;
  316.  
  317.  
  318.  
  319.  
  320. }
  321.  
  322.  
  323.  
  324. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement