Advertisement
Guest User

Untitled

a guest
May 26th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 45.01 KB | None | 0 0
  1. #include "bitmap_image.hpp"
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<math.h>
  6. #include<vector>
  7. #include<stack>
  8. #include<iostream>
  9.  
  10. #include <windows.h>
  11. #include <glut.h>
  12.  
  13. #define pi (2*acos(0.0))
  14.  
  15. using std::vector;
  16. using namespace::std;
  17.  
  18. double image_height = 768;
  19. double image_width=768;
  20. double window_height=500;
  21. double window_width= 500;
  22. double VIEW_ANGLE = 80;
  23.  
  24. double cameraHeight;
  25. double cameraAngle;
  26. int drawgrid;
  27. int drawaxes;
  28. double angle;
  29. double height = 60;
  30. double radius = 20;
  31.  
  32. int RecursionLevel=4;
  33.  
  34.  
  35.  
  36. struct point
  37. {
  38. double x,y,z;
  39. };
  40.  
  41. double Magnitude(point a){
  42. double val;
  43. val = a.x*a.x+a.y*a.y+a.z*a.z;
  44. val = sqrt(val);
  45. return val;
  46. }
  47.  
  48. point VectorSub(point a, point b){
  49. point Sub;
  50. Sub.x = a.x-b.x;
  51. Sub.y = a.y-b.y;
  52. Sub.z = a.z-b.z;
  53.  
  54. return Sub;
  55. }
  56.  
  57. point CrossProduct(point a, point b){
  58. point cross;
  59.  
  60. cross.x = a.y*b.z - a.z *b.y;
  61. cross.y = -(a.x*b.z - a.z*b.x);
  62. cross.z = a.x*b.y-b.x*a.y;
  63.  
  64. return cross;
  65. }
  66.  
  67. double DotProduct(point a, point b){
  68. double val;
  69.  
  70. val = a.x*b.x + a.y*b.y + a.z*b.z;
  71. //cout <<"a.x*b.x" << a.x*b.x<<endl;
  72. //cout <<"a.y*b.y" << a.y*b.y <<endl;
  73.  
  74. return val;
  75. }
  76.  
  77.  
  78.  
  79. point pos,u,r,l;
  80. double tempX;
  81. double tempY;
  82. double tempZ;
  83.  
  84.  
  85.  
  86. class Ray{
  87. public:
  88. point start;
  89. point dir;
  90.  
  91. Ray(point start1, point dir1){
  92. start = start1;
  93. dir = dir1;
  94. }
  95. };
  96.  
  97. point Normalize(point point1){
  98. double value1= sqrt(point1.x*point1.x+ point1.y*point1.y+point1.z*point1.z);
  99. point1.x = point1.x/value1;
  100. point1.y = point1.y/value1;
  101. point1.z = point1.z/value1;
  102.  
  103. return point1;
  104. }
  105.  
  106.  
  107. class Object{
  108. public:
  109. point reference_point;
  110. double height,width,length;
  111. int shine;
  112. double color[3];
  113. double coefficients[4];
  114.  
  115. Object(){
  116.  
  117. }
  118. virtual double getIntersectingT(Ray r){
  119. return -1;
  120. }
  121. virtual void draw(){};
  122. virtual double intersect(Ray r, double *current_color, int level){
  123. return -1;
  124. }
  125. void setColor(double r, double g, double b){
  126. color[0] = r;
  127. color[1] = g;
  128. color[2] = b;
  129. }
  130. void setShine(int a) {
  131. shine = a;
  132. }
  133. void setCoefficients(double a, double b, double c, double d){
  134. coefficients[0] = a;
  135. coefficients[1] = b;
  136. coefficients[2] = c;
  137. coefficients[3] = d;
  138. }
  139. };
  140.  
  141. vector<Object*> Objects;
  142. vector<point> lights;
  143.  
  144. void drawSphere(double radius)
  145. {
  146. struct point points[100][100];
  147. int i,j;
  148. double h,r;
  149. int stacks = 16;
  150. int slices = 40;
  151. for(i=0;i<=stacks;i++)
  152. {
  153. h=radius*sin(((double)i/(double)stacks)*(pi/2));
  154. r=radius*cos(((double)i/(double)stacks)*(pi/2));
  155. for(j=0;j<=slices;j++)
  156. {
  157. points[i][j].x=r*cos(((double)j/(double)slices)*2*pi);
  158. points[i][j].y=r*sin(((double)j/(double)slices)*2*pi);
  159. points[i][j].z=h;
  160. }
  161. }
  162. //draw quads using generated points
  163. for(i=0;i<stacks;i++)
  164. {
  165. //glColor3f(1,0,0);
  166. for(j=0;j<slices;j++)
  167. {
  168. glBegin(GL_QUADS);{
  169. //upper hemisphere
  170. glVertex3f(points[i][j].x,points[i][j].y,points[i][j].z);
  171. glVertex3f(points[i][j+1].x,points[i][j+1].y,points[i][j+1].z);
  172. glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,points[i+1][j+1].z);
  173. glVertex3f(points[i+1][j].x,points[i+1][j].y,points[i+1][j].z);
  174. //lower hemisphere
  175. glVertex3f(points[i][j].x,points[i][j].y,-points[i][j].z);
  176. glVertex3f(points[i][j+1].x,points[i][j+1].y,-points[i][j+1].z);
  177. glVertex3f(points[i+1][j+1].x,points[i+1][j+1].y,-points[i+1][j+1].z);
  178. glVertex3f(points[i+1][j].x,points[i+1][j].y,-points[i+1][j].z);
  179. }glEnd();
  180. }
  181. }
  182. }
  183. class Sphere: public Object
  184. {
  185. public:
  186. Sphere(point Center, double Radius){
  187. reference_point = Center;
  188. length = Radius;
  189. }
  190. void draw(){
  191. glColor3f(color[0],color[1], color[2]);
  192. glPushMatrix();{
  193. glTranslatef(reference_point.x, reference_point.y, reference_point.z);
  194. drawSphere(length);
  195. }
  196. glPopMatrix();
  197. }
  198.  
  199. double getIntersectingT(Ray r){
  200. double a = 1;
  201. double b = 2*DotProduct(r.dir, VectorSub(r.start, reference_point));
  202. //cout <<"b: " << b <<endl;
  203. double c = Magnitude(VectorSub(r.start,reference_point))*Magnitude(VectorSub(r.start,reference_point)) - length*length;
  204. //cout <<"c: " << c <<endl;
  205.  
  206. double d = b*b - 4*a*c;
  207. //cout << d<<endl;
  208.  
  209. if(d<0) return -1;
  210.  
  211. d= sqrt(d);
  212.  
  213. double t1 = (-b+d)/(2*a);
  214. double t2 = (-b-d)/(2*a);
  215. //cout << "t1: "<<t1<<endl;
  216. //cout <<"t2: " << t2<<endl;
  217.  
  218.  
  219.  
  220. if(t1>0 && t2>0){
  221. if(t1<t2){
  222. //cout << t1 << endl;
  223. //cout << pos.x + r.dir.x*t1<<endl;
  224. //cout << pos.y + r.dir.y*t1<<endl;
  225. //cout << pos.z +r.dir.z*t1 << endl;
  226. return t1;
  227. }
  228. else{
  229. //cout << t2<<endl;
  230. return t2;
  231. }
  232. }
  233. else if(t1<0 && t2<0){
  234. //cout << pos.x - r.dir.x<<endl;
  235. //cout << pos.y - r.dir.y<<endl;
  236. //cout << pos.z -r.dir.z << endl;
  237. //cout <<"Eikhane"<<endl;
  238. return -1;
  239. }
  240. else if(t1>0 && t2<0){
  241. //cout << t2<<endl;
  242. return t1;
  243. }
  244. else if(t1<0 && t2>0){
  245. //cout << t2<<endl;
  246. return t2;
  247. }
  248. }
  249. void setColorAt(double* current_color, double* colorAt){
  250. current_color[0] = colorAt[0]*coefficients[0];
  251. current_color[1] = colorAt[1]*coefficients[0];
  252. current_color[2] = colorAt[2]*coefficients[0];
  253. }
  254.  
  255. point getNormal(point intersectionPoint){
  256. point Normal;
  257.  
  258. Normal.x = intersectionPoint.x - reference_point.x;
  259. Normal.y = intersectionPoint.y - reference_point.y;
  260. Normal.z = intersectionPoint.z - reference_point.z;
  261.  
  262. Normal = Normalize(Normal);
  263.  
  264. return Normal;
  265. }
  266.  
  267. point getReflection(Ray r, point intersectionPoint, point Normal){
  268. point reflection;
  269. double a = 2*DotProduct(r.dir,Normal);
  270.  
  271. reflection.x = -a*Normal.x + r.dir.x;
  272. reflection.y = -a*Normal.y + r.dir.y;
  273. reflection.z = -a*Normal.z + r.dir.z;
  274.  
  275. reflection = Normalize(reflection);
  276.  
  277. return reflection;
  278. }
  279.  
  280. double intersect(Ray r, double *current_color, int level){
  281. double t = getIntersectingT(r);
  282.  
  283.  
  284. if(t<=0)
  285. return -1;
  286. if(level == 0 )
  287. return t;
  288. point intersectionPoint;
  289. intersectionPoint.x = r.start.x + r.dir.x*t;
  290. intersectionPoint.y = r.start.y + r.dir.y*t;
  291. intersectionPoint.z = r.start.z + r.dir.z*t;
  292.  
  293. double* colorAt = new double[3];
  294. colorAt = color;
  295.  
  296. setColorAt(current_color,colorAt);
  297.  
  298. point normal;
  299. point reflection;
  300.  
  301. normal = getNormal(intersectionPoint);
  302. reflection = getReflection(r,intersectionPoint,normal);
  303.  
  304. for(int i=0 ;i<lights.size(); i++){
  305. point Start;
  306. point Direction;
  307.  
  308. Direction = VectorSub(lights.at(i), intersectionPoint);
  309. Direction = Normalize(Direction);
  310. Start.x = intersectionPoint.x + Direction.x;
  311. Start.y = intersectionPoint.y + Direction.y;
  312. Start.z = intersectionPoint.z + Direction.z;
  313.  
  314. Ray L(Start,Direction);
  315.  
  316. double Lambert = max(DotProduct(L.dir,normal),0.0);
  317.  
  318. point V;
  319. V.x = -r.dir.x;
  320. V.y = -r.dir.y;
  321. V.z = -r.dir.z;
  322.  
  323. point R;
  324. double scalar = 2*DotProduct(L.dir,normal);
  325. R.x = scalar*normal.x - L.dir.x;
  326. R.y = scalar*normal.y - L.dir.y;
  327. R.z = scalar*normal.z - L.dir.z;
  328.  
  329.  
  330. double phong = max(pow(DotProduct(R,V),shine),0.0);
  331.  
  332. current_color[0] += Lambert*coefficients[1]*colorAt[0]+phong*coefficients[2]*colorAt[0];
  333. current_color[1] += Lambert*coefficients[1]*colorAt[1]+phong*coefficients[2]*colorAt[1];
  334. current_color[2] += Lambert*coefficients[1]*colorAt[2]+phong*coefficients[2]*colorAt[2];
  335. }
  336. if(level < RecursionLevel){
  337. point start;
  338. start.x = intersectionPoint.x + reflection.x;
  339. start.y = intersectionPoint.y + reflection.y;
  340. start.z = intersectionPoint.z + reflection.z;
  341.  
  342. Ray reflectionRay(start, reflection);
  343.  
  344.  
  345. int nearest = -1;
  346.  
  347. double tmin= 999999;
  348.  
  349. double dummyColorAt[3];
  350. for(int k=0; k<Objects.size(); k++){
  351.  
  352. double t= Objects.at(k)->intersect(reflectionRay,dummyColorAt,0);
  353. //cout << t<<endl;
  354.  
  355. if(t<=0) continue;
  356.  
  357. if(t<tmin){
  358. tmin = t;
  359. nearest = k;
  360. }
  361. //double t = Objects.at(k)->intersect(ray, dummyColorAt, 1);
  362. //cout << t<<endl;
  363. }
  364. double Reflected_color[3];
  365. if(nearest!=-1){
  366. //double ColorAt[3]={1,234,45};
  367. double t = Objects.at(nearest)->intersect(reflectionRay,Reflected_color, level+1);
  368. //cout <<"i: " << i<<"j: " << j<< "t: " << t<<endl;
  369. current_color[0]+=Reflected_color[0] * coefficients[3];
  370. current_color[1]+=Reflected_color[1] * coefficients[3];
  371. current_color[2]+=Reflected_color[2] * coefficients[3];
  372.  
  373. //image.set_pixel(i,j,ColorAt[0]*255,ColorAt[1]*255,ColorAt[2]*255);
  374. }
  375. //for(int l=0; l<3; l++){
  376. // cout << current_color[l] << endl;
  377. //}
  378. if(current_color[0]>1)
  379. current_color[0] = 1;
  380. if(current_color[1]>1)
  381. current_color[1] = 1;
  382. if(current_color[2]>1)
  383. current_color[2] = 1;
  384. }
  385. return t;
  386. }
  387. };
  388.  
  389. class Triangle: public Object
  390. {
  391. public:
  392. point points[3];
  393. Triangle(point point1, point point2, point point3){
  394. points[0] = point1;
  395. points[1] = point2;
  396. points[2] = point3;
  397. }
  398. void draw(){
  399. glColor3f(color[0],color[1], color[2]);
  400. glBegin(GL_TRIANGLES);
  401. {
  402. glVertex3f(points[0].x, points[0].y,points[0].z);
  403. glVertex3f(points[1].x, points[1].y,points[1].z);
  404. glVertex3f(points[2].x, points[2].y, points[2].z);
  405. }
  406. glEnd();
  407. }
  408.  
  409. double getIntersectingT(Ray r){
  410. point e1, e2;
  411. point P,Q,T;
  412.  
  413. double det, inv_det, u, v;
  414. double t;
  415.  
  416. e1 = VectorSub(points[1],points[0]);
  417. e2 = VectorSub(points[2],points[0]);
  418.  
  419. P = CrossProduct(r.dir, e2);
  420.  
  421. det = DotProduct(e1, P);
  422.  
  423. if(det > -0.000001 && det < 0.000001) return 0;
  424. inv_det = 1.0 / det;
  425.  
  426. T = VectorSub(r.start,points[0]);
  427. u = DotProduct(T,P) * inv_det;
  428.  
  429. if(u<0.0000 || u>1.00000) return 0;
  430.  
  431. Q = CrossProduct(T, e1);
  432. v = DotProduct(r.dir,Q)*inv_det;
  433.  
  434. if(v<0.000 || u+v > 1.0000) return 0;
  435.  
  436. t = DotProduct(e2, Q)*inv_det;
  437.  
  438. if(t>0.000001){
  439. return t;
  440. }
  441. }
  442. void setColorAt(double* current_color, double* colorAt){
  443. current_color[0] = colorAt[0]*coefficients[0];
  444. current_color[1] = colorAt[1]*coefficients[0];
  445. current_color[2] = colorAt[2]*coefficients[0];
  446. }
  447.  
  448. point getNormal(){
  449. point Normal;
  450.  
  451. Normal = CrossProduct(VectorSub(points[1],points[0]),VectorSub(points[2],points[0]));
  452. Normal = Normalize(Normal);
  453.  
  454. return Normal;
  455. }
  456.  
  457. point getReflection(Ray r, point intersectionPoint, point Normal){
  458. point reflection;
  459. double a = 2*DotProduct(r.dir,Normal);
  460.  
  461. reflection.x = -a*Normal.x + r.dir.x;
  462. reflection.y = -a*Normal.y + r.dir.y;
  463. reflection.z = -a*Normal.z + r.dir.z;
  464.  
  465. reflection = Normalize(reflection);
  466.  
  467. return reflection;
  468. }
  469.  
  470. double intersect(Ray r, double *current_color, int level){
  471. double t = getIntersectingT(r);
  472.  
  473.  
  474. if(t<=0)
  475. return -1;
  476. if(level == 0 )
  477. return t;
  478. point intersectionPoint;
  479. intersectionPoint.x = r.start.x + r.dir.x*t;
  480. intersectionPoint.y = r.start.y + r.dir.y*t;
  481. intersectionPoint.z = r.start.z + r.dir.z*t;
  482.  
  483. double* colorAt = new double[3];
  484. colorAt = color;
  485.  
  486. setColorAt(current_color,colorAt);
  487.  
  488. point normal;
  489. point reflection;
  490.  
  491.  
  492. normal = getNormal();
  493. reflection = getReflection(r,intersectionPoint,normal);
  494.  
  495. for(int i=0 ;i<lights.size(); i++){
  496. point Start;
  497. point Direction;
  498.  
  499. Direction = VectorSub(lights.at(i), intersectionPoint);
  500. double sumVal = sqrt(pow(Direction.x,2)+pow(Direction.y,2)+pow(Direction.z,2));
  501.  
  502. Direction = Normalize(Direction);
  503. Start.x = intersectionPoint.x + Direction.x;
  504. Start.y = intersectionPoint.y + Direction.y;
  505. Start.z = intersectionPoint.z + Direction.z;
  506.  
  507. Ray L(Start,Direction);
  508.  
  509. int flag = -1;
  510.  
  511. for(int j=0; j<Objects.size(); j++){
  512. double tempVal;
  513. tempVal = Objects.at(j) ->getIntersectingT(L);
  514.  
  515. if(tempVal>0 && tempVal<sumVal){
  516. flag =1;
  517. break;
  518. }
  519. }
  520.  
  521. if(flag!=1){
  522. double Lambert = max(DotProduct(L.dir,normal),0.0);
  523.  
  524. point V;
  525. V.x = -r.dir.x;
  526. V.y = -r.dir.y;
  527. V.z = -r.dir.z;
  528.  
  529. point R;
  530. double scalar = 2*DotProduct(L.dir,normal);
  531. R.x = scalar*normal.x - L.dir.x;
  532. R.y = scalar*normal.y - L.dir.y;
  533. R.z = scalar*normal.z - L.dir.z;
  534.  
  535. R= Normalize(R);
  536.  
  537.  
  538. double phong = max(pow(DotProduct(R,V),shine),0.0);
  539.  
  540. current_color[0] += Lambert*coefficients[1]*colorAt[0]+phong*coefficients[2]*colorAt[0];
  541. current_color[1] += Lambert*coefficients[1]*colorAt[1]+phong*coefficients[2]*colorAt[1];
  542. current_color[2] += Lambert*coefficients[1]*colorAt[2]+phong*coefficients[2]*colorAt[2];
  543. }
  544.  
  545. if(level < RecursionLevel){
  546. point start;
  547. start.x = intersectionPoint.x + reflection.x;
  548. start.y = intersectionPoint.y + reflection.y;
  549. start.z = intersectionPoint.z + reflection.z;
  550. //cout << "KICHU" <<endl;
  551. //cout << "Start: " << start.x <<" " << start.y << " " << start.z<<endl;
  552. //cout << "ref: " << reflection.x << " " << reflection.y << " " << reflection.z << endl;
  553. Ray reflectionRay(start, reflection);
  554.  
  555.  
  556. int nearest = -1;
  557.  
  558. double tmin= 999999;
  559.  
  560. double dummyColorAt[3];
  561. for(int k=0; k<Objects.size(); k++){
  562. //double dummyColorAt[3]={1,234,45};
  563. double t= Objects.at(k)->intersect(reflectionRay,dummyColorAt,0);
  564. //cout << t<<endl;
  565.  
  566. if(t<=0) continue;
  567.  
  568. if(t<tmin){
  569. tmin = t;
  570. nearest = k;
  571. }
  572. //double t = Objects.at(k)->intersect(ray, dummyColorAt, 1);
  573. //cout << t<<endl;
  574. }
  575. double Reflected_color[3];
  576. if(nearest!=-1){
  577. //double ColorAt[3]={1,234,45};
  578. double t = Objects.at(nearest)->intersect(reflectionRay,Reflected_color, level+1);
  579. //cout <<"i: " << i<<"j: " << j<< "t: " << t<<endl;
  580. current_color[0]+=Reflected_color[0] * coefficients[3];
  581. current_color[1]+=Reflected_color[1] * coefficients[3];
  582. current_color[2]+=Reflected_color[2] * coefficients[3];
  583.  
  584. //image.set_pixel(i,j,ColorAt[0]*255,ColorAt[1]*255,ColorAt[2]*255);
  585. }
  586. //for(int l=0; l<3; l++){
  587. // cout << current_color[l] << endl;
  588. //}
  589. if(current_color[0]>1)
  590. current_color[0] = 1;
  591. if(current_color[1]>1)
  592. current_color[1] = 1;
  593. if(current_color[2]>1)
  594. current_color[2] = 1;
  595. }
  596. }
  597. return t;
  598.  
  599. }
  600. };
  601.  
  602.  
  603. class Floor: public Object{
  604. public:
  605. Floor(double FloorWidth,double TileWidth){
  606. reference_point.x = -FloorWidth/2;
  607. reference_point.y = -FloorWidth/2;
  608. reference_point.z = 0;
  609.  
  610. length = TileWidth;
  611. }
  612.  
  613. void draw(){
  614. int noOfIterations = -(reference_point.x * 2)/ length;
  615. double row = reference_point.x;
  616. int flag = 0; // White
  617.  
  618. for(int i=0; i<noOfIterations; i++){
  619. double column = reference_point.x;
  620. for(int j=0; j<noOfIterations; j++){
  621. if(flag == 0){
  622. glColor3f(0,0,0);
  623. flag =1;
  624. }
  625. else{
  626. glColor3f(255,255,255);
  627. flag = 0;
  628. }
  629.  
  630. glBegin(GL_QUADS);{
  631. glVertex3f( row, column,0);
  632. glVertex3f( row,column+length,0);
  633. glVertex3f( row+length,column+length,0);
  634. glVertex3f(row+length, column,0);
  635. }glEnd();
  636. column = column+ length;
  637. }
  638. row = row+length;
  639. if(flag==0) flag=1;
  640. else flag = 0;
  641. }
  642. }
  643.  
  644.  
  645. void setColorAt(double* current_color, double* colorAt){
  646. current_color[0] = colorAt[0]*coefficients[0];
  647. current_color[1] = colorAt[1]*coefficients[0];
  648. current_color[2] = colorAt[2]*coefficients[0];
  649. }
  650. double getIntersectingT(Ray r){
  651. double t = -r.start.z/r.dir.z;
  652.  
  653. if(t<0){
  654. return -1;
  655. }
  656. else
  657. return t;
  658. }
  659.  
  660. double* getColorAt(point intersection){
  661. int row = floor((intersection.x - reference_point.x)/(length));
  662. int col = floor((intersection.y -reference_point.y)/length);
  663.  
  664. int a = row%2;
  665. int b = col%2;
  666. if(a==b){
  667. for(int i=0; i<3; i++){
  668. color[i] = 1;
  669. }
  670. }
  671. else{
  672. for(int i=0; i<3; i++){
  673. color[i] = 0;
  674. }
  675. }
  676. return color;
  677. }
  678.  
  679.  
  680. point getReflection(Ray r, point intersectionPoint, point Normal){
  681. point reflection;
  682. double a = 2*DotProduct(r.dir,Normal);
  683.  
  684. reflection.x = -a*Normal.x + r.dir.x;
  685. reflection.y = -a*Normal.y + r.dir.y;
  686. reflection.z = -a*Normal.z + r.dir.z;
  687.  
  688. reflection = Normalize(reflection);
  689.  
  690. return reflection;
  691. }
  692.  
  693. double intersect(Ray r, double *current_color, int level){
  694. double t = getIntersectingT(r);
  695.  
  696.  
  697. if(t<=0)
  698. return -1;
  699. if(level == 0 )
  700. return t;
  701. point intersectionPoint;
  702. intersectionPoint.x = r.start.x + r.dir.x*t;
  703. intersectionPoint.y = r.start.y + r.dir.y*t;
  704. intersectionPoint.z = r.start.z + r.dir.z*t;
  705.  
  706. double* colorAt = new double[3];
  707.  
  708. colorAt = getColorAt(intersectionPoint);
  709.  
  710. setColorAt(current_color,colorAt);
  711.  
  712. point normal;
  713. point reflection;
  714.  
  715. normal.x = 0;
  716. normal.y = 0;
  717. normal.z = 1;
  718. reflection = getReflection(r,intersectionPoint,normal);
  719.  
  720. for(int i=0 ;i<lights.size(); i++){
  721. point Start;
  722. point Direction;
  723.  
  724. Direction = VectorSub(lights.at(i), intersectionPoint);
  725. Direction = Normalize(Direction);
  726. Start.x = intersectionPoint.x + Direction.x;
  727. Start.y = intersectionPoint.y + Direction.y;
  728. Start.z = intersectionPoint.z + Direction.z;
  729.  
  730. Ray L(Start,Direction);
  731.  
  732. double Lambert = max(DotProduct(L.dir,normal),0.0);
  733.  
  734. point V;
  735. V.x = -r.dir.x;
  736. V.y = -r.dir.y;
  737. V.z = -r.dir.z;
  738.  
  739. point R;
  740. double scalar = 2*DotProduct(L.dir,normal);
  741. R.x = scalar*normal.x - L.dir.x;
  742. R.y = scalar*normal.y - L.dir.y;
  743. R.z = scalar*normal.z - L.dir.z;
  744.  
  745.  
  746. double phong = max(pow(DotProduct(R,V),shine),0.0);
  747.  
  748. current_color[0] += Lambert*coefficients[1]*colorAt[0]+phong*coefficients[2]*colorAt[0];
  749. current_color[1] += Lambert*coefficients[1]*colorAt[1]+phong*coefficients[2]*colorAt[1];
  750. current_color[2] += Lambert*coefficients[1]*colorAt[2]+phong*coefficients[2]*colorAt[2];
  751. }
  752. if(level < RecursionLevel){
  753. point start;
  754. start.x = intersectionPoint.x + reflection.x;
  755. start.y = intersectionPoint.y + reflection.y;
  756. start.z = intersectionPoint.z + reflection.z;
  757.  
  758. //printf("start %lf %lf %lf \n",start.x,start.y,start.z);
  759. //printf("refle %lf %lf %lf \n",reflection.x,reflection.y,reflection.z);
  760.  
  761.  
  762. Ray reflectionRay(start, reflection);
  763.  
  764.  
  765. int nearest = -1;
  766.  
  767. double tmin= 999999;
  768.  
  769.  
  770. for(int k=0; k<Objects.size(); k++){
  771. double dummyColorAt[3]={1,234,45};
  772. double t= Objects.at(k)->intersect(reflectionRay,dummyColorAt,0);
  773. //cout << t<<endl;
  774.  
  775. if(t<=0) continue;
  776.  
  777. if(t<tmin){
  778. tmin = t;
  779. nearest = k;
  780. }
  781. //double t = Objects.at(k)->intersect(ray, dummyColorAt, 1);
  782. //cout << t<<endl;
  783. }
  784. double Reflected_color[3];
  785. if(nearest!=-1){
  786. //double ColorAt[3]={1,234,45};
  787. double t = Objects.at(nearest)->intersect(reflectionRay,Reflected_color, level+1);
  788. //cout <<"i: " << i<<"j: " << j<< "t: " << t<<endl;
  789. current_color[0]+=Reflected_color[0] * coefficients[3];
  790. current_color[1]+=Reflected_color[1] * coefficients[3];
  791. current_color[2]+=Reflected_color[2] * coefficients[3];
  792.  
  793. //image.set_pixel(i,j,ColorAt[0]*255,ColorAt[1]*255,ColorAt[2]*255);
  794. }
  795. //for(int l=0; l<3; l++){
  796. // cout << current_color[i] << endl;
  797. //}
  798. if(current_color[0]>1)
  799. current_color[0] = 1;
  800. if(current_color[1]>1)
  801. current_color[1] = 1;
  802. if(current_color[2]>1)
  803. current_color[2] = 1;
  804. }
  805. return t;
  806. }
  807.  
  808. };
  809.  
  810. class General: public Object
  811. {
  812. public:
  813. //point points[3];
  814. double A,B,C,D,E,F,G,H,I,J;
  815. //double Coeff[10];
  816. General(double* C1, point ref_point, double length1, double width1, double height1){
  817.  
  818. A=C1[0];
  819. B=C1[1];
  820. C = C1[2];
  821. D = C1[3];
  822. E = C1[4];
  823. F = C1[5];
  824. G = C1[6];
  825. H = C1[7];
  826. I = C1[8];
  827. J = C1[9];
  828.  
  829. reference_point.x = ref_point.x;
  830. reference_point.y = ref_point.y;
  831. reference_point.z = ref_point.z;
  832.  
  833. this->length = length1;
  834. this->width = width1;
  835. this->height = height1;
  836. }
  837. void draw(){
  838.  
  839. }
  840.  
  841. double getIntersectingT(Ray r){
  842. double 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.z*r.dir.x;
  843. double b = 2*A*r.start.x*r.dir.x + 2*B*r.dir.y*r.start.y + 2*C*r.dir.z* r.start.z
  844. + D*r.start.x*r.dir.y + E*r.start.y*r.dir.z+E*r.dir.y*r.start.z
  845. + F*r.start.z*r.dir.x + F*r.dir.z*r.start.x + G*r.dir.x
  846. + H*r.dir.y + I*r.dir.z;
  847. double 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.z*r.start.x
  848. +G*r.start.x + H*r.start.y + I*r.start.z + J;
  849.  
  850.  
  851. double d = b*b-4*a*c;
  852. if(d<0)
  853. return -1;
  854. d = sqrt(d);
  855. double t1 = (-b+d)/(2*a);
  856. double t2 = (-b-d)/(2*a);
  857.  
  858. point intersectionPT1;
  859. point intersectionPT2;
  860.  
  861. intersectionPT1.x = r.start.x + t1*r.dir.x;
  862. intersectionPT1.y = r.start.y + t1*r.dir.y;
  863. intersectionPT1.z = r.start.z + t1*r.dir.z;
  864.  
  865.  
  866. intersectionPT2.x = r.start.x + t2*r.dir.x;
  867. intersectionPT2.y = r.start.y + t2*r.dir.y;
  868. intersectionPT2.z = r.start.z + t2*r.dir.z;
  869.  
  870. int temp1 = 0;
  871. int temp2 = 0;
  872.  
  873. if(this->length != 0 && temp1==0){
  874. if(intersectionPT1.x<reference_point.x || intersectionPT1.x>reference_point.x + this->length)
  875. temp1 = 1;
  876. }
  877. if(temp1 == 0 && this->width!=0){
  878. if(intersectionPT1.y < reference_point.y || intersectionPT1.y > reference_point.y + this->width)
  879. temp1 = 1;
  880. }
  881. if(temp1 == 0 && this->height!=0){
  882. if(intersectionPT1.z < reference_point.z || intersectionPT1.z > reference_point.z + this->height)
  883. temp1 = 1;
  884. }
  885.  
  886. if(temp2 == 0 && this->length!=0){
  887. if(intersectionPT2.x < reference_point.x || intersectionPT2.x > reference_point.x + this->length)
  888. temp2 = 1;
  889. }
  890. if(temp2 == 0 && this->width!=0){
  891. if(intersectionPT2.y < reference_point.y || intersectionPT2.y > reference_point.y + this->width)
  892. temp2 = 1;
  893. }
  894. if(temp2 == 0 && this->height!=0){
  895. if(intersectionPT2.z < reference_point.z || intersectionPT2.z > reference_point.z + this->height)
  896. temp2 = 1;
  897. }
  898. if(temp1 != 0 && temp2 !=0)
  899. return -1;
  900. if(temp1!=0)
  901. return t2;
  902. if(temp2!=0)
  903. return t1;
  904. if(t1<t2)
  905. return t1;
  906. else
  907. return t2;
  908. }
  909. void setColorAt(double* current_color, double* colorAt){
  910. current_color[0] = colorAt[0]*coefficients[0];
  911. current_color[1] = colorAt[1]*coefficients[0];
  912. current_color[2] = colorAt[2]*coefficients[0];
  913. }
  914.  
  915. point getNormal(point IntersectionPoint){
  916. point Normal;
  917. Normal.x = IntersectionPoint.x*A*2 + IntersectionPoint.y*D + F*IntersectionPoint.z + G;
  918. Normal.y = IntersectionPoint.y*B*2 + IntersectionPoint.x*D + E*IntersectionPoint.z + H;
  919. Normal.z = IntersectionPoint.z*C*2 + IntersectionPoint.z*E + F*IntersectionPoint.x + I;
  920.  
  921. return Normal;
  922. }
  923.  
  924. point getReflection(Ray r, point intersectionPoint, point Normal){
  925. point reflection;
  926. double a = 2*DotProduct(r.dir,Normal);
  927.  
  928. reflection.x = -a*Normal.x + r.dir.x;
  929. reflection.y = -a*Normal.y + r.dir.y;
  930. reflection.z = -a*Normal.z + r.dir.z;
  931.  
  932. reflection = Normalize(reflection);
  933.  
  934. return reflection;
  935. }
  936.  
  937. double intersect(Ray r, double *current_color, int level){
  938. double t = getIntersectingT(r);
  939.  
  940. if(t<=0)
  941. return -1;
  942. if(level == 0 )
  943. return t;
  944. point intersectionPoint;
  945. intersectionPoint.x = r.start.x + r.dir.x*t;
  946. intersectionPoint.y = r.start.y + r.dir.y*t;
  947. intersectionPoint.z = r.start.z + r.dir.z*t;
  948.  
  949. double* colorAt = new double[3];
  950. colorAt = color;
  951.  
  952. setColorAt(current_color,colorAt);
  953.  
  954. point normal;
  955. point reflection;
  956.  
  957.  
  958. normal = getNormal(intersectionPoint);
  959. reflection = getReflection(r,intersectionPoint,normal);
  960.  
  961. for(int i=0 ;i<lights.size(); i++){
  962. point Start;
  963. point Direction;
  964.  
  965. Direction = VectorSub(lights.at(i), intersectionPoint);
  966. double sumVal = sqrt(pow(Direction.x,2)+pow(Direction.y,2)+pow(Direction.z,2));
  967.  
  968. Direction = Normalize(Direction);
  969. Start.x = intersectionPoint.x + Direction.x;
  970. Start.y = intersectionPoint.y + Direction.y;
  971. Start.z = intersectionPoint.z + Direction.z;
  972.  
  973. Ray L(Start,Direction);
  974.  
  975. int flag = -1;
  976.  
  977. for(int j=0; j<Objects.size(); j++){
  978. double tempVal;
  979. tempVal = Objects.at(j) ->getIntersectingT(L);
  980.  
  981. if(tempVal>0 && tempVal<sumVal){
  982. flag =1;
  983. break;
  984. }
  985. }
  986.  
  987. if(flag!=1){
  988. double Lambert = max(DotProduct(L.dir,normal),0.0);
  989.  
  990. point V;
  991. V.x = -r.dir.x;
  992. V.y = -r.dir.y;
  993. V.z = -r.dir.z;
  994.  
  995. point R;
  996. double scalar = 2*DotProduct(L.dir,normal);
  997. R.x = scalar*normal.x - L.dir.x;
  998. R.y = scalar*normal.y - L.dir.y;
  999. R.z = scalar*normal.z - L.dir.z;
  1000.  
  1001. R= Normalize(R);
  1002.  
  1003.  
  1004. double phong = max(pow(DotProduct(R,V),shine),0.0);
  1005.  
  1006. current_color[0] += Lambert*coefficients[1]*colorAt[0]+phong*coefficients[2]*colorAt[0];
  1007. current_color[1] += Lambert*coefficients[1]*colorAt[1]+phong*coefficients[2]*colorAt[1];
  1008. current_color[2] += Lambert*coefficients[1]*colorAt[2]+phong*coefficients[2]*colorAt[2];
  1009. }
  1010.  
  1011. if(level < RecursionLevel){
  1012. point start;
  1013. start.x = intersectionPoint.x + reflection.x;
  1014. start.y = intersectionPoint.y + reflection.y;
  1015. start.z = intersectionPoint.z + reflection.z;
  1016. //cout << "KICHU" <<endl;
  1017. //cout << "Start: " << start.x <<" " << start.y << " " << start.z<<endl;
  1018. //cout << "ref: " << reflection.x << " " << reflection.y << " " << reflection.z << endl;
  1019. Ray reflectionRay(start, reflection);
  1020.  
  1021.  
  1022. int nearest = -1;
  1023.  
  1024. double tmin= 999999;
  1025.  
  1026. double dummyColorAt[3];
  1027. for(int k=0; k<Objects.size(); k++){
  1028. //double dummyColorAt[3]={1,234,45};
  1029. double t= Objects.at(k)->intersect(reflectionRay,dummyColorAt,0);
  1030. //cout << t<<endl;
  1031.  
  1032. if(t<=0) continue;
  1033.  
  1034. if(t<tmin){
  1035. tmin = t;
  1036. nearest = k;
  1037. }
  1038. //double t = Objects.at(k)->intersect(ray, dummyColorAt, 1);
  1039. //cout << t<<endl;
  1040. }
  1041. double Reflected_color[3];
  1042. if(nearest!=-1){
  1043. //double ColorAt[3]={1,234,45};
  1044. double t = Objects.at(nearest)->intersect(reflectionRay,Reflected_color, level+1);
  1045. //cout <<"i: " << i<<"j: " << j<< "t: " << t<<endl;
  1046. current_color[0]+=Reflected_color[0] * coefficients[3];
  1047. current_color[1]+=Reflected_color[1] * coefficients[3];
  1048. current_color[2]+=Reflected_color[2] * coefficients[3];
  1049.  
  1050. //image.set_pixel(i,j,ColorAt[0]*255,ColorAt[1]*255,ColorAt[2]*255);
  1051. }
  1052. //for(int l=0; l<3; l++){
  1053. // cout << current_color[l] << endl;
  1054. //}
  1055. if(current_color[0]>1)
  1056. current_color[0] = 1;
  1057. if(current_color[1]>1)
  1058. current_color[1] = 1;
  1059. if(current_color[2]>1)
  1060. current_color[2] = 1;
  1061. }
  1062. }
  1063. return t;
  1064. }
  1065. };
  1066.  
  1067.  
  1068. void drawAxes()
  1069. {
  1070. if(drawaxes==1)
  1071. {
  1072.  
  1073. glBegin(GL_LINES);{
  1074. glColor3f(0, 0, 1); // BLUE
  1075. glVertex3f( 100,0,0);
  1076. glVertex3f(0,0,0);
  1077.  
  1078. glColor3f(0,1,1);
  1079. glVertex3f( -100,0,0);
  1080. glVertex3f(0,0,0);
  1081.  
  1082. glColor3f(0, 1, 0); // Green
  1083. glVertex3f(0,100,0);
  1084. glVertex3f(0, 0,0);
  1085.  
  1086. glColor3f(0, 1, 1); // Green
  1087. glVertex3f(0,-100,0);
  1088. glVertex3f(0, 0,0);
  1089.  
  1090. glColor3f(1, 0, 0);
  1091. glVertex3f(0,0, 100);
  1092. glVertex3f(0,0,0);
  1093.  
  1094. glColor3f(0, 1, 1); // Green
  1095. glVertex3f(0,0,-100);
  1096. glVertex3f(0, 0,0);
  1097.  
  1098. }glEnd();
  1099. }
  1100. }
  1101.  
  1102. void Capture(){
  1103. bitmap_image image(image_width,image_height);
  1104. for(int i=0;i<image_width;i++){
  1105. for(int j=0;j<image_height;j++){
  1106. image.set_pixel(i,j,0,0,0);
  1107. }
  1108. }
  1109. double plane_distance = (window_height/2)/(tan(2*pi*VIEW_ANGLE/(2*360)));
  1110.  
  1111. point topLeft;
  1112.  
  1113. topLeft.x = pos.x + l.x*plane_distance - r.x*(window_width/2)+u.x*(window_height/2);
  1114. topLeft.y= pos.y + l.y*plane_distance - r.y*(window_width/2)+u.y*(window_height/2);
  1115. topLeft.z = pos.z + l.z*plane_distance - r.z*(window_width/2)+u.z*(window_height/2);
  1116.  
  1117. //cout << "Top Left: " << topLeft.x << " " << topLeft.y << " " << topLeft.z <<endl;
  1118.  
  1119. double du = window_width/image_width;
  1120. double dv = window_height/image_height;
  1121.  
  1122. //Ray ray(pos, topLeft);
  1123.  
  1124. point Corner;
  1125.  
  1126. for(int i=0; i<image_width; i++){
  1127. for(int j=0; j<image_width; j++){
  1128. Corner.x = topLeft.x + ( i*du*r.x + j*dv*(-u.x));
  1129. Corner.y = topLeft.y + (i*du*r.y + j*dv*(-u.y));
  1130. Corner.z = topLeft.z +(i*du*r.z + j*dv*(-u.z));
  1131.  
  1132. //Corner - Pos
  1133. point Corner_dir_pos;
  1134. Corner_dir_pos.x = Corner.x - pos.x;
  1135. Corner_dir_pos.y = Corner.y - pos.y;
  1136. Corner_dir_pos.z = Corner.z - pos.z;
  1137.  
  1138. Corner_dir_pos = Normalize(Corner_dir_pos);
  1139.  
  1140. Ray ray(pos, Corner_dir_pos);
  1141.  
  1142. int nearest = -1;
  1143.  
  1144. double tmin= 999999;
  1145.  
  1146.  
  1147. for(int k=0; k<Objects.size(); k++){
  1148.  
  1149. double dummyColorAt[3]={1,234,45};
  1150. double t= Objects.at(k)->intersect(ray,dummyColorAt,0);
  1151. //cout << t<<endl;
  1152.  
  1153. if(t<=0) continue;
  1154.  
  1155. if(t<tmin){
  1156. tmin = t;
  1157. nearest = k;
  1158. }
  1159. //double t = Objects.at(k)->intersect(ray, dummyColorAt, 1);
  1160. //cout << t<<endl;
  1161. }
  1162. if(nearest!=-1){
  1163. double ColorAt[3]={1,234,45};
  1164. double t = Objects.at(nearest)->intersect(ray,ColorAt, 1);
  1165. //cout <<"i: " << i<<"j: " << j<< "t: " << t<<endl;
  1166.  
  1167. image.set_pixel(i,j,ColorAt[0]*255,ColorAt[1]*255,ColorAt[2]*255);
  1168. }
  1169. }
  1170. }
  1171. image.save_image("F:\\CSE 4-2 Study Resources\\CSE_410_Computer_Graphics_Sessional\\Assignment1 Solution\\New folder\\OpenGL\\OpenGL_CodeBlocks\\output.bmp");
  1172. }
  1173.  
  1174.  
  1175. void keyboardListener(unsigned char key, int x,int y){
  1176. switch(key){
  1177. case '0':
  1178. Capture();
  1179. break;
  1180. case '1':
  1181. l.x = l.x*cos((3*pi)/180) + r.x*sin((3*pi)/180);
  1182. l.y = l.y*cos((3*pi)/180) + r.y*sin((3*pi)/180);
  1183. l.z = l.z*cos((3*pi)/180) + r.z*sin((3*pi)/180);
  1184.  
  1185. r.x = r.x*cos((3*pi)/180) - l.x* sin((3*pi)/180);
  1186. r.y = r.y*cos((3*pi)/180) - l.y *sin((3*pi)/180);
  1187. r.z = r.z*cos((3*pi)/180) - l.z *sin((3*pi)/180);
  1188.  
  1189. break;
  1190. case '2':
  1191. l.x = l.x*cos((3*pi)/180) - r.x*sin((3*pi)/180);
  1192. l.y = l.y*cos((3*pi)/180) - r.y*sin((3*pi)/180);
  1193. l.z = l.z*cos((3*pi)/180) - r.z*sin((3*pi)/180);
  1194.  
  1195.  
  1196. r.x = r.x*cos((3*pi)/180) + l.x* sin((3*pi)/180);
  1197. r.y = r.y*cos((3*pi)/180) + l.y *sin((3*pi)/180);
  1198. r.z = r.z*cos((3*pi)/180) + l.z *sin((3*pi)/180);
  1199.  
  1200.  
  1201. break;
  1202.  
  1203. case '3':
  1204. tempX = l.x;
  1205. tempY = l.y;
  1206. tempZ = l.z;
  1207.  
  1208.  
  1209. u.x = u.x*cos((3*pi)/180) - tempX*sin((3*pi)/180);
  1210. u.y = u.y*cos((3*pi)/180) - tempY*sin((3*pi)/180);
  1211. u.z = u.z*cos((3*pi)/180) - tempZ*sin((3*pi)/180);
  1212.  
  1213. l.x = tempX*cos((3*pi)/180) + u.x* sin((3*pi)/180);
  1214. l.y = tempY*cos((3*pi)/180) + u.y *sin((3*pi)/180);
  1215. l.z = tempZ*cos((3*pi)/180) + u.z *sin((3*pi)/180);
  1216. break;
  1217. case '4':
  1218.  
  1219. u.x = u.x*cos((3*pi)/180) + tempX*sin((3*pi)/180);
  1220. u.y = u.y*cos((3*pi)/180) + tempY*sin((3*pi)/180);
  1221. u.z = u.z*cos((3*pi)/180) + tempZ*sin((3*pi)/180);
  1222.  
  1223.  
  1224. l.x = l.x*cos((3*pi)/180) - u.x* sin((3*pi)/180);
  1225. l.y = l.y*cos((3*pi)/180) - u.y *sin((3*pi)/180);
  1226. l.z = l.z*cos((3*pi)/180) - u.z *sin((3*pi)/180);
  1227.  
  1228. break;
  1229.  
  1230. //tilt clockwise
  1231. case '5':
  1232.  
  1233. r.x = r.x*cos((3*pi)/180) - u.x* sin((3*pi)/180);
  1234. r.y = r.y*cos((3*pi)/180) - u.y *sin((3*pi)/180);
  1235. r.z = r.z*cos((3*pi)/180) - u.z *sin((3*pi)/180);
  1236.  
  1237. u.x = u.x*cos((3*pi)/180) + r.x*sin((3*pi)/180);
  1238. u.y = u.y*cos((3*pi)/180) + r.y*sin((3*pi)/180);
  1239. u.z = u.z*cos((3*pi)/180) + r.z*sin((3*pi)/180);
  1240.  
  1241. break;
  1242.  
  1243.  
  1244. //tilt anticlockwise
  1245. case '6':
  1246.  
  1247. r.x = r.x*cos((3*pi)/180) + u.x* sin((3*pi)/180);
  1248. r.y = r.y*cos((3*pi)/180) + u.y *sin((3*pi)/180);
  1249. r.z = r.z*cos((3*pi)/180) + u.z *sin((3*pi)/180);
  1250.  
  1251. u.x = u.x*cos((3*pi)/180) - r.x*sin((3*pi)/180);
  1252. u.y = u.y*cos((3*pi)/180) - r.y*sin((3*pi)/180);
  1253. u.z = u.z*cos((3*pi)/180) - r.z*sin((3*pi)/180);
  1254.  
  1255. break;
  1256.  
  1257.  
  1258. default:
  1259. break;
  1260. }
  1261. }
  1262.  
  1263.  
  1264. void specialKeyListener(int key, int x,int y){
  1265. switch(key){
  1266. case GLUT_KEY_DOWN: //down arrow key
  1267. //cameraHeight -= 3.0;
  1268. pos.x = pos.x +2*l.x;
  1269. pos.y = pos.y + 2*l.y;
  1270. break;
  1271. case GLUT_KEY_UP: // up arrow key
  1272. //cameraHeight += 3.0;
  1273. pos.x = pos.x - 2*l.x;
  1274. pos.y = pos.y - 2*l.y;
  1275. break;
  1276.  
  1277. case GLUT_KEY_RIGHT:
  1278. //cameraAngle += 0.03;
  1279. pos.x = pos.x + 2*r.x;
  1280. pos.y = pos.y + 2*r.y;
  1281. break;
  1282. case GLUT_KEY_LEFT:
  1283. pos.x = pos.x - 2*r.x;
  1284. pos.y = pos.y - 2*r.y;
  1285. //cameraAngle -= 0.03;
  1286. break;
  1287.  
  1288. case GLUT_KEY_PAGE_UP:
  1289. pos.z = pos.z + 2*u.z;
  1290. break;
  1291. case GLUT_KEY_PAGE_DOWN:
  1292. pos.z = pos.z - 2*u.z;
  1293. break;
  1294.  
  1295. case GLUT_KEY_INSERT:
  1296. break;
  1297.  
  1298. case GLUT_KEY_HOME:
  1299. if(height != 0){
  1300. height = height-1;
  1301. radius = radius+1;
  1302. }
  1303. break;
  1304. case GLUT_KEY_END:
  1305. if(radius !=0){
  1306. height=height+1;
  1307. radius = radius-1;
  1308. }
  1309. break;
  1310.  
  1311. default:
  1312. break;
  1313. }
  1314. }
  1315.  
  1316.  
  1317.  
  1318. void mouseListener(int button, int state, int x, int y){ //x, y is the x-y of the screen (2D)
  1319. switch(button){
  1320. case GLUT_LEFT_BUTTON:
  1321. if(state == GLUT_DOWN){ // 2 times?? in ONE click? -- solution is checking DOWN or UP
  1322. drawaxes=1-drawaxes;
  1323. }
  1324. break;
  1325.  
  1326. case GLUT_RIGHT_BUTTON:
  1327. //........
  1328. break;
  1329.  
  1330. case GLUT_MIDDLE_BUTTON:
  1331. //........
  1332. break;
  1333.  
  1334. default:
  1335. break;
  1336. }
  1337. }
  1338.  
  1339.  
  1340. void display(){
  1341.  
  1342. //clear the display
  1343. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1344. glClearColor(0,0,0,0); //color black
  1345. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1346.  
  1347. /********************
  1348. / set-up camera here
  1349. ********************/
  1350. //load the correct matrix -- MODEL-VIEW matrix
  1351. glMatrixMode(GL_MODELVIEW);
  1352.  
  1353. //initialize the matrix
  1354. glLoadIdentity();
  1355.  
  1356. //now give three info
  1357. //1. where is the camera (viewer)?
  1358. //2. where is the camera looking?
  1359. //3. Which direction is the camera's UP direction?
  1360.  
  1361. //gluLookAt(100,100,100, 0,0,0, 0,0,1);
  1362. //gluLookAt(200*cos(cameraAngle), 200*sin(cameraAngle), cameraHeight, 0,0,0, 0,0,1);
  1363. //gluLookAt(0,0,200, 0,0,0, 0,1,0);
  1364. gluLookAt(pos.x,pos.y,pos.z,pos.x+l.x, pos.y + l.y, pos.z + l.z, u.x, u.y, u.z);
  1365.  
  1366.  
  1367. //again select MODEL-VIEW
  1368. glMatrixMode(GL_MODELVIEW);
  1369.  
  1370.  
  1371. /****************************
  1372. / Add your objects from here
  1373. ****************************/
  1374. //add objects
  1375.  
  1376. drawAxes();
  1377. //drawGrid();
  1378.  
  1379. for(int i=0; i<Objects.size(); i++){
  1380. Objects.at(i)->draw();
  1381. }
  1382. for(int i=0; i<lights.size(); i++){
  1383. glColor3f(1,0,0);
  1384. glBegin(GL_QUADS);{
  1385. glVertex3f(lights.at(i).x, lights.at(i).y, lights.at(i).z);
  1386. }glEnd();
  1387. }
  1388.  
  1389.  
  1390. //glColor3f(1,0,0);
  1391. //drawSquare(10);
  1392.  
  1393. //drawSS();
  1394.  
  1395. //drawCircle(30,24);
  1396.  
  1397. //drawCone(20,50,24);
  1398.  
  1399. //drawSphere(30,24,20);
  1400. //drawCylinder(30,24,20);
  1401. //DrawObject(height,radius);
  1402.  
  1403.  
  1404.  
  1405. //ADD this line in the end --- if you use double buffer (i.e. GL_DOUBLE)
  1406. glutSwapBuffers();
  1407. }
  1408.  
  1409.  
  1410. void animate(){
  1411. angle+=0.05;
  1412. //codes for any changes in Models, Camera
  1413. glutPostRedisplay();
  1414. }
  1415.  
  1416. void init(){
  1417. //codes for initialization
  1418. drawgrid=0;
  1419. drawaxes=1;
  1420. cameraHeight=150.0;
  1421. cameraAngle=1.0;
  1422.  
  1423. pos.x = 0;
  1424. pos.y = -200;
  1425. pos.z = 50;
  1426.  
  1427. u.x = 0;
  1428. u.y = 0;
  1429. u.z = 1;
  1430.  
  1431. r.x = 1;
  1432. r.y = 0;
  1433. r.z = 0;
  1434.  
  1435. l.x = 0;
  1436. l.y = 1;
  1437. l.z = 0;
  1438.  
  1439. //clear the screen
  1440. glClearColor(0,0,0,0);
  1441.  
  1442. /************************
  1443. / set-up projection here
  1444. ************************/
  1445. //load the PROJECTION matrix
  1446. glMatrixMode(GL_PROJECTION);
  1447.  
  1448. //initialize the matrix
  1449. glLoadIdentity();
  1450.  
  1451. //give PERSPECTIVE parameters
  1452. gluPerspective(80, 1, 1, 1000.0);
  1453. //field of view in the Y (vertically)
  1454. //aspect ratio that determines the field of view in the X direction (horizontally)
  1455. //near distance
  1456. //far distance
  1457. }
  1458.  
  1459.  
  1460.  
  1461. void loadTestData(){
  1462. Object *temp;
  1463.  
  1464. point Center;
  1465. Center.x = 40.0;
  1466. Center.y = 0;
  1467. Center.z = 10.0;
  1468.  
  1469. temp = new Sphere(Center,10);
  1470. temp->setColor(0,1,0);
  1471. temp->setCoefficients(0.4,0.2,0.2,0.2);
  1472. temp->setShine(10);
  1473.  
  1474. Objects.push_back(temp);
  1475.  
  1476. //point Center;
  1477. Center.x = -30;
  1478. Center.y = 60;
  1479. Center.z = 20;
  1480.  
  1481. temp = new Sphere(Center,20);
  1482. temp->setColor(0,0,1);
  1483. temp->setCoefficients(0.2,0.2,0.4,0.2);
  1484. temp->setShine(15);
  1485.  
  1486. Objects.push_back(temp);
  1487.  
  1488. Center.x = -15;
  1489. Center.y = 15;
  1490. Center.z = 45;
  1491.  
  1492. temp = new Sphere(Center,15);
  1493. temp->setColor(1,0,0);
  1494. temp->setCoefficients(0.4,0.3,0.1,0.2);
  1495. temp->setShine(5);
  1496.  
  1497. Objects.push_back(temp);
  1498.  
  1499. point point1,point2, point3;
  1500. point1.x = 50;
  1501. point1.y = 30;
  1502. point1.z = 0;
  1503.  
  1504. point2.x = 70;
  1505. point2.y = 60;
  1506. point2.z = 0;
  1507.  
  1508. point3.x = 50;
  1509. point3.y = 45;
  1510. point3.z = 50;
  1511.  
  1512. temp = new Triangle(point1, point2, point3);
  1513. temp->setColor(1,0,0);
  1514. temp->setCoefficients(0.4,0.2,0.1,0.3);
  1515. temp->setShine(5);
  1516. Objects.push_back(temp);
  1517.  
  1518. double* C1= new double[10];
  1519. C1[0] = 1;
  1520. C1[1] = 1;
  1521. C1[2] = 1;
  1522. C1[3] = 0;
  1523. C1[4] = 0;
  1524. C1[5] = 0;
  1525. C1[6] =0;
  1526. C1[7] =0;
  1527. C1[8] = 0;
  1528. C1[9] = -100;
  1529.  
  1530. point Ref;
  1531. Ref.x =0;
  1532. Ref.y = 0;
  1533. Ref.z = 0;
  1534.  
  1535. temp = new General(C1,Ref,0,0,20);
  1536. temp->setColor(0,1,0);
  1537. temp->setCoefficients(0.4,0.2,0.1,0.3);
  1538. temp->setShine(10);
  1539. Objects.push_back(temp);
  1540.  
  1541. point light1;
  1542. light1.x = -50;
  1543. light1.y = 50;
  1544. light1.z = 50;
  1545.  
  1546. lights.push_back(light1);
  1547.  
  1548. temp = new Floor(1000,20);
  1549. temp->setCoefficients(0.4,0.2,0.2,0.2);
  1550. temp->setShine(1);
  1551. Objects.push_back(temp);
  1552. }
  1553.  
  1554.  
  1555.  
  1556. int main(int argc, char **argv){
  1557. loadTestData();
  1558. glutInit(&argc,argv);
  1559. glutInitWindowSize(500, 500);
  1560. glutInitWindowPosition(0, 0);
  1561. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB); //Depth, Double buffer, RGB color
  1562.  
  1563. glutCreateWindow("My OpenGL Program");
  1564.  
  1565. init();
  1566.  
  1567. glEnable(GL_DEPTH_TEST); //enable Depth Testing
  1568.  
  1569. glutDisplayFunc(display); //display callback function
  1570. glutIdleFunc(animate); //what you want to do in the idle time (when no drawing is occuring)
  1571.  
  1572. glutKeyboardFunc(keyboardListener);
  1573. glutSpecialFunc(specialKeyListener);
  1574. glutMouseFunc(mouseListener);
  1575.  
  1576. glutMainLoop(); //The main loop of OpenGL
  1577.  
  1578.  
  1579. point Start;
  1580. Start.x = 0;
  1581. Start.y = 100;
  1582. Start.z = 10;
  1583. point Dir;
  1584. Dir.x = 0;
  1585. Dir.y = 1;
  1586. Dir.z = 0;
  1587.  
  1588. Ray ray1(Start,Dir);
  1589. return 0;
  1590. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement