2022年5月24日 星期二

Different Dimension Demon的契約書 week14

 step01

file-new-empty week14-1.cpp

1.寫檔(write)

```c

#include <stdio.h>

int main()

{

    FILE*fout=fopen("file.txt","w+");///指標fout開啟檔案

    printf("Hello World\n");

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

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

}

```


file-new-empty week14-2.cpp

2.讀檔(read)

```c

#include <stdio.h>

int main()

{

    FILE*fout=fopen("file.txt","w+");///指標fout開啟檔案

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

}

```


用week13_rect_many_TRT改

3.

```c++

#include <GL/glut.h>

#include <stdio.h>

float angle[20],oldX=0;

int angleID=0;

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)

{

    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);///把手臂掛回身體

    glRotatef(angle[0],0,0,1);///旋轉,對z軸轉

    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);///把手臂掛回身體

            glRotatef(angle[1],0,0,1);///旋轉,對z軸轉

            glTranslatef(-0.7,-0.4,0);///把手臂旋轉中心放在正中心

            glColor3f(0,1,0);///綠色

            glRectf(0.7,0.5,1.0,0.3);///下手肘

        glPopMatrix();

    glPopMatrix();



    glPushMatrix();///左半部

    glTranslatef(-0.3,0.4,0);///把手臂掛回身體

    glRotatef(angle[2],0,0,1);///旋轉,對z軸轉

    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);///把手臂掛回身體

            glRotatef(angle[3],0,0,1);///旋轉,對z軸轉

            glTranslatef(0.7,-0.4,0);///把手臂旋轉中心放在正中心

            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("week13 rect TRT TRT");


    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}

```

step02

file-new-project week14_angle_fprintf_fscanf

1.複製動作(壓住r鍵)

```c++

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

    }

    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)

{

    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);///把手臂掛回身體

    glRotatef(angle[0],0,0,1);///旋轉,對z軸轉

    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);///把手臂掛回身體

            glRotatef(angle[1],0,0,1);///旋轉,對z軸轉

            glTranslatef(-0.7,-0.4,0);///把手臂旋轉中心放在正中心

            glColor3f(0,1,0);///綠色

            glRectf(0.7,0.5,1.0,0.3);///下手肘

        glPopMatrix();

    glPopMatrix();



    glPushMatrix();///左半部

    glTranslatef(-0.3,0.4,0);///把手臂掛回身體

    glRotatef(angle[2],0,0,1);///旋轉,對z軸轉

    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);///把手臂掛回身體

            glRotatef(angle[3],0,0,1);///旋轉,對z軸轉

            glTranslatef(0.7,-0.4,0);///把手臂旋轉中心放在正中心

            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 angle fprintf fscanf");


    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}

```


2.

notepad++的.cbp檔之中的 working_dir="......."改成working_dir="."並存檔

codeblocks reload

freeglut.dll 從freeglut/bin複製至專案


step03

1.glutTimeFunc()計時器

```c++

#include <GL/glut.h>

#include <stdio.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");


    glutTimerFunc(1000,timer,1);

    glutTimerFunc(2000,timer,2);

    glutTimerFunc(3000,timer,3);

    glutTimerFunc(4000,timer,4);

    glutTimerFunc(5000,timer,5);

    glutDisplayFunc(display);

    glutMainLoop();

}

```



2.自動計時

```c++

#include <GL/glut.h>

#include <stdio.h>

void timer(int t)

{

    printf("起床,現在時間: %d\n",t);

    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);///設定5秒後,從0開始計時

    glutDisplayFunc(display);

    glutMainLoop();

}

```


3.播放聲音PlaySound()

#include <mmsystem.h>

PlaySound("do.wav",NULL,SND_ASYNC);

```c++

#include <GL/glut.h>

#include <stdio.h>

#include <mmsystem.h>

void timer(int t)

{

    printf("我起床囉! %d\n",t);

    PlaySound("do.wav",NULL,SND_ASYNC);

    glutTimerFunc(2000,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);///設定5秒後,從0開始計時

    glutDisplayFunc(display);

    glutMainLoop();

}

```

沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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