之前使用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();
    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 = );
    ~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>

;

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

GLWidget::~GLWidget()
{

}

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

glClearDepth();
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
}

void GLWidget::resizeGL(int w, int h)
{
    ];
    glViewport( , w, h );
    glGetIntegerv( GL_VIEWPORT, viewport );
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity( );
    gluPerspective(  );
    ];
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity( );
    gluLookAt(  );
}

void GLWidget::paintGL()
{

glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );

//The big gray floor like polygon
    glBegin( GL_POLYGON );
    glColor3f(  );
    glVertex3f( -  );
    glVertex3f( -  );
    glVertex3f(  );
    glVertex3f(  );
    glEnd( );
    //DrawFloor();
    DrawAxis();

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

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    updateGL();
    ];
    ];
    float x = event->pos().rx();
    float y = event->pos().ry();
    GLfloat  z = ;
    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[, 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[]-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(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glEnd();
    glBegin(GL_QUAD_STRIP);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glColor3f(.0f);
    glEnd();
    glPopMatrix();
}

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

glNormal3d();
    )
    {
        glVertex3i(i, , -s_Scale);
        glVertex3i(i, , s_Scale);
        glVertex3i(-s_Scale, , i);
        glVertex3i(s_Scale, , i);
    }
    glEnd();
    glDepthMask(GL_TRUE);
}
/*---------------------------------------------------------------------------*/
void GLWidget::DrawAxis( void )
{
    // Local Space
;

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

// X轴
 );
    glVertex3i(  );
    glVertex3i( s_Scale,  );

// Y轴
 );
    glVertex3i(  );
    glVertex3i(  );

// Z轴
 );
    glVertex3i(  );
    glVertex3i( , s_Scale );

glEnd( );
    glPopAttrib( );
}

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

OpenGL 获取当前屏幕坐标的三维坐标(gluUnProject使用例子 Qt)的更多相关文章

  1. OpenGL 获取当前屏幕坐标的三维坐标(gluUnProject使用例子 VS+glut)

    本例子参考于网络,并进行了一些修改,使用glut+vs2008来实现. 在鼠标点击处重新画一个旋转的红色立方体! 参考代码如下:  C++ Code  123456789101112131415161 ...

  2. OpenGL 获取当前屏幕坐标对应的三维坐标

    转自原文 OpenGL 获取当前屏幕坐标对应的三维坐标,使用很简单glu库中的一个函数 #include <GL/glut.h> #include <stdlib.h> #in ...

  3. Unity 坐标 转换 详解 World世界坐标 Screen屏幕坐标 View视口坐标 GUI坐标 NGUI坐标 localPosition相对父级坐标

    在制作游戏中我们经常会遇到这样一个需求: 在人物模型的上面显示 名字.称号 一类的文字或者图片 如下图 人物模型属于是Camera1   UI Title信息属于NGUI Camera2 如下图 这时 ...

  4. OpenGL入门程序五:三维绘制

    1.现实世界观察一个物体的时候,可能涉及到的三维变化: 1>视图变化------从不同的角度观察. 2>模型变化------移动.旋转物体,计算机中当然还可以对物体进行缩放. 3>投 ...

  5. PIE SDK专题制图下屏幕坐标转地图坐标

    1.    功能简介 PIESDK提供了专题制图下鼠标屏幕坐标转地图坐标功能. 2. 功能实现说明 2.1屏幕坐标转地图坐标 此功能用到了IPageLayout.ToMapPoint()方法,它的主要 ...

  6. “为什么DirectX里表示三维坐标要建一个4*4的矩阵?”

    0x00 前言 首先要说明的是,本文的标题事实上来自于知乎上的一个同名问题:为什么directX里表示三维坐标要建一个4*4的矩阵? - 编程 .因此,正如Milo Yip大神所说的这个标题事实上是存 ...

  7. C# JackLib系列之如何获取地球上两经纬度坐标点间的距离

    获取地球上两经纬度坐标点间的距离,利用[大圆距离公式]   A diagram illustrating great-circle distance (drawn in red) between tw ...

  8. ACM1174_爆头解题思路_空间三维坐标求点到直线的距离

    /* 爆头 Description gameboy是一个CS高手,他最喜欢的就是扮演警察, 手持M4爆土匪的头.也许这里有人没玩过CS,有必 要介绍一下“爆头”这个术语:所谓爆头,就是子 弹直接命中对 ...

  9. MATLAB在三维坐标中显示图片 并 使得图片部分透明

    要画一个光路图,本来可以用proe,但是鼠标不好用,有些操作也忘了,用MATLAB画了个.下面是用到的图片. 但是三维坐标中显示彩色图片的目标没有搞定,做了个灰度图,然后用仿射程序将彩色图片贴到了二维 ...

随机推荐

  1. .net 4.0 中的特性总结(四):Tuple类型

    Tuple是具有指定数量和顺序的值的一种数据结构.针对这种数据结构,.Net4.0中提供了一组Tuple类型,具体如下: Tuple   Tuple<T>   Tuple<T1, T ...

  2. springboot的常见问题错误

    一: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 2 ...

  3. redis:消息发布与订阅频道

    1. 发布与订阅频道 消息发布与订阅像收音机与广播台的关系 1.1. publish channel message 发布频道 语法:publish channel message 作用:发布频道消息 ...

  4. JDBC接口

    Jmeter实例8:JDBC接口 加线程组.加JDBC配置信息: 配置信息详情如下:默认展示值不用修改 必填信息: Database UR:固定格式,将IP.端口号.数据库名修改成自己要用的数据库信息 ...

  5. Jmeter接口测试实例3-登录

    Jmeter实例3:登录 添加http协议—添加IP.路径.方法,察看结果树,运行 登录成功

  6. 常用的sort打乱数组方法真的有用?

    JavaScript 开发中有时会遇到要将一个数组随机排序(shuffle)的需求,一个常见的写法是这样: function shuffle(arr) { arr.sort(function () { ...

  7. java C 类自动转换规则

    C类型转换规则

  8. 倾斜摄影数据OSGB进入到ArcGIS平台相关问题小结

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zglybl/article/details/75252288      随着倾斜摄影技术的发展,大家 ...

  9. gpg使用说明

    http://blog.163.com/ywz_306/blog/static/13257711201121921452480/

  10. C++ WINDOWS下 wchar_t *和char * 相互转化总结篇

    说道wchar_t和char两个类型大家都不会陌生 wchar_t:在windows下是Unicode 16编码,也就是俗称宽字节 char:当然就是指一个字节,在windows下面默认是gbk编码的 ...