2020年5月26日 星期二

賴胖筆記 week13

FILE fopen(), fprintf()

先開啟一個可以書寫的motion.txt檔
再在裡面書寫hellow world
在檔案中用for迴圈書寫0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
                                         0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
在TRT機械手臂中加入FILE fopen(), fprintf()以紀錄個別關節的旋轉角度
程式碼:
#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()
{
    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();
}

fscanf()

在程式中加入按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>
FILE * fout = NULL;
FILE * fin = NULL;
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'){
        if(fin==NULL) fin=fopen("motion.txt","r");
        for(int i=0;i<10;i++){
            fscanf(fin,"%d",&angle[i]);
        }
        glutPostRedisplay();
    }
}
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;///TODO2: mouse按下去時, 記得在哪裡
}
void saveAll()
{
    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();
}
增加timer以增加按下p鍵後自動轉動
程式碼:
#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>
FILE * fout = NULL;
FILE * fin = NULL;
void readAll()
{
    if(fin==NULL) fin=fopen("motion.txt","r");
        for(int i=0;i<10;i++){
            fscanf(fin,"%d",&angle[i]);
    }
}
void timer(int t)
{
    glutTimerFunc(20,timer,t+1);
    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'){
        readAll();
        glutPostRedisplay();
        }
    else if(key=='p'){
        glutTimerFunc(20,timer,0);
    }
}
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;///TODO2: mouse按下去時, 記得在哪裡
}
void saveAll()
{
    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();
}



沒有留言:

張貼留言