zoukankan      html  css  js  c++  java
  • 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(
    640480);
        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.00.00.00.0);

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

    void GLWidget::resizeGL(int w, int h)
    {
        
    int viewport[4];
        glViewport( 
    00, w, h );
        glGetIntegerv( GL_VIEWPORT, viewport );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity( );
        gluPerspective( 
    451.330.1400 );
        
    double projection[16];
        glGetDoublev( GL_PROJECTION_MATRIX, projection );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity( );
        gluLookAt( 
    01010000010 );
    }

    void GLWidget::paintGL()
    {

        glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );

        
    //The big gray floor like polygon
        glBegin( GL_POLYGON );
        glColor3f( 
    0.50.50.5 );
        glVertex3f( -
    100,  10  );
        glVertex3f( -
    100, -10  );
        glVertex3f( 
    10,  0, -10 );
        glVertex3f( 
    10,  010 );
        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) ", 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, 11, 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) ", z, objx, objy, objz);

        updateGL();
    }

    void GLWidget::draw()
    {
        glPushMatrix();
        glBegin(GL_QUAD_STRIP);
        glColor3f(
    100); glVertex3f(0.0f, 0.0f, 0.0f);
        glColor3f(
    110); glVertex3f(0.0f, 1.0f, 0.0f);
        glColor3f(
    010); glVertex3f(1.0f, 0.0f, 0.0f);
        glColor3f(
    011); glVertex3f(1.0f, 1.0f, 0.0f);
        glColor3f(
    100); glVertex3f(1.0f, 0.0f, -1.0f);
        glColor3f(
    110); glVertex3f(1.0f, 1.0f, -1.0f);
        glColor3f(
    010); glVertex3f(0.0f, 0.0f, -1.0f);
        glColor3f(
    011); glVertex3f(0.0f, 1.0f, -1.0f);
        glColor3f(
    100); glVertex3f(0.0f, 0.0f, 0.0f);
        glColor3f(
    110); glVertex3f(0.0f, 1.0f, 0.0f);
        glEnd();
        glBegin(GL_QUAD_STRIP);
        glColor3f(
    001); glVertex3f(0.0f, 0.0f, 0.0f);
        glColor3f(
    101); glVertex3f(1.0f, 0.0f, 0.0f);
        glColor3f(
    010); glVertex3f(0.0f, 0.0f, -1.0f);
        glColor3f(
    100); glVertex3f(1.0f, 0.0f, -1.0f);
        glColor3f(
    110); glVertex3f(0.0f, 1.0f, 0.0f);
        glColor3f(
    101); glVertex3f(1.0f, 1.0f, 0.0f);
        glColor3f(
    001); glVertex3f(0.0f, 1.0f, -1.0f);
        glColor3f(
    100); glVertex3f(1.0f, 1.0f, -1.0f);
        glEnd();
        glPopMatrix();
    }

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

        glNormal3d(
    0.01.00.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( 25500 );
        glVertex3i( 
    000 );
        glVertex3i( s_Scale, 
    00 );

        
    // Y轴
        glColor3ub( 02550 );
        glVertex3i( 
    000 );
        glVertex3i( 
    0, s_Scale, 0 );

        
    // Z轴
        glColor3ub( 00255 );
        glVertex3i( 
    000 );
        glVertex3i( 
    00, s_Scale );

        glEnd( );
        glPopAttrib( );
    }

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

  • 相关阅读:
    CentOS6.2编译安装Nginx1.2.0
    mysql之主从复制篇
    CentOS6.2编译安装PHP5.4.0
    c# 多线程 编程
    QQ空间及邮箱验证码登录的校验方式及自动登录的解决方案
    C# 动态编译、动态执行、动态调试
    在Visual C#中用ListView显示数据记录
    推荐一个免费的HTTP抓包分析工具 Fiddler Web Debugger
    C#简繁体转换方法(Microsoft.Office.Interop.Word)
    C#读取字符串类型XML
  • 原文地址:https://www.cnblogs.com/MakeView660/p/10709428.html
Copyright © 2011-2022 走看看