👩💻/OpenGL
[실습 14] 도형 2개 그리기 및 변환하기
글로랴
2021. 6. 30. 20:31
#include <GL/glut.h>
#include <stdlib.h>
void drawscene();
void Reshape(int w, int h);
void Keyboard(unsigned char key, int x, int y);
void MenuFunc(int);
void Timerfunction(int value);
static GLfloat angleX = 0.0f;
static GLfloat angleY = 0.0f;
static GLfloat angleZ = 0.0f;
static GLfloat angleR = 0.0f;
bool turn_L = false;
bool turn_R = false;
const int fps = 1;
int whatshape = 0; //1.육면체 2.원뿔 3.구 4.주전자
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA|GLUT_DEPTH);
glutInitWindowPosition(300, 100);
glutInitWindowSize(800, 600);
glutCreateWindow("컴그_실습14");
glutKeyboardFunc(Keyboard);
glutDisplayFunc(drawscene);
glutReshapeFunc(Reshape);
// 메뉴
int mainmenu;
mainmenu = glutCreateMenu(MenuFunc);
glutAddMenuEntry("Square", 1);
glutAddMenuEntry("Cone", 2);
glutAddMenuEntry("Sphere", 3);
glutAddMenuEntry("Teapot", 4);
glutAddMenuEntry("Exit", 5);
//
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutTimerFunc(fps, Timerfunction, 1);
glutMainLoop();
}
void Reshape(int w, int h)
{
//GLfloat nRange = 800.0f;
// 뷰포트 변환 설정
glViewport(0, 0, w, h);
// 투영 행렬 스택 재설정
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// 원근투영
gluPerspective(80.0f, w / h, 1.0, 600.0); // 원근 거리
glTranslatef(0.0, 0.0, -195.0); // 어느 정도 거리에서 볼 것인가. (아마 카메라..?)
//모델 뷰 행렬 스택 재설정
glMatrixMode(GL_MODELVIEW);
glRotatef(angleX, 1.0, 0.0, 0.0);
glRotatef(angleY, 0.0, 1.0, 0.0);
glRotatef(angleZ, 0.0, 0.0, 1.0);
// 카메라 *관측 변환: 카메라의 위치 설정 (필요한 경우, 다른 곳에 설정 가능)
gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);
glViewport(0, 0, w, h);
//glLoadIdentity();
}
void Keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'x': // X축회전
angleY = 0.0;
angleZ = 0.0;
angleR = 0.0;
angleX += 0.1;
break;
case 'y': // Y축회전
angleX = 0.0;
angleZ = 0.0;
angleR = 0.0;
angleY += 0.1;
break;
case'z': // Z축회전
angleX = 0.0;
angleY = 0.0;
angleR = 0.0;
angleZ += 0.1;
break;
case 'i': //초기화
angleX = 0.0f;
angleY = 0.0f;
angleZ = 0.0f;
angleR = 0.0f;
turn_L = false;
turn_R = false;
glLoadIdentity();
break;
case 'w': // 바닥은 x축회전, 도형은 y축회전
angleX += 0.1;
turn_L = true;
turn_R = true;
break;
case 'l': //L(왼쪽 도형)만 제자리에서 Y축 회전
turn_L = true;
break;
case 'r': //R(오른쪽 도형)만 제자리에서 Y축 회전
turn_R = true;
break;
default:
break;
}
Reshape(800, 600);
glutPostRedisplay();
}
void drawscene()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // 바탕색 흰 색
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// 초기화
// --- XZ축 바닥
glColor3f(0.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(-70.0, -20.0, 70.0);
glVertex3f(70.0, -20.0, 70.0);
glVertex3f(70.0, -20.0, -70.0);
glVertex3f(-70.0, -20.0, -70.0);
glEnd();
glPushMatrix();
glTranslatef(0.0, 10.0, 0.0);
glLineWidth(3);
glBegin(GL_LINES);
//X축
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(8.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
//Y축
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, 8.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
//Z축
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 8.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
glPopMatrix();
// 육면체
if (whatshape == 1) {
glLineWidth(1);
glPushMatrix();
glColor3f(1.0, 1.0, 0.0);
glTranslatef(-25.0, -10.0, -10.0);
glScalef(1.0, 2.0, 1.0);
if (turn_L == true)
{
glRotatef(angleR, 0.0, 1.0, 0.0);
}
glutSolidCube(20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 0.0);
glTranslatef(25.0, -10.0, -5.0);
glScalef(2.0, 1.0, 1.0);
if (turn_R == true)
{
glRotatef(angleR, 0.0, 1.0, 0.0);
}
glutWireCube(20);
glPopMatrix();
}
//원뿔
else if (whatshape == 2)
{
glLineWidth(1);
glPushMatrix();
glColor3f(1.0f, 0.0f, 0.5f);
glRotatef(270.0, 1.0, 0.0, 0.0);
glTranslatef(-25.0, -10.0, -10.0);
if (turn_L == true)
{
glRotatef(angleR, 0.0, 1.0, 0.0);
}
glutSolidCone(15.0, 40.0, 5.0, 5.0);
glPopMatrix();
glPushMatrix();
glColor3f(1.0f, 0.0f, 0.5f);
glRotatef(270.0, 1.0, 0.0, 0.0);
glTranslatef(25.0, -10.0, -10.0);
if (turn_R == true)
{
glRotatef(angleR, 0.0, 1.0, 0.0);
}
glutWireCone(15.0, 40.0, 5.0, 5.0);
glPopMatrix();
}
//구
else if (whatshape == 3)
{
glLineWidth(1);
glPushMatrix();
glColor3f(1.0f, 0.5f, 0.0f);
glTranslatef(-25.0, -10.0, -10.0);
if (turn_L == true)
{
glRotatef(angleR, 0.0, 1.0, 0.0);
}
glutSolidSphere(15.0, 50.0, 50.0);
glPopMatrix();
glPushMatrix();
glColor3f(1.0f, 0.5f, 0.0f);
glTranslatef(25.0, -10.0, -10.0);
if (turn_R == true)
{
glRotatef(angleR, 0.0, 1.0, 0.0);
}
glutWireSphere(15.0, 10.0, 10.0);
glPopMatrix();
}
//주전자
else if (whatshape == 4) {
glLineWidth(1);
glPushMatrix();
glColor3f(0.5f, 0.5f, 1.0f);
glTranslatef(-30.0, -10.0, -10.0);
if (turn_L == true)
{
glRotatef(angleR, 0.0, 1.0, 0.0);
}
glutSolidTeapot(20);
glPopMatrix();
glPushMatrix();
glColor3f(0.5f, 0.5f, 1.0f);
glTranslatef(30.0, -10.0, -10.0);
if (turn_R == true)
{
glRotatef(angleR, 0.0, 1.0, 0.0);
}
glutWireTeapot(20);
glPopMatrix();
}
else
{
;
}
glutSwapBuffers();
}
void Timerfunction(int value)
{
angleR += 0.1;
glutPostRedisplay();
glutTimerFunc(fps,Timerfunction,1);
}
void MenuFunc(int button)
{
switch (button)
{
case 1:
whatshape = 1;
break;
case 2:
whatshape = 2;
break;
case 3:
whatshape = 3;
break;
case 4:
whatshape = 4;
break;
case 5:
exit(1);
break;
default:
break;
}
glutPostRedisplay();
}
🚨 원뿔 회전 수정 필요
반응형