2022年5月17日 星期二

AhFatKr的電腦圖學筆記week13

 #week13


##step01-1

glRectf(x1,y1,x2,y2)方塊

1.File-New-Project,GLUT專案

2.貼上GLUT 10行程式,不用茶壺

3.把手臂接到身體

#include<GL/glut.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);//白色的

    glRectf(0.3,0.5,-0.3,-0.5);//四邊形

    glPushMatrix();

        ///glTranslatef(x,y,z);(3)把手臂掛回身體

        ///glRotatef(angle,0,0,1);(2)旋轉 對Z軸轉

        ///glTranslatef(x2,y2,z2);(1)把手臂的旋轉中心,放中心

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

        glRectf(0.3,0.5,0.7,0.3);

    glPopMatrix();

    glutSwapBuffers();

}

int main(int argc, char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    //glutInitWindowSize(600,600);

    glutCreateWindow("week13 rect TRT");


    glutDisplayFunc(display);


    glutMainLoop();

}



#step01-2
先把旋轉中心放在正中心
把(0.3 0.4)移到(0 0)
加一行glTranslatef(-0.3,-0.4,0);將旋轉中心移動到正中心 
#step01-3
再加入一行glRotatef(45,0,0,1);讓手臂旋轉45度

#step01-4
在加一行glTranslatef(0.3,0.4,0);把手臂掛回身體
#step02-1
加入另一隻手臂
#step02-2
加入三行程式讓另一隻手臂也能轉,並且與第一條手臂相接
#step3-1
File-New-Project,GLUT新的專案week13_rect_many_TRT
複製剛剛的程式碼,並加入左半邊的手臂(將座標正負互換,形成鏡射效果)

#include<GL/glut.h>
float angle=45,oldX=0;
void mouse(int button,int state,int x,int y){
    oldX=x;
}
void motion(int x,int y){
    angle += (x-oldX);
    oldX = x;
    glutPostRedisplay();///請GLUT重畫畫面Re display
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///白色的
    glRectf(0.3,0.5,-0.3,-0.5);///四邊形
    glPushMatrix();///右半部
        glTranslatef(0.3,0.4,0);///(3)把手臂掛回身體
        glRotatef(angle,0,0,1);///(2)旋轉 對Z軸轉
        glTranslatef(-0.3,-0.4,0);///(1)把手臂的旋轉中心,放中心
        glColor3f(1,0,0);//紅色的
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            glTranslatef(0.7,0.4,0);///(3)把手肘掛回剛剛的位置
            glRotatef(angle,0,0,1);///(2)旋轉
            glTranslatef(-0.7,-0.4,0);///(1)把手肘旋轉中心,放中心
            glColor3f(0,1,0);///綠色的
            glRectf(0.7,0.5,1.0,0.3);///上手臂
        glPopMatrix();

        glPushMatrix();///左半部
        glTranslatef(-0.3,0.4,0);///(3)把手臂掛回身體
        glRotatef(angle,0,0,1);///(2)旋轉 對Z軸轉
        glTranslatef(0.3,-0.4,0);///(1)把手臂的旋轉中心,放中心
        glColor3f(1,0,0);///紅色的
        glRectf(-0.3,0.5,-0.7,0.3);

        glPushMatrix();
            glTranslatef(-0.7,0.4,0);///(3)把手肘掛回剛剛的位置
            glRotatef(angle,0,0,1);///(2)旋轉
            glTranslatef(0.7,-0.4,0);///(1)把手肘旋轉中心,放中心
            glColor3f(0,1,0);///綠色的
            glRectf(-0.7,0.5,-1.0,0.3);///上手臂
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    //glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
}


#step03-2
如果只有一個角度是不行的
所有關節那些加起來大約需要20個角度
#include<GL/glut.h>
float angle[20],oldX=0;
int angleID=0;
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;
}
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();///請GLUT重畫畫面Re display
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);///白色的
    glRectf(0.3,0.5,-0.3,-0.5);///四邊形
    glPushMatrix();///右半部
        glTranslatef(0.3,0.4,0);///(3)把手臂掛回身體
        glRotatef(angle[0],0,0,1);///(2)旋轉 對Z軸轉
        glTranslatef(-0.3,-0.4,0);///(1)把手臂的旋轉中心,放中心
        glColor3f(1,0,0);//紅色的
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            glTranslatef(0.7,0.4,0);///(3)把手肘掛回剛剛的位置
            glRotatef(angle[1],0,0,1);///(2)旋轉
            glTranslatef(-0.7,-0.4,0);///(1)把手肘旋轉中心,放中心
            glColor3f(0,1,0);///綠色的
            glRectf(0.7,0.5,1.0,0.3);///上手臂
        glPopMatrix();

        glPushMatrix();///左半部
        glTranslatef(-0.3,0.4,0);///(3)把手臂掛回身體
        glRotatef(angle[2],0,0,1);///(2)旋轉 對Z軸轉
        glTranslatef(0.3,-0.4,0);///(1)把手臂的旋轉中心,放中心
        glColor3f(1,0,0);///紅色的
        glRectf(-0.3,0.5,-0.7,0.3);

        glPushMatrix();
            glTranslatef(-0.7,0.4,0);///(3)把手肘掛回剛剛的位置
            glRotatef(angle[3],0,0,1);///(2)旋轉
            glTranslatef(0.7,-0.4,0);///(1)把手肘旋轉中心,放中心
            glColor3f(0,1,0);///綠色的
            glRectf(-0.7,0.5,-1.0,0.3);///上手臂
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    //glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);

    glutMainLoop();
}




沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...