-
#include <GL/glut.h>
static GLfloat theta[] = {0.0, 0.0, 0.0};
static GLint axis =1;static GLfloat theta_degree=0.1;
static GLfloat zoom_factor = 0.0;
static GLfloat transZ = 0.0;
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} };
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();
glFlush();
}
void cube()
{
quad(0, 3, 2, 1); // 앞 면
quad(2, 3, 7, 6); // 오른쪽 면
quad(0, 4, 7, 3); // 아랫 면
quad(1, 2, 6, 5); // 윗 면
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();
glFlush();
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;
if(theta[axis] > 360)
theta[axis] -= 360;
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;
case 'y':
axis=1;
break;
case'z':
axis=2;
break;
case '=':
theta_degree += 0.1;
break;
case '-':
theta_degree -= 0.1;
break;
case'[':
transZ -=0.2;
break;
case ']':
transZ += 0.2;
break;
}
}void special_key(int k, int x, int y)
{
switch(k){
case GLUT_KEY_PAGE_UP:
zoom_factor -= 0.2;
break;
case GLUT_KEY_PAGE_DOWN:
zoom_factor += 0.2;
break;
}
}
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.0f, 1.0f, 0.0f);
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;
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();
glFlush();
}
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();
}