#include // Header File For The GLUT Library #include // Header File For The OpenGL32 Library #include // Header File For The GLu32 Library #include #include #include #include #include "adjust.h" #include "comm.h" using namespace std; #define IMG_WIDTH 360 #define IMG_HEIGHT 296 int window; Comm *comm; int point[NB_POINTS_UI][2] = {{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}}; int width=538; int height=395; int* GetPpoint() { return (int*)point; } /// Sortie du programme void Leave (int numSignal) { exit(0); } /// Sortie du programme anormal void TooLate(int numSignal) { // Affichage d'un message d'erreur cerr << "TooLate : Temps d'attente FIFO depasse\n"; Leave(-1); } /// Lecture du fifo et execution de la commande void ExecuteUiCmds(int numSignal) { // Reassignement du signal handler signal(SIGUSR1, ExecuteUiCmds); char buffer[50]; // On test si le fifo a ete initialiser if (numSignal == SIGUSR2) { // Permet l'envoie du message d'initialisation strcpy(buffer, "i\n"); } else { // Timer signal(ALARM, TooLate); alarm(5); // Lecture du FIFO if (!read (comm->fifo, buffer, 50)) { cerr << "ReadUicmds : Error FIFO is empty" << endl; return; } alarm(0); } // Parse de la commande recue comm->ExecuteUiCmds(buffer); // Envoyer un signal de réponse à UI kill(comm->uiPid, SIGUSR1); } /// Chargement d'une texture a partir de donnees RGB unsigned int LoadImage(int width, int height, unsigned char *data, unsigned int lastTex) { GLuint tex; if (glIsTexture(lastTex)) glDeleteTextures(1, &lastTex); // Mode de chargement des donnees glPixelStorei (GL_UNPACK_ALIGNMENT, 1); // Creer une id pour stocker l'image glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); // Stocke l'image //glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data); // Config des parametres glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Retourne l'id de l'image return tex; } /// Initialisation des fonctions OpenGL void InitGL(int width, int height) { // Initialisation du mode d'affichage glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Mode de chargement des donnees glPixelStorei (GL_UNPACK_ALIGNMENT, 1); glShadeModel(GL_FLAT); glEnable(GL_TEXTURE_2D); // Remise a zero de la matrice de projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Plan de projection en 2D glOrtho(0.0, WIDTH, 0.0, HEIGHT, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glPointSize(4.0f); } /// Redimenssionement d'une fenetre void ReSizeGLScene(int w, int h) { // Evite div par 0 if (height==0) h=1; height = h; width = w; // Initialise le point de vue glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); // Remise a zero et mode 2D glLoadIdentity(); glOrtho(0.0, WIDTH, 0.0, HEIGHT, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); } /// Affiche une image /// @param texId numero de la texture a afficher void DrawImage(GLuint texId) { int wMin, wMax; int hMin, hMax; // Selection de l'image glBindTexture(GL_TEXTURE_2D, comm->tex[texId]); // Calcul des coordonnees de l'image wMin = BORDER+(texId%3)*(352+BORDER); wMax = 352+BORDER+(texId%3)*(352+BORDER); hMin = HEIGHT-288-BORDER-(int)(texId/3)*(288+BORDER); hMax = HEIGHT-BORDER-(int)(texId/3)*(288+BORDER); // Dessine un carre contenant l'image glBegin(GL_QUADS); glNormal3f( 0.0f, 0.0f, 1.0f); glColor3f(1.0f,1.0f,1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(wMax, hMax, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(wMin, hMax, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(wMin, hMin, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(wMax, hMin, -1.0f); glEnd(); } /// Dessine les images a l'ecran void DrawGLScene() { // Efface le buffer glClear(GL_COLOR_BUFFER_BIT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Affiche toutes les images for (int i=0; i= 0) /* x = (int)((double)WIDTH/width * x); y = height - y; y = (int)((double)HEIGHT/height * y);*/ glBegin(GL_POINTS); glVertex3f( (float)((point[i][0] + BORDER) * (double)352 / IMG_WIDTH), (float)((point[i][1] + BORDER) * (double)288 / IMG_HEIGHT), -0.9f); glVertex3f( (float)((point[i][0] + BORDER) * (double)352 / IMG_WIDTH), (float)((point[i][1] + BORDER) * (double)288 / IMG_HEIGHT + 288 + BORDER), -0.9f); glEnd(); } glEnable(GL_TEXTURE_2D); glFlush(); // Affiche le buffer suivant glutSwapBuffers(); } /// Interruptions du clavier void KeyPressed(unsigned char key, int x, int y) { // Touche Echap if (key == 27) { glutDestroyWindow(window); Leave(0); } if (key == 'r') comm->ReloadConfig ("rc/vision.conf"); } /// Gestion souris void MouseFunc(int button, int state, int x, int y) { if (state == GLUT_UP) { // On calcul les coordonnees de l'image x = (int)((double)WIDTH/width * x); y = (int)((double)HEIGHT/height * y); int imgX = -1; int imgY = -1; if ((x < 352+BORDER) && (y < BORDER+288) && (x > BORDER) && (y > BORDER)) { imgX = x * IMG_WIDTH / 352 - BORDER; imgY = y * IMG_HEIGHT / 288 - BORDER; } if ((x < 352+BORDER) && (y < HEIGHT-BORDER) && (x > BORDER) && (y > 2*BORDER+288)) { imgX = x * IMG_WIDTH / 352 - BORDER; imgY = y * IMG_HEIGHT / 288 - 3 * BORDER - 288; } cout << "x " << imgX << " y " << imgY << endl; int i=0; if (imgX >= 0) { imgY = IMG_HEIGHT - imgY; // Ajout de point if (button == GLUT_LEFT_BUTTON) { while(i < NB_POINTS_UI) { if (point[i][0] < 0) { point[i][0] = imgX; point[i][1] = imgY; i=NB_POINTS_UI+3; cout << "Point ajoute: " << imgX << ", " << imgY << endl; } i++; } if (i == NB_POINTS_UI+3) cout << "MouseFunc : 3 points sont deja selectionnes" << endl; } // Suppression de point else if (button == GLUT_RIGHT_BUTTON) { while(i