-
#include <GL/glut.h>
GLfloat vertices[8][3] = {{ -1.0, -1.0, 1.0}, {-1.0, 1.0, 1.0}, // 도형 좌표 지정
{1.0, 1.0, 1.0}, {1.0, -1.0, 1.0}, {-1.0, -1.0, -1.0},
{-1.0, 1.0, -1.0}, {1.0, 1.0, -1.0}, {1.0, -1.0, -1.0}};GLfloat colors[8][3] = {{0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, // 도형 색상 지정
{1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0},
{1.0, 0.0, 1.0}, {1.0, 1.0, 1.0}, {0.0, 1.0, 1.0}};static GLfloat theta[] = {0.0, 0.0, 0.0}; // 회전 축 지정
static GLint axis = 1; // 회전 축 Y 축으로 초기화
static GLfloat theta_degree = 0.1; // 회전 각도 조절용
static GLfloat zoom_factor = 0.0; // 줌
static GLfloat transZ = 0.0; // Z 축 이동void quad(int a, int b, int c, int d) // 도형 객체의 한면 만들기
{
glBegin(GL_QUADS);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}void cube() // 면을 합쳐 도형 객체 완성
{
quad(0, 3, 2, 1);//앞면
quad(2, 3, 7, 6);//오른쪽면
quad(0, 4, 7, 3);//아랫면
quad(0, 4, 7, 3);//윗면
quad(4, 5, 6, 7);//뒷면
quad(0, 1, 5, 4);//왼쪽면
}void coodiaxis() // 좌표축 그리기
{
glColor3f(1.0, 0.0, 0.0);
//각 좌표축 그려줌
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(2.5, 0.0, 0.0);
glVertex3f(2.5, 0.0, 0.0);
glVertex3f(2.45, 0.05, 0.0); //화살표
glVertex3f(2.5, 0.0, 0.0);
glVertex3f(2.45, -0.05, 0.0);glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 2.5, 0.0);
glVertex3f(0.0, 2.5, 0.0);
glVertex3f(0.05, 2.45, 0.0);
glVertex3f(0.0, 2.5, 0.0);
glVertex3f(-0.05, 2.45, 0.0);glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 2.5);
glVertex3f(0.0, 0.0, 2.5);
glVertex3f(0.0, 0.05, 2.45); //화살표'
glVertex3f(0.0, 0.0, 2.5);
glVertex3f(0.0, -0.05, 2.45);
glEnd();glColor3f(1.0, 0.0, 0.0);
glRasterPos3f(2.6, 0.0, 0.0);//X축 표시
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'X');
glRasterPos3f(0.0, 2.6, 0.0);//Y축 표시
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'Y');
glRasterPos3f(0.0, 0.0, 2.6);//Z축 표시
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'Z');
}void spinCube(void)
{
theta[axis] += theta_degree;
// 0.1도씩 회전하다가 360도가 넘으면 다시 0도로 초기화
if(theta[axis] > 360.0)
theta[axis] -= 360.0;
glutPostRedisplay();
}void key(unsigned char k, int x, int y)
{
theta[0] = theta[1] = theta[2] = 0.0;
glLoadIdentity();
switch(k){
case 'x': axis = 0; break; // X 축 중심으로 회전
case 'y': axis = 1; break; // Y 축 중심으로 회전
case 'z': axis = 2; break; // Z 축 중심으로 회전
case '=': theta_degree += 0.1; break; // 회전속도 (1회 회전 각도) 0.1 증가
case '-': theta_degree -= 0.1; break; // 회전속도 (1회 회전 각도) 0.1 감소
case '[': transZ -= 0.2; break; // Z 축으로 -0.2 만큼이동
case ']': transZ += 0.2; break; // Z 축으로 +0.2 만큼이동
}
}void special_key(int k, int x, int y)
{
switch(k){
case GLUT_KEY_PAGE_UP: zoom_factor -= 0.2; break;
// z 축을 zoom factor로 사용하고 PAGE_UP 버튼을 누르면 0.2 만큼 가까이 감
case GLUT_KEY_PAGE_DOWN: zoom_factor += 0.2; break;
// page down 버튼은 0.2 만큼 멀어짐
}
}void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( 2.5+zoom_factor, 2.5+zoom_factor, 3.0+zoom_factor,
-(2.5 + zoom_factor), -(2.5 + zoom_factor), -(3.0 + zoom_factor), 0.0, 1.0, 0.0);
glTranslatef(0.0f, 0.0f, transZ);
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);cube();
coodiaxis();glFlush();
glutSwapBuffers();
}void reshape(GLsizei w, GLsizei h)
{
GLfloat fovy = 60.0;
GLfloat aspect;
if(h == 0) h = 1; //0으로 나누지 않도록 함
aspect = (GLfloat)w / (GLfloat)h;
//클리핑 영역 설정
glViewport(0, 0, w, h);
//뷰포트를 차의 크기에 맞게 설정
glMatrixMode(GL_PROJECTION);
glLoadIdentity();//좌표계 초기화
gluPerspective(fovy, aspect, 1.0, 100.0); // 원근설정
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}void init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}void main(int argc, char** argv)
{
glutInit (&argc, argv);
glClearDepth(1.0f);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize( 500, 500 );
glutInitWindowPosition( 0, 0 );
glutCreateWindow ("Spin Cube");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutKeyboardFunc(key);
glutSpecialFunc(special_key);
init();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
/*// 마우스 입력
#include <GL/glut.h>GLfloat vertices[8][3] = {{ -1.0, -1.0, 1.0}, {-1.0, 1.0, 1.0}, // 도형 좌표 지정
{1.0, 1.0, 1.0}, {1.0, -1.0, 1.0}, {-1.0, -1.0, -1.0},
{-1.0, 1.0, -1.0}, {1.0, 1.0, -1.0}, {1.0, -1.0, -1.0}};GLfloat colors[8][3] = {{0.0, 0.0, 0.0}, {1.0, 0.0, 0.0}, // 도형 색상 지정
{1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0},
{1.0, 0.0, 1.0}, {1.0, 1.0, 1.0}, {0.0, 1.0, 1.0}};static GLfloat theta[] = {0.0, 0.0, 0.0}; // 회전 축 지정
static GLint axis = 1; // 회전 축 Y 축으로 초기화void quad(int a, int b, int c, int d) // 도형 객체의 한면 만들기
{
glBegin(GL_QUADS);
glColor3fv(colors[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[b]);
glVertex3fv(vertices[b]);
glColor3fv(colors[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glVertex3fv(vertices[d]);
glEnd();
}void cube() // 면을 합쳐 도형 객체 완성
{
quad(0, 3, 2, 1);//앞면
quad(2, 3, 7, 6);//오른쪽면
quad(0, 4, 7, 3);//아랫면
quad(0, 4, 7, 3);//윗면
quad(4, 5, 6, 7);//뒷면
quad(0, 1, 5, 4);//왼쪽면
}void coodiaxis() // 좌표축 그리기
{
glColor3f(1.0, 0.0, 0.0);
//각 좌표축 그려줌
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(2.5, 0.0, 0.0);
glVertex3f(2.5, 0.0, 0.0);
glVertex3f(2.45, 0.05, 0.0); //화살표
glVertex3f(2.5, 0.0, 0.0);
glVertex3f(2.45, -0.05, 0.0);glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 2.5, 0.0);
glVertex3f(0.0, 2.5, 0.0);
glVertex3f(0.05, 2.45, 0.0);
glVertex3f(0.0, 2.5, 0.0);
glVertex3f(-0.05, 2.45, 0.0);glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 2.5);
glVertex3f(0.0, 0.0, 2.5);
glVertex3f(0.0, 0.05, 2.45); //화살표'
glVertex3f(0.0, 0.0, 2.5);
glVertex3f(0.0, -0.05, 2.45);
glEnd();glColor3f(1.0, 0.0, 0.0);
glRasterPos3f(2.6, 0.0, 0.0);//X축 표시
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'X');
glRasterPos3f(0.0, 2.6, 0.0);//Y축 표시
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'Y');
glRasterPos3f(0.0, 0.0, 2.6);//Z축 표시
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'Z');
}
void spinCube(void) //
{
theta[axis] += 0.2;
if(theta[axis] > 360.0)
theta[axis] -= 360;
glutPostRedisplay();
}void mouse(int btn, int state, int x, int y)
{
theta[0] = theta[1] = theta[2] = 0;
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if(btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
}void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();gluLookAt(-5.0f, 3.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);cube();
coodiaxis();glFlush();
glutSwapBuffers();
}void reshape(GLsizei w, GLsizei h)
{
GLfloat fovy = 60.0;
GLfloat aspect;
if(h == 0) h = 1; //0으로 나누지 않도록 함
aspect = (GLfloat)w / (GLfloat)h;
//클리핑 영역 설정
glViewport(0, 0, w, h);
//뷰포트를 차의 크기에 맞게 설정
glMatrixMode(GL_PROJECTION);
glLoadIdentity();//좌표계 초기화
gluPerspective(fovy, aspect, 1.0, 100.0); // 원근설정
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}void init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}void main(int argc, char** argv)
{
glutInit( &argc, argv );
glClearDepth(1.0f);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500); // 윈도우 창 크기
glutInitWindowPosition ( 0, 0 ); // 윈도우 창 위치
glutCreateWindow("Spin Cube"); // 창 타이틀
glutReshapeFunc(reshape); //
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse); //mouse 등록
init();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}*/