Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.28 KB | None | 0 0
  1. /********************************************************/
  2. /* cube.cpp */
  3. /********************************************************/
  4. /* Affiche a l'ecran un cube en 3D */
  5. /********************************************************/
  6.  
  7. /* inclusion des fichiers d'en-tete freeglut */
  8.  
  9. #ifdef __APPLE__
  10. #include <GLUT/glut.h> /* Pour Mac OS X */
  11. #else
  12. #include <GL/freeglut.h> /* Pour les autres systemes */
  13. #endif
  14. #include <cstdlib>
  15. #include <windows.h>
  16. #include <string>
  17. #include <iostream>
  18. #include <math.h>
  19. using namespace std;
  20.  
  21. /*class Point*/
  22. class Point{
  23. public :
  24. //coordonnées x, y et z du point
  25. double x;
  26. double y;
  27. double z;
  28. // couleur r, v et b du point
  29. float r;
  30. float g;
  31. float b;
  32. };
  33.  
  34.  
  35. char presse;
  36. int anglex,angley,x,y,xold,yold;
  37.  
  38. //Variable de l'animation des jambes
  39. GLfloat angleJambeGPos = 0;
  40. GLfloat angleJambeGNeg = -20;
  41.  
  42. GLfloat angleJambeDPos = 0;
  43. GLfloat angleJambeDNeg = 20;
  44.  
  45. GLfloat anglePos = 0;
  46. GLfloat angleNeg = -40;
  47.  
  48. //variables de l'animation du bras droit
  49. GLfloat angleBHaut = 0;
  50. GLfloat angleBBas = 0;
  51.  
  52. //camera
  53. GLfloat g_angle_y = 32.0f;
  54. GLfloat g_inc = 0.0f;
  55. GLfloat g_eye_y = 0;
  56.  
  57.  
  58. /* Prototype des fonctions */
  59. void affichage();
  60. void clavier(unsigned char touche,int x,int y);
  61. void reshape(int x,int y);
  62. void idle();
  63. void mouse(int bouton,int etat,int x,int y);
  64. void mousemotion(int x,int y);
  65. void marche(int i);
  66. void dessinMain();
  67.  
  68. GLUquadricObj *pObj;
  69.  
  70.  
  71.  
  72. int main(int argc,char **argv)
  73. {
  74. /* initialisation de glut et creation
  75. de la fenetre */
  76. glutInit(&argc,argv);
  77. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  78. glutInitWindowPosition(200,200);
  79. glutInitWindowSize(500,500);
  80. glutCreateWindow("cube");
  81.  
  82. /* Initialisation d'OpenGL */
  83. glClearColor(0.0,0.0,0.0,0.0);
  84. glColor3f(1.0,1.0,1.0);
  85. glPointSize(2.0);
  86. glEnable(GL_DEPTH_TEST);
  87.  
  88.  
  89. GLfloat diffuse_0[]={1.0,1.0,1.0,1.0};
  90. GLfloat ambient_0[]={0.2,0.2,0.2,1.0};
  91. GLfloat specular_0[]={1.0,1.0,1.0,1.0};
  92. GLfloat spot_direction[]={-1.0,-1.0,0.0};
  93.  
  94.  
  95.  
  96. glEnable(GL_DEPTH_TEST);
  97. glEnable(GL_LIGHTING);
  98.  
  99. GLfloat position_source0[]={-2.0,5.0,1.0,0.5};
  100.  
  101. glLightfv(GL_LIGHT0,GL_POSITION,position_source0);
  102. glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse_0);
  103. glLightfv(GL_LIGHT0,GL_SPECULAR,specular_0);
  104. glLightfv(GL_LIGHT0,GL_AMBIENT,ambient_0);
  105. glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse_0);
  106. /*glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION,1.5);
  107. glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION,0.5);
  108. glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION,0.2);
  109.  
  110. glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
  111. glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
  112. glLightf(GL_LIGHT0, GL_SPOT_EXPONENT,2.0);*/
  113.  
  114. glEnable(GL_LIGHT0);
  115.  
  116. pObj = gluNewQuadric();
  117. gluQuadricNormals(pObj, GLU_SMOOTH);
  118.  
  119.  
  120. /* enregistrement des fonctions de rappel */
  121. glutDisplayFunc(affichage);
  122. glutKeyboardFunc(clavier);
  123. glutReshapeFunc(reshape);
  124. glutMouseFunc(mouse);
  125. glutMotionFunc(mousemotion);
  126. glutTimerFunc(200,marche,1);
  127.  
  128. /* Entree dans la boucle principale glut */
  129. glutMainLoop();
  130. return 0;
  131. }
  132.  
  133. void torusMain(int nParallele, int nMeridien)
  134. {
  135. double s, t, x, y, z;
  136. GLfloat phi, theta;
  137. GLfloat R = 1.0;
  138. GLfloat r = 0.2;
  139. GLfloat deux_pi = 2 * M_PI;
  140. for (int i = 0; i < nParallele; i++) {
  141. glBegin(GL_QUAD_STRIP);
  142. for (int j = 0; j <= 3*nMeridien/4; j++) {
  143. for (int k = 1; k >= 0; k--) {
  144. s = (i + k) % nParallele + 0.5;
  145. t = j % nMeridien;
  146.  
  147. theta = s * deux_pi / nParallele;
  148. phi =t*deux_pi/nMeridien;
  149.  
  150. x = (R+r*cos(theta))*cos(phi);
  151. y = (R+r*cos(theta))*sin(phi);
  152. z = r * sin(theta);
  153.  
  154. glVertex3f(x, y, z);
  155. }
  156. }
  157. glEnd();
  158. }
  159. }
  160. void dessinMain()
  161. {
  162. glPushMatrix();
  163. glColor3d(1.0,1.0,0.0);
  164. glPushMatrix();
  165. glTranslatef(0.0,0.4,-0.9);
  166. glutSolidCylinder(0.5,0.5,50,50);
  167. glPopMatrix();
  168. glPushMatrix();
  169. glTranslatef(0.0,0.8,0.7);
  170. glRotatef(-90,1.0,0.0,0.0);
  171. glRotatef(-45,0.0,0.0,1.0);
  172. glScalef(1.0,1.0,4.0);
  173. torusMain(50,50);
  174. glPopMatrix();
  175. glPopMatrix();
  176. }
  177.  
  178. void marche(int i)
  179. {
  180. for(int k=0; k<7; k++)
  181. {
  182. if(anglePos>-40)
  183. {
  184. angleJambeGPos -=1;
  185. angleJambeDPos +=1;
  186. anglePos -=1;
  187. }
  188. else if (angleNeg<40)
  189. {
  190. angleJambeGNeg +=1;
  191. angleJambeDNeg -=1;
  192. angleNeg +=1;
  193. }
  194.  
  195. else if (anglePos == -40&&angleNeg==40)
  196. {
  197. anglePos =40;
  198. angleNeg =-40;
  199. }
  200.  
  201. }
  202.  
  203. glutPostRedisplay();
  204. glutTimerFunc(40,marche,i);
  205. }
  206.  
  207. void dessinCorps()
  208. {
  209. //dimension corps lego : larg : 4.4, prof = 2 haut : 4
  210. //Tableau pour stocker les sommets du cube et leur couleur
  211.  
  212. Point pCorps[8]={
  213. //
  214. {-0.5,-0.5, 0.25,1.0,1.0,1.0},
  215. { 0.5, -0.5, 0.25,1.0,1.0,1.0},
  216. { 0.5, -0.5, -0.25,1.0,1.0,1.0},
  217. { -0.5, -0.5, -0.25,1.0,1.0,1.0},
  218.  
  219. { -0.4,0.5, 0.25,1.0,1.0,1.0},
  220. { 0.4, 0.5, 0.25,1.0,1.0,1.0},
  221. { 0.4, 0.5, -0.25,1.0,1.0,1.0},
  222. { -0.4, 0.5, -0.25,1.0,1.0,1.0}};
  223.  
  224. //Tableau pour stocker les indices des sommets par face pour le cube
  225. int fCorps[6][4]={
  226. {0,3,2,1},
  227. {0,1,5,4},
  228. {1,2,6,5},
  229. {2,3,7,6},
  230. {0,4,7,3},
  231. {4,5,6,7}};
  232. glPushMatrix();
  233.  
  234. glTranslatef(0.0,5.1,0.0);
  235. glTranslatef(0.0,2.0,0.0);
  236. glScalef(4.4,4.0,4.0);
  237.  
  238. glPushMatrix();
  239.  
  240. // Dessin du Corps
  241. for (int i=0;i<6;i++)
  242. {
  243. glBegin(GL_POLYGON);
  244. for (int j=0;j<4;j++){
  245. glColor3f(pCorps[fCorps[i][j]].r,pCorps[fCorps[i][j]].g,pCorps[fCorps[i][j]].b);
  246. glVertex3f(pCorps[fCorps[i][j]].x,pCorps[fCorps[i][j]].y,pCorps[fCorps[i][j]].z);
  247. }
  248. glEnd();
  249. }
  250. glPopMatrix();
  251. glPopMatrix();
  252. }
  253.  
  254. void dessinCeinture()
  255. {
  256. //dessin haut ceinture
  257. glPushMatrix();
  258. glColor3f(1.0,0.0,0.0);
  259. glTranslatef(0.0,4.9,0.0);
  260. glScalef(4.4,0.5,2);
  261. glutSolidCube(1);
  262. glPopMatrix();
  263.  
  264. //dessin cylindre entre jambe
  265. glPushMatrix();
  266. glColor3f(1.0,0.0,0.0);
  267. glTranslatef(-0.2,4.0,0.0);
  268. glRotatef(90,0.0,1.0,0.0);
  269. glutSolidCylinder(1.0,0.4,20,20);
  270. glPopMatrix();
  271. }
  272.  
  273. void tete()
  274. {
  275. glPushMatrix();
  276. glColor3f(1.0,1.0,0.0);
  277. glTranslatef(0.0,9.1,0.0);
  278. glScalef(0.5,0.5,0.5);
  279.  
  280. //cou
  281. glPushMatrix();
  282. glRotatef(-90,1.0,0.0,0.0);
  283. glutSolidCylinder(2,0.3,20,1);
  284. glPopMatrix();
  285.  
  286. //tete
  287. glPushMatrix();
  288. glTranslatef(0.0,0.3,0.0);
  289. //glRotatef();
  290. //glScalef();
  291.  
  292. glPushMatrix();
  293. glRotatef(-90,1.0,0.0,0.0);
  294. glutSolidCylinder(2.0,1,20,20);
  295. glPopMatrix();
  296.  
  297. glPushMatrix();
  298. glTranslatef(0.0,1.0,0.0);
  299. glRotatef(-90,1.0,0.0,0.0);
  300. glutSolidTorus(1.0,2.0,20,20);
  301. glPopMatrix();
  302.  
  303. glPushMatrix();
  304. glTranslatef(0.0,1.0,0.0);
  305. glRotatef(-90,1.0,0.0,0.0);
  306. glutSolidCylinder(3,4,20,20);
  307. glPopMatrix();
  308.  
  309. glPushMatrix();
  310. glTranslatef(0.0,5.0,0.0);
  311. glRotatef(-90,1.0,0.0,0.0);
  312. glutSolidTorus(1.0,2.0,20,20);
  313. glPopMatrix();
  314.  
  315. glPushMatrix();
  316. glTranslatef(0.0,5.0,0.0);
  317. glRotatef(-90,1.0,0.0,0.0);
  318. glutSolidCylinder(2.0,1,20,20);
  319. glPopMatrix();
  320. glPopMatrix();
  321.  
  322. //bitoniau
  323. glPushMatrix();
  324. glTranslatef(0.0,6.3,0.0);
  325. glRotatef(-90,1.0,0.0,0.0);
  326. glutSolidCylinder(1.5,0.6,20,20);
  327. glPopMatrix();
  328. glPopMatrix();
  329. }
  330.  
  331. void dessinBrasGauche()
  332. {
  333. glPushMatrix();
  334.  
  335. glTranslated(2.2,8.2,0.0);
  336. glRotated(-angleBBas,1.0,0.0,0.0);
  337. glRotated(-angleBHaut,1.0,0.0,0.0);
  338. glTranslated(-2.2,-8.2,0.0);
  339.  
  340. glPushMatrix();
  341. glColor3f(0.0,0.0,1.0);
  342. glTranslatef(2.2,8.2,0.0);
  343. glRotatef(18,0,0,1.0);
  344.  
  345. //epaule
  346. glPushMatrix();
  347. glutSolidSphere(0.75,20,20);
  348. glPopMatrix();
  349.  
  350. //apres bras
  351. glPushMatrix();
  352. glRotatef(90,1.0,0.0,0.0);
  353. glutSolidCylinder(0.75,1.8,20,20);
  354. glPopMatrix();
  355.  
  356. //avant bras et main
  357. glPushMatrix();
  358. glTranslatef(0.0,-1.4,-0.12);
  359. glRotatef(60,1.0,0.0,0.0);
  360. glutSolidCylinder(0.75,1.3,20,20);
  361.  
  362. glPushMatrix();
  363. glTranslatef(0.0,-0.4,2.2);
  364. dessinMain();
  365. glPopMatrix();
  366. glPopMatrix();
  367. glPopMatrix();
  368. glPopMatrix();
  369. }
  370.  
  371. void dessinBrasDroit()
  372. {
  373. glPushMatrix();
  374. glColor3f(0.0,0.0,1.0);
  375. glTranslatef(-2.2,8.2,0.0);
  376. glRotatef(-18,0,0,1.0);
  377. glPushMatrix();
  378. glutSolidSphere(0.75,20,20);
  379. glPopMatrix();
  380. glPushMatrix();
  381. glRotatef(90,1.0,0.0,0.0);
  382. glutSolidCylinder(0.75,1.8,20,20);
  383. glPopMatrix();
  384.  
  385. glPushMatrix();
  386. glTranslatef(0.0,-1.4,-0.12);
  387. glRotatef(60,1.0,0.0,0.0);
  388. glutSolidCylinder(0.75,1.3,20,20);
  389.  
  390. glPushMatrix();
  391. glTranslatef(0.0,-0.4,2.2);
  392. dessinMain();
  393. glPopMatrix();
  394. glPopMatrix();
  395.  
  396.  
  397. glPopMatrix();
  398. }
  399.  
  400. void dessinJambeDroite()
  401. {
  402. glPushMatrix();
  403. glTranslatef(0.0,4.0,0.0);
  404. glRotatef(angleJambeDPos, 1.0,0.0,0.0);
  405. glRotatef(angleJambeDNeg, 1.0,0.0,0.0);
  406. glTranslatef(0.0,-4.0,0.0);
  407.  
  408. glPushMatrix();
  409. glTranslatef(0.0,4.0,0.0);
  410. glRotatef(-20, 1.0,0.0,0.0);
  411. glTranslatef(0.0,-4.0,0.0);
  412. glPushMatrix();
  413. glColor3f(1.0,0.0,0.0);
  414. glTranslatef(-0.2,4.0,0.0);
  415. glRotatef(-90,0.0,1.0,0.0);
  416. glutSolidCylinder(1.0,2.0,20,1);
  417. glPopMatrix();
  418.  
  419. glPushMatrix();
  420. glColor3f(1.0,0.0,0.0);
  421. glTranslatef(-1.2,0.5,0.0);
  422. glScalef(1.0,0.5,1.0);
  423. glutSolidCube(2);
  424. glPopMatrix();
  425.  
  426. glPushMatrix();
  427. glColor3f(1.0,0.0,0.0);
  428. glTranslatef(-1.2,2.5,-0.3);
  429. glScalef(1.0,1.5,0.7);
  430. glutSolidCube(2);
  431. glPopMatrix();
  432. glPopMatrix();
  433. glPopMatrix();
  434. }
  435. void dessinJambeGauche()
  436. {
  437. glPushMatrix();
  438. glTranslatef(0.0,4.0,0.0);
  439. glRotatef(angleJambeGPos, 1.0,0.0,0.0);
  440. glRotatef(angleJambeGNeg, 1.0,0.0,0.0);
  441. glTranslatef(0.0,-4.0,0.0);
  442.  
  443. glPushMatrix();
  444. glTranslatef(0.0,4.0,0.0);
  445. glRotatef(20, 1.0,0.0,0.0);
  446. glTranslatef(0.0,-4.0,0.0);
  447. //glScalef();
  448. glPushMatrix();
  449. glColor3f(1.0,0.0,0.0);
  450. glTranslatef(1.2,0.5,0.0);
  451. glScalef(1.0,0.5,1.0);
  452. glutSolidCube(2);
  453. glPopMatrix();
  454.  
  455. glPushMatrix();
  456. glColor3f(1.0,0.0,0.0);
  457. glTranslatef(0.2,4.0,0.0);
  458. glRotatef(90,0.0,1.0,0.0);
  459. glutSolidCylinder(1.0,2.0,20,1);
  460. glPopMatrix();
  461.  
  462. glPushMatrix();
  463. glColor3f(1.0,0.0,0.0);
  464. glTranslatef(1.2,2.5,-0.3);
  465. glScalef(1.0,1.5,0.7);
  466. glutSolidCube(2);
  467. glPopMatrix();
  468. glPopMatrix();
  469. glPopMatrix();
  470. }
  471.  
  472. void affichage()
  473. {
  474. glMatrixMode(GL_PROJECTION);
  475. glLoadIdentity();
  476. //glOrtho(-5,5,5,15,-5,5); // vue tete
  477. //glOrtho(-3,3,-3,3,-3,3); //vue proche en 0
  478. glOrtho(-10,10,-3,17,-5,5); // vue tout corps
  479.  
  480. glMatrixMode(GL_MODELVIEW);
  481. int i,j;
  482. /* effacement de l'image avec la couleur de fond */
  483. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  484. glShadeModel(GL_SMOOTH);
  485.  
  486. glLoadIdentity();
  487. glRotatef(angley,1.0,0.0,0.0);
  488. glRotatef(anglex,0.0,1.0,0.0);
  489.  
  490. dessinCorps();
  491. dessinCeinture();
  492. dessinJambeDroite();
  493. dessinJambeGauche();
  494. dessinBrasDroit();
  495. dessinBrasGauche();
  496. tete();
  497.  
  498. //Repère
  499. //axe x en rouge
  500. glBegin(GL_LINES);
  501. glColor3f(1.0,0.0,0.0);
  502. glVertex3f(0, 0,0.0);
  503. glVertex3f(1, 0,0.0);
  504. glEnd();
  505. //axe des y en vert
  506. glBegin(GL_LINES);
  507. glColor3f(0.0,1.0,0.0);
  508. glVertex3f(0, 0,0.0);
  509. glVertex3f(0, 1,0.0);
  510. glEnd();
  511. //axe des z en bleu
  512. glBegin(GL_LINES);
  513. glColor3f(0.0,0.0,1.0);
  514. glVertex3f(0, 0,0.0);
  515. glVertex3f(0, 0,1.0);
  516. glEnd();
  517.  
  518. glFlush();
  519.  
  520. //On echange les buffers
  521. glutSwapBuffers();
  522. }
  523.  
  524. void clavier(unsigned char touche,int x,int y)
  525. {
  526. switch (touche)
  527. {
  528. case 'p': /* affichage du carre plein */
  529. glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
  530. glutPostRedisplay();
  531. break;
  532. case 'f': /* affichage en mode fil de fer */
  533. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  534. glutPostRedisplay();
  535. break;
  536. case 's' : /* Affichage en mode sommets seuls */
  537. glPolygonMode(GL_FRONT_AND_BACK,GL_POINT);
  538. glutPostRedisplay();
  539. break;
  540. case 'd':
  541. glEnable(GL_DEPTH_TEST);
  542. glutPostRedisplay();
  543. break;
  544. case 'D':
  545. glDisable(GL_DEPTH_TEST);
  546. glutPostRedisplay();
  547. break;
  548. case 'b':
  549. angleBHaut+=5;
  550. glutPostRedisplay();
  551. break;
  552. case 'B':
  553. angleBBas -=5;
  554. glutPostRedisplay();
  555. break;
  556.  
  557. case 'q' : /*la touche 'q' permet de quitter le programme */
  558. exit(0);
  559. }
  560. }
  561.  
  562. void reshape(int x,int y)
  563. {
  564. if (x<y)
  565. glViewport(0,(y-x)/2,x,x);
  566. else{
  567. glViewport((x-y)/2,0,y,y);}
  568.  
  569. /*gluLookAt( 0.0f, 3.5f * sin( g_eye_y ), 3.5f * cos( g_eye_y ),
  570. 0.0f, 0.0f, 0.0f,
  571. 0.0f, ( cos( g_eye_y ) < 0 ? -1.0f : 1.0f ), 0.0f );*/
  572. }
  573.  
  574. void mouse(int button, int state,int x,int y)
  575. {
  576. /* si on appuie sur le bouton gauche */
  577. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  578. {
  579. presse = 1; /* le booleen presse passe a 1 (vrai) */
  580. xold = x; /* on sauvegarde la position de la souris */
  581. yold=y;
  582. }
  583. /* si on relache le bouton gauche */
  584. if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
  585. presse=0; /* le booleen presse passe a 0 (faux) */
  586. }
  587.  
  588. void mousemotion(int x,int y)
  589. {
  590. if (presse) /* si le bouton gauche est presse */
  591. {
  592. /* on modifie les angles de rotation de l'objet
  593. en fonction de la position actuelle de la souris et de la derniere
  594. position sauvegardee */
  595. anglex=anglex+(x-xold);
  596. angley=angley+(y-yold);
  597. glutPostRedisplay(); /* on demande un rafraichissement de l'affichage */
  598. }
  599.  
  600. xold=x; /* sauvegarde des valeurs courante de le position de la souris */
  601. yold=y;
  602. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement