#include #include #include #include #include #include #pragma comment(lib,"OpenGL32.lib") #pragma comment(lib,"GLu32.lib") #pragma comment(lib,"SDLmain.lib") #pragma comment(lib,"SDL.lib") const double PI=3.14159; const int SCREEN_WIDTH=800; const int SCREEN_HEIGHT=600; const int SCREEN_BPP=32; const char titre[30]="Picking"; const int BUFSIZE=512; void initGL() { //on vide les tampons de profondeur et chromatique glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); //on met le fond de la fenetre en noir glClearColor(0.0f,0.0f,0.0f,0.0f); //On règle le point de vue glViewport(0,0,SCREEN_WIDTH,SCREEN_HEIGHT); //on initialise notre matrice de projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); //on est en projection orthogonale gluOrtho2D(0,SCREEN_WIDTH,0,SCREEN_HEIGHT); // idem avec la matrice de vue glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void processHits (GLint hits, GLuint buffer[]) { int i, j; GLuint names, *ptr; GLuint id; printf ("hits = %d\n", hits); ptr = (GLuint *) buffer; for (i = 0; i < hits; i++) { /* for each hit */ id = buffer[(i<<2)+3]; printf("Nom du hit=%d",id); names = *ptr; printf (" number of names for this hit = %d\n", names); ptr++; printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++; printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++; printf (" names are "); for (j = 0; j < (int)names; j++) { /* for each name */ printf ("%d ", *ptr); } printf ("\n"); } } void affiche_carre() { glLoadName(1); glColor3f(1,0,0); glBegin(GL_QUADS); glVertex2i(0,0); glVertex2i(0,SCREEN_HEIGHT/2); glVertex2i(SCREEN_WIDTH/2,SCREEN_HEIGHT/2); glVertex2i(SCREEN_WIDTH/2,0); glEnd(); glLoadName(2); glColor3f(1,1,1); glBegin(GL_QUADS); glVertex2i(SCREEN_WIDTH/2,0); glVertex2i(SCREEN_WIDTH/2,SCREEN_HEIGHT/2); glVertex2i(SCREEN_WIDTH,SCREEN_HEIGHT/2); glVertex2i(SCREEN_WIDTH,0); glEnd(); glLoadName(3); glColor3f(0,1,0); glBegin(GL_QUADS); glVertex2i(SCREEN_WIDTH/2,SCREEN_HEIGHT/2); glVertex2i(SCREEN_WIDTH/2,SCREEN_HEIGHT); glVertex2i(SCREEN_WIDTH,SCREEN_HEIGHT); glVertex2i(SCREEN_WIDTH,SCREEN_HEIGHT/2); glEnd(); glLoadName(4); glColor3f(0,0,1); glBegin(GL_QUADS); glVertex2i(0,SCREEN_HEIGHT/2); glVertex2i(0,SCREEN_HEIGHT); glVertex2i(SCREEN_WIDTH/2,SCREEN_HEIGHT); glVertex2i(SCREEN_WIDTH/2,SCREEN_HEIGHT/2); glEnd(); } void pick(int x,int y) { GLuint selectBuf[BUFSIZE]; GLint hits; GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(BUFSIZE,selectBuf); //on passe en mode selection glRenderMode(GL_SELECT); //on initialise la pile de noms glInitNames(); //on empile qqchose dans la matrice de noms sinon erreur avec le glLoadName glPushName(0); glMatrixMode (GL_PROJECTION); glPushMatrix (); /* push projection */ glLoadIdentity (); gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); gluOrtho2D(0,SCREEN_WIDTH,0,SCREEN_HEIGHT); //on empile les 4 carrées dans la matrice de noms affiche_carre(); glPopMatrix(); glFlush(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); /* push modelview */ glLoadIdentity(); //on voit les differents hits atteint hits = glRenderMode(GL_RENDER); processHits (hits, selectBuf); } int main(int argc, char **argv) { SDL_Surface *screen=NULL; SDL_Event event; bool fini=false; //initialisation de la SDL if(SDL_Init(SDL_INIT_VIDEO) == -1) { std::cout<<"L'initialisation de la SDL a echoue!"<