OpenGL 获取当前屏幕坐标的三维坐标(gluUnProject使用例子 Qt)


之前使用VS+glut实现了gluUnProject使用例子,用于渲染管道的逆过程,将屏幕坐标转换为opengl三维坐标,本文将尝试使用QT来实现。

代码如下:

 main.cpp 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
#include   "GLWidget.h"
#include  <QApplication>

int  main( int  argc,  char  *argv[])
{
    QApplication a(argc, argv);

    GLWidget glw;
    glw.resize(
640 480 );
    glw.setWindowTitle(
"gluUnProject Demo" );
    glw.show();

    
return  a.exec();
}

 

GLWidget.h 
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
 
#ifndef  GLWIDGET_H
#define  GLWIDGET_H

#include  <QGLWidget>

class  GLWidget :  public  QGLWidget
{
    Q_OBJECT

public :
    GLWidget(QWidget *parent = 
0 );
    ~GLWidget();

protected :
    
virtual   void  initializeGL();
    
virtual   void  resizeGL( int  w,  int  h);
    
virtual   void  paintGL();

    
virtual   void  mousePressEvent(QMouseEvent *event);

private :
    
void  draw();
    
void  DrawFloor();
    
void  DrawAxis();

    GLdouble   objx;
    GLdouble   objy;
    GLdouble   objz;
};

#endif   // GLWIDGET_H

 

GLWidget.cpp
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
 
#include   "GLWidget.h"
#include  <QMouseEvent>
#include  <GL/glu.h>
#include  <QDebug>

static   const  qint32 s_Scale =  8 ;

GLWidget::GLWidget(QWidget *parent)
    : QGLWidget(parent)
{
    objx = 
0 . 0 ;
    objy = 
0 . 0 ;
    objz = 
0 . 0 ;
}

GLWidget::~GLWidget()
{

}

void  GLWidget::initializeGL()
{
    glClearColor(
0 . 0 0 . 0 0 . 0 0 . 0 );

    glClearDepth(
1 . 0 );
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
}

void  GLWidget::resizeGL( int  w,  int  h)
{
    
int  viewport[ 4 ];
    glViewport( 
0 0 , w, h );
    glGetIntegerv( GL_VIEWPORT, viewport );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity( );
    gluPerspective( 
45 1 . 33 0 . 1 400  );
    
double  projection[ 16 ];
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity( );
    gluLookAt( 
0 10 10 0 0 0 0 1 0  );
}

void  GLWidget::paintGL()
{

    glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );

    
//The big gray floor like polygon
    glBegin( GL_POLYGON );
    glColor3f( 
0 . 5 0 . 5 0 . 5  );
    glVertex3f( -
10 0 ,   10   );
    glVertex3f( -
10 0 , - 10   );
    glVertex3f( 
10 ,   0 , - 10  );
    glVertex3f( 
10 ,   0 10  );
    glEnd( );
    
//DrawFloor();
    DrawAxis();

    
//The red cube to be drawn at clicked position
    glPushMatrix( );
    glTranslatef(objx, objy, objz );
    draw();
    
double  modelview[ 16 ];
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glPopMatrix( );
}

void  GLWidget::mousePressEvent(QMouseEvent *event)
{
    updateGL();
    
double  modelview[ 16 ], projection[ 16 ];
    
int  viewport[ 4 ];
    
float  x = event->pos().rx();
    
float  y = event->pos().ry();
    GLfloat  z = 
0 ;
    
double  winx, winy, winz;
    qDebug(
"Window coords are (%d, %d)\n" , x, y);
    
/*Read the projection, modelview and viewport matrices using the glGet functions.*/
    glGetIntegerv( GL_VIEWPORT, viewport );
    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetDoublev( GL_PROJECTION_MATRIX, projection );

    
//Read the window z value from the z-buffer

    glReadBuffer(GL_FRONT);
    glReadPixels( x, viewport[
3 ]-y,  1 1 , GL_DEPTH_COMPONENT, GL_FLOAT, &z );

    
//Use the gluUnProject to get the world co-ordinates of
     //the point the user clicked and save in objx, objy, objz.
    gluUnProject( x, viewport[
3 ]-y, z, modelview, projection, viewport, &objx, &objy, &objz );
    qDebug(
"World coords at z=%.1f are (%.3f, %.3f, %.3f)\n" , z, objx, objy, objz);

    updateGL();
}

