-
#include <GL/glut.h>
#include <stdlib.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 ambientLight[] = {0.25f, 0.25f, 0.25f, 1.0f};
//주변광
GLfloat diffuseLight[] = {0.9f, 0.9f, 0.9f, 1.0f};
//난반사
GLfloat specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
//정반사
GLfloat spotlightPos[] = {-10.0f, 3.0f, 5.0f, 1.0f};
//스포트라이트 위치
GLfloat spotlightDirection[] = {20.0f, -0.6f, -15.0f};
//스포트라이트 방향GLfloat specref[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat ambientmat[] = {0.25f, 0.25f, 0.25f, 1.0f};
//주변광 재질
GLfloat diffusemat[] = {0.5f, 0.5f, 0.5f, 1.0f};
//난반사 재질
GLfloat specularmat[] = {1.0f, 1.0f, 1.0f, 1.0f};
//정반사 재질GLfloat vertices[8][3] = {{-1.0, -1.0, 1.0}, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, -1.0, 1.0}, {-1.0, -1.0, -1.0}, {0.0, 1.0, 0.0}, {0.0, 1.0, 0.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} };
//법선벡터 지정
GLfloat normal[][3] = {{0.0f, 0.0f, 1.0f}, //앞면
{1.0f, 0.0f, 0.0f}, //오른쪽 면
{0.0f, -1.0f, 0.0f}, //아랫면
{0.0f, 1.0f, 0.0f}, //윗 면
{0.0f, 0.0f, -1.0f}, //뒷면
{-1.0f, 0.0f, 0.0f}}; //왼쪽 면void quad(int a, int b, int c, int d, int index)
{
glBegin(GL_QUADS);
glNormal3fv(normal[index]);
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, 0); // 앞 면
quad(2, 3, 7, 6, 1); // 오른쪽 면
quad(0, 4, 7, 3, 2); // 아랫 면
quad(1, 2, 6, 5, 3); // 윗 면
quad(4, 5, 6, 7, 4); // 뒷 면
quad(0, 1, 5, 4, 5); // 왼쪽 면
}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] += 0.2;
if(theta[axis] > 360)
theta[axis] -= 360;
glutPostRedisplay();
}int LightingEffect(GLvoid) //라이트닝 효과 설정
{
glShadeModel(GL_SMOOTH);
//매끄러운 세위딩 사용
glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);
glEnable(GL_CCW);
glEnable(GL_LIGHTING);glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, spotlightPos);glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 30.0f);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 20.0f);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotlightDirection);glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);glMaterialfv(GL_FRONT, GL_SPECULAR, specularmat);
glMateriali(GL_FRONT, GL_SHININESS, 10);glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return 1;
}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;
case 'i':
glDisable(GL_LIGHTING);
break;
case 'k':
glEnable(GL_LIGHTING);
break;
case 27:
exit(0);
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);
glPushMatrix();
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();
glPopMatrix();
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)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
LightingEffect();
}void main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(50,50);
glutCreateWindow("color cube");init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);glutIdleFunc(spinCube);
glutKeyboardFunc(key);glutSpecialFunc(special_key);
glutMainLoop();
}