👩💻/OpenGL
[실습 21] RGB 컬러 모델 만들기
글로랴
2021. 6. 30. 20:40
#include <GL\glut.h>
void Drawscene();
void Timerfunc(int);
void Menufunc(int);
void Reshape(int, int);
static int fps = 100;
static GLfloat angleY = 0.0f;
static GLfloat posY_T = 60.0;
static GLfloat posZ_T = 50.0;
static GLfloat posY_F = 60.0;
static GLfloat posZ_F = 50.0;
bool Nothiding = false; // 은면제거 on
bool Culling = false; // 컬링 off
bool flat = false; // smooth on
bool top_open = false; // 윗면 닫힘
bool front_open = false; // 앞면 닫힘
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(300, 100);
glutInitWindowSize(800,600);
glutCreateWindow("컴그_실습21");
glutDisplayFunc(Drawscene);
glutReshapeFunc(Reshape);
glutTimerFunc(fps, Timerfunc, 1);
//메뉴
int menu = glutCreateMenu(Menufunc);
glutAddMenuEntry("은면 제거 ON", 1);
glutAddMenuEntry("은면 제거 OFF", 2);
glutAddMenuEntry("컬링 ON", 3);
glutAddMenuEntry("컬링 OFF", 4);
glutAddMenuEntry("쉐이딩 FLAT", 5);
glutAddMenuEntry("쉐이딩 SMOOTH", 6);
glutAddMenuEntry("윗면 yes ON", 7);
glutAddMenuEntry("윗면 no OFF", 8);
glutAddMenuEntry("앞면 yes ON", 9);
glutAddMenuEntry("앞면 no OFF", 10);
glutAttachMenu(GLUT_RIGHT_BUTTON);
//
glutMainLoop();
}
void Reshape(int w, int h)
{
// 뷰포트 변환 설정
glViewport(0, 0, w, h);
// 투영 행렬 스택 재설정
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// 원근투영
gluPerspective(60.0f, w / h, 1.0, 1000.0); // 원근 거리
glTranslatef(0.0, 0.0, -300.0);
glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
//모델 뷰 행렬 스택 재설정
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glViewport(0, 0, w, h);
}
void Timerfunc(int value)
{
// 뒷면 애니메이션
if (top_open == true)
{
posY_T += 1.2;
posZ_T -= 1.0;
if (posY_T >= 120 && posZ_T <= -100)
{
posY_T = 120;
posZ_T = -100;
}
}
else
{
posY_T = 60.0;
posZ_T = 50.0;
}
// 앞면 애니메이션
if (front_open == true)
{
posY_F -= 2.0;
posZ_F += 1.0;
if (posY_F <= -60 && posZ_F >= 120)
{
posY_F = -60.0;
posZ_F = 120.0;
}
}
else
{
posY_F = 60.0;
posZ_F = 50.0;
}
angleY += 1;
glutPostRedisplay();
glutTimerFunc(fps, Timerfunc, 1);
}
void Drawscene()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glLoadIdentity();
glRotatef(angleY, 0.0f, 1.0f, 0.0f);
// shading
if (flat == true)
{
glShadeModel(GL_FLAT);
}
else
{
glShadeModel(GL_SMOOTH); // 쉐이딩_스무딩
}
// 은면제거_깊이
if (Nothiding == true)
{
glDisable(GL_DEPTH_TEST);
}
else
{
glEnable(GL_DEPTH_TEST); // 은면제거_깊이검사
}
//은면제거_컬링
if (Culling == true)
{
glEnable(GL_CULL_FACE); // 은면제거_뒷면제거 (내부는 잘라낸다.)
}
else
{
glDisable(GL_CULL_FACE);
}
glFrontFace(GL_CCW); // 반시계 방향을 앞면
glBegin(GL_QUADS);
{
//앞면
glColor3f(0.0f, 1.0f, 1.0f); // Cyan
glVertex3f(50.0, posY_F, posZ_F);
glColor3f(1.0f, 1.0, 1.0f); // 흰색
glVertex3f(-50.0, posY_F, posZ_F);
glColor3f(1.0f, 0.0f, 1.0f); // 마젠타
glVertex3f(-50.0, -60.0, 50.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(50.0, -60.0, 50.0f);
//뒷면
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(50.0, 60.0, -50.0);
glColor3f(1.0f, 1.0f, 0.0f); // 노란색
glVertex3f(-50.0, 60.0, -50.0);
glColor3f(1.0f, 0.0f, 0.0f); // 빨간색
glVertex3f(-50.0, -60.0, -50.0);
glColor3f(0.0f, 0.0f, 0.0f); // 검은색
glVertex3f(50.0f, -60.0f, -50.0f);
//윗면
glColor3f(0.0f, 1.0f, 1.0f); // 민트
glVertex3f(50.0, posY_T, posZ_T);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(50.0, 60.0, -50.0);
glColor3f(1.0f, 1.0f, 0.0f); // 노란색
glVertex3f(-50.0, 60.0, -50.0);
glColor3f(1.0f, 1.0, 1.0f); // 흰색
glVertex3f(-50.0, posY_T, posZ_T);
//아랫면
glColor3f(0.0f, 0.0f, 0.0f); // 검은색
glVertex3f(50.0f, -60.0f, -50.0f);
glColor3f(1.0f, 0.0f, 0.0f); // 빨간색
glVertex3f(-50.0, -60.0, -50.0);
glColor3f(1.0f, 0.0f, 1.0f); // 마젠타
glVertex3f(-50.0, -60.0, 50.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(50.0, -60.0, 50.0f);
//왼면
glColor3f(1.0f, 1.0f, 1.0f); // White
glVertex3f(-50.0, 60.0, 50.0f);
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f(-50.0, 60.0, -50.0);
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(-50.0, -60.0, -50.0);
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(-50.0, -60.0, 50.0f);
//오른면
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(50.0, 60.0, -50.0);
glColor3f(0.0f, 1.0f, 1.0f); // Cyan
glVertex3f(50.0, 60.0, 50.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(50.0, -60.0, 50.0f);
glColor3f(0.0f, 0.0f, 0.0f); // Black
glVertex3f(50.0f, -60.0f, -50.0f);
}
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void Menufunc(int button)
{
switch (button)
{
case 1:
Nothiding = false;
break;
case 2:
Nothiding = true;
break;
case 3:
Culling = true;
break;
case 4:
Culling = false;
break;
case 5:
flat = true;
break;
case 6:
flat = false;
break;
case 7:
top_open = false;
break;
case 8:
top_open = true;
break;
case 9:
front_open = false;
break;
case 10:
front_open = true;
break;
default:
break;
}
glutPostRedisplay();
}
반응형