OpenGL - 로봇 팔 만들기
설명
로봇의 팔을 만들어서 회전하고 꺽을수 있다.
이번에도 마찬가지로 소스에 대한 설명은 주석을 참조하세요
또한 생략된 소스는 맨 앞의 글을 참조하세요
2017/09/12 - [SW/OpenGL] - OpenGL - 사각형 회전, 색바꾸기
로봇의 base
void base(void) { glPushMatrix();//push { glColor3f(1, 0, 0);//red glTranslatef(0, -0.8, 0);//y axis glRotatef(-90, 1, 0, 0);//x axis glRotatef(Base_Angle, 0, 0, 1);//z axis gluCylinder(p, 0.5, 0.5, 0.3, 20, 1);//draw cylinder } glPopMatrix();//pop }
로봇의 lower
/// <summary> /// arm's lower /// </summary> void lower_arm(void) { glPushMatrix();//push { glColor3f(0, 1, 0);//green glRotatef(Base_Angle, 0, 1, 0);//rotate with y axis glTranslatef(0, -0.7, 0);//translate with axis glRotatef(Lower_Arm_Angle, 0, 0, 1);//rotate with z axis glTranslatef(0, 0.5, 0);//translate with y axis glScalef(0.2, 1, 0.2);//scaling glutWireCube(1);//draw cube } glPopMatrix();//pop }
로봇의 upper
/// <summary> /// arm's upper /// </summary> void upper_arm(void) { glPushMatrix(); //push { glColor3f(0, 0, 1); // blue glRotatef(Base_Angle, 0, 1, 0); //rotate y axis glTranslatef(0, -0.7, 0);//translate y axis glRotatef(Lower_Arm_Angle, 0, 0, 1);//rotate z axis glTranslatef(0, 1.0, 0);//translate y axis glRotatef(Upper_Arm_Angle, 0, 0, 1);//rotate z axis glTranslatef(0, 0.4, 0);//translate y axis glScalef(0.2, 0.8, 0.2);//scaling glutWireCube(1);//draw cube } glPopMatrix();//pop }
렌더링 함수
void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.5, 0.5, 0.5, 0, 0, 0, 0, 1, 0);//setting camera viewpoint base(); //draw base lower_arm(); //draw lower part upper_arm(); //draw upper part glutSwapBuffers(); }
스페셜 키 이벤트 (화살표4개, home, end)
/// <summary> /// Special key event /// </summary> /// <param name="key">Special key.</param> /// <param name="x">The x</param> /// <param name="y">The y</param> void specialkeys(int key, int x, int y) { switch (key) { case GLUT_KEY_UP: //up arrow Lower_Arm_Angle += 2; break; case GLUT_KEY_DOWN: //down arrow Lower_Arm_Angle -= 2; break; case GLUT_KEY_LEFT: //left arrow Base_Angle -= 2; break; case GLUT_KEY_RIGHT: //right arrow Base_Angle += 2; break; case GLUT_KEY_HOME: //home key Upper_Arm_Angle += 2; break; case GLUT_KEY_END: //end key Upper_Arm_Angle -= 2; break; default: break; } }
메인함수
void main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(500, 500); glutCreateWindow("Simple"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutSpecialFunc(specialkeys); //Special key event glutMouseFunc(mouseButton); glutMotionFunc(mouseMotion); glutTimerFunc(1000 / 30, timer, 1); glutKeyboardFunc(keyboard); init(); SetupRc(); glutMainLoop(); }
타이머 함수
void timer(int value) { if (rot) { theta += 2.0; if (theta >= 360.0) theta -= 360.0; } else { theta -= 2.0; if (theta <= 360.0) theta += 360.0; } glutTimerFunc(1000 / 30, timer, 1); glutPostRedisplay(); }
결과
'SW > OpenGL' 카테고리의 다른 글
OpenGL - 쉐이딩(Shading, line, flat smooth) (0) | 2017.09.13 |
---|---|
OpenGL - 16개의 움직이는 랜덤 티팟 (0) | 2017.09.12 |
OpenGL - 정육면체 xyz축 회전 (0) | 2017.09.12 |
OpenGL - 3점을 찍어 삼각형 그리기 (0) | 2017.09.12 |
OpenGL - 사각형 회전, 색바꾸기 (0) | 2017.09.12 |