2019年5月2日 星期四

ˊ_>ˋ_week11

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

glPushMatrix();
    glTranslatef(x,y,z);
    glRotatef(angle,x,y,z);
    glTranslatef(x,y,z);
    drawHand();
glPopMatrix();

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












(以中心轉動)
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(255,255,255);
    glutSolidTeapot(0.3);
    glPushMatrix();
        glTranslatef(0.04,-0.05,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.28,0,0);

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

    glutSwapBuffers();
    angle+=0.1;
}
int main(int argc,char **argv )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week11_TRT ");
    glutIdleFunc(display);
    glutDisplayFunc(display);

    glutMainLoop();
}
---------------------------------------------------------------------------




#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(255,255,255);
    glutSolidTeapot(0.3);
    glPushMatrix();///右邊的手臂
        glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肩
        glRotatef(angle, 0,0,1);///(2)旋轉
        glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
        glColor3f(255,0,5);///紅色
        glutSolidTeapot( 0.2 );///上手臂
        glPushMatrix();///右下手臂
            glTranslatef(0.2, 0,0 );///(3)往右移動, 掛在身體的右肘
            glRotatef(angle, 0,0,1);///(2)旋轉
            glTranslatef(0.28, 0,0);///(1)往右推,把茶壼的柄,放到中心,變 轉動軸
            glColor3f(255,0,0);///紅色
            glutSolidTeapot( 0.2 );///右下手臂
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();///左邊的手臂
        glTranslatef(-0.2, 0,0 );///(3)往左移動, 掛在身體的左肩
        glRotatef(angle, 0,0,1);///(2)旋轉
        glTranslatef(-0.28, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
        glColor3f(255,0,5);///紅色
        glutSolidTeapot( 0.2 );///上手臂
        glPushMatrix();///左下手臂
            glTranslatef(-0.2, 0,0 );///(3)往左移動, 掛在身體的左肘
            glRotatef(angle, 0,0,1);///(2)旋轉
            glTranslatef(-0.28, 0,0);///(1)往左推,把茶壼的柄,放到中心,變 轉動軸
            glColor3f(255,0,0);///紅色
            glutSolidTeapot( 0.2 );///左下手臂
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
    angle+=0.1;
}
int main(int argc,char **argv )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week11_TRT ");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}
---------------------------------------------------------------------------




///angle+=0.1;   ///Now2!!!
void motion(int x, int y)
{
    angle = x;///Now2!!!
    display();///Now2!!!
}
int main裡面
 glutMotionFunc(motion);///Now2!!!

(3)主題:鍵盤按鍵


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



float angle1=0,angle2,angle3,angle4;
int jointID=1; ///1,2,3,4///Now3!!!

void motion(int x, int y)
{
    angle[jointID]= x;///Now2!!!///NOW3!!!
    ///if(jointID==1) angle1 = x;
    ///if(jointID==2) angle2 = x;
    ///if(jointID==3) angle3 = x;
    ///if(jointID==4) angle4 = x;

    display();///Now2!!!
}

void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') jointID = 1;///NOW3!!!
    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_TRT ");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMotionFunc(motion);///Now2!!!
    glutKeyboardFunc(keyboard);///Now3!!!
    glutMainLoop();
}
----------------------------------------------------------------------------

滑鼠控制





int oldX, oldY;///NOW5!!
void mouse(int button, int state, int x, int y)///NOW5!!
{
    oldX=x;///NOW5!!
    oldY=y;///NOW5!!
}
void motion(int x, int y)
{
    angle[jointID] += x - oldX;  ///NOW5!!
    oldX = x; ///NOW5!!
    ///angle[jointID]= x;///Now2!!!///NOW3!!!
    ///if(jointID==1) angle1 = x;
    ///if(jointID==2) angle2 = x;
    ///if(jointID==3) angle3 = x;
    ///if(jointID==4) angle4 = x;

    display();///Now2!!!
}

void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') jointID = 1;///NOW3!!!
    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_TRT ");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMotionFunc(motion);///Now2!!!
    glutMouseFunc(mouse);///NOW5!!
    glutKeyboardFunc(keyboard);///Now3!!!
    glutMainLoop();
}


沒有留言:

張貼留言