ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [OpenGL] 예제2_정육면체 그리기
    Programming Language/OpenGL 2017. 4. 11. 16:06
    반응형

    glLoadIdentity,

    glPushMatrix,

    glPopMatrix,

    glutSwapBuffers,

    glMatrixMode,

    glEnable,

    glLineStipple,

    glTranslatef,

    glRotatef,

    glScalef


    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
    /*
    glLoadIdentity,
    glPushMatrix,
    glPopMatrix,
    glutSwapBuffers,
    glMatrixMode,
    glEnable,
    glLineStipple,
    glTranslatef,
    glRotatef,
    glScalef
    */
     
    #include <iostream>
    #include <gl/glut.h>
     
    void drawBox(){
        glColor3f(100);
        glBegin(GL_LINE_LOOP);
            glVertex3f(-1-1-1);
            glVertex3f(1-1-1);
            glVertex3f(11-1);
            glVertex3f(-11-1);
        glEnd();
     
        glColor3f(010);
        glBegin(GL_LINE_LOOP);
        glVertex3f(-1-11);
        glVertex3f(1-11);
        glVertex3f(111);
        glVertex3f(-111);
        glEnd();
     
        glColor3f(001);
        glBegin(GL_LINES);
        glVertex3f(-1-1-1);
        glVertex3f(-1-11);
        glVertex3f(1-1-1);
        glVertex3f(1-11);
        glVertex3f(11-1);
        glVertex3f(111);
        glVertex3f(-11-1);
        glVertex3f(-111);
        glEnd();
    }
     
    void display(){
        glClearColor(1111);
        glClear(GL_COLOR_BUFFER_BIT);
     
        glViewport(2500250250);
     
        glMatrixMode(GL_PROJECTION);/*
                                    glMatrixMode : specify which matrix is current matrix. (sets the current matrix mode)
                                    GL_PROJECTION : applies subsequent matrix operations to the projection matrix stack.
                                    GL_MODELVIEW : applies subsequent matrix operations to the modelview matrix stack.
                                    GL_MODELVIEW의 행렬을 곱하면 물체의 실제 위치를 지정할 수 있다.
                                    GL_PROJECTION의 행렬을 곱하면 최종적으로 어떻게 화면에 뿌릴 것인가를 결정할 수 있다. - ex. 직교투영, 원근투영
                                    예) 서로 다른 모드들은 각자에게 영향을 주지 않는다.
                                    glMatrixMode(GL_PROJECTION);
                                    glLoadIdentity(); // GL_PROJECTION matrix mode에 대한 행렬 초기화. modelview matrix를 초기화하지 않음.
                                    glMatrixMode(GL_MODELVIEW);
                                    glLoadIdentity();// GL_MODELVIEW matrix mode에 대한 행렬 초기화
                                    https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glMatrixMode.xml
                                    http://sune.tistory.com/270
                                    */
        glLoadIdentity();
        glOrtho(-11-110.150);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();/* glLoadIdentity : replace the current matrix with the identity matrix.
                            두개 이상의 물체를 좌표상에 나타낼 때, 첫 번째 물체를 그릴 때 사용되었던 변환행렬이 두 번째 물체를 그릴 때에도 계속 남아있게 된다.
                            첫 번째 물체와는 다른 변환행렬을 사용해서 그리기 위해서는 행렬을 단위행렬로 초기화해 주어야 한다. 그러기 위해서 사용하는 함수가 glLoadIdentity()이다.
                            단, glLoadIdentity()로 행렬을 초기화하면 예전의 변환행렬 값을 잃게 된다.
                            예전의 변환행렬 값을 잃지 않기 위해 사용하는 것이 Matrix stack이다. 스택에 이전 행렬들을 glPushMatrix()로 저장하고, glPopMatrix()로 꺼낼 수 있다.
                            http://nobilitycat.tistory.com/entry/glLoadIdentity-%ED%95%A8%EC%88%98
                         */
     
     
        glEnable(GL_LINE_STIPPLE);//enable or disable server-side GL capabilities. /* https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glEnable.xml */
        glLineStipple(10xF0F0);//specify the line stipple pattern https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glLineStipple.xml
     
     
        glPushMatrix();/* 
                       https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glPushMatrix.xml
                       http://www.gpgstudy.com/forum/viewtopic.php?t=3537
                       */
            glTranslatef(00-2);//정점의 이동
            glRotatef(180111);//회전 변환
            glScalef(0.50.50.5);//크기 변환 : 벡터의 크기를 늘리고 줄이는 것.
            drawBox();
        glPopMatrix();
     
        glutSwapBuffers();/*glutSwapBuffers : swaps the buffers of the current window if double buffered.
                          double buffered mode : front buffer 내용이 화면에 나타내어지는 동안, 새로운 내용이 back buffer에 쓰여지고, back buffer에 기록이 다 되면 front buffer와 back buffer가 바뀐다.
                          glutSwapBuffers는 back buffer과 front buffer를 swapping하는 것.
                          An implicit glFlush is done by glutSwapBuffers before it returns. Subsequent OpenGL commands can be issued immediately after calling glutSwapBuffers, but are not executed until the buffer exchange is completed.
                          If the layer in use is not double buffered, glutSwapBuffers has no effect.
                          https://www.opengl.org/resources/libraries/glut/spec3/node21.html
                          http://kuroikuma.tistory.com/115
                          */
        glFinish();
    }
     
    int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);/*sets initial display mode. GLUT_DOUBLE : select a double buffered window.(front buffer & back buffer)
                                                    https://www.opengl.org/resources/libraries/glut/spec3/node12.html
                                                    */
        glutInitWindowSize(500500);
        glutInitWindowPosition(00);
        glutCreateWindow("cubecube");
        glutDisplayFunc(display);
        glutMainLoop();
    }
     
     
     
    /*
    modelview matrix와 projection matrix에 대해 더 알고싶다면 http://sune.tistory.com/270 참고.
    glPushMatrix, glPopMatrix에 대한 이해 : http://www.gpgstudy.com/forum/viewtopic.php?t=3537 참고.
    */
    cs


    반응형
Designed by Tistory.