2016年5月23日 星期一

Week14_許皓翔 - 河蟹牌Part 11

複習大一下讀檔案 :

開啟貝殼專案 - C++ - 路徑

程式碼 :

#include <stdio.h>
FILE *fout=NULL, *fin=NULL;
int main(int argc, char**argv)
{
    if(fout==NULL) fout=fopen("output.txt", "w+");
    ///printf("Hello world");
    fprintf(fout, "Hello world");
}


結果 : 




~ 滑鼠寫檔案 ~



將剛剛的讀檔案改寫加強

設置程式的路徑跟五咒語






程式碼 : 

#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL, *fin=NULL;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void motion(int x, int y)
{
    if(fout==NULL) fout=fopen("output.txt", "w+");
    fprintf(fout, "%d %d\n", x,y);;
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}


結果會跑出一個茶壺





然後在茶壺上點住滑動,發現 output.txt 檔案大小增加了 !!!  (0~4KB)

打開 output.txt ,結果如下 : 








進階 ~ ~ ~ ~ ~ ~ ~ ~ ~


按 r  使圖形跟著滑鼠軌跡移動

程式碼 :

#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL, *fin=NULL;///檔案的指標
int nowX=150, nowY=150;///等一下讀檔案,把mouse motion讀回來
void display()
{
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef( (nowX-150)/150.0, -(nowY-150)/150.0, 0);
    ///視窗是300x300,所以一半是150, 減掉150,就變再-150...+150
    ///再除以150.0 就會變介於 -1 ... +1 之間的float數
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x, int y)
{
    if(fout==NULL) fout=fopen("output.txt", "w+");
    fprintf(fout, "%d %d\n", x,y);;
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='r')///按小寫的r 會讀去1行/一組x,y
    {
        if(fin==NULL) fin=fopen("output.txt", "r");
        fscanf(fin, "%d %d", &nowX, &nowY);
    }
    glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}


成品 :


按 r 會移動




按7次 r 以後 :




進進階~~~~~~ (研究老師的程式碼)

#include <GL/glut.h>
#include <stdio.h>
float rot[10]={0};
int rotNow=0, oldX=0, oldY=0;;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();

    glPushMatrix();
        glutWireCube(0.3);///body
        glPushMatrix();
             glTranslatef(0,0.2,0);
             glutWireCube(0.1);///head
        glPopMatrix();

        glPushMatrix();

            glTranslatef(0.15,0.1,0);
            glRotatef(rot[0],0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);///upper arm

            glTranslatef(0.05,0,0);
            glRotatef(rot[1],0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);///lower arm

            glTranslatef(0.05,0,0);
            glRotatef(rot[2],0,0,1);
            glTranslatef(0.05,0,0);
            glutWireCube(0.1);///right arm

        glPopMatrix();

        glPushMatrix();
            ///glTranslatef(-0.2,0.1,0);
            glTranslatef(-0.15,0.1,0);
            glRotatef(-rot[4],0,0,1);
            glTranslatef(-0.05,0,0);

            glutWireCube(0.1);///upper arm

            glTranslatef(-0.05,0,0);
            glRotatef(-rot[5],0,0,1);
            glTranslatef(-0.05,0,0);

            glutWireCube(0.1);///lower arm

            glTranslatef(-0.05,0,0);
            glRotatef(-rot[6],0,0,1);
            glTranslatef(-0.05,0,0);

            glutWireCube(0.1);///right hand
        glPopMatrix();


    glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
FILE *fout=0, *fin=0;
void keyboard(unsigned char key, int x, int y)
{
    if(key=='0') rotNow=0;
    if(key=='1') rotNow=1;
    if(key=='2') rotNow=2;
    if(key=='3') rotNow=3;
    if(key=='4') rotNow=4;
    if(key=='5') rotNow=5;
    if(key=='6') rotNow=6;
    if(key=='r'){///按小寫的r 會去讀1行/一組x,y
        if(fin==NULL) fin=fopen("motion.txt", "r");
        for(int i=0;i<7;i++){
            fscanf(fin, "%f", &rot[i]);
        }
    }
    glutPostRedisplay();///電腦貼個Post-It便利貼,告訴GLUT有空要重畫畫面哦
}
void motion(int x, int y)
{
    rot[rotNow] += x-oldX;
    oldX = x;
    glutPostRedisplay();
    if(fout==NULL) fout=fopen("motion.txt", "w+");
    for(int i=0;i<7;i++){
        fprintf(fout, "%3.1f ", rot[i]);
        printf("%3.1f ", rot[i]);
    }
    fprintf(fout, "\n");
    printf("\n");
}
void mouse(int button, int state, int x, int y)
{///先開冰箱門(滑鼠按下去),把大象塞進去(滑鼠drag),最後再關上冰箱門(起來)
    if(state==GLUT_DOWN){
        oldX=x; oldY=y;
    }
}
int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Robot");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();

}


成品 :


首先先執行程式,然後依序按1,2,3,4,5,6來調整手臂位置 (每按一個就調一次)



之後把執行視窗關掉,再重開一次

嗚嗚哇 RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR !!!!!!!   (呃,意思是按住 R 不動)


然後搭拉 ~~~

手臂就會照剛剛你滑鼠移動的路徑動了ㄟ ~


沒有留言:

張貼留言