void  GLWidget::draw()
{
    glPushMatrix();
    glBegin(GL_QUAD_STRIP);
    glColor3f(
1 0 0 ); glVertex3f( 0 .0f,  0 .0f,  0 .0f);
    glColor3f(
1 1 0 ); glVertex3f( 0 .0f,  1 .0f,  0 .0f);
    glColor3f(
0 1 0 ); glVertex3f( 1 .0f,  0 .0f,  0 .0f);
    glColor3f(
0 1 1 ); glVertex3f( 1 .0f,  1 .0f,  0 .0f);
    glColor3f(
1 0 0 ); glVertex3f( 1 .0f,  0 .0f, - 1 .0f);
    glColor3f(
1 1 0 ); glVertex3f( 1 .0f,  1 .0f, - 1 .0f);
    glColor3f(
0 1 0 ); glVertex3f( 0 .0f,  0 .0f, - 1 .0f);
    glColor3f(
0 1 1 ); glVertex3f( 0 .0f,  1 .0f, - 1 .0f);
    glColor3f(
1 0 0 ); glVertex3f( 0 .0f,  0 .0f,  0 .0f);
    glColor3f(
1 1 0 ); glVertex3f( 0 .0f,  1 .0f,  0 .0f);
    glEnd();
    glBegin(GL_QUAD_STRIP);
    glColor3f(
0 0 1 ); glVertex3f( 0 .0f,  0 .0f,  0 .0f);
    glColor3f(
1 0 1 ); glVertex3f( 1 .0f,  0 .0f,  0 .0f);
    glColor3f(
0 1 0 ); glVertex3f( 0 .0f,  0 .0f, - 1 .0f);
    glColor3f(
1 0 0 ); glVertex3f( 1 .0f,  0 .0f, - 1 .0f);
    glColor3f(
1 1 0 ); glVertex3f( 0 .0f,  1 .0f,  0 .0f);
    glColor3f(
1 0 1 ); glVertex3f( 1 .0f,  1 .0f,  0 .0f);
    glColor3f(
0 0 1 ); glVertex3f( 0 .0f,  1 .0f, - 1 .0f);
    glColor3f(
1 0 0 ); glVertex3f( 1 .0f,  1 .0f, - 1 .0f);
    glEnd();
    glPopMatrix();
}

/*---------------------------------------------------------------------------*/
void  GLWidget::DrawFloor()
{
    glDepthMask(GL_FALSE);
    glColor4ub(
175 175 175 255 );
    glBegin(GL_LINES);

    glNormal3d(
0 . 0 1 . 0 0 . 0 );
    
for  (qint32 i = -s_Scale; i <= s_Scale; i += s_Scale >>  3 )
    {
        glVertex3i(i, 
0 , -s_Scale);
        glVertex3i(i, 
0 , s_Scale);
        glVertex3i(-s_Scale, 
0 , i);
        glVertex3i(s_Scale, 
0 , i);
    }
    glEnd();
    glDepthMask(GL_TRUE);
}
/*---------------------------------------------------------------------------*/
void  GLWidget::DrawAxis(  void  )
{
    
// Local Space
     static   const  qint32 s_Scale =  64 ;

    glPushAttrib( GL_LINE_BIT );
    glLineWidth(
1 .0f );
    glBegin( GL_LINES );

    
// X轴
    glColor3ub(  255 0 0  );
    glVertex3i( 
0 0 0  );
    glVertex3i( s_Scale, 
0 0  );

    
// Y轴
    glColor3ub(  0 255 0  );
    glVertex3i( 
0 0 0  );
    glVertex3i( 
0 , s_Scale,  0  );

    
// Z轴
    glColor3ub(  0 0 255  );
    glVertex3i( 
0 0 0  );
    glVertex3i( 
0 0 , s_Scale );

    glEnd( );
    glPopAttrib( );
}

代码仅供参考,最近一直在研究这个,用于实现场景中单个对象的拖拽,效果还欠佳,欢迎交流讨论。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM