2022年5月24日 星期二

一定是大拇指的啦

 step 1 : fprintf 讀寫檔案

1.1 先設立資料夾在桌面 命名為week14-1_ fprintf

1.2  open-empty file 存檔為week14-1.cpp

1.3 程式碼

#include <stdio.h>

int main()

{///檔案指標  fout  開啟檔案(檔名 , 寫)

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

    printf(     "Hello world\n");

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

    fclose(fout);///關閉檔案

}

1.4 資料夾內會有四個檔案


step 2 : fprintf fscanf 

21 先設立資料夾在桌面 命名為week14-1_ fprintf_fscanf

2.2  open-empty file 存檔為week14-2.cpp

2.3 程式碼

#include <stdio.h>

int main()

{///檔案指標  fout  開啟檔案(檔名 , 寫)

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

}


step 3 : 結合上週many_TRT

3.1 先設立資料夾在桌面 命名為week14-3_ angle_fprintf

3.2  開新專案 命名為week14-3.cpp

3.3 貼上上週many_TRT程式碼,並新增紅色程式碼

#include <GL/glut.h>

#include <stdio.h>///printf用

float angle[4] , oldx=0;///angle變陣列以致可以放更多選轉角度

int angleID=0;///0號關節 1號關節

FILE * fout = 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] );

    }

}

void keyboard(unsigned char key , int x  , int y)///根據按數字鍵多少決定第幾個關節要轉動

{ ///按數字鍵決定要改變哪個angleID

    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);///angle變成陣列

    oldx=x;

    mywrite();///執行用

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

        glRotatef(angle[0],0,0,1);///旋轉 對Z軸轉 ///angle[0] 第一個值

        glTranslatef(-0.3,-0.4,0);///把手臂選轉中心放世界中心

        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)旋轉角度  ///angle[1] 第二個值

            glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心

            glColor3f(0,1,10);

            glRectf(0.7 , 0.5 , 1.0 , 0.3);///右上手臂

        glPopMatrix();

    glPopMatrix();


///左手跟右手的X軸相反,所以要加負號

    glPushMatrix();

        glTranslatef(-0.3,0.4,0);///

        glRotatef(angle[2],0,0,1);///旋轉 對Z軸轉  ///angle[2] 第三個值

        glTranslatef(0.3,-0.4,0);///把手臂選轉中心放世界中心

        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)旋轉角度  ///angle[3] 第四個值

            glTranslatef(0.7,-0.4,0);///(1)將軸心放到世界中心

            glColor3f(0,1,10);

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

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}

step 4 :做動畫
4.1  開新專案 命名為week_angle_fprintf_fscanf
4.2 貼上剛剛的程式碼 新增紅色的程式碼///發現有問題可以去freeglut/bin/file.txt查看
#include <GL/glut.h>
#include <stdio.h>
float angle[4] , oldx=0;///angle變陣列以致可以放更多選轉角度
int angleID=0;///0號關節 1號關節
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] );///檔案印出來
    }
    printf("\n");///每次呼叫一次 小黑跳行
    fprintf(fout , "\n");///每呼叫一次 檔案也跳行
}
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)///根據按數字鍵多少決定第幾個關節要轉動
{ ///按數字鍵決定要改變哪個angleID
    if (key=='r') myRead();///按著r可以重播畫面
    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);///angle變成陣列
    oldx=x;
    myWrite();
    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);///
        glRotatef(angle[0],0,0,1);///旋轉 對Z軸轉 ///angle[0] 第一個值
        glTranslatef(-0.3,-0.4,0);///把手臂選轉中心放世界中心
        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)旋轉角度  ///angle[1] 第二個值
            glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心
            glColor3f(0,1,10);
            glRectf(0.7 , 0.5 , 1.0 , 0.3);///右上手臂
        glPopMatrix();
    glPopMatrix();

///左手跟右手的X軸相反,所以要加負號
    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///
        glRotatef(angle[2],0,0,1);///旋轉 對Z軸轉  ///angle[2] 第三個值
        glTranslatef(0.3,-0.4,0);///把手臂選轉中心放世界中心
        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)旋轉角度  ///angle[3] 第四個值
            glTranslatef(0.7,-0.4,0);///(1)將軸心放到世界中心
            glColor3f(0,1,10);
            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_many_TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
4.3 更改freeglut/bin 位置
4.3.1 week14_angle_fprintf_fscanf 右鍵使用notepad++開啟
4.3.2 將紅色框框內working_dir 中的 " "內改寫成.

4.3.3 存檔後回codeblock  會有reload project 要按Yes
4.3.4 桌面freeglut/bin/freeglut.dll 這個檔案要複製貼上到當前的資料夾
(兩個是綁在一起的 否則無法執行)

step 5 : 計時器
5.1 開新專案 命名為week14_timer
5.2 程式碼(一次設定好)
#include <stdio.h>
#include <GL/glut.h>
void timer(int t)
{
    printf("起床,現在時間 : %d\n",t);
}
void display()
{

}
int main(int argc ,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14 timer");
    ///一次設3次
    glutTimerFunc(1000,timer,1);///第一秒 1 
    glutTimerFunc(2000,timer,2);///第二秒 2 
    glutTimerFunc(3000,timer,3);///第三秒 3 
    glutDisplayFunc(display);
    glutMainLoop();
}
5.3 程式碼(自動設定+1)
#include <stdio.h>
#include <GL/glut.h>
void timer(int t)
{
    printf("起床,現在時間 : %d\n",t);
    glutTimerFunc(1000,timer,t+1);///自動加1
}
void display()
{

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

    glutTimerFunc(5000,timer,0);///第5秒開始執行
    glutDisplayFunc(display);
    glutMainLoop();
}
5.4程式碼(會秒會發出聲音)
#include <stdio.h>
#include <GL/glut.h>
#include <mmsystem.h>///do wav 用
void timer(int t)
{
    printf("我起床囉: %d\n",t);
    PlaySound("do.wav",NULL,SND_ASYNC);
    glutTimerFunc(1000,timer,t+1);
}
void display()
{

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

    glutTimerFunc(5000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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