Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.42 KB | None | 0 0
  1.  
  2.  
  3.  
  4. /************************************************************/
  5. /* TP2: Primitives 3D et couleur */
  6. /************************************************************/
  7. /* */
  8. /* ESGI: algorithmique pour l'infographie */
  9. /* */
  10. /************************************************************/
  11. /* */
  12. /* Objectif: afficher des formes 3D et illuminer la scène */
  13. /* */
  14. /************************************************************/
  15.  
  16.  
  17.  
  18.  
  19. #include<windows.h>
  20. #include <math.h>
  21. #ifdef __APPLE__
  22. #include <GLUT/glut.h>
  23. #else
  24. #include <glut.h>
  25. #endif
  26.  
  27. #include<stdlib.h>
  28. #include<stdio.h>
  29.  
  30.  
  31.  
  32. float epaule = 0.0;
  33. float coude = 0.0;
  34. float counter = 0.0;
  35. float bounceman = 0.0;
  36. float counterb = 0.0;
  37. // camera centree
  38. /*int x = 0;
  39. int y = 0;
  40. int z = 0;
  41. int u=0;
  42. int v=1;
  43. int w=0;
  44. float R= 10.0;
  45. float alpha=0.1;
  46. float b = 0.1;
  47. float x0;
  48. float yy0;
  49. float z0;*/
  50.  
  51. int xm = 0;
  52. int ym = 0;
  53. int zm = 0;
  54. int u=0;
  55. int v=1;
  56. int w=0;
  57. int R= 10;
  58. float alpha=0.1;
  59. float b = 1;
  60.  
  61. /* prototypes de fonctions */
  62. void initRendering();
  63. void display(void);
  64. void reshape(int w,int h);
  65. void update(int value);
  66. void keyboard(unsigned char key, int x, int y);
  67.  
  68.  
  69.  
  70.  
  71. /* Programme principal */
  72. int main(int argc, char **argv){
  73.  
  74. /* Initialisation de glut et creation de la fenetre */
  75. glutInit(&argc, argv);
  76. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  77. glutInitWindowSize(500, 500);
  78. glutInitWindowPosition (100, 100);
  79. glutCreateWindow("Robot Articulé");
  80.  
  81. /* Initialisation d'OpenGL */
  82. initRendering();
  83.  
  84. /* Enregistrement des fonctions de rappel */
  85. glutDisplayFunc(display);
  86. glutReshapeFunc(reshape);
  87. glutTimerFunc (20,update, 0);
  88. glutKeyboardFunc(keyboard);
  89.  
  90. /* Entrée dans la boucle principale de glut, traitement des évènements */
  91. glutMainLoop();
  92. return 0;
  93. }
  94.  
  95.  
  96. /* Initialisation d'OpenGL pour le rendu de la scène */
  97. void initRendering() {
  98.  
  99. /* Active le z-buffer */
  100. glEnable(GL_DEPTH_TEST);
  101.  
  102. /* Activation des couleurs */
  103. glEnable(GL_COLOR_MATERIAL);
  104.  
  105. /* définit la couleur d'effacement et la couleur de fond */
  106. glClearColor(0.0, 0.0, 0.0, 0.0);
  107.  
  108. /* Activation des lumières */
  109. glEnable(GL_LIGHTING); // Activation du mode
  110. glEnable(GL_LIGHT0); // Activation lumière n°0
  111. glEnable(GL_LIGHT1); // Activation lumière n°1
  112.  
  113. /* Les normales (crées par glNormal(*)) sont automatiquement unitaires */
  114. glEnable(GL_NORMALIZE);
  115.  
  116.  
  117. /* Activation du mode ombrage (shading) et lissage (smooth) des couleur */
  118. glShadeModel(GL_SMOOTH);
  119.  
  120. /* définit la taille d'un pixel*/
  121. glPointSize(2.0);
  122.  
  123. }
  124.  
  125. /* Création de la scène avec lampes */
  126. void display(void){
  127.  
  128. /* Déclaration des couleurs et positions des lampes */
  129. GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; // Color (0.2, 0.2, 0.2)
  130.  
  131. GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; // Color (0.5, 0.5, 0.5)
  132. GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; // Positioned at (4, 0, 8)
  133.  
  134. GLfloat lightColor1[] = {0.8f, 0.2f, 0.2f, 1.0f}; // Color (0.5, 0.2, 0.2)
  135. GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f}; // Coming from the direction (-1, 0.5, 0.5)
  136.  
  137. /* Déclaration des différents types de matière des sphères */
  138. GLfloat no_mat[] = {0.0, 0.0, 0.0, 1.0};
  139. GLfloat mat_ambient_color[] = {0.8, 0.8, 0.2, 1.0};
  140. GLfloat mat_diffuse[] = {0.1, 0.5, 0.8, 1.0};
  141. GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
  142. GLfloat no_shininess[] = {0.0};
  143. GLfloat low_shininess[] = {5.0};
  144. GLfloat high_shininess[] = {100.0};
  145. GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
  146.  
  147. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  148. glMatrixMode(GL_MODELVIEW);
  149. glLoadIdentity();
  150. // Ajout lumière ambiante
  151. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
  152.  
  153. // Ajout lumière positionnelle L0
  154. glLightfv(GL_LIGHT0 , GL_DIFFUSE , lightColor0); // lumière diffuse
  155. glLightfv(GL_LIGHT0 , GL_POSITION , lightPos0); // position
  156.  
  157. // Ajout lumière positionnelle L1
  158. glLightfv(GL_LIGHT1, GL_DIFFUSE , lightColor1);
  159. glLightfv(GL_LIGHT1, GL_SPECULAR , lightColor1); // lumiére spéculaire
  160. glLightfv(GL_LIGHT1, GL_POSITION , lightPos1);
  161.  
  162. float x0 = R*cos(b)*sin(alpha);
  163. float yy0 = R*sin(b);
  164. float z0 = R*cos(alpha)*cos(b);
  165. gluLookAt(x0,yy0,z0,xm,ym,zm,u,v,w);
  166.  
  167.  
  168. /*----------------ENSEMBLE HUMAIN-----------------------*/
  169. glPushMatrix();
  170. glPushMatrix();
  171. /*-------------------- CORPS ---------------------*/
  172. glScalef(1,1,1);
  173. glTranslatef(-1, 0, 0); // au milieu ecrire variable bounceman pour le balancement
  174.  
  175. /* TETE */
  176. glTranslatef(-1, 2, 0);
  177. glutSolidSphere(1, 10.0, 10.0);
  178.  
  179. /* SQUELETTE ET PIED DE BICHE */
  180. glPushMatrix();
  181. glScalef(1,7,1);
  182. glTranslatef(-0.1, -1, 0);
  183. glutSolidCube(1.0);
  184. glPopMatrix();
  185.  
  186. /* VENTRE*/
  187. glPushMatrix();
  188. glScalef(2,2,2);
  189. glTranslatef(-0.1, -2.1, 0);
  190. glutSolidSphere(1.1, 35.0, 10.0);
  191. glPopMatrix();
  192. /*-------------------- CORPS FIN ---------------------*/
  193.  
  194. /*-------------------- BRAS ---------------------*/
  195. /* BRAS DROIT */
  196. glPushMatrix();
  197.  
  198. /* PREMIERE EPAULE HAUTE */
  199. glTranslatef(0, -3, 1.5);
  200. glRotatef(epaule, 0.0, 0.0, 1);
  201. glutSolidSphere(0.6, 10.0, 10.0);
  202. glTranslatef(1,0,0);
  203. /* PREMIER COUDE HAUT */
  204. glPushMatrix();
  205. glScalef(2,1,1);
  206. glRotatef(250,1,1,1);
  207. glutSolidCube(1.0);
  208. glPopMatrix();
  209. /* PREMIERE ROTULE MILIEU */
  210. glTranslatef(1,0,0);
  211. glRotatef(coude, 0.0, 0.0, 1);
  212. glutSolidSphere(0.6, 10.0, 10.0);
  213. glTranslatef(1,0,0);
  214. /* AVANT BRAS */
  215. glPushMatrix();
  216. glScalef(2,1,1);
  217. glutSolidCube(1.0);
  218. glPopMatrix();
  219.  
  220. /* MAINS */
  221. glTranslatef(1,0,0);
  222. glScalef(1.5,0.2,0.5);
  223. glutSolidCube(1.0);
  224.  
  225. /* DOIGTS*/
  226. glPushMatrix();
  227.  
  228. glTranslatef(0.6,0,0.5);
  229. glPushMatrix();
  230. glScalef(0.3,0.2,0.2);
  231. glRotatef(50, 0, 5, 1);
  232. glutSolidCube(1.0);
  233. glPopMatrix();
  234.  
  235. glTranslatef(0,0,-0.9);
  236. glPushMatrix();
  237. glScalef(0.3,0.2,0.2);
  238. glRotatef(50, 0, 1, 5);
  239. glutSolidCube(1.0);
  240. glPopMatrix();
  241.  
  242. glTranslatef(0,0,0.7);
  243. glPushMatrix();
  244. glScalef(0.3,0.2,0.2);
  245. glRotatef(50, 0, 2, 3);
  246. glutSolidCube(1.0);
  247. glPopMatrix();
  248.  
  249. glTranslatef(0,0,-0.4);
  250. glPushMatrix();
  251. glScalef(0.3,0.2,0.2);
  252. glRotatef(50, 0, 2, 3);
  253. glutSolidCube(1.0);
  254. glPopMatrix();
  255.  
  256. glTranslatef(0,0,-0.4);
  257. glPushMatrix();
  258. glScalef(0.3,0.2,0.2);
  259. glRotatef(180, 0, 3, 5);
  260. glutSolidCube(1.0);
  261. glPopMatrix();
  262.  
  263. glPopMatrix();
  264.  
  265. glPopMatrix();
  266. /* FIN BRAS DROIT */
  267.  
  268. /* BRAS GAUCHE*/
  269. glPushMatrix();
  270. /* PREMIERE EPAULE HAUTE */
  271. glTranslatef(0, -3, -1.5);
  272. glRotatef(epaule, 0.0, 0.0, 1);
  273. glutSolidSphere(0.6, 10.0, 10.0);
  274. glTranslatef(1,0,0);
  275. /* PREMIER COUDE HAUT */
  276. glPushMatrix();
  277. glScalef(2,1,1);
  278. glRotatef(250,1,1,1);
  279. glutSolidCube(1.0);
  280. glPopMatrix();
  281. /* PREMIERE ROTULE MILIEU */
  282. glTranslatef(1,0,0);
  283. glRotatef(coude, 0.0, 0.0, 1);
  284. glutSolidSphere(0.6, 10.0, 10.0);
  285. glTranslatef(1,0,0);
  286. /* AVANT BRAS */
  287. glPushMatrix();
  288. glScalef(2,1,1);
  289. glutSolidCube(1.0);
  290. glPopMatrix();
  291. /* MAINS */
  292. glTranslatef(1,0,0);
  293. glutSolidSphere(0.6, 10.0, 10.0);
  294.  
  295. glPopMatrix();
  296. /*-------------------- BRAS FIN---------------------*/
  297. glPopMatrix();
  298. glPopMatrix();
  299.  
  300. /*----------------ENSEMBLE HUMAIN FIN-----------------------*/
  301.  
  302. glutSwapBuffers();
  303.  
  304. /* On force l'affichage */
  305. glFlush();
  306. }
  307.  
  308. /* Fonction de mise à jour: mouvements des objets*/
  309. void update(int value){
  310. /*epaule += 0.2;
  311. if (epaule > 360){
  312. epaule -= 360;
  313. }
  314. */
  315.  
  316. if( counterb > 1.5 ){
  317. counterb = 0.0;
  318. }
  319. counterb += 0.1;
  320.  
  321. if (counterb > 1 ){
  322. bounceman -= 0.15;
  323. }else{
  324. bounceman += 0.1;
  325. }
  326.  
  327. if( counter > 180 ){
  328. counter = 0.0;
  329. }
  330. counter += 0.2;
  331. if (counter > 90 && counter < 180 ){
  332. coude -= 0.2;
  333. }else{
  334. coude += 0.2;
  335. }
  336. glutPostRedisplay();
  337. glutTimerFunc(10,update, 0);
  338. }
  339.  
  340.  
  341. /* Mise en forme de la scène pour l'affichage */
  342. void reshape(int w, int h){
  343. glViewport(0, 0,(GLsizei) w, (GLsizei) h);
  344. glMatrixMode(GL_PROJECTION);
  345. glLoadIdentity();
  346. gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 1.0, 200.0);
  347. }
  348.  
  349.  
  350.  
  351. /* Fonction de gestion au clavier des activations des lumières */
  352. void keyboard(unsigned char key, int x, int y) {
  353. switch (key){
  354.  
  355. case 'a': /* activation lumière n°0 */
  356. alpha += 0.2;
  357. // printf(" alpha : %f ",alpha);
  358. if(alpha > (2*3.14-0.02)){
  359. alpha = 0.0;
  360. }
  361. glutPostRedisplay();
  362. break;
  363.  
  364. case 'b': /* activation lumière n°1*/
  365. alpha -= 0.2;
  366. if(alpha > (2*3.14-0.02)){
  367. alpha = 0.0;
  368. }
  369. glutPostRedisplay();
  370. break;
  371.  
  372. case 'c': /* activation lumière n°2*/
  373. R += 2;
  374. glutPostRedisplay();
  375. break;
  376.  
  377. case 'e': /* activation des lumières */
  378. glEnable(GL_LIGHTING);
  379. glEnable(GL_LIGHT0);
  380. glEnable(GL_LIGHT1);
  381. glutPostRedisplay();
  382. break;
  383.  
  384. case 'f': /* désactivation des lumières */
  385. glDisable(GL_LIGHTING);
  386. glutPostRedisplay();
  387. break;
  388.  
  389. case 't':
  390. alpha += 0.2;
  391. if(alpha > (2*M_PI-0.02)) alpha = 0.0;
  392. glutPostRedisplay();
  393. break;
  394.  
  395. case 'g':
  396. alpha -= 0.2;
  397. if(alpha > (2*M_PI-0.02)) alpha = 0.0;
  398. glutPostRedisplay();
  399. break;
  400.  
  401. case 'z':
  402. if(R > 2)
  403. R -= 1;
  404. glutPostRedisplay();
  405. break;
  406.  
  407. case 's':
  408. R += 1;
  409. glutPostRedisplay();
  410. break;
  411.  
  412. case 'p':
  413. b += 0.1;
  414. glutPostRedisplay();
  415. break;
  416.  
  417. case 'n':
  418. zm += 1;
  419. glutPostRedisplay();
  420. break;
  421.  
  422. case 'j':
  423. zm -= 1;
  424. glutPostRedisplay();
  425. break;
  426.  
  427. case 'q': /* Quitter le programme */
  428. exit(0);
  429. }
  430. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement