Bonjour à tous,

J'ai développez un petit programme en Qt permettant de visualiser des molécules mais j'ai un souci avec le picking. L'idée ici est de pouvoir cliquer sur un atome dans la fenetre GL afin d'obtenir des informations sur celui-ci. Cependant, et après maintes recherches, je n'y arrives pas. Je me retournes donc vers vous pour m'aider :

Voici le header :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
 
#ifndef GLWIDGET_H
#define GLWIDGET_H
 
#include <QGLWidget>
#include "drawmol.h"  // Classe permettant de dessiner les atomes et molécules
 
//! [0]
class GLWidget : public QGLWidget
{
    Q_OBJECT
 
public:
    GLWidget(ICFace*,QWidget *parent = 0);
    ~GLWidget();
 
    QSize minimumSizeHint() const;
    QSize sizeHint() const;
    bool needupdate;
    DrawMol *mole;
    void Update();
    QList<ICMole::Molecule*> ListMoles; //Liste des molécules à dessiner
// Option d'affichages : 
        bool seeArCenter; 
        bool seeHydrogen;
        bool line;
 
        void drawData(GLenum mode);
        void processHits(GLint hits, GLuint buffer[]);
//! [0]
 
//! [1]
public slots:
    void setXRotation(int angle);
    void setYRotation(int angle);
    void setZRotation(int angle);
 
signals:
    void xRotationChanged(int angle);
    void yRotationChanged(int angle);
    void zRotationChanged(int angle);
//! [1]
 
//! [2]
protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void wheelEvent(QWheelEvent *e);
//! [2]
 
//! [3]
private:
    float scale;
 
    float nearVal,farVal;
    int xRot;
    int yRot;
    int zRot;
    ICFace* ICF;
    QPoint lastPos;
    QColor qtGreen;
    QColor qtPurple;
};
//! [3]
 
#endif
Et le fichier source :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
 
#include <QtGui>
#include <QtOpenGL>
 
#include <math.h>
#include <iostream>
#include "glwidget.h"
#include "drawmol.h"
 
 
 
#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE  0x809D
#endif
 
 
GLuint _displaylistid;
 
//! [0]
GLWidget::GLWidget( ICFace* ICF_,QWidget *parent)
    : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
        needupdate=false;
        mole = 0;
        nearVal=4;
        farVal=15;
    xRot = 0;
    yRot = 0;
    zRot = 0;
scale=100;
    qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
    qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
    ICF = ICF_;
    line=false;
    seeArCenter = false;
 
}
 
 
GLWidget::~GLWidget()
{
}
 
 
 
QSize GLWidget::minimumSizeHint() const
{
    return QSize(50, 50);
}
 
 
QSize GLWidget::sizeHint() const
{
    return QSize(1500, 1000);
}
 
 
static void qNormalizeAngle(int &angle)
{
    while (angle < 0)    angle += 360 * 16;
    while (angle > 360 * 16) angle -= 360 * 16;
}
 
 
void GLWidget::setXRotation(int angle)
{
    qNormalizeAngle(angle);
    if (angle != xRot) {
        xRot = angle;
        emit xRotationChanged(angle);
        updateGL();
    }
}
 
 
void GLWidget::setYRotation(int angle)
{
    qNormalizeAngle(angle);
    if (angle != yRot) {
        yRot = angle;
        emit yRotationChanged(angle);
        updateGL();
    }
}
 
void GLWidget::setZRotation(int angle)
{
    qNormalizeAngle(angle);
    if (angle != zRot) {
        zRot = angle;
        emit zRotationChanged(angle);
        updateGL();
    }
}
/*
La fonction update est appelé à chaque update de la fenetre GL.
On regarde alors si la liste de molecule est la même ou si l'utilisateur
à fait des modifications du style suppression d'un ou plusieurs éléments (via le boolean needupdate).
Si rien n'as changé alors on rappelle la liste _displaylist
sinon on appelle drawData pour redessiner la totalité
*/
    void GLWidget::Update()
        {
            QList<ICMole::Molecule*> NewList = ICF->getMolForGL(0);
            if (!needupdate &&NewList == ListMoles)
            {
                glCallList(_displaylistid);
            }
            else
            {
                ListMoles = NewList;
                glNewList(_displaylistid,GL_COMPILE);
                glClearColor( .3, .4, .6, 1.0 );
                glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
                drawData(GL_RENDER);
                glEndList();
                glCallList(_displaylistid);
            }
 
        }
 
 
// En fonction du mode de rendu : GL_RENDER pour dessiner et GL_SELECT pour la selection on appelle glLoadName ou pas
    void GLWidget::drawData(GLenum mode)
        {
 
 
            GLuint intName=1;
 
            if (mode== GL_RENDER)
            {
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_NORMAL_ARRAY);
            }
            for (QList<ICMole::Molecule*>::const_iterator M = ListMoles.begin(); M != ListMoles.end(); M++)
            {
// Ici on dessine les spheres correspondant aux atomes : 
                if (!line)
                FOR_ATOM_IN_MOLE(atm,*M)
                    {
                        if (mode == GL_SELECT) {   glLoadName(intName);  intName++; }
                        mole->drawAtomSphere(*atm);
                        if (mode == GL_SELECT)  glPopName();
                     }
                FOR_BOND_IN_MOLE(bd, *M)
                    {
 
                         if (mode == GL_SELECT) {
                             glLoadName(intName); intName++;
                         }
                        if (line){ mole->drawBondLine(*bd); continue;}
                        mole->drawBondSingle(*bd);
                        if (mode == GL_SELECT)
                            glPopName();
                    }
 
            }
 
            if (mode == GL_RENDER)
            {
            glDisableClientState(GL_VERTEX_ARRAY);
            glDisableClientState(GL_NORMAL_ARRAY);
            }
 
 
        }
 
//! [6]
void GLWidget::initializeGL()
{
    qglClearColor(qtPurple.dark());
    GLfloat light0_pos[4]   = { -50.0, 50.0, 0.0, 0.0 };
    GLfloat light0_color[4] = { .6, .6, .6, 1.0 }; /* white light */
    GLfloat light1_pos[4]   = {  50.0, 50.0, 0.0, 0.0 };
    GLfloat light1_color[4] = { .4, .4, 1, 1.0 };  /* cold blue light */
        GLfloat front_shininess[] = {30.0};
        GLfloat front_specular[] = {0.5, 0.5, 0.5, 1.0};
 
    // suppression visualisation des faces "arrires"
    glDisable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
 
    // methodes de rendu
    glEnable(GL_DITHER);
    glShadeModel(GL_SMOOTH);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
 
    // lumires
    glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light0_color);
    glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
    glLightfv(GL_LIGHT1, GL_DIFFUSE,  light1_color);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
    glEnable(GL_LIGHTING);
 
        // rendu des surfaces (matriel)
       glMaterialfv(GL_FRONT, GL_SHININESS, front_shininess);
        glMaterialfv(GL_FRONT, GL_SPECULAR, front_specular);
    glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
    glEnable(GL_COLOR_MATERIAL);
 
 
    mole = new DrawMol(this,ICF);
// Creation d'une nouvelle liste gl
    _displaylistid = glGenLists(1);
 
}
//! [6]
 
//! [7]
void GLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -10.0);
    glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
    glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
    glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
    glScalef(scale/(float)width(), scale/(float)width(), scale/(float)width());
   Update();
   needupdate=false;
 
 
}
//! [7]
 
//! [8]
void GLWidget::resizeGL(int width, int height)
{
        float m_ratio;
        glViewport(0, 0, width, height);
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            m_ratio = width/float(height);
            glOrtho(-m_ratio, m_ratio, -1, 1, nearVal, farVal);
 
            glMatrixMode(GL_MODELVIEW);
 
}
//! [8]
 
//! [9]
void GLWidget::mousePressEvent(QMouseEvent *event)
{
    lastPos = event->pos();
 
    if (event->button() & Qt::LeftButton) // Ici on recherche la selection : 
    {
        GLuint selectBuf[512];
        GLint hits;
        GLint viewport[4];
        int x = lastPos.x();
        int y = lastPos.y();
        std::cout<< x << "\t" << y <<std::endl;
        glGetIntegerv(GL_VIEWPORT,viewport);
        glSelectBuffer(512, selectBuf);
        (void) glRenderMode(GL_SELECT);
        glInitNames();												// Initializes The Name Stack
        glPushName(0);												// Push 0 (At Least One Entry) Onto The Stack
 
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3]-y), 5.0, 5.0, viewport);
        gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f);
        glMatrixMode(GL_MODELVIEW);									// Select The Modelview Matrix
 
        drawData(GL_SELECT); // On rappelle drawData pour "redessiner" les molecules
        glMatrixMode(GL_PROJECTION);								// Select The Projection Matrix
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);									// Select The Modelview Matrix
        hits = glRenderMode(GL_RENDER);
 
        processHits(hits,selectBuf); // Ici hits est toujours égal à 0.
    }
}
//! [9]
 
void GLWidget::processHits(GLint hits, GLuint buffer[])
    {
    std::cout << hits << std::endl; // 0 hits retrouvé à chaque fois ... 
 
    }
 
//! [10]
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
    int dx = event->x() - lastPos.x();
    int dy = event->y() - lastPos.y();
 
    if (event->buttons() & Qt::LeftButton) {
        setXRotation(xRot + 8 * dy);
        setYRotation(yRot + 8 * dx);
    } else if (event->buttons() & Qt::RightButton) {
        dy > 0 ? scale += scale*0.1f : scale -= scale*0.1f;
        updateGL();
    }
    lastPos = event->pos();
}
//! [10]
 
void GLWidget::wheelEvent(QWheelEvent *e)
{
        e->delta() > 0 ? farVal += farVal*0.005f : farVal -= farVal*0.005f;
        resizeGL(this->width(),this->height());
   updateGL();
}
Si vous avez aussi des idées ou des critiques, je suis bien sur preneur !!
Merci d'avance et bonne journée.