2022年5月16日 星期一

Week13 TRT 複習

 1-1

畫出正方形

#include <GL/glut.h>


void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glRectf(0.5,0.5,-0.5,-0.5)畫出正方形

    glutSwapBuffers();

}



int main( int argc,char**argv)

{


    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);


    glutCreateWindow("week13 T-R-T");



    glutDisplayFunc(display);


    glutMainLoop();


}

1-2

加上一小塊


    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);

    glRectf(0.5,0.5,-0.5,-0.5);

     glPushMatrix();

            ///glTranslatef(0.2, 0, 0);///把下面旋轉中的東西 "掛在" 某處

            ///glRotatef( angle, 0,0,1 );///旋轉

            ///glTranslatef(0.2, 0, 0);///???

            glColor3f(1,0,.5);///粉紅色的

            glRectf(0.5,0.5,1.0,0.3);///手臂

        glPopMatrix();

    glutSwapBuffers();

1-3

            glRotatef( angle, 0,0,1 );///旋轉angle的角度 

            glTranslatef(-0.5,-0.4, 0);//旋轉中心置中


2-1

讓小方塊可以動

oldX=0;

void mouse(int button,int state,int x,int y){

    oldX=x;

}///新增mouse函式

void motion(int x,int y){

    angle+=(x-oldX);

    oldX=x;

    glutPostRedisplay();

}///新增motion函式

int main( int argc,char**argv)

{


    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);


    glutCreateWindow("week13 T-R-T");


    glutMouseFunc(mouse);///呼叫mouse函式

    glutMotionFunc(motion);///呼叫motion函式

    glutDisplayFunc(display);


    glutMainLoop();


}


glRectf(0.3,0.5,-0.3,-0.2);///瘦身

     glPushMatrix();

            glTranslatef(0.3, 0.5, 0);///把下面旋轉中的東西 "掛在"本體(改位子)

            glRotatef( angle, 0,0,1 );///旋轉

            glTranslatef(-0.5,-0.4, 0);///

            glColor3f(1,0,0.5);///粉紅色的

            glRectf(0.5,0.5,1.0,0.3);///手臂

        glPopMatrix();

    glutSwapBuffers();



接上手肘

 glPushMatrix();

            glTranslatef(0.3, 0.5, 0);///把下面旋轉中的東西 "掛在"本體

            glRotatef( angle, 0,0,1 );///旋轉

            glTranslatef(-0.3,-0.4, 0);///

            glColor3f(1,0,0.5);///粉紅色的

            glRectf(0.3,0.5,0.8,0.3);///手臂

            glPushMatrix();

                ///glTranslatef(0.3, 0.5, 0);///把下面旋轉中的東西 "掛在"手臂

                ///glRotatef( angle, 0,0,1 );///旋轉

                ///glTranslatef(-0.5,-0.4, 0);///

                glColor3f(1,0.5,0);///橘色的

                glRectf(0.8,0.5,1.1,0.3);///手肘

            glPopMatrix();

        glPopMatrix();


讓手肘動起來

glPushMatrix();

                glTranslatef(0.8, 0.4, 0);///把下面旋轉中的東西 "掛在"手臂

                glRotatef( angle, 0,0,1 );///旋轉

                glTranslatef(-0.8,-0.4, 0);///

                glColor3f(1,0.5,0);///橘色的

                glRectf(0.8,0.5,1.1,0.3);///手肘

            glPopMatrix();



兩條手臂,點對稱轉動

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);

    glRectf(0.3,0.5,-0.3,-0.2);///瘦身


        glPushMatrix();///左半邊

            glTranslatef(-0.3, 0.5, 0);///把下面旋轉中的東西 "掛在"本體

            glRotatef( angle, 0,0,1 );///旋轉

            glTranslatef(0.3,-0.4, 0);///

            glColor3f(1,0,0.5);///粉紅色的

            glRectf(-0.3,0.5,-0.8,0.3);///手臂

            glPushMatrix();

                glTranslatef(-0.8, 0.4, 0);///把下面旋轉中的東西 "掛在"手臂

                glRotatef( angle, 0,0,1 );///旋轉

                glTranslatef(0.8,-0.4, 0);///

                glColor3f(1,0.5,0);///橘色的

                glRectf(-0.8,0.5,-1.1,0.3);///手肘

            glPopMatrix();

        glPopMatrix();


        glPushMatrix();///右半邊

            glTranslatef(0.3, 0.5, 0);///把下面旋轉中的東西 "掛在"本體

            glRotatef( angle, 0,0,1 );///旋轉

            glTranslatef(-0.3,-0.4, 0);///

            glColor3f(1,0,0.5);///粉紅色的

            glRectf(0.3,0.5,0.8,0.3);///手臂

            glPushMatrix();

                glTranslatef(0.8, 0.4, 0);///把下面旋轉中的東西 "掛在"手臂

                glRotatef( angle, 0,0,1 );///旋轉

                glTranslatef(-0.8,-0.4, 0);///

                glColor3f(1,0.5,0);///橘色的

                glRectf(0.8,0.5,1.1,0.3);///手肘

            glPopMatrix();

        glPopMatrix();

    glutSwapBuffers();

}



將控制多個節點
#include <GL/glut.h>
float angle[20], oldX=0;
int angleID=0;將angle用陣列表示
void keyboard(unsigned char key,int x,int y){
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}///新增keyboard函式
void mouse(int button,int state,int x,int y){
    oldX=x;
}
void motion(int x,int y){
    angle[angleID]+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glRectf(0.3,0.5,-0.3,-0.2);///瘦身

        glPushMatrix();///左半邊
            glTranslatef(-0.3, 0.5, 0);///把下面旋轉中的東西 "掛在"本體
            glRotatef( angle[2], 0,0,1 );///旋轉
            glTranslatef(0.3,-0.4, 0);///
            glColor3f(1,0,0.5);///粉紅色的
            glRectf(-0.3,0.5,-0.8,0.3);///手臂
            glPushMatrix();
                glTranslatef(-0.8, 0.4, 0);///把下面旋轉中的東西 "掛在"手臂
                glRotatef( angle[3], 0,0,1 );///旋轉
                glTranslatef(0.8,-0.4, 0);///
                glColor3f(1,0.5,0);///橘色的
                glRectf(-0.8,0.5,-1.1,0.3);///手肘
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///右半邊
            glTranslatef(0.3, 0.5, 0);///把下面旋轉中的東西 "掛在"本體
            glRotatef( angle[0], 0,0,1 );///旋轉
            glTranslatef(-0.3,-0.4, 0);///
            glColor3f(1,0,0.5);///粉紅色的
            glRectf(0.3,0.5,0.8,0.3);///手臂
            glPushMatrix();
                glTranslatef(0.8, 0.4, 0);///把下面旋轉中的東西 "掛在"手臂
                glRotatef( angle[1], 0,0,1 );///旋轉
                glTranslatef(-0.8,-0.4, 0);///
                glColor3f(1,0.5,0);///橘色的
                glRectf(0.8,0.5,1.1,0.3);///手肘
            glPopMatrix();
        glPopMatrix();
    glutSwapBuffers();
}


int main( int argc,char**argv)
{

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week13 T-R-T");

    glutKeyboardFunc(keyboard);///呼叫keyboard函式
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();

}

沒有留言:

張貼留言