ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [OpenGL]예제3_회전하는 정사면체, 애니메이션
    Programming Language/OpenGL 2017. 4. 12. 17:40
    반응형

    OpenGL 애니메이션, stipple pattern 사용 예제


    OpenGL을 이용해 애니메이션을 만드는 방법 중 하나는

     : glutIdleFunc()를 이용해 idle 상태에서도 새롭게 scene을 호출해서 그리도록 구현하면 된다.


    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
    #include <iostream>
    #include <gl/glut.h>
    #include <math.h>
     
    double cubeLen = 2 * sqrt(2);
     
    GLubyte halftone[] = {
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55,
        0xAA0xAA0xAA0xAA0x550x550x550x55 };
     
     
    GLubyte fly[] = {
        0x000x000x000x000x000x000x000x00,
        0x030x800x010xC00x060xC00x030x60,
        0x040x600x060x200x040x300x0C0x20,
        0x040x180x180x200x040x0C0x300x20,
        0x040x060x600x200x440x030xC00x22,
        0x440x010x800x220x440x010x800x22,
        0x440x010x800x220x440x010x800x22,
        0x440x010x800x220x440x010x800x22,
        0x660x010x800x660x330x010x800xCC,
        0x190x810x810x980x0C0xC10x830x30,
        0x070xe10x870xe00x030x3f0xfc0xc0,
        0x030x310x8c0xc00x030x330xcc0xc0,
        0x060x640x260x600x0c0xcc0x330x30,
        0x180xcc0x330x180x100xc40x230x08,
        0x100x630xC60x080x100x300x0c0x08,
        0x100x180x180x080x100x000x000x08 };
     
     
    int angle = 0;
     
    void drawBox(){
        glColor3f(100);
        glPolygonStipple(fly);
        glRotatef(angle, 010);
        glBegin(GL_POLYGON);
        glVertex3f(-cubeLen, cubeLen, -cubeLen);
        glVertex3f(-cubeLen, -cubeLen, cubeLen);
        glVertex3f(cubeLen, -cubeLen, -cubeLen);
        glEnd();
     
     
        glColor3f(010);
        glPolygonStipple(halftone);
        glBegin(GL_POLYGON);
        glVertex3f(-cubeLen, cubeLen, -cubeLen);
        glVertex3f(-cubeLen, -cubeLen, cubeLen);
        glVertex3f(cubeLen, cubeLen, cubeLen);
        glEnd();
     
     
        glColor3f(001);
        glPolygonStipple(fly);
        glBegin(GL_POLYGON);
        glVertex3f(-cubeLen, cubeLen, -cubeLen);
        glVertex3f(cubeLen, cubeLen, cubeLen);
        glVertex3f(cubeLen, -cubeLen, -cubeLen);
        glEnd();
     
     
        glColor3f(110);
        glPolygonStipple(halftone);
        glBegin(GL_POLYGON);
        glVertex3f(cubeLen, cubeLen, cubeLen);
        glVertex3f(-cubeLen, -cubeLen, cubeLen);
        glVertex3f(cubeLen, -cubeLen, -cubeLen);
        glEnd();
        
    }
     
    void display(){
        glClearColor(0001);
        glClear(GL_COLOR_BUFFER_BIT);
     
        glViewport(00700700);
     
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-44-440.150);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glEnable(GL_POLYGON_STIPPLE);
        glPushMatrix();
        glTranslatef(000);
        glRotatef(50111);
        glScalef(0.50.50.5);
        angle += 1;
        drawBox();
        glPopMatrix();
        glutSwapBuffers();
        glFlush();
    }
     
     
     
     
    int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
        glutInitWindowSize(700700);
        glutInitWindowPosition(00);
        glutCreateWindow("tetrahedron");
        glutDisplayFunc(display);
        glutIdleFunc(display); //idle 상태에서도 display를 호출해서 새롭게 그린다. -> 애니메이션 제작 가능.
        glutMainLoop();
    }
    cs


    반응형
Designed by Tistory.