#include #include #include "parserOBJ.h" #include "trackball.h" #include "travail.h" #include "facet.h" #include #include using namespace M_PARSER; // parser obj using namespace std; // **************************************************** // Environnement global : class Global { public: Objet *obj3d; Point3d obs; bool attendre; GLuint tex_exemple; float eye_translate; Global() { attendre=false; eye_translate=-20.0; } }; Global global; // **************************************************** // **************************************************** static void erreur(const string &mesg) { cout << "Erreur dans refombre.cpp :" << endl; cout << mesg << endl; exit(1); } // **************************************************** // affectation d'une image à un identifiant de texture GLuint initTexture(char *nom) { GLubyte *image; GLuint tex_id; GLint format,longueur,largeur,bpp; ILenum il_erreur; GLuint image_id=ilGenImage(); ilBindImage(image_id); ilEnable(IL_ORIGIN_SET); ilOriginFunc(IL_ORIGIN_LOWER_LEFT); bool ok=ilLoadImage(nom); if (!ok) { cout << "impossible de lire " << nom << endl; erreur("initTexture"); } longueur=ilGetInteger(IL_IMAGE_WIDTH); largeur=ilGetInteger(IL_IMAGE_HEIGHT); format=ilGetInteger(IL_IMAGE_FORMAT); bpp=ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL); cout << "lecture image " << nom << " : " << longueur << "," << largeur << "," << bpp << endl; image=new GLubyte[longueur*largeur*bpp]; // Attention : memory leak ! ilCopyPixels(0,0,0,longueur,largeur,1,IL_RGB,IL_UNSIGNED_BYTE,image); il_erreur=ilGetError(); if (il_erreur!=IL_NO_ERROR) { erreur("Chargement texture"); } // affectation identifiant glGenTextures(1,&tex_id); // alloue 1 identifiant à tex_id. glBindTexture(GL_TEXTURE_2D,tex_id); // la texture courante 2D correspond à // l'identifiant tex_id. glPixelStorei(GL_UNPACK_ALIGNMENT,1); glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,longueur,largeur,0,GL_RGB,GL_UNSIGNED_BYTE,image); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE); cout << "ok" << endl; return tex_id; } void initLumiere(GLint light) { GLfloat ambient[4]={1.0,1.0,1.0,1.0}; GLfloat diffuse[4]={1.0,1.0,1.0,1.0}; GLfloat speculaire[4]={.4,.4,.4,1}; // pas de spéculaire par défaut glEnable(light); glLightfv(light,GL_AMBIENT,ambient); glLightfv(light,GL_DIFFUSE,diffuse); glLightfv(light,GL_SPECULAR,speculaire); } void materielFrontBack() { GLfloat ambientf[4]={0.1,0.1,0.1,1.0}; GLfloat diffusf[4]={0,0.4,0.4,1.0}; GLfloat speculairef[4]={0,0.8,0.0,1.0}; // spéculaire vert GLfloat brillance=50; glMaterialfv(GL_FRONT,GL_AMBIENT,ambientf); glMaterialfv(GL_FRONT,GL_DIFFUSE,diffusf); glMaterialfv(GL_FRONT,GL_SPECULAR,speculairef); glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,brillance); GLfloat ambientb[4]={0.1,0,0,0.0}; GLfloat diffusb[4]={0.8,0,0,0.30}; GLfloat speculaireb[4]={0,0.8,0.8,1.0}; glMaterialfv(GL_BACK,GL_AMBIENT,ambientb); glMaterialfv(GL_BACK,GL_DIFFUSE,diffusb); glMaterialfv(GL_BACK,GL_SPECULAR,speculaireb); } void desactiverTexture() { glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); glDisable(GL_TEXTURE_2D); } void myDisplay() { glClearColor(0,0,0,0); glClearDepth(1.0); glClearStencil(0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glPushMatrix(); glTranslatef(0,0,global.eye_translate); tbMatrix(); tracerScene(global.obj3d); glPopMatrix(); glutSwapBuffers(); } void myReshape(GLint width,GLint height) { glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45,(GLfloat)width/height,1,100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); tbReshape(width, height); } void myIdle(void) { glutPostRedisplay(); } // CallBack clavier void myKey(unsigned char c,int x,int y) { switch (c) { case 27: exit(1); break; // sortie violente si appui sur "Echap" // case ' ': case 'a':global.attendre=!global.attendre;break; case 'z':global.eye_translate+=0.3;glutPostRedisplay();break; case 's':global.eye_translate-=0.3;glutPostRedisplay();break; case 'g':global.obj3d->gonfler(0.1);break; default: break; } } void myMouse(int button, int state, int x, int y) { tbMouse(button, state, x, y); } void myMotion(int x,int y) { tbMotion(x, y); } // ***************************************************** // INITIALISATION OPENGL (+lecture texture+lecture objet) void myInit() { tbInit(GLUT_LEFT_BUTTON); ilInit(); //global.tex_exemple=initTexture("../data/marble.jpg"); glEnable(GL_DEPTH_TEST); glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); initLumiere(GL_LIGHT0); materielFrontBack(); glEnable(GL_COLOR_MATERIAL); } main(int argv,char **argc) { if (argv!=2) { printf("usage : run \n");exit(0);} ParserOBJ parse(argc[1]); global.obj3d=parse.lire(); global.obj3d->calculerNormaleSommet(); glutInit(&argv,argc); glutInitWindowSize(512,512); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL); glutCreateWindow("Mise en évidence"); glutReshapeFunc(myReshape); glutDisplayFunc(myDisplay); glutIdleFunc(myIdle); glutKeyboardFunc(myKey); glutMouseFunc(myMouse); glutMotionFunc(myMotion); myInit(); glutMainLoop(); }