zoukankan      html  css  js  c++  java
  • OpenGL 获取当前屏幕坐标的三维坐标(gluUnProject使用例子 VS+glut)

    本例子参考于网络,并进行了一些修改,使用glut+vs2008来实现。

    在鼠标点击处重新画一个旋转的红色立方体!

    参考代码如下:

     C++ Code 
    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
     
    #include <GL/glut.h>
    #include <GL/glu.h>
    #include <GL/gl.h>
    #include <vector>

    using namespace std;

    #define HEIGHT 480
    #define WIDTH 640

    typedef struct Point3D
    {
        
    double objx;
        
    double objy;
        
    double objz;
    }POINT_3D, *PPOINT_3D;

    double objx = 0, objy = 0, objz = 0;
    double objnx = 0, objny = 0, objnz = 0;
    double objfx = 0, objfy = 0, objfz = 0;
    double angle = 0.0;
    vector<POINT_3D> vec3dPoint;
    vector<POINT_3D>::iterator it;


    #pragma comment(linker, "/subsystem:"windows" /entry:"mainCRTStartup"")


    /*THE FUNCTION TO DRAW THE STUFF ON THE SCREEN*/
    void display( )
    {
        glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
        
        
    //The big green floor like polygon
        glBegin( GL_POLYGON );
        glColor3f( 
    010 );
        glVertex3f( -
    1000,  100  );
        glVertex3f( -
    1000, -100  );
        glVertex3f( 
    100,  0, -100 );
        glVertex3f( 
    100,  0100 );
        glEnd( );

        
    for (it = vec3dPoint.begin(); it != vec3dPoint.end(); it++)
        {
            
    //The red cube to be drawn at clicked position
            glPushMatrix( );
            glColor3f( 
    100 );
            glTranslatef( it->objx,it->objy,it->objz );
            glRotatef(angle, 
    1.01.01.0);
            angle++;
            
    if (angle > 360)
            {
                angle = 
    0.0;
            }
            glutSolidCube( 
    10 );
            glPopMatrix( );
        }

        glFlush( );
        glutSwapBuffers( );
    }

    void mouse( int button, int state, int x, int y)
    {
        
    double modelview[16], projection[16];
        
    int viewport[4];
        
    float z = 1;
        
        
    /*Read the projection, modelview and viewport matrices
        using the glGet functions.*/

        glGetDoublev( GL_PROJECTION_MATRIX, projection );
        glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
        glGetIntegerv( GL_VIEWPORT, viewport );

        
    //Read the window z value from the z-buffer 
        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, 0.0, modelview, projection, viewport, &objnx, &objny, &objnz );
        gluUnProject( x, viewport[
    3]-y, z, modelview, projection, viewport, &objx, &objy, &objz );
        gluUnProject( x, viewport[
    3]-y, 1.0, modelview, projection, viewport, &objfx, &objfy, &objfz );
        POINT_3D objxyz;
        objxyz.objx = objx;
        objxyz.objy = objy;
        objxyz.objz = objz;
        vec3dPoint.push_back(objxyz);
    }

    void initVector()
    {
        vec3dPoint.clear();
        POINT_3D objxyz;
        objxyz.objx = objx;
        objxyz.objy = objy;
        objxyz.objz = objz;
        vec3dPoint.push_back(objxyz);
    }

    void init( int width, int height )
    {
        glViewport( 
    00, width, height );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity( );
        gluPerspective( 
    451.330.1400 );
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity( );
        gluLookAt( 
    0100200000010 );
    }

    int main( int argc, char **argv )
    {
        glutInit( &argc, argv );
        
    //The most important part specify the things your
        //glut window should provide
        glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
        glutInitWindowSize( 
    640480 );
        glutCreateWindow(
    "gluUnProject Demo");
        
        glClearColor( 
    0000 );
        
    //enable z buffer
        glEnable( GL_DEPTH_TEST );
        
    //set the value in z-buffer as 1.0
        glClearDepth( 1.0 );
        initVector();
        init( 
    640480 );
        glutDisplayFunc( display );
        glutReshapeFunc( init );
        glutIdleFunc( display );
        glutMouseFunc( mouse );
        glutMainLoop( );
    }
  • 相关阅读:
    linux_ext4恢复超级块.txt
    bayaim_Centos7.6_mysql源码5.7-多my.cnf_20190424.txt
    bayaim_Centos7.6_mysql源码5.7-multi_20190424.txt
    zhy2_rehat6_mysql04
    zhy2_rehat6_mysql03
    Neo4j安装,入门到深入了解完整教程
    程序员千万不要裸辞,对你没有任何好处
    减薪是变相的裁员所以我还是走吧
    python正则表达式匹配中文日期时间
    python用正则表达式匹配字符串里的日期
  • 原文地址:https://www.cnblogs.com/MakeView660/p/10535585.html
Copyright © 2011-2022 走看看