2019年2月24日 星期日

多軸機械手臂簡單模型建立

尚未熟悉glTranslatef及glRotatef的座標變化技巧,因此利用參數調整方式作出一個簡單的機械手臂模型,之後有機會再慢慢更新,程式碼如下:

#include <gl/glut.h>
#include <iostream>

using namespace std;

//各軸旋轉角度變數
static int Axis1theta = 0;
static int Axis2theta = 0;
static int Axis3theta = 0;
static int Axis4theta = 0;
static int Axis5theta = 0;
static int Axis6theta = 0;
//座標轉換用連桿長,之後再調整測試,目前還沒用到
static int h1 = 1;
static int h2 = 1;
static int h3 = 1;
static int h4 = 1;
static int h5 = 1;
static int h6 = 1;

以下為設定各軸的副程式
void Axis1()
{
glPushMatrix();
glTranslatef(0.0, 0.5, 0.0);
glScalef(1.0, 0.5, 1.0); //尺寸
glColor3f(0.0, 1.0, 0.0);
glutSolidCube(1.0);
glPopMatrix();
}


void Axis2()
{
glPushMatrix();
glTranslatef(0.0, 1.0, 0.0);
glScalef(0.5, 2.0, 0.5);
glColor3f(1.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix();
}

void Axis3()
{
glPushMatrix();
glTranslatef(0.0, 1.0, 0.0);
glScalef(0.5,2.0, 0.5);
glColor3f(0.0, 0.0, 1.0);
glutWireCube(1.0);
glPopMatrix();
}

void Axis4()//白
{
glPushMatrix();
glTranslatef(0.0, 1.5, 0.0);
glScalef(0.5, 1.0, 0.5);
glColor3f(1.0, 1.0, 1.0);
glutWireCube(1.0);
glPopMatrix();
}

void Axis5()//綠框
{
glPushMatrix();
glTranslatef(0.0, 1.0, 0.0);
glScalef(0.5, 2.0, 0.5);
glColor3f(0, 1.0, 0);
glutWireCube(1.0);
glPopMatrix();
}

void Axis6()
{
glPushMatrix();
glTranslatef(0.0, 1.5, 0.0);
glScalef(0.5, 1.0, 0.5);
glColor3f(0.0, 0.0, 1.0);
glutWireCube(1.0);
glPopMatrix();
}

//初始化程式
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
}
//顯示圖形程式
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();

glRotatef(Axis1theta, 0.0, 1.0, 0.0);
Axis1();
glTranslatef(0.0, 0.8, 0.0);
glRotatef(Axis2theta, 0.0, 0.0, 1.0);
Axis2();
glTranslatef(0.0, 2.0, 0.0);
glRotatef(Axis3theta, 0.0, 0.0, 1.0);
Axis3();
glTranslatef(0.0, 1.0, 0.0);
glRotatef(Axis4theta, 0.0, 1.0, 0.0);
Axis4();
glTranslatef(0.0, 2, 0.0);
glRotatef(Axis5theta, 0.0, 0.0, 1.0);
Axis5();
glTranslatef(0.0, 1.0, 0.0);
glRotatef(Axis6theta, 0.0, 1.0, 0.0);
Axis6();
//glTranslatef(0.0, 0.5, 0.0);

glPopMatrix();
glutSwapBuffers();
}
//這段程式碼還沒熟悉
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, -3.0, -5.0);
}
//設定鍵盤控制各軸旋轉
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'a':
Axis1theta = (Axis1theta + 5) % 360;
glutPostRedisplay();
break;

case 'z':
Axis1theta = (Axis1theta - 5) % 360;
glutPostRedisplay();
break;

case 's':
Axis2theta = (Axis2theta + 5) % 360;
glutPostRedisplay();
break;

case 'x':
Axis2theta = (Axis2theta - 5) % 360;
glutPostRedisplay();
break;
case 'd':
Axis3theta = (Axis3theta + 5) % 360;
glutPostRedisplay();
break;

case 'c':
Axis3theta = (Axis3theta - 5) % 360;
glutPostRedisplay();
break;
case 'f':
Axis4theta = (Axis4theta + 5) % 360;
glutPostRedisplay();
break;
case 'v':
Axis4theta = (Axis4theta - 5) % 360;
glutPostRedisplay();
break;
case 'g':
Axis5theta = (Axis5theta + 5) % 360;
glutPostRedisplay();
break;
case 'b':
Axis5theta = (Axis5theta - 5) % 360;
glutPostRedisplay();
break;
case 'h':
Axis6theta = (Axis6theta + 5) % 360;
glutPostRedisplay();
break;
case 'n':
Axis6theta = (Axis6theta - 5) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
//主程式
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 400);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();

return 0;
}

沒有留言:

張貼留言