2022年5月24日 星期二

(OwO)電腦圖學week14_5/24

開一個empty file(不是專案)


#include<stdio.h>

int main()

{

///檔案指標fout 開啟檔案(檔名,write模式)


    FILE * fout = fopen("write.txt","w+");

    printf("Hello world\n");

    fprintf(fout,"Hello world\n");

    fclose(fout);


}




再另外開一個empty file


#include<stdio.h>

int main()

{

///檔案指標fout 開啟檔案(檔名,write模式)


    FILE * fout = fopen("file.txt","w+");

    fprintf(fout,"3.1415926\n");

    fclose(fout);///關檔



    float angle =0;

    FILE * fin=fopen("file.txt","r");

    fscanf(fin,"%f",&angle);///沒加&會當

    printf("讀到了角度:%f",angle);

    fclose(fin);


}



拿上禮拜的程式碼再加點東西

#include<GL/glut.h>

#include<stdio.h>

float angle[20] ,oldX=0;

int angleID=0;

FILE * fout = NULL , * fin = NULL;

void myWrite()

{


    if(fout==NULL)fout=fopen("File.txt","W+");

    for(int i=0;i<20;i++)


    {

        printf("%.1f ", angle[i]);///小黑印出來


        fprintf(fout,"%.1f ", angle[i]);///檔案印出來


    ///1f要空格


   }


    printf("\n");///每呼叫一次小黑會跳行


    fprintf(fout,"\n");///每呼叫一次檔案會跳行




}///這邊沒有fclose


void myRead(){


    if(fout != NULL){fclose(fout);fout=NULL;}

    if(fin==NULL) fin =fopen("file.txt","r");


    for(int i=0;i<20;i++){


        fscanf(fin,"%f",&angle[i]);


    }

    glutPostRedisplay();///重新清畫面


}


void keyboard(unsigned char key,int x,int y)


{

    if(key=='r') myRead();


    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);


    myWrite();


    oldX=x;


    glutPostRedisplay();///請GLUT重畫畫面


}


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)


        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();


    glPopMatrix();






     glPushMatrix();///左邊


        glTranslatef(-0.3,0.4,0);///(3)整體移位


        glRotatef(angle[2],0,0,1);///(2)


        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("week14_TRT");




    glutMouseFunc(mouse);


    glutMotionFunc(motion);


    glutKeyboardFunc(keyboard);


    glutDisplayFunc(display);




    glutMainLoop();


}


產生的檔案file.txt跑到 C:\Users\user\Desktop\freeglut\gin ;想要放在程式專案的那個目錄裡

需要 freeglut.dll ( working_dir被設到 freeglut\bin 裡面)

在 .cbp CodeBlocks Project 檔裡, 有 working_dir的設定 工作執行的目錄

使用 Notepad++ 把 .cbp 的 working_dir="....." 改 working_dir="." (小數點)

存檔後, CodeBlocks Reload它

放到week14_angles_fprintf_fscanf 程式專案的同目錄, 再執行時,  mouse motion & 按r重播就ok

file.txt 放在你的程式專案目錄囉!!!



沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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