OpenGL - 로봇 팔 만들기

설명

로봇의 팔을 만들어서 회전하고 꺽을수 있다.


이번에도 마찬가지로 소스에 대한 설명은 주석을 참조하세요

또한 생략된 소스는 맨 앞의 글을 참조하세요

2017/09/12 - [SW/OpenGL] - OpenGL - 사각형 회전, 색바꾸기



로봇의 base

void base(void)
{
	glPushMatrix();//push
	{
		glColor3f(100);//red
		glTranslatef(0, -0.80);//y axis
		glRotatef(-90100);//x axis
		glRotatef(Base_Angle001);//z axis
		gluCylinder(p0.50.50.3201);//draw cylinder
	}
	glPopMatrix();//pop
}


로봇의 lower

/// <summary>
/// arm's lower
/// </summary>
void lower_arm(void)
{
	glPushMatrix();//push
	{
		glColor3f(010);//green
		glRotatef(Base_Angle010);//rotate with y axis
		glTranslatef(0, -0.70);//translate with axis
		glRotatef(Lower_Arm_Angle001);//rotate with z axis
		glTranslatef(00.50);//translate with y axis
		glScalef(0.210.2);//scaling
		glutWireCube(1);//draw cube
	}
	glPopMatrix();//pop
}


로봇의 upper

/// <summary>
/// arm's upper
/// </summary>
void upper_arm(void)
{
	glPushMatrix(); //push
	{
		glColor3f(001); // blue
		glRotatef(Base_Angle010); //rotate y axis
		glTranslatef(0, -0.70);//translate y axis
		glRotatef(Lower_Arm_Angle001);//rotate z axis
		glTranslatef(01.00);//translate y axis
		glRotatef(Upper_Arm_Angle001);//rotate z axis
		glTranslatef(00.40);//translate y axis
		glScalef(0.20.80.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.50.50.5000010);//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 keyint xint 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 argccharargv[])
{
	glutInit(&argcargv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(500500);
	glutCreateWindow("Simple");
	glutDisplayFunc(RenderScene);
	glutReshapeFunc(ChangeSize);
	glutSpecialFunc(specialkeys); //Special key event
	glutMouseFunc(mouseButton);
	glutMotionFunc(mouseMotion);
	glutTimerFunc(1000 / 30timer1);
	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 / 30timer1);
	glutPostRedisplay();
}


결과