2019年5月2日 星期四

瞎78打-8

( 1 ) 小考 : T - R - T 轉動

( 2 ) 主題 : 階層轉動 骨架

( 3 ) 主題 : 鍵盤按鍵

----------------------------------------------------------------------------------

 加入手臂的程式碼(用滑鼠來動)

#include <GL/glut.h>
float angle=0;
 void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///茶壺身體
    glColor3f(255,255,255);
        glutSolidTeapot(0.3);///大小
    glPopMatrix();
    glPushMatrix();///右手臂
        glTranslated(0.39,0,0); //(3) 把它掛在你要放的地方
        glRotatef(angle, 0,0,1);  // (1) 先有轉動,但怪怪的
        glTranslated(0.28,0,0);//(2) 要更早去移動旋轉中心

        glColor3f(255,0,0);
        glutSolidTeapot(0.2);

            glPushMatrix();///接右手肘
            glTranslated(0.30,0,0); //(3) 把它掛在你要放的地方
            glRotatef(angle, 0,0,1);  // (1) 先有轉動,但怪怪的
            glTranslated(0.28,0,0);//(2) 要更早去移動旋轉中心

            glColor3f(255,0,0);
            glutSolidTeapot(0.2);
            glPopMatrix();
    glPopMatrix();


    glPushMatrix();///左手臂
        glTranslated(-0.39,0,0); //(3) 把它掛在你要放的地方
        glRotatef(angle, 0,0,1);  // (1) 先有轉動,但怪怪的
        glTranslated(-0.28,0,0);//(2) 要更早去移動旋轉中心

        glColor3f(255,0,0);
        glutSolidTeapot(0.2);

            glPushMatrix();///接左手肘
            glTranslated(-0.30,0,0); //(3) 把它掛在你要放的地方
            glRotatef(angle, 0,0,1);  // (1) 先有轉動,但怪怪的
            glTranslated(-0.28,0,0);//(2) 要更早去移動旋轉中心

            glColor3f(255,0,0);
            glutSolidTeapot(0.2);
            glPopMatrix();
    glPopMatrix();
    angle++;
   glutSwapBuffers();
}
void motion(int x,int y) //加入滑鼠控制
{
    angle=x;
    display();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMotionFunc(motion); //使用滑鼠的函式
    glutMainLoop();
}



加入鍵盤控制旋轉軸

#include <GL/glut.h>
float angle[20];//滑鼠變數用陣列命名
int jointID=1;
 void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///茶壺身體
    glColor3f(255,255,255);
        glutSolidTeapot(0.3);///大小
    glPopMatrix();
    glPushMatrix();///右手臂
        glTranslated(0.39,0,0); //(3) 把它掛在你要放的地方
        glRotatef(angle[1], 0,0,1);  // (1) 先有轉動,但怪怪的
        glTranslated(0.28,0,0);//(2) 要更早去移動旋轉中心

        glColor3f(255,0,0);
        glutSolidTeapot(0.2);

            glPushMatrix();///接右手肘
            glTranslated(0.30,0,0); //(3) 把它掛在你要放的地方
            glRotatef(angle[2], 0,0,1);  // (1) 先有轉動,但怪怪的
            glTranslated(0.28,0,0);//(2) 要更早去移動旋轉中心

            glColor3f(255,0,0);
            glutSolidTeapot(0.2);
            glPopMatrix();
    glPopMatrix();


    glPushMatrix();///左手臂
        glTranslated(-0.39,0,0); //(3) 把它掛在你要放的地方
        glRotatef(-angle[3], 0,0,1);  // (1) 先有轉動,但怪怪的
        glTranslated(-0.28,0,0);//(2) 要更早去移動旋轉中心

        glColor3f(255,0,0);
        glutSolidTeapot(0.2);

            glPushMatrix();///接左手肘
            glTranslated(-0.30,0,0); //(3) 把它掛在你要放的地方
            glRotatef(-angle[4], 0,0,1);  // (1) 先有轉動,但怪怪的
            glTranslated(-0.28,0,0);//(2) 要更早去移動旋轉中心

            glColor3f(255,0,0);
            glutSolidTeapot(0.2);
            glPopMatrix();
    glPopMatrix();
   glutSwapBuffers();
}
int oldX,oldY; //滑鼠按下
void mouse(int button,int state,int x,int y)
{
    oldX=x;
    oldY=y;
}
void motion(int x,int y)
{
    angle[jointID]+=x-oldX;
    oldX=x;
    display();
}
void keyboard(unsigned char key,int x,int y) //設定鍵盤按下的變數名稱
{
    if(key=='1') jointID = 1;
    if(key=='2') jointID = 2;
    if(key=='3') jointID = 3;
    if(key=='4') jointID = 4;

}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

沒有留言:

張貼留言