zoukankan      html  css  js  c++  java
  • OpenGL 太阳系行星拾取例子(GL_SELECT) VS2008 + glut实现

    太阳系:Solar System

    以太阳(Sun)为中心,由内到外分别是:

    水星(Mercury)

    金星(Venus)

    地球(Earth)

    火星(Mars)

    木星(Jupiter)

    土星(Saturn)

    天王星(Uranus)

    海王星(Neptune)

    冥王星(Pluto)

    原来是太阳系九大行星,但是最后一个冥王星被除名,就变成八大行星了~

     main.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
    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
    310
    311
    312
    313
    314
    315
     
    //#include <windows.h>
    #include <afx.h>
    #include <gl/gl.h>
    #include <gl/glu.h>
    #include <gl/glut.h>
    #include <math.h>

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

    #define glRGB(x, y, z)  glColor3ub((GLubyte)x, (GLubyte)y, (GLubyte)z)

    #define SUN         1           // 太阳
    #define MERCURY     2           // 水星
    #define VENUS       3           // 金星
    #define EARTH       4           // 地球
    #define MARS        5           // 火星
    #define JUPITER     6           // 木星
    #define SATURN      7           // 土星
    #define URANUS      8           // 天王星
    #define NEPTUNE     9           // 海王星
    #define PLUTO       10          // 冥王星


    GLfloat fAspect;

    // Called to draw scene
    void RenderScene(GLenum mode)
    {
        
    //此处可加gluLookAt()函数

        
    // Save the matrix state and do the rotations
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();

        
    // Translate the whole scene out and into view
        glTranslatef(0.0f, 0.0f, -300.0f);

        
    // Initialize the names stack
        glInitNames();
        glPushName(
    0);

        
    // Set material color, Yellow
        // Draw Sun
        glRGB(2552550);
        
    if (mode == GL_SELECT)
            glLoadName(SUN);
        glutSolidSphere(
    15.0f, 1515);

        
    // Draw Mercury
        glRGB(12800);
        glPushMatrix();
        glTranslatef(
    20.0f, 0.0f, 0.0f);
        
    if (mode == GL_SELECT)
            glLoadName(MERCURY);
        glutSolidSphere(
    2.0f, 1515);
        glPopMatrix();

        
    // Draw Venus
        glPushMatrix();
        glRGB(
    128128255);
        glTranslatef(
    30.0f, 0.0f, 0.0f);
        
    if (mode == GL_SELECT)
            glLoadName(VENUS);
        glutSolidSphere(
    4.0f, 1515);
        glPopMatrix();

        
    // Draw the Earth
        glPushMatrix();
        glRGB(
    00255);
        glTranslatef(
    50.0f, 0.0f, 0.0f);
        
    if (mode == GL_SELECT)
            glLoadName(EARTH);
        glutSolidSphere(
    8.0f, 1515);
        glPopMatrix();

        
    // Draw Mars
        glRGB(25500);
        glPushMatrix();
        glTranslatef(
    65.0f, 0.0f, 0.0f);
        
    if (mode == GL_SELECT)
            glLoadName(MARS);
        glutSolidSphere(
    4.0f, 1515);
        glPopMatrix();

        
    // Draw Jupiter
        glRGB(238216174);
        glPushMatrix();
        glTranslatef(
    80.0f, 0.0f, 0.0f);
        
    if (mode == GL_SELECT)
            glLoadName(JUPITER);
        glutSolidSphere(
    10.0f, 1515);
        glPopMatrix();

        
    // Draw Saturn
        glRGB(255192203);
        glPushMatrix();
        glTranslatef(
    105.0f, 0.0f, 0.0f);
        
    if (mode == GL_SELECT)
            glLoadName(SATURN);
        glutSolidSphere(
    10.0f, 1515);
        glPopMatrix();

        
    // Draw Uranus
        glRGB(173216230);
        glPushMatrix();
        glTranslatef(
    130.0f, 0.0f, 0.0f);
        
    if (mode == GL_SELECT)
            glLoadName(URANUS);
        glutSolidSphere(
    12.0f, 1515);
        glPopMatrix();

        
    // Draw Neptune
        glRGB(30144255);
        glPushMatrix();
        glTranslatef(
    160.0f, 0.0f, 0.0f);
        
    if (mode == GL_SELECT)
            glLoadName(NEPTUNE);
        glutSolidSphere(
    8.0f, 1515);
        glPopMatrix();

        
    // Draw Pluto
        glRGB(238174238);
        glPushMatrix();
        glTranslatef(
    190.0f, 0.0f, 0.0f);
        
    if (mode == GL_SELECT)
            glLoadName(PLUTO);
        glutSolidSphere(
    12.0f, 1515);
        glPopMatrix();

        
    // Restore the matrix state
        glPopMatrix();
        
    // Modelview matrix
        glutSwapBuffers();
        
    // redraw...
        glutPostRedisplay();
    }

    void Display(void)
    {
        
    // Clear the window with current clearing color
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        RenderScene(GL_RENDER);
    }

    // Present the information on which planet/sun was selected and displayed
    void ProcessPlanet(GLuint id)
    {
        
    switch(id)
        {
        
    case SUN:
            MessageBox(
    NULL, _T("You clicked on the Sun!"), _T("Info"), MB_OK | MB_ICONEXCLAMATION);
            
    break;

        
    case MERCURY:
            MessageBox(
    NULL, _T("You clicked on Mercury!"), _T("Info"), MB_OK | MB_ICONEXCLAMATION);
            
    break;

        
    case VENUS:
            MessageBox(
    NULL, _T("You clicked on Venus!"), _T("Info"), MB_OK | MB_ICONEXCLAMATION);
            
    break;

        
    case EARTH:
            MessageBox(
    NULL, _T("You clicked on Earth!"), _T("Info"), MB_OK | MB_ICONEXCLAMATION);
            
    break;

        
    case MARS:
            MessageBox(
    NULL, _T("You clicked on Mars!"), _T("Info"), MB_OK | MB_ICONEXCLAMATION);
            
    break;

        
    case JUPITER:
            MessageBox(
    NULL, _T("You clicked on Jupiter!"), _T("Info"), MB_OK | MB_ICONEXCLAMATION);
            
    break;

        
    case SATURN:
            MessageBox(
    NULL, _T("You clicked on Saturn!"), _T("Info"), MB_OK | MB_ICONEXCLAMATION);
            
    break;

        
    case URANUS:
            MessageBox(
    NULL, _T("You clicked on Uranus!"), _T("Info"), MB_OK | MB_ICONEXCLAMATION);
            
    break;

        
    case NEPTUNE:
            MessageBox(
    NULL, _T("You clicked on Neptune!"), _T("Info"), MB_OK | MB_ICONEXCLAMATION);
            
    break;

        
    case PLUTO:
            MessageBox(
    NULL, _T("You clicked on Pluto!"), _T("Info"), MB_OK | MB_ICONEXCLAMATION);
            
    break;

        
    default:
            MessageBox(
    NULL, _T("Nothing was clicked on!"), _T("Error"), MB_OK | MB_ICONEXCLAMATION);
            
    break;
        }
    }

    // Process the selection, which is triggered by a right mouse
    // click at (xPos, yPos).
    #define BUFFER_LENGTH 64
    void ProcessSelection(int xPos, int yPos)
    {
        
    // Space for selection buffer
        GLuint selectBuff[BUFFER_LENGTH];

        
    // Hit counter and viewport storeage
        GLint hits, viewport[4];

        
    // Setup selection buffer
        glSelectBuffer(BUFFER_LENGTH, selectBuff);

        
    // Get the viewport
        glGetIntegerv(GL_VIEWPORT, viewport);

        
    // Switch to projection and save the matrix
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();

        
    // Change render mode
        glRenderMode(GL_SELECT);

        
    // Establish new clipping volume to be unit cube around
        // mouse cursor point (xPos, yPos) and extending two pixels
        // in the vertical and horzontal direction
        glLoadIdentity();
        gluPickMatrix(xPos, viewport[
    3] - yPos, 22, viewport);

        
    // Apply perspective matrix
        gluPerspective(45.0f, fAspect, 1.0425.0);

        
    // Draw the scene
        RenderScene(GL_SELECT);

        
    // Collect the hits
        hits = glRenderMode(GL_RENDER);


        
    // If a single hit occured, display the info.
        if(hits >= 1)
        {
            
    int choose = selectBuff[3];
            
    int depth = selectBuff[1];

            
    for(int i = 0 ; i < hits ; i++)
            {
                
    if(selectBuff[i * 4 + 1] < (GLuint)depth) //获取深度最小的物体(selectBuff是按照ID从小到大排列的)
                {
                    choose = selectBuff[i * 
    4 + 3];
                    depth  = selectBuff[i * 
    4 + 1];
                }
            }
            ProcessPlanet(choose);
        }

        
    // Restore the projection matrix
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();

        
    // Go back to modelview for normal rendering
        glMatrixMode(GL_MODELVIEW);
    }

    void init(void)
    {
        glClearColor(
    0.00.00.00.0);
        glEnable(GL_DEPTH_TEST);
        glShadeModel(GL_FLAT);
        glDepthRange(
    0.01.0);  /* The default z mapping */
    }

    // Process the mouse click
    void MouseCallback(int button, int state, int x, int y)
    {
        
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
            ProcessSelection(x, y);
    }

    void ChangeSize(int w, int h)
    {
        
    // Prevent a divide by zero
        if(h == 0)
            h = 
    1;

        
    // Set Viewport to window dimensions
        glViewport(00, w, h);

        
    // Calculate aspect ratio of the window
        fAspect = (GLfloat)w / (GLfloat)h;

        
    // Set the perspective coordinate system
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        
    // Field of view of 45 degrees, near and far planes 1.0 and 425
        gluPerspective(45.0f, fAspect, 1.0425.0);

        
    // Modelview matrix reset
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }

    int main(int argc, char *argv[])
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize(
    600300);
        init();
        glutCreateWindow(
    "Pick a Planet");
        glutReshapeFunc(ChangeSize);
        glutMouseFunc(MouseCallback);
        glutDisplayFunc(Display);

        glutMainLoop();

        
    return 0;
    }

  • 相关阅读:
    iterm2 Mac 常用快捷键
    软件测试:测试方法
    maccms(苹果cms)采集过程报错--MySQL server has gone away错误的解决办法
    ArrayLIst的使用
    hashmap的使用
    hashset机LinkedHashSet的使用
    java集合Collection总结
    maxPostSize属性改变tomcat的post请求的请求体大小设置
    postman中 form-data、x-www-form-urlencoded、raw、binary的区别
    用synchronized关键字及concurrent.locks解决线程并发问题
  • 原文地址:https://www.cnblogs.com/MakeView660/p/10599046.html
Copyright © 2011-2022 走看看