Advertisement
Guest User

super:D

a guest
Oct 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.86 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. /****************************************************************/
  4. /* works on mac using freeglut and xquartz */
  5. /* you can install freeglut using homebrew */
  6. /* xquartz can be downloaded at http://xquartz.macosforge.org */
  7. /****************************************************************/
  8. #include <GL/freeglut.h>
  9. #include <jpeglib.h>
  10. #include <jerror.h>
  11. #include <math.h>
  12.  
  13. #ifdef __WIN32
  14. #pragma comment (lib, "jpeg.lib")
  15. #endif
  16.  
  17. class Point{
  18. public :
  19. //coordonnées x, y et z du point
  20. double x;
  21. double y;
  22. double z;
  23. // couleur r, v et b du point
  24. float r;
  25. float g;
  26. float b;
  27. };
  28.  
  29. const int n = 100;
  30. float r = 0.5;
  31. float h = 1;
  32.  
  33. Point pCylindre[2*n];
  34. int fCylindre[n][4];
  35. const int largimg = 256, hautimg=256;
  36. unsigned char image[largimg*hautimg*3];
  37. unsigned char texture2[largimg*2][hautimg*2][3];
  38. char presse;
  39. int anglex=30,angley=20,x,y,xold,yold;
  40.  
  41. void affichage();
  42. void clavier(unsigned char touche,int x,int y);
  43. void souris(int boutton, int etat,int x,int y);
  44. void sourismouv(int x,int y);
  45. void redim(int l,int h);
  46. void loadJpegImage(char *fichier);
  47.  
  48. int main(int argc,char **argv)
  49.  
  50. {
  51. /* Chargement de la texture */
  52. loadJpegImage("./calimero.jpg");
  53.  
  54. /* Creation de la fenetre OpenGL */
  55. glutInit(&argc,argv);
  56. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  57. glutInitWindowSize(200,200);
  58. glutCreateWindow("Texture JPEG");
  59.  
  60. /* Initialisation de l'etat d'OpenGL */
  61. glClearColor(0.0,0.0,0.0,0.0);
  62. glShadeModel(GL_FLAT);
  63. glEnable(GL_DEPTH_TEST);
  64.  
  65. /* Mise en place de la projection perspective */
  66. glMatrixMode(GL_PROJECTION);
  67. glLoadIdentity();
  68. gluPerspective(45.0,1,1.0,5.0);
  69. glMatrixMode(GL_MODELVIEW);
  70.  
  71. /* Parametrage du placage de textures */
  72. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  73. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  74. glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,largimg*2,hautimg*2,0,
  75. GL_RGB,GL_UNSIGNED_BYTE,texture2);
  76. glEnable(GL_TEXTURE_2D);
  77.  
  78. /* Mise en place des fonctions de rappel */
  79. glutDisplayFunc(affichage);
  80. glutKeyboardFunc(clavier);
  81. glutMouseFunc(souris);
  82. glutMotionFunc(sourismouv);
  83. glutReshapeFunc(redim);
  84.  
  85. /* Entrée dans la boucle principale glut */
  86. glutMainLoop();
  87. return 0;
  88. }
  89.  
  90.  
  91.  
  92. void affichage()
  93. {
  94. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  95.  
  96. glLoadIdentity();
  97.  
  98. gluLookAt(0.0,0.0,2.5,0.0,0.0,0.0,0.0,1.0,0.0);
  99. glRotatef(angley,1.0,0.0,0.0);
  100. glRotatef(anglex,0.0,1.0,0.0);
  101.  
  102. /*glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
  103. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
  104. glBegin(GL_POLYGON);
  105. glTexCoord2f(0.0,0.0); glVertex3f(-0.5, 0.5, 0.5);
  106. glTexCoord2f(0.0,1.0); glVertex3f(-0.5,-0.5, 0.5);
  107. glTexCoord2f(1.0,1.0); glVertex3f( 0.5,-0.5, 0.5);
  108. glTexCoord2f(1.0,0.0); glVertex3f( 0.5, 0.5, 0.5);
  109. glEnd();
  110. glBegin(GL_POLYGON);
  111.  
  112. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
  113. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
  114. glTexCoord2f(0.0,0.0); glVertex3f( 0.5, 0.5, 0.5);
  115. glTexCoord2f(0.0,3.0); glVertex3f( 0.5,-0.5, 0.5);
  116. glTexCoord2f(3.0,3.0); glVertex3f( 0.5,-0.5,-0.5);
  117. glTexCoord2f(3.0,0.0); glVertex3f( 0.5, 0.5,-0.5);
  118. glEnd();
  119.  
  120. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  121. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
  122. glBegin(GL_POLYGON);
  123. glTexCoord2f(0.0,0.0); glVertex3f( 0.5, 0.5,-0.5);
  124. glTexCoord2f(0.0,3.0); glVertex3f( 0.5,-0.5,-0.5);
  125. glTexCoord2f(3.0,3.0); glVertex3f(-0.5,-0.5,-0.5);
  126. glTexCoord2f(3.0,0.0); glVertex3f(-0.5, 0.5,-0.5);
  127. glEnd();
  128.  
  129. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
  130. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  131. glBegin(GL_POLYGON);
  132. glTexCoord2f(0.0,0.0); glVertex3f(-0.5, 0.5,-0.5);
  133. glTexCoord2f(0.0,3.0); glVertex3f(-0.5,-0.5,-0.5);
  134. glTexCoord2f(3.0,3.0); glVertex3f(-0.5,-0.5, 0.5);
  135. glTexCoord2f(3.0,0.0); glVertex3f(-0.5, 0.5, 0.5);
  136. glEnd();
  137.  
  138. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
  139. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
  140. glBegin(GL_POLYGON);
  141. glTexCoord2f(0.0,0.0); glVertex3f(-0.5, 0.5,-0.5);
  142. glTexCoord2f(0.0,3.0); glVertex3f(-0.5, 0.5, 0.5);
  143. glTexCoord2f(3.0,3.0); glVertex3f( 0.5, 0.5, 0.5);
  144. glTexCoord2f(3.0,0.0); glVertex3f( 0.5, 0.5,-0.5);
  145. glEnd();
  146.  
  147. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
  148. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
  149. glBegin(GL_POLYGON);
  150. glTexCoord2f(0.0,0.0); glVertex3f(-0.5,-0.5,-0.5);
  151. glTexCoord2f(0.0,0.5); glVertex3f(-0.5,-0.5, 0.5);
  152. glTexCoord2f(0.5,0.5); glVertex3f( 0.5,-0.5, 0.5);
  153. glTexCoord2f(0.5,0.0); glVertex3f( 0.5,-0.5,-0.5);
  154. glEnd();*/
  155.  
  156.  
  157. for(int i =0; i < n; i++)
  158. {
  159. pCylindre[i].x = r*cos((i*2*M_PI)/n);
  160. pCylindre[i].y = h/2;
  161. pCylindre[i].z = r*sin((i*2*M_PI)/n);
  162. pCylindre[i+n].x = r*cos((i*2*M_PI)/n);
  163. pCylindre[i+n].y = -h/2;
  164. pCylindre[i+n].z = r*sin((i*2*M_PI)/n);
  165. }
  166. for(int i=0; i <n;i++)
  167. {
  168. fCylindre[i][0] = i;
  169. fCylindre[i][1] = i+n;
  170. fCylindre[i][2] = (i+1)%n+n;
  171. fCylindre[i][3] = (i+1)%n;
  172. }
  173.  
  174. int i,j;
  175.  
  176. /*
  177. Texture qui fait le tour
  178.  
  179. for (i=0;i<n;i++)
  180. {
  181. glBegin(GL_POLYGON);
  182. glTexCoord2f((float)i/n,0.0); glVertex3f(pCylindre[fCylindre[i][0]].x,pCylindre[fCylindre[i][0]].y, pCylindre[fCylindre[i][0]].z);
  183. glTexCoord2f((float)i/n,1.0); glVertex3f(pCylindre[fCylindre[i][1]].x,pCylindre[fCylindre[i][1]].y, pCylindre[fCylindre[i][1]].z);
  184. glTexCoord2f(((float)i/n)+((float)1/n),1.0); glVertex3f(pCylindre[fCylindre[i][2]].x,pCylindre[fCylindre[i][2]].y, pCylindre[fCylindre[i][2]].z);
  185. glTexCoord2f(((float)i/n)+((float)1/n),0.0); glVertex3f(pCylindre[fCylindre[i][3]].x,pCylindre[fCylindre[i][3]].y, pCylindre[fCylindre[i][3]].z);
  186. glEnd();
  187. }*/
  188.  
  189. /*
  190. Texture face par face
  191. */
  192. for (i=0;i<n;i++)
  193. {
  194. glBegin(GL_POLYGON);
  195. glTexCoord2f(0.0,0.0); glVertex3f(pCylindre[fCylindre[i][0]].x,pCylindre[fCylindre[i][0]].y, pCylindre[fCylindre[i][0]].z);
  196. glTexCoord2f(0.0,1.0); glVertex3f(pCylindre[fCylindre[i][1]].x,pCylindre[fCylindre[i][1]].y, pCylindre[fCylindre[i][1]].z);
  197. glTexCoord2f(1.1,1.0); glVertex3f(pCylindre[fCylindre[i][2]].x,pCylindre[fCylindre[i][2]].y, pCylindre[fCylindre[i][2]].z);
  198. glTexCoord2f(1.0,0.0); glVertex3f(pCylindre[fCylindre[i][3]].x,pCylindre[fCylindre[i][3]].y, pCylindre[fCylindre[i][3]].z);
  199. glEnd();
  200. }
  201.  
  202.  
  203. glutSwapBuffers();
  204.  
  205. }
  206.  
  207.  
  208.  
  209. void clavier(unsigned char touche,int x,int y)
  210. {
  211. switch(touche) {
  212. case 'l':
  213. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  214. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  215. glutPostRedisplay();
  216. break;
  217. case 'n':
  218. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  219. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  220. glutPostRedisplay();
  221. break;
  222.  
  223. case 27: /* touche ESC */
  224. exit(0);
  225. default:
  226. break;
  227. }
  228. }
  229.  
  230. void souris(int bouton, int etat,int x,int y)
  231. {
  232. if (bouton == GLUT_LEFT_BUTTON && etat == GLUT_DOWN)
  233. {
  234. presse = 1;
  235. xold = x;
  236. yold=y;
  237. }
  238. if (bouton == GLUT_LEFT_BUTTON && etat == GLUT_UP)
  239. presse=0;
  240. }
  241.  
  242. void sourismouv(int x,int y)
  243. {
  244. if (presse)
  245. {
  246. anglex=anglex+(x-xold);
  247. angley=angley+(y-yold);
  248. glutPostRedisplay();
  249. }
  250.  
  251. xold=x;
  252. yold=y;
  253. }
  254.  
  255. void redim(int l,int h)
  256. {
  257. if (l<h)
  258. glViewport(0,(h-l)/2,l,l);
  259. else
  260. glViewport((l-h)/2,0,h,h);
  261. }
  262.  
  263.  
  264.  
  265. void loadJpegImage(char *fichier)
  266. {
  267. struct jpeg_decompress_struct cinfo;
  268. struct jpeg_error_mgr jerr;
  269. FILE *file;
  270. unsigned char *ligne;
  271.  
  272. cinfo.err = jpeg_std_error(&jerr);
  273. jpeg_create_decompress(&cinfo);
  274.  
  275. /*largimg = cinfo.image_width;
  276. hautimg = cinfo.image_height;*/
  277. #ifdef __WIN32
  278. if (fopen_s(&file,fichier,"rb") != 0)
  279. {
  280. fprintf(stderr,"Erreur : impossible d'ouvrir le fichier texture.jpg\n");
  281. exit(1);
  282. }
  283. #elif __GNUC__
  284. if ((file = fopen(fichier,"rb")) == 0)
  285. {
  286. fprintf(stderr,"Erreur : impossible d'ouvrir le fichier texture.jpg\n");
  287. exit(1);
  288. }
  289. #endif
  290. jpeg_stdio_src(&cinfo, file);
  291. jpeg_read_header(&cinfo, TRUE);
  292.  
  293. /*if ((cinfo.image_width!=256)||(cinfo.image_height!=256)) {
  294. fprintf(stdout,"Erreur : l'image doit etre de taille 256x256\n");
  295. exit(1);
  296. }*/
  297. if (cinfo.jpeg_color_space==JCS_GRAYSCALE) {
  298. fprintf(stdout,"Erreur : l'image doit etre de type RGB\n");
  299. exit(1);
  300. }
  301.  
  302. jpeg_start_decompress(&cinfo);
  303. ligne=image;
  304. while (cinfo.output_scanline<cinfo.output_height)
  305. {
  306. ligne=image+3*largimg*cinfo.output_scanline;
  307. jpeg_read_scanlines(&cinfo,&ligne,1);
  308. }
  309. jpeg_finish_decompress(&cinfo);
  310. jpeg_destroy_decompress(&cinfo);
  311. for(int i = 0; i < largimg*2; i++){
  312. for (int j = 0; j < hautimg*2;j++){
  313. if((i>largimg-1 && j <= hautimg-1)||((i <= largimg-1 && j > hautimg-1)))
  314. {
  315. texture2[i][j][0] = 255;
  316. texture2[i][j][1] = 255;
  317. texture2[i][j][2] = 255;
  318. }
  319. else {
  320. if(i <= largimg-1 && j <= hautimg-1){
  321. texture2[i][j][0] = image[i*largimg*3+j*3];
  322. texture2[i][j][1] =image[i*largimg*3+j*3+1];
  323. texture2[i][j][2] =image[i*largimg*3+j*3+2];
  324. }
  325. else {
  326. texture2[i][j][0] = image[(i-largimg)*largimg*3+(j-hautimg)*3];
  327. texture2[i][j][1] = image[(i-largimg)*largimg*3+(j-hautimg)*3+1];
  328. texture2[i][j][2] = image[(i-largimg)*largimg*3+(j-hautimg)*3+2];
  329. }
  330. }
  331.  
  332. }
  333. }
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement