👩💻/OpenGL
[실습 16] 구르는 공 그리기
글로랴
2021. 6. 30. 20:34
#include <GL/glut.h>
#include <stdlib.h>
#define PI 3.141592
#define R 10
// 원 둘레 = 2*PI*R = 63
// 호의 길이 = R*angle = 1
void drawscene();
void Reshape(int w, int h);
void Keyboard(unsigned char key, int x, int y);
static GLfloat direcX = 0.0f;
static GLfloat direcZ = 0.0f;
static GLfloat angleX = 0.0f;
static GLfloat angleY = 0.0f;
static GLfloat angleZ = 0.0f;
static GLfloat pos_Z = -210.0;
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(300, 100);
glutInitWindowSize(800, 600);
glutCreateWindow("컴그_실습16");
glutKeyboardFunc(Keyboard);
glutDisplayFunc(drawscene);
glutReshapeFunc(Reshape);
glutMainLoop();
}
void Reshape(int w, int h)
{
//GLfloat nRange = 800.0f;
// 뷰포트 변환 설정
glViewport(0, 0, w, h);
// 투영 행렬 스택 재설정
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// 원근투영
gluPerspective(60.0f, w / h, 1.0, 1000.0); // 원근 거리
glTranslatef(0.0, 0.0, pos_Z); // 투영 공간을 화면 안쪽으로 이동하여 시야를 확보.
//모델 뷰 행렬 스택 재설정
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, w, h);
//glLoadIdentity();
}
void Keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'x': // X축회전
direcX += 1;
angleX += 0.1;
if (direcX >= 100)
{
direcX = 100;
}
break;
case 'y': // Y축회전
angleY += 10;
break;
case'z': // Z축회전
direcZ += 1;
angleZ += 0.5;
if (direcZ >= 75)
{
direcZ = 75;
}
break;
case 'i': //초기화
direcX = 0.0f;
direcZ = 0.0f;
angleX = 0.0f;
angleY = 0.0f;
angleZ = 0.0f;
pos_Z = -210.0;
glLoadIdentity();
break;
case '+':
pos_Z += 10;
break;
case '-':
pos_Z -= 10;
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(1.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(-100.0, -50.0, 100.0);
glVertex3f(100.0, -50.0, 100.0);
glVertex3f(100.0, -50.0, -100.0);
glVertex3f(-100.0, -50.0, -100.0);
glEnd();
// 좌표축
glPushMatrix();
glTranslatef(0.0, -60.0, 0.0);
glLineWidth(3);
glBegin(GL_LINES);
glColor3f(1.0, .0, 0.0);
glVertex3f(30.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 30.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 30.0);
glVertex3f(0.0, 0.0, 0.0);
glEnd();
glPopMatrix();
// 구
glPushMatrix();
glLineWidth(1);
glColor3f(0.3f, 0.0f, 1.0f);
glTranslatef(0.0f, -50.0f, 0.0f);
glTranslatef(direcX, 0.0, 0.0);
glRotatef(angleX, 1.0, 0.0, 0.0);
glRotatef(angleY, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, direcZ);
glRotatef(angleZ, 0.0, 0.0, 1.0);
glutWireSphere(R, 20, 20);
glPopMatrix();
glutSwapBuffers();
}
반응형