Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- /****************************************************************/
- /* works on mac using freeglut and xquartz */
- /* you can install freeglut using homebrew */
- /* xquartz can be downloaded at http://xquartz.macosforge.org */
- /****************************************************************/
- #include <GL/freeglut.h>
- #include <jpeglib.h>
- #include <jerror.h>
- #include <math.h>
- #ifdef __WIN32
- #pragma comment (lib, "jpeg.lib")
- #endif
- class Point{
- public :
- //coordonnées x, y et z du point
- double x;
- double y;
- double z;
- // couleur r, v et b du point
- float r;
- float g;
- float b;
- };
- const int n = 100;
- float r = 0.5;
- float h = 1;
- Point pCylindre[2*n];
- int fCylindre[n][4];
- const int largimg = 256, hautimg=256;
- unsigned char image[largimg*hautimg*3];
- unsigned char texture2[largimg*2][hautimg*2][3];
- char presse;
- int anglex=30,angley=20,x,y,xold,yold;
- void affichage();
- void clavier(unsigned char touche,int x,int y);
- void souris(int boutton, int etat,int x,int y);
- void sourismouv(int x,int y);
- void redim(int l,int h);
- void loadJpegImage(char *fichier);
- int main(int argc,char **argv)
- {
- /* Chargement de la texture */
- loadJpegImage("./calimero.jpg");
- /* Creation de la fenetre OpenGL */
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutInitWindowSize(200,200);
- glutCreateWindow("Texture JPEG");
- /* Initialisation de l'etat d'OpenGL */
- glClearColor(0.0,0.0,0.0,0.0);
- glShadeModel(GL_FLAT);
- glEnable(GL_DEPTH_TEST);
- /* Mise en place de la projection perspective */
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0,1,1.0,5.0);
- glMatrixMode(GL_MODELVIEW);
- /* Parametrage du placage de textures */
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,largimg*2,hautimg*2,0,
- GL_RGB,GL_UNSIGNED_BYTE,texture2);
- glEnable(GL_TEXTURE_2D);
- /* Mise en place des fonctions de rappel */
- glutDisplayFunc(affichage);
- glutKeyboardFunc(clavier);
- glutMouseFunc(souris);
- glutMotionFunc(sourismouv);
- glutReshapeFunc(redim);
- /* Entrée dans la boucle principale glut */
- glutMainLoop();
- return 0;
- }
- void affichage()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- gluLookAt(0.0,0.0,2.5,0.0,0.0,0.0,0.0,1.0,0.0);
- glRotatef(angley,1.0,0.0,0.0);
- glRotatef(anglex,0.0,1.0,0.0);
- /*glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
- glBegin(GL_POLYGON);
- glTexCoord2f(0.0,0.0); glVertex3f(-0.5, 0.5, 0.5);
- glTexCoord2f(0.0,1.0); glVertex3f(-0.5,-0.5, 0.5);
- glTexCoord2f(1.0,1.0); glVertex3f( 0.5,-0.5, 0.5);
- glTexCoord2f(1.0,0.0); glVertex3f( 0.5, 0.5, 0.5);
- glEnd();
- glBegin(GL_POLYGON);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
- glTexCoord2f(0.0,0.0); glVertex3f( 0.5, 0.5, 0.5);
- glTexCoord2f(0.0,3.0); glVertex3f( 0.5,-0.5, 0.5);
- glTexCoord2f(3.0,3.0); glVertex3f( 0.5,-0.5,-0.5);
- glTexCoord2f(3.0,0.0); glVertex3f( 0.5, 0.5,-0.5);
- glEnd();
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
- glBegin(GL_POLYGON);
- glTexCoord2f(0.0,0.0); glVertex3f( 0.5, 0.5,-0.5);
- glTexCoord2f(0.0,3.0); glVertex3f( 0.5,-0.5,-0.5);
- glTexCoord2f(3.0,3.0); glVertex3f(-0.5,-0.5,-0.5);
- glTexCoord2f(3.0,0.0); glVertex3f(-0.5, 0.5,-0.5);
- glEnd();
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
- glBegin(GL_POLYGON);
- glTexCoord2f(0.0,0.0); glVertex3f(-0.5, 0.5,-0.5);
- glTexCoord2f(0.0,3.0); glVertex3f(-0.5,-0.5,-0.5);
- glTexCoord2f(3.0,3.0); glVertex3f(-0.5,-0.5, 0.5);
- glTexCoord2f(3.0,0.0); glVertex3f(-0.5, 0.5, 0.5);
- glEnd();
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
- glBegin(GL_POLYGON);
- glTexCoord2f(0.0,0.0); glVertex3f(-0.5, 0.5,-0.5);
- glTexCoord2f(0.0,3.0); glVertex3f(-0.5, 0.5, 0.5);
- glTexCoord2f(3.0,3.0); glVertex3f( 0.5, 0.5, 0.5);
- glTexCoord2f(3.0,0.0); glVertex3f( 0.5, 0.5,-0.5);
- glEnd();
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
- glBegin(GL_POLYGON);
- glTexCoord2f(0.0,0.0); glVertex3f(-0.5,-0.5,-0.5);
- glTexCoord2f(0.0,0.5); glVertex3f(-0.5,-0.5, 0.5);
- glTexCoord2f(0.5,0.5); glVertex3f( 0.5,-0.5, 0.5);
- glTexCoord2f(0.5,0.0); glVertex3f( 0.5,-0.5,-0.5);
- glEnd();*/
- for(int i =0; i < n; i++)
- {
- pCylindre[i].x = r*cos((i*2*M_PI)/n);
- pCylindre[i].y = h/2;
- pCylindre[i].z = r*sin((i*2*M_PI)/n);
- pCylindre[i+n].x = r*cos((i*2*M_PI)/n);
- pCylindre[i+n].y = -h/2;
- pCylindre[i+n].z = r*sin((i*2*M_PI)/n);
- }
- for(int i=0; i <n;i++)
- {
- fCylindre[i][0] = i;
- fCylindre[i][1] = i+n;
- fCylindre[i][2] = (i+1)%n+n;
- fCylindre[i][3] = (i+1)%n;
- }
- int i,j;
- /*
- Texture qui fait le tour
- for (i=0;i<n;i++)
- {
- glBegin(GL_POLYGON);
- glTexCoord2f((float)i/n,0.0); glVertex3f(pCylindre[fCylindre[i][0]].x,pCylindre[fCylindre[i][0]].y, pCylindre[fCylindre[i][0]].z);
- glTexCoord2f((float)i/n,1.0); glVertex3f(pCylindre[fCylindre[i][1]].x,pCylindre[fCylindre[i][1]].y, pCylindre[fCylindre[i][1]].z);
- 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);
- 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);
- glEnd();
- }*/
- /*
- Texture face par face
- */
- for (i=0;i<n;i++)
- {
- glBegin(GL_POLYGON);
- glTexCoord2f(0.0,0.0); glVertex3f(pCylindre[fCylindre[i][0]].x,pCylindre[fCylindre[i][0]].y, pCylindre[fCylindre[i][0]].z);
- glTexCoord2f(0.0,1.0); glVertex3f(pCylindre[fCylindre[i][1]].x,pCylindre[fCylindre[i][1]].y, pCylindre[fCylindre[i][1]].z);
- glTexCoord2f(1.1,1.0); glVertex3f(pCylindre[fCylindre[i][2]].x,pCylindre[fCylindre[i][2]].y, pCylindre[fCylindre[i][2]].z);
- glTexCoord2f(1.0,0.0); glVertex3f(pCylindre[fCylindre[i][3]].x,pCylindre[fCylindre[i][3]].y, pCylindre[fCylindre[i][3]].z);
- glEnd();
- }
- glutSwapBuffers();
- }
- void clavier(unsigned char touche,int x,int y)
- {
- switch(touche) {
- case 'l':
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glutPostRedisplay();
- break;
- case 'n':
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
- glutPostRedisplay();
- break;
- case 27: /* touche ESC */
- exit(0);
- default:
- break;
- }
- }
- void souris(int bouton, int etat,int x,int y)
- {
- if (bouton == GLUT_LEFT_BUTTON && etat == GLUT_DOWN)
- {
- presse = 1;
- xold = x;
- yold=y;
- }
- if (bouton == GLUT_LEFT_BUTTON && etat == GLUT_UP)
- presse=0;
- }
- void sourismouv(int x,int y)
- {
- if (presse)
- {
- anglex=anglex+(x-xold);
- angley=angley+(y-yold);
- glutPostRedisplay();
- }
- xold=x;
- yold=y;
- }
- void redim(int l,int h)
- {
- if (l<h)
- glViewport(0,(h-l)/2,l,l);
- else
- glViewport((l-h)/2,0,h,h);
- }
- void loadJpegImage(char *fichier)
- {
- struct jpeg_decompress_struct cinfo;
- struct jpeg_error_mgr jerr;
- FILE *file;
- unsigned char *ligne;
- cinfo.err = jpeg_std_error(&jerr);
- jpeg_create_decompress(&cinfo);
- /*largimg = cinfo.image_width;
- hautimg = cinfo.image_height;*/
- #ifdef __WIN32
- if (fopen_s(&file,fichier,"rb") != 0)
- {
- fprintf(stderr,"Erreur : impossible d'ouvrir le fichier texture.jpg\n");
- exit(1);
- }
- #elif __GNUC__
- if ((file = fopen(fichier,"rb")) == 0)
- {
- fprintf(stderr,"Erreur : impossible d'ouvrir le fichier texture.jpg\n");
- exit(1);
- }
- #endif
- jpeg_stdio_src(&cinfo, file);
- jpeg_read_header(&cinfo, TRUE);
- /*if ((cinfo.image_width!=256)||(cinfo.image_height!=256)) {
- fprintf(stdout,"Erreur : l'image doit etre de taille 256x256\n");
- exit(1);
- }*/
- if (cinfo.jpeg_color_space==JCS_GRAYSCALE) {
- fprintf(stdout,"Erreur : l'image doit etre de type RGB\n");
- exit(1);
- }
- jpeg_start_decompress(&cinfo);
- ligne=image;
- while (cinfo.output_scanline<cinfo.output_height)
- {
- ligne=image+3*largimg*cinfo.output_scanline;
- jpeg_read_scanlines(&cinfo,&ligne,1);
- }
- jpeg_finish_decompress(&cinfo);
- jpeg_destroy_decompress(&cinfo);
- for(int i = 0; i < largimg*2; i++){
- for (int j = 0; j < hautimg*2;j++){
- if((i>largimg-1 && j <= hautimg-1)||((i <= largimg-1 && j > hautimg-1)))
- {
- texture2[i][j][0] = 255;
- texture2[i][j][1] = 255;
- texture2[i][j][2] = 255;
- }
- else {
- if(i <= largimg-1 && j <= hautimg-1){
- texture2[i][j][0] = image[i*largimg*3+j*3];
- texture2[i][j][1] =image[i*largimg*3+j*3+1];
- texture2[i][j][2] =image[i*largimg*3+j*3+2];
- }
- else {
- texture2[i][j][0] = image[(i-largimg)*largimg*3+(j-hautimg)*3];
- texture2[i][j][1] = image[(i-largimg)*largimg*3+(j-hautimg)*3+1];
- texture2[i][j][2] = image[(i-largimg)*largimg*3+(j-hautimg)*3+2];
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement