-
#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}};void init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}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 cube()
{/*
glBegin(GL_LINE_LOOP);
//glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 1.0f);
glEnd();glBegin(GL_LINE_LOOP);
//glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glEnd();glBegin(GL_LINES);
//glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 1.0f);//glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glEnd();*/
//color cube 만들기
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);glColor3f(1.0f, 1.0f, 0.5f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glEnd();}
void coordinateaxis()
{
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 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 display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glLookAt(-5.0f, 5.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);glTranslatef(0.0f, 0.0f, -10.0f);
glRotatef(-30.0f, 0.0f, 1.0f, 0.0f);
glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
cube();
coordinateaxis();
glFlush();
}void main(int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0,0);
glutCreateWindow("Cube 그리기");glutReshapeFunc(reshape);
glutDisplayFunc(display);
init();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}