2020年5月26日 星期二

第十三(十四?)週紀錄手臂動作的卡小

老師說來複習!
複習大一下的東西
雖然我大一下根本沒學過這個就是了:(

總之先打開code:blocks
開新ㄉ空白檔案
然後打上面這些程式碼
執行成功之後會出現一個txt的檔案
.cpp → .o → .exe → .txt
點開來看出現hello world就完成ㄌ!
像醬
之後再做其他測試

小葉說這個程式要完全跑完,motion才會出現、更新
有更快速的方法可以讓motion出現
把檔案關掉之後紅線以下就不會做了
----------------------------------------
#include <stdio.h>
int a[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int main(){
    ///檔案指標變數,檔案開啟("檔名","模型")
    FILE * fout = fopen("motion.txt","w+");

    for(int i=0;i<16;i++){
        fprintf(fout,"%d ",a[i]);
    }

    fprintf(fout,"\n");

    fclose(fout);
    for(int i=0;i<16;i++){
        fprintf(fout,"%d ",a[i]);
    }
}
----------------------------------------

幹正事辣!

打開建立GLUT新專案
然後老師講了超級多
程式碼也超級多
所以老師就自己把程式碼傳給大家複製貼上了,安捏
總之就是要做4個關節!
會長醬子
給你程式碼辣!
----------------------------------------
#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
        glColor3f(1,1,1);///白色
glutSolidTeapot(0.3);///body

glPushMatrix();///右邊
            glTranslatef(0.3, 0,0);
            glRotatef(angle[0], 0,0,1);///轉動
            glTranslatef(0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(1,0,0);///紅色
            glutSolidTeapot(0.2);///右上手臂
            glPushMatrix();///右下手臂
                glTranslatef(0.2, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(0.2, 0,0);

                glColor3f(1,1,0);///黃
                glutSolidTeapot(0.2);
            glPopMatrix();
glPopMatrix();

glPushMatrix();///左邊
            glTranslatef(-0.3, 0,0);
            glRotatef(angle[3], 0,0,1);///轉動
            glTranslatef(-0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(0,1,0);///綠色
            glutSolidTeapot(0.2);///左上手臂
            glPushMatrix();
                glTranslatef(-0.2, 0,0);
                glRotatef(angle[4], 0,0,1);
                glTranslatef(-0.2, 0,0);

                glColor3f(0,0,1);///藍色
                glutSolidTeapot(0.2);///左下手臂
            glPopMatrix();
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14 file");

glutDisplayFunc(display);

glutMainLoop();
}
----------------------------------------
儲存它旋轉的角度
變醬
老師有把程式碼給我們
我們只需要在打TODO3:的部分就可以囉!
----------------------------------------
#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
        glColor3f(1,1,1);///白色
glutSolidTeapot(0.3);///body

glPushMatrix();///右邊
            glTranslatef(0.3, 0,0);
            glRotatef(angle[0], 0,0,1);///轉動
            glTranslatef(0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(1,0,0);///紅色
            glutSolidTeapot(0.2);///右上手臂
            glPushMatrix();///右下手臂
                glTranslatef(0.2, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(0.2, 0,0);

                glColor3f(1,1,0);///黃
                glutSolidTeapot(0.2);
            glPopMatrix();
glPopMatrix();

glPushMatrix();///左邊
            glTranslatef(-0.3, 0,0);
            glRotatef(angle[2], 0,0,1);///轉動
            glTranslatef(-0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(0,1,0);///綠色
            glutSolidTeapot(0.2);///左上手臂
            glPushMatrix();
                glTranslatef(-0.2, 0,0);
                glRotatef(angle[3], 0,0,1);
                glTranslatef(-0.2, 0,0);

                glColor3f(0,0,1);///藍色
                glutSolidTeapot(0.2);///左下手臂
            glPopMatrix();
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
}
int angleID=0;///TODO2:要動哪個關節
int oldX=0, oldY=0;///TODO2:
void keyboard(unsigned char key, int x, int y)
{
    if(key=='0') angleID=0;///TODO2: 要動的關節
    if(key=='1') angleID=1;///TODO2: 要動的關節
    if(key=='2') angleID=2;///TODO2: 要動的關節
    if(key=='3') angleID=3;///TODO2: 要動的關節
}
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;///TODO2: mouse按下去時, 記得在哪裡
}

#include <stdio.h>
FILE * fout = NULL;
void saveALL(){///TODO3:(第一次是NULL,要把檔案開起來,以後就不用開了)
    if(fout==NULL) fout=fopen("motion.txt","w+");
    for(int i=0;i<10;i++){
        printf("%d ",angle[i]);
        fprintf(fout,"%d ",angle[i]);
    }
    printf("\n");
    fprintf(fout,"\n");
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);///TODO2: 讓關節動起來
    oldX=x;
    saveALL();
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14 file");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);///TODO2: keyboard運作
glutMouseFunc(mouse);///TODO2: mouse
glutMotionFunc(motion);///TODO2: motion

glutMainLoop();
}
----------------------------------------
接著結合第一節教過的
我們要做TODO4!
讀入旋轉過的角度
之後按住 r 就可以做ㄌ!

但是這樣只能一直按住
所以我們要讓他按一下就可以一次跑完
直接給你程式碼辣!
----------------------------------------
#include <GL/glut.h>
int angle[10]={0,0,0,0,0, 0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
        glColor3f(1,1,1);///白色
glutSolidTeapot(0.3);///body

glPushMatrix();///右邊
            glTranslatef(0.3, 0,0);
            glRotatef(angle[0], 0,0,1);///轉動
            glTranslatef(0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(1,0,0);///紅色
            glutSolidTeapot(0.2);///右上手臂
            glPushMatrix();///右下手臂
                glTranslatef(0.2, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(0.2, 0,0);

                glColor3f(1,1,0);///黃
                glutSolidTeapot(0.2);
            glPopMatrix();
glPopMatrix();

glPushMatrix();///左邊
            glTranslatef(-0.3, 0,0);
            glRotatef(angle[2], 0,0,1);///轉動
            glTranslatef(-0.2, 0,0);///最重要,旋轉中心要放在中心

            glColor3f(0,1,0);///綠色
            glutSolidTeapot(0.2);///左上手臂
            glPushMatrix();
                glTranslatef(-0.2, 0,0);
                glRotatef(angle[3], 0,0,1);
                glTranslatef(-0.2, 0,0);

                glColor3f(0,0,1);///藍色
                glutSolidTeapot(0.2);///左下手臂
            glPopMatrix();
glPopMatrix();

glPopMatrix();
glutSwapBuffers();
}
int angleID=0;///TODO2:要動哪個關節
int oldX=0, oldY=0;///TODO2:
#include <stdio.h>///TODO3: //TODO4:記得要移高一點哦!
FILE * fout=NULL;///TODO3: 檔案是NULL
FILE * fin=NULL;///TODO4: File Input (fin)
void readAll()///TODO5: 把程式碼拉出來, 變成函式 readAll();
{
    if(fin==NULL) fin=fopen("motion.txt", "r" );///TODO4: 開檔案
    for(int i=0; i<10; i++){
        ///scanf(  "%d", &a[i]);
        fscanf(fin,"%d", &angle[i]);///TODO4: 讀入
    }
}
void timer(int t)///TODO5: 用timer自動readAll()再更新
{
    glutTimerFunc(20, timer, t+1);///TODO5: 再註冊下一個時間點 20ms 之後
    readAll();
    glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='0') angleID=0;///TODO2: 要動的關節
    if(key=='1') angleID=1;///TODO2: 要動的關節
    if(key=='2') angleID=2;///TODO2: 要動的關節
    if(key=='3') angleID=3;///TODO2: 要動的關節
    if(key=='r'){///TODO4: 我們要讀檔案囉!!!!
        readAll();///TODO5:
        glutPostRedisplay();///TODO4: 更新畫面/重畫
    }else if(key=='p'){///TODO5: Play播放
        glutTimerFunc(20, timer, 0);///TODO5: 要用timer來自動播放
    }
}
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;///TODO2: mouse按下去時, 記得在哪裡
}
void saveAll()///TODO3:全部存起來
{///TODO3: (第1次是NULL,要把檔案開起來, 以後就不用再開它了...)
    if(fout==NULL) fout=fopen("motion.txt", "w+");///TODO3: 檔案是NULL
    for(int i=0; i<10; i++){
        printf("%d ", angle[i]);
        fprintf(fout, "%d ", angle[i]);
    }
    printf("\n");
    fprintf(fout, "\n");
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);///TODO2: 讓關節動起來
    oldX=x;
    saveAll();///TODO3:全部存起來
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14 file");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);///TODO2: keyboard運作
glutMouseFunc(mouse);///TODO2: mouse
glutMotionFunc(motion);///TODO2: motion

glutMainLoop();
}
----------------------------------------
找不同!
安裝compare
就可以進行比較了!


沒有留言:

張貼留言