Advertisement
sajid006

tree

Jun 13th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.92 KB | None | 0 0
  1. /*
  2. * GLUT Shapes Demo
  3. *
  4. * Written by Nigel Stewart November 2003
  5. *
  6. * This program is test harness for the sphere, cone
  7. * and torus shapes in GLUT.
  8. *
  9. * Spinning wireframe and smooth shaded shapes are
  10. * displayed until the ESC or q key is pressed. The
  11. * number of geometry stacks and slices can be adjusted
  12. * using the + and - keys.
  13. */
  14. #include <GL/gl.h>
  15. #ifdef __APPLE__
  16. #include <GLUT/glut.h>
  17. #else
  18. #include <GL/glut.h>
  19. #endif
  20.  
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <stdlib.h>
  26. #include <vector>
  27. #include <iostream>
  28.  
  29.  
  30. using namespace std;
  31.  
  32. const double PI = 3.14159265389;
  33.  
  34.  
  35. /* GLUT callback Handlers */
  36.  
  37.  
  38. int anglex= 0, angley = 0, anglez = 0; //rotation angles
  39. int window;
  40. int wired=0;
  41. int shcpt=1;
  42. int animat = 0;
  43. const int L=20;
  44. const int dgre=3;
  45. int ncpt=L+1;
  46. int clikd=0;
  47. const int nt = 40; //number of slices along x-direction
  48. const int ntheta = 20;
  49.  
  50.  
  51. GLfloat ctrlpoints[33][3] =
  52. {
  53. { 0.0, 0.0, 0.0}, { -0.3, 0.5, 0.0},
  54. { 0.1, 1.7, 0.0},{ 0.5, 1.5, 0.0},
  55. {2.5, 1.2, 0.0},
  56. { 3.0, 0.0, 0.0}, { 2.7, 0.5, 0.0},
  57. { 3.1, 1.7, 0.0},{ 3.5, 1.5, 0.0},
  58. {5.5, 1.2, 0.0},{5.5, 2.2, 0.0},{5.5, 3.2, 0.0},{5.5, 3.2, 0.0},{6.5, 0.2, 0.0},
  59. {4.5, 0.2, 0.0},{7.5, 1.2, 0.0},{ 3.0, 0.0, 0.0}, { 2.7, 0.5, 0.0},
  60. { 10.1, 1.7, 0.0},{ 10.5, 1.5, 0.0},
  61. {12.5, 1.2, 0.0},{12.5, 2.2, 0.0},{12.5, 3.2, 0.0},{12.5, 3.2, 0.0},{13.5, 0.2, 0.0},
  62. {11.5, 0.2, 0.0},{14.5, 1.2, 0.0}
  63. };
  64.  
  65. /*
  66. , {1.4, 1.4, 0.0},
  67. {1.8, 0.4, 0.0},{2.2, 0.4, 0.0},
  68. {2.6, 1.5, 0.0}, {3.0, 1.4, 0.0},
  69. {3.4, 1.4, 0.0},{3.8, 1.4, 0.0},
  70. {4.2, 1.0, 0.0},{4.6, 1.0, 0.0},
  71. {5.0, 1.0, 0.0},{5.4, 1.0, 0.0},
  72. {5.8, 0.5, 0.0},{6.2, 0.5, 0.0},
  73. {6.6, 0.5, 0.0},{7.2, 0.2, 0.0},
  74. {6.8, 0.52, 0.0}
  75.  
  76. */
  77. double ex=0, ey=0, ez=15, lx=0,ly=0,lz=0, hx=0,hy=1,hz=0;
  78.  
  79. float wcsClkDn[3],wcsClkUp[3];
  80. ///////////////////////////////
  81. class point1
  82. {
  83. public:
  84. point1()
  85. {
  86. x=0;
  87. y=0;
  88. }
  89. int x;
  90. int y;
  91. } clkpt[2];
  92. int flag=0;
  93. GLint viewport[4]; //var to hold the viewport info
  94. GLdouble modelview[16]; //var to hold the modelview info
  95. GLdouble projection[16]; //var to hold the projection matrix info
  96.  
  97. //////////////////////////
  98. void scsToWcs(float sx,float sy, float wcsv[3] );
  99. void processMouse(int button, int state, int x, int y);
  100. void matColor(float kdr, float kdg, float kdb, float shiny, int frnt_Back=0, float ambFactor=1.0, float specFactor=1.0);
  101. ///////////////////////////
  102.  
  103. void scsToWcs(float sx,float sy, float wcsv[3] )
  104. {
  105.  
  106. GLfloat winX, winY, winZ; //variables to hold screen x,y,z coordinates
  107. GLdouble worldX, worldY, worldZ; //variables to hold world x,y,z coordinates
  108.  
  109. //glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
  110. glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get the projection matrix info
  111. glGetIntegerv( GL_VIEWPORT, viewport ); //get the viewport info
  112.  
  113. winX = sx;
  114. winY = (float)viewport[3] - (float)sy;
  115. winZ = 0;
  116.  
  117. //get the world coordinates from the screen coordinates
  118. gluUnProject( winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ);
  119. wcsv[0]=worldX;
  120. wcsv[1]=worldY;
  121. wcsv[2]=worldZ;
  122.  
  123.  
  124. }
  125. void processMouse(int button, int state, int x, int y)
  126. {
  127. if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  128. {
  129. if(flag!=1)
  130. {
  131. flag=1;
  132. clkpt[0].x=x;
  133. clkpt[0].y=y;
  134. }
  135.  
  136.  
  137. scsToWcs(clkpt[0].x,clkpt[0].y,wcsClkDn);
  138. cout<<"\nD: "<<x<<" "<<y<<" wcs: "<<wcsClkDn[0]<<" "<<wcsClkDn[1];
  139. }
  140. else if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
  141. {
  142. if (flag==1)
  143. {
  144. clkpt[1].x=x;
  145. clkpt[1].y=y;
  146. flag=0;
  147. }
  148. float wcs[3];
  149. scsToWcs(clkpt[1].x,clkpt[1].y,wcsClkUp);
  150. cout<<"\nU: "<<x<<" "<<y<<" wcs: "<<wcsClkUp[0]<<" "<<wcsClkUp[1];
  151.  
  152. clikd=!clikd;
  153. }
  154. }
  155.  
  156. //control points
  157. long long nCr(int n, int r)
  158. {
  159. if(r > n / 2) r = n - r; // because C(n, r) == C(n, n - r)
  160. long long ans = 1;
  161. int i;
  162.  
  163. for(i = 1; i <= r; i++)
  164. {
  165. ans *= n - r + i;
  166. ans /= i;
  167. }
  168.  
  169. return ans;
  170. }
  171.  
  172. //polynomial interpretation for N points
  173. void BezierCurve ( double t, float xy[2])
  174. {
  175. double y=0;
  176. double x=0;
  177. t=t>1.0?1.0:t;
  178. for(int i=0; i<=L; i++)
  179. {
  180. int ncr=nCr(L,i);
  181. double oneMinusTpow=pow(1-t,double(L-i));
  182. double tPow=pow(t,double(i));
  183. double coef=oneMinusTpow*tPow*ncr;
  184. x+=coef*ctrlpoints[i][0];
  185. y+=coef*ctrlpoints[i][1];
  186.  
  187. }
  188. xy[0] = float(x);
  189. xy[1] = float(y);
  190.  
  191. //return y;
  192. }
  193.  
  194. ///////////////////////
  195. void setNormal(GLfloat x1, GLfloat y1,GLfloat z1, GLfloat x2, GLfloat y2,GLfloat z2, GLfloat x3, GLfloat y3,GLfloat z3)
  196. {
  197. GLfloat Ux, Uy, Uz, Vx, Vy, Vz, Nx, Ny, Nz;
  198.  
  199. Ux = x2-x1;
  200. Uy = y2-y1;
  201. Uz = z2-z1;
  202.  
  203. Vx = x3-x1;
  204. Vy = y3-y1;
  205. Vz = z3-z1;
  206.  
  207. Nx = Uy*Vz - Uz*Vy;
  208. Ny = Uz*Vx - Ux*Vz;
  209. Nz = Ux*Vy - Uy*Vx;
  210.  
  211. glNormal3f(-Nx,-Ny,-Nz);
  212. }
  213.  
  214. void bottleBezier()
  215. {
  216. int i, j;
  217. float x, y, z, r; //current coordinates
  218. float x1, y1, z1, r1; //next coordinates
  219. float theta;
  220.  
  221. const float startx = 0, endx = ctrlpoints[L][0];
  222. //number of angular slices
  223. const float dx = (endx - startx) / nt; //x step size
  224. const float dtheta = 2*PI / ntheta; //angular step size
  225.  
  226. float t=0;
  227. float dt=1.0/nt;
  228. float xy[2];
  229. BezierCurve( t, xy);
  230. x = xy[0];
  231. r = xy[1];
  232. //rotate about z-axis
  233. float p1x,p1y,p1z,p2x,p2y,p2z;
  234. for ( i = 0; i < nt; ++i ) //step through x
  235. {
  236. theta = 0;
  237. t+=dt;
  238. BezierCurve( t, xy);
  239. x1 = xy[0];
  240. r1 = xy[1];
  241.  
  242. //draw the surface composed of quadrilaterals by sweeping theta
  243. glBegin( GL_QUAD_STRIP );
  244. for ( j = 0; j <= ntheta; ++j )
  245. {
  246. theta += dtheta;
  247. double cosa = cos( theta );
  248. double sina = sin ( theta );
  249. y = r * cosa;
  250. y1 = r1 * cosa; //current and next y
  251. z = r * sina;
  252. z1 = r1 * sina; //current and next z
  253.  
  254. //edge from point at x to point at next x
  255. glVertex3f (x, y, z);
  256.  
  257. if(j>0)
  258. {
  259. setNormal(p1x,p1y,p1z,p2x,p2y,p2z,x, y, z);
  260. }
  261. else
  262. {
  263. p1x=x;
  264. p1y=y;
  265. p1z=z;
  266. p2x=x1;
  267. p2y=y1;
  268. p2z=z1;
  269.  
  270. }
  271. glVertex3f (x1, y1, z1);
  272.  
  273. //forms quad with next pair of points with incremented theta value
  274. }
  275. glEnd();
  276. x = x1;
  277. r = r1;
  278. } //for i
  279.  
  280. }
  281.  
  282.  
  283. static void resize(int width, int height)
  284. {
  285. const float ar = (float) width / (float) height;
  286.  
  287. glViewport(0, 0, width, height);
  288. glMatrixMode(GL_PROJECTION);
  289. glLoadIdentity();
  290. //glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
  291. // gluPerspective(60,ar, 2.0, 100.0);
  292.  
  293. glOrtho(-8.0, 8.0, -8.0*(GLfloat)height/(GLfloat)width, 8.0*(GLfloat)height/(GLfloat)width, 2.0, 25.0);
  294. glMatrixMode(GL_MODELVIEW);
  295. glLoadIdentity() ;
  296. gluLookAt(ex,ey,ez,lx,ly,lz,hx,hy,hz);
  297.  
  298. }
  299.  
  300.  
  301. void showControlPoints()
  302. {
  303. glPointSize(5.0);
  304. glColor3f(1.0, 0.0, 1.0);
  305. glBegin(GL_POINTS);
  306. for (int i = 0; i <=L; i++)
  307. glVertex3fv(&ctrlpoints[i][0]);
  308. glEnd();
  309. }
  310. static void display(void)
  311. {
  312. const double t = glutGet(GLUT_ELAPSED_TIME) / 5000.0;
  313. const double a = t*90.0;
  314.  
  315. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  316.  
  317. if(wired)
  318. {
  319. glPolygonMode( GL_FRONT, GL_LINE ) ;
  320. glPolygonMode( GL_BACK, GL_LINE ) ;
  321.  
  322. }
  323. else
  324. {
  325. glPolygonMode( GL_FRONT,GL_FILL ) ;
  326. glPolygonMode( GL_BACK, GL_FILL ) ;
  327. }
  328.  
  329. glPushMatrix();
  330.  
  331. if(animat)
  332. glRotated(a,0,0,1);
  333. //else
  334. //glRotated(135,0,0,1);
  335.  
  336. glRotatef( anglex, 1.0, 0.0, 0.0);
  337. glRotatef( angley, 0.0, 1.0, 0.0); //rotate about y-axis
  338. glRotatef( anglez, 0.0, 0.0, 1.0);
  339. glScaled(0.5,0.5,0.5);
  340. glRotatef( 270, 0.0, 0.0, 1.0);
  341. glTranslated(-3.5,0,0);
  342. glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
  343.  
  344. matColor(0.9,0.5,0.1,20); // front face color
  345. matColor(0.0,0.5,0.8,20,1); // back face color
  346.  
  347.  
  348. bottleBezier();
  349.  
  350.  
  351. if(shcpt)
  352. {
  353. matColor(0.0,0.0,0.9,20);
  354. showControlPoints();
  355. }
  356.  
  357. glPopMatrix();
  358.  
  359.  
  360.  
  361. glutSwapBuffers();
  362. }
  363.  
  364.  
  365. static void key(unsigned char key, int x, int y)
  366. {
  367. switch (key)
  368. {
  369. case 'A':
  370. animat=!animat;
  371. break;
  372.  
  373. case 's':
  374. case 'S':
  375. shcpt=!shcpt;
  376. break;
  377.  
  378. case 'w':
  379. case 'W':
  380. wired=!wired;
  381. break;
  382.  
  383. case 'x':
  384. anglex = ( anglex + 3 ) % 360;
  385. break;
  386. case 'X':
  387. anglex = ( anglex - 3 ) % 360;
  388. break;
  389.  
  390. case 'y':
  391. angley = ( angley + 3 ) % 360;
  392. break;
  393. case 'Y':
  394. angley = ( angley - 3 ) % 360;
  395. break;
  396.  
  397. case 'z':
  398. anglez = ( anglez + 3 ) % 360;
  399. break;
  400. case 'Z':
  401. anglez = ( anglez - 3 ) % 360;
  402. break;
  403.  
  404.  
  405. case 'q':
  406. case 27 :
  407. glutDestroyWindow(window);
  408. exit(0);
  409. break;
  410. }
  411.  
  412. glutPostRedisplay();
  413. }
  414.  
  415. static void idle(void)
  416. {
  417. glutPostRedisplay();
  418. }
  419.  
  420. const GLfloat light_ambient[] = { 0.5f, 0.5f, 0.5f, 1.0f };
  421. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  422. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  423. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 1.0f };
  424.  
  425. void matColor(float kdr, float kdg, float kdb, float shiny, int frnt_Back, float ambFactor, float specFactor)
  426. {
  427.  
  428. const GLfloat mat_ambient[] = { kdr*ambFactor, kdg*ambFactor, kdb*ambFactor, 1.0f };
  429. const GLfloat mat_diffuse[] = { kdr, kdg, kdb, 1.0f };
  430. const GLfloat mat_specular[] = { 1.0f*specFactor, 1.0f*specFactor, 1.0f*specFactor, 1.0f };
  431. const GLfloat high_shininess[] = { shiny };
  432. if(frnt_Back==0)
  433. {
  434. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  435. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  436. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  437. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  438. }
  439. else if(frnt_Back==1)
  440. {
  441. glMaterialfv(GL_BACK, GL_AMBIENT, mat_ambient);
  442. glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffuse);
  443. glMaterialfv(GL_BACK, GL_SPECULAR, mat_specular);
  444. glMaterialfv(GL_BACK, GL_SHININESS, high_shininess);
  445. }
  446. else if(frnt_Back==2)
  447. {
  448. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  449. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  450. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  451. glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, high_shininess);
  452. }
  453.  
  454. }
  455.  
  456. /* Program entry point */
  457. void myInit()
  458. {
  459. glClearColor(.1,.1,.1,1);
  460.  
  461. glEnable(GL_LIGHT0);
  462. glEnable(GL_NORMALIZE);
  463. glEnable(GL_COLOR_MATERIAL);
  464. glEnable(GL_LIGHTING);
  465.  
  466. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  467. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  468. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  469. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  470. }
  471. int main(int argc, char *argv[])
  472. {
  473. glutInit(&argc, argv);
  474. glutInitWindowSize(640,480);
  475. glutInitWindowPosition(10,10);
  476. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  477.  
  478. glutCreateWindow("Curved Surface");
  479.  
  480. myInit();
  481.  
  482. glutReshapeFunc(resize);
  483. glutDisplayFunc(display);
  484. glutKeyboardFunc(key);
  485. glutMouseFunc(processMouse);
  486. glutIdleFunc(idle);
  487.  
  488.  
  489. glutMainLoop();
  490.  
  491. return EXIT_SUCCESS;
  492. }
  493.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement