wisdiom 아니고 wisdom

[실습 23] 회전하면 내려오는 토러스 그리기 (미완성) 본문

👩‍💻/OpenGL

[실습 23] 회전하면 내려오는 토러스 그리기 (미완성)

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

🚨🔥 내려올 때,  훌라후프처럼 회전이 안됨 + 바닥에 닿으면 그 다음 토러스가 안 내려옴

 

#include <GL\glut.h>
#include <math.h>



#define bottom 0
#define top 1



void drawscene();
void Reshape(int w, int h);
void Keyboard(unsigned char key, int x, int y);
void Timerfunction(int value);

​

const int fps = 100;
static int count = 0;
static int Maxcount = 10;

​

typedef struct T10
{
 float posY = 20.0;
 float posZ = 30.0;
 //float dirY;
 float angleY;
};

​

bool whPos = false;
bool couple = false;

​

struct T10 T[10] = {};

​

int main()
{
 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
 glutInitWindowPosition(300, 100);
 glutInitWindowSize(800, 600);
 glutCreateWindow("컴그_실습23");
 glutKeyboardFunc(Keyboard);
 glutDisplayFunc(drawscene);
 glutReshapeFunc(Reshape);
 glutTimerFunc(fps, Timerfunction, 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, -80.0);

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

​

void Keyboard(unsigned char key, int x, int y)
{
 switch (key)
 {
 default:
  break;
 }
 Reshape(800, 600);
 glutPostRedisplay();
}

​

void drawscene()
{
 // 초기화
 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glEnable(GL_DEPTH_TEST);
 glEnable(GL_CULL_FACE);
 glFrontFace(GL_CCW);

 // --- XZ축 바닥
 glColor3f(0.8, 1.0, 0.3);
 glBegin(GL_POLYGON);
 glVertex3f(-50.0, -20.0, 50.0);
 glVertex3f(50.0, -20.0, 50.0);
 glVertex3f(50.0, -20.0, -50.0);
 glVertex3f(-50.0, -20.0, -50.0);
 glEnd();

 //  막대기
 glPushMatrix();
 {
  glTranslatef(0.0f, -20.0f, 30.0f);
  glBegin(GL_LINES);
  {
   glColor3f(1.0f, 0.0f, 0.0f);
   glVertex3f(0.0f, 0.0f, 0.0f);
   glVertex3f(0.0f, 40.0f, 0.0f);
  }
  glEnd();
 }
 glPopMatrix();

 // 토러스
 for (int i = 0; i < Maxcount; i++)
 {
  glPushMatrix();
  {
   glTranslatef(0.0, T[i].posY, T[i].posZ);

   glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
   glutWireTorus(1.0, 5.0, 50.0, 50.0);
  }
  glPopMatrix();
 }
 

 glutSwapBuffers();
}

​

void Timerfunction(int value)
{
 for (int i = 0; i < Maxcount; i++)
 {
   //T[i].angleY += 1;
   T[i].posY -= 1;

   if (T[i].posY <= -18.0)
   {
    T[i].posY = -18.0;
   }
  
 }
 glutPostRedisplay();
 glutTimerFunc(fps, Timerfunction, 1);
}
반응형
Comments