bonjour,
svp j'ai besoin d'un aide urgent.
en effet, j'ai realiser un programme en c++ qui utilise opengl. ce programme permet de dessiner plusieurs points dans l'espace, j'ai réussi de selectionner un seul point par un clic à la souris mais dans mon programme je ne peux pas selectionner plusieurs points en même temps.
donc mon probléme est comment selectionner plusieurs points en même temps l'une aprés l'autre tous en conserver les autres points selectionés ou bien comment selectionner avec la souris une zone de ecran à la place d'une seul point pour que je puisse selectionner plus point en même temps.
mon programme a la même principe que le programme suivant qui permet de desiner deux points dans l'espace est sectionner une seul point.
#include<windows.h>
#include "glut.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
class Scene{
public:
GLfloat spinG_x, spinG_y; //Rotation globale de la sc�ne
GLint selected; //Nom du point s�lectionn�
int mouse_x, mouse_y; //Sauvegarde des coordonn�es de la souris
int mode_selection; //Est-on en mode s�lection ?
GLfloat x0,y0,z0,x1,y1,z1;//Coordonn�es de deux points P0 et P1
Scene(){
spinG_x = spinG_y = 0;
selected = -1;
x0 = y0 = z0 = -2;
x1 = y1 = z1 = 2;
mode_selection = false;
}
void Init(){
//On �paissit les points
glPointSize(10);
//On �paissit les lignes
glLineWidth(2.0);
}
void setProjection(){
glOrtho(-5, 5, -5, 5, -5, 5);
}
void Draw(GLenum mode);
~Scene(){};
};
void Scene::Draw(GLenum mode)
{
glPushMatrix();
{
glRotatef(spinG_y, 1, 0, 0);
glRotatef(spinG_x, 0, 1, 0);
if (mode == GL_RENDER)
{
glColor3f(1, 1, 1);
glBegin(GL_LINES);
glVertex3f(x0, y0, z0);
glVertex3f(x1, y1, z1);
glEnd();
glBegin(GL_POINTS);
if (selected == 1)
glColor3f(1, 1, 0);
else
glColor3f(0, 0, .8);
glVertex3f(x0, y0, z0);
if (selected == 2)
glColor3f(1, 1, 0);
else
glColor3f(0, 0, .8);
glVertex3f(x1, y1, z1);
glEnd();
}
else
{
glLoadName(1);
glBegin(GL_POINTS);
glVertex3f(x0, y0, z0);
glEnd();
glLoadName(2);
glBegin(GL_POINTS);
glVertex3f(x1, y1, z1);
glEnd();
}
}
glPopMatrix();
}
Scene s;
void myReshapeFunc(int w,int h)
{
glViewport(0,0,(GLint) w,(GLint) h);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
s.setProjection();
glMatrixMode( GL_MODELVIEW );
}
void myDisplayFunc()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
s.Draw(GL_RENDER);
glutSwapBuffers();
}
void processHits (GLint hits, GLuint buffer[])
{
GLuint i, nb_names, name, *ptr;
GLdouble z1, z2;
s.selected = -1;
printf ("hits = %d\n", hits);
ptr = (GLuint *) buffer;
for (i = 0; i < hits; i++) { /* for each hit */
nb_names = ptr[0];
if (nb_names != 1) {
cerr << "erreur, ce programme ne supporte qu'un seul nom dans la pile, et non pas " << nb_names << endl;
exit(1);
}
z1 = (double) ptr[1]/0x7fffffff;
z2 = (double) ptr[2]/0x7fffffff;
cout << "zmin = " << z1 << ", zmax= " << z2 << endl;
name = ptr[3];
s.selected = name;
cout << "selected = " << s.selected << endl;
}
}
#define BUFSIZE 512
void myPick(int x, int y){
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(BUFSIZE, selectBuf);
(void) glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y),
10.0, 10.0, viewport);
s.setProjection();
glMatrixMode(GL_MODELVIEW);
s.Draw(GL_SELECT);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();
hits = glRenderMode(GL_RENDER);
processHits (hits, selectBuf);
glutPostRedisplay();
}
void myKeyboardFunc(unsigned char k,int x,int y)
{
switch(k)
{
case 27 : exit(0);break;
default: break;
}
glutPostRedisplay();
}
void myMouseFunc(int button, int state, int x, int y)
{
if ((button == GLUT_RIGHT_BUTTON)
&& (state == GLUT_DOWN)){
s.mode_selection = true;
cout << "Picking" <<endl;
myPick(x, y);
}
if ((button == GLUT_LEFT_BUTTON)
&& (state == GLUT_DOWN)){
s.mode_selection = false;
s.mouse_x = x;
s.mouse_y = y;
}
glutPostRedisplay();
}
void myMotionFunc(int x, int y){
if (!s.mode_selection){
s.spinG_x += (s.mouse_x - x);
if (s.spinG_x > 360.0) s.spinG_x -= 360.0;
if (s.spinG_x < -360.0) s.spinG_x += 360.0;
s.spinG_y += (s.mouse_y - y);
if (s.spinG_y > 360.0) s.spinG_y -= 360.0;
if (s.spinG_y < -360.0) s.spinG_y += 360.0;
s.mouse_x = x;
s.mouse_y = y;
glutPostRedisplay();
}
}
void init(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode( GLUT_DOUBLE |GLUT_DEPTH );
glutInitWindowSize(500, 500);
glutInitWindowPosition(50,50);
glutCreateWindow("Selection et Picking");
glutReshapeFunc(myReshapeFunc);
glutDisplayFunc(myDisplayFunc);
glutKeyboardFunc(myKeyboardFunc);
glutMouseFunc(myMouseFunc);
glutMotionFunc(myMotionFunc);
glClearColor(.5, 0, 0, 1);
glEnable(GL_DEPTH_TEST);
s.Init();
}
int main(int argc,char **argv)
{
init(argc,argv);
glutMainLoop();
return 0;
}
merci d'avance