wisdiom 아니고 wisdom

[실습 05] 실습 04 변형 - 사각형 튀기기 본문

👩‍💻/OpenGL

[실습 05] 실습 04 변형 - 사각형 튀기기

글로랴 2021. 6. 30. 20:25
#include <GL/glut.h>  
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <Windows.h>



#define PI 3.14
//#define _cos( angle )  cos ( PI * angle / 180. )
//#define _sin( angle )  sin ( PI * angle / 180. )



GLvoid selectdraw(GLvoid);
GLvoid drawScene(GLvoid);
GLvoid Reshape(int w, int h);
GLvoid Mouse(int button, int state, int x, int y);
GLvoid Keyboard(int key, int x, int y);
void Timerfunction(int value);
void MenuFunc(int);

​

const int fps = 100;
const int Maxsqcount = 21;
int count = 0;
int SpeedMax = 0;
bool IsSquare = true; // 사각형이니

​

typedef struct sq
{
 int x = -10;
 int y = -10;
 int speedX, speedY;
 bool lie = false;
};



struct sq sq[21] = {};

​

void main(int argc, char *argv[])
{   //초기화 함수들 
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);  // 디스플레이 모드 설정
 glutInitWindowPosition(100, 100);    // 윈도우의 위치지정 
 glutInitWindowSize(800, 600);    // 윈도우의 크기 지정
 glutCreateWindow("Example_5");    // 윈도우 생성 (윈도우 이름)

​

 //메뉴
 int mainmenu;
 mainmenu = glutCreateMenu(MenuFunc);
 glutAddMenuEntry("Square", 1);
 glutAddMenuEntry("Circle", 2);
 glutAddMenuEntry("Exit", 3);
 glutAttachMenu(GLUT_RIGHT_BUTTON);

//

​

 glutDisplayFunc(drawScene); // 출력 함수의 지정
 glutTimerFunc(fps, Timerfunction, 1);
 glutMouseFunc(Mouse);
 glutSpecialFunc(Keyboard);
 glutReshapeFunc(Reshape);
 glutMainLoop();

}

​

GLvoid drawScene(GLvoid)
{
 glClearColor(1.0f, 1.0f, 1.0f, 1.0f);    // 바탕색을  흰 색으로 지정
 glClear(GL_COLOR_BUFFER_BIT);    // 설정된 색으로 전체를 칠하기


 for (int i = 0; i < Maxsqcount; i++)
 {
  if (IsSquare) // 사각형
  {
   if (sq[i].lie == true)
   {
    glColor4f(1.0f, 0.0f, 1.0f, 1.0f);    // 그리기 색을 분홍색으로 지정
    glRectf(sq[i].x - 10, sq[i].y - 20, sq[i].x + 10, sq[i].y + 20); //사각형 그리기
   }
   else
   {
    glColor4f(0.0f, 1.0f, 1.0f, 1.0f);    // 그리기 색을 하늘색으로 지정
    glRectf(sq[i].x - 20, sq[i].y - 10, sq[i].x + 20, sq[i].y + 10); //사각형 그리기
   }
  }

​
  else //원
  {
   glColor4f(1.0f, 1.0f, 0.0f, 1.0f);    // 그리기 색을 노랑색으로 지정

   glBegin(GL_POLYGON);
   for (int degree = 0; degree < 360; degree += 10)
   {
    float radian = (float)degree * 3.141592f / 180.f;
    glVertex2f(sq[i].x + cos(radian) * 10
     , sq[i].y + sin(radian) * 10);
   }
   glEnd();
  }
 }

​

 glutSwapBuffers(); // 화면에 출력하기
}


GLvoid Reshape(int w, int h)
{
 glViewport(0, 0, w, h);
 glOrtho(0.0, 800.0, 600.0, 0.0, -1.0, 1.0);
}

​

GLvoid Mouse(int button, int state, int x, int y)
{

 if (button == GLUT_LEFT_BUTTON && state == GLUT_LEFT)
 {
   sq[count].x = x;
   sq[count].y = y;
   sq[count].speedX = 1;
   sq[count].speedY = -1; // 오른쪽 위로 움직이게 값을 초기화;

​

   count++;

​

   printf("%d %d", x, y);

​

   if (count == Maxsqcount) // 초기화
   {
    for (int i = 0; i < 20; i++)
    {
     sq[i].x = -10;
     sq[i].y = -10;
    }
    count = 0;
   }



   if (SpeedMax < Maxsqcount) // 최고 속도 20
    SpeedMax++;
  }
  glutPostRedisplay();
}

​

void Timerfunction(int value)
{
  for (int i = 0; i < Maxsqcount; i++)
  {
   sq[i].x += sq[i].speedX; // x축으로 움직이기

​
   if (sq[i].x < 0 || sq[i].x>800) // 벽에 부딪히면
   {
    sq[i].speedX *= -1; // 반대로 움직이기
   }

​

   sq[i].y += sq[i].speedY; // y축으로 움직이기

​
   if (sq[i].y < 0 || sq[i].y>600)
   {
    sq[i].speedY *= -1;
   }

​
   sq[i].lie = !(sq[i].lie); // 참, 거짓이 계속 바뀜.

​
  }

​
 glutPostRedisplay(); //화면재출력
 glutTimerFunc(fps, Timerfunction, 1); // 타이머함수 재 설정
}

​

void MenuFunc(int button)
{
 switch (button)
 {
 case 1:
  IsSquare = true;
  break;
 case 2:
  IsSquare = false;
  break;
 case 3:
  exit(1);
 }
 glutPostRedisplay();
}

​

GLvoid Keyboard(int key, int x, int y)
{
 if (key == GLUT_KEY_DOWN) //속도 줄이기
 {
  if (IsSquare)
  {
   for (int i = 0; i < SpeedMax; i++)
   {
    if (sq[i].speedX > 1) // 최소 속도 1
    {
     if (sq[i].speedX > 0)
      sq[i].speedX--;

​
     else
      sq[i].speedX++;

​

     if (sq[i].speedY > 0)
      sq[i].speedY--;

​
     else
      sq[i].speedY++;
    }
   }
  }
 }

​

 else if (key == GLUT_KEY_UP) // 속도 증가
 {
  if (IsSquare)
  {
   for (int i = 0; i < SpeedMax; i++)
   {
    if (sq[i].speedX > 0)
     sq[i].speedX++;

​
    else
     sq[i].speedX--;

​

    if (sq[i].speedY > 0)
     sq[i].speedY++;

​
    else
     sq[i].speedY--;
   }
  }
 }
}
반응형
Comments