wisdiom 아니고 wisdom

[실습 18] 실습 16번과 17번 합치기 (미완성) 본문

👩‍💻/OpenGL

[실습 18] 실습 16번과 17번 합치기 (미완성)

글로랴 2021. 6. 30. 20:40

🚨🔥 구와 육면체 충돌체크 필요

 

#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);
void MyTimer(int);



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;

static GLfloat craneX = 0.0f;
static GLfloat craneZ = 0.0f;



bool plus = false;

const int fps = 1000;



int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
 glutInitWindowPosition(300, 100);
 glutInitWindowSize(800, 600);
 glutCreateWindow("컴그_실습18");
 glutKeyboardFunc(Keyboard);
 glutDisplayFunc(drawscene);
 glutReshapeFunc(Reshape);
 glutTimerFunc(fps, MyTimer, 1);
 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, pos_Z); // 투영 공간을 화면 안쪽으로 이동하여 시야를 확보.

 glRotatef(angleY, 0.0, 1.0, 0.0);
 //모델 뷰 행렬 스택 재설정
 glMatrixMode(GL_MODELVIEW);
 glViewport(0, 0, w, h);
}



void Keyboard(unsigned char key, int x, int y)
{
 switch (key)
 {
 case 'a': // X축회전
  direcX -= 1;
  angleX -= 0.1;
  if (direcX <= -100)
  {
   direcX = -100;
  }
  break;
 case 'd': // X축회전
  direcX += 1;
  angleX += 0.1;
  if (direcX >= 100)
  {
   direcX = 100;
  }
  break;
 case 'y': // Y축회전
  angleY += 10;
  break;
 case'w': // Z축회전
  direcZ -= 1;
  angleZ -= 0.5;
  if (direcZ <= -100)
  {
   direcZ = -100;
  }
  break;
 case's': // 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(0.8, 0.8, 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();
  glLineWidth(1);
  glColor3f(1.0f, 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);

  glTranslatef(0.0, 0.0, direcZ);
  //glRotatef(angleZ, 0.0, 0.0, 1.0);

  glutWireCube(20);
  glutSolidSphere(R, 20, 20);
 glPopMatrix();

 // 크레인
 glPushMatrix();
 {
  glColor3f(1.0f, 0.0f, 0.0f);
  glTranslatef(80.0, -50.0, -50.0);

  glTranslatef(craneX, 0.0, 0.0);
  //glTranslatef(0.0, 0.0, craneZ);
  //glScalef(2.0, 1.0, 2.0);
  glutSolidCube(20);

  glPushMatrix();
  {
   glColor3f(0.0f, 1.0f, 0.0f);
   glTranslatef(0.0, 30.0, 0.0);
   glScalef(0.5, 4.0, 0.5);
   glutSolidCube(10);

   glPushMatrix();
   {
    glColor3f(0.0f, 1.0f, 1.0f);
    glTranslatef(0.0, 8.5, 0.0);
    glScalef(0.3, 0.7, 0.3);
    glutSolidCube(10);
   }
   glPopMatrix();
  }
  glPopMatrix();
 }
 glPopMatrix();

 glutSwapBuffers();
}



void MyTimer(int Value) {
 
 if (plus == true)
 {
  craneX += 0.1;

  if (craneX >= 10)
  {
   plus = false;
  }
 }
 else
 {
  craneX -= 0.1;

  if (craneX <= -170)
  {
   plus = true;
  }
 }

 glutPostRedisplay();
 glutTimerFunc(10, MyTimer, 1);
}
반응형
Comments