-
[OpenGL]예제3_회전하는 정사면체, 애니메이션Programming Language/OpenGL 2017. 4. 12. 17:40반응형
OpenGL 애니메이션, stipple pattern 사용 예제
OpenGL을 이용해 애니메이션을 만드는 방법 중 하나는
: glutIdleFunc()를 이용해 idle 상태에서도 새롭게 scene을 호출해서 그리도록 구현하면 된다.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121#include <iostream>#include <gl/glut.h>#include <math.h>double cubeLen = 2 * sqrt(2);GLubyte halftone[] = {0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55 };GLubyte fly[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08 };int angle = 0;void drawBox(){glColor3f(1, 0, 0);glPolygonStipple(fly);glRotatef(angle, 0, 1, 0);glBegin(GL_POLYGON);glVertex3f(-cubeLen, cubeLen, -cubeLen);glVertex3f(-cubeLen, -cubeLen, cubeLen);glVertex3f(cubeLen, -cubeLen, -cubeLen);glEnd();glColor3f(0, 1, 0);glPolygonStipple(halftone);glBegin(GL_POLYGON);glVertex3f(-cubeLen, cubeLen, -cubeLen);glVertex3f(-cubeLen, -cubeLen, cubeLen);glVertex3f(cubeLen, cubeLen, cubeLen);glEnd();glColor3f(0, 0, 1);glPolygonStipple(fly);glBegin(GL_POLYGON);glVertex3f(-cubeLen, cubeLen, -cubeLen);glVertex3f(cubeLen, cubeLen, cubeLen);glVertex3f(cubeLen, -cubeLen, -cubeLen);glEnd();glColor3f(1, 1, 0);glPolygonStipple(halftone);glBegin(GL_POLYGON);glVertex3f(cubeLen, cubeLen, cubeLen);glVertex3f(-cubeLen, -cubeLen, cubeLen);glVertex3f(cubeLen, -cubeLen, -cubeLen);glEnd();}void display(){glClearColor(0, 0, 0, 1);glClear(GL_COLOR_BUFFER_BIT);glViewport(0, 0, 700, 700);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-4, 4, -4, 4, 0.1, 50);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glEnable(GL_POLYGON_STIPPLE);glPushMatrix();glTranslatef(0, 0, 0);glRotatef(50, 1, 1, 1);glScalef(0.5, 0.5, 0.5);angle += 1;drawBox();glPopMatrix();glutSwapBuffers();glFlush();}int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);glutInitWindowSize(700, 700);glutInitWindowPosition(0, 0);glutCreateWindow("tetrahedron");glutDisplayFunc(display);glutIdleFunc(display); //idle 상태에서도 display를 호출해서 새롭게 그린다. -> 애니메이션 제작 가능.glutMainLoop();}cs 반응형'Programming Language > OpenGL' 카테고리의 다른 글
glut32.lib 파일을 열 수 없습니다 (0) 2017.12.10 [OpenGL] Rasterization (0) 2017.04.11 [OpenGL] 예제2_정육면체 그리기 (0) 2017.04.11 [OpenGL] 예제1_화면에 도형 띄우기 (0) 2017.04.10 [Visual Studio] OpenGL 프로젝트 생성하기 (0) 2017.04.10