2022年5月24日 星期二

OuO圖學筆記Week14

 1-1.開 CodeBlocks/empty file-->week14-1.cpp

1-2.寫程式

#include<stdio.h>

int main(){指標fout開檔案(檔名,write模式)

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

        printf("Hello World\n");

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

    fclose(fout);關檔案

}

1-3.

#include<stdio.h>

int main(){

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

}

1-4.開上週程式改


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

    }

}

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

}


(2)

2-1.

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

    }

    printf("\n");

    fprintf(fout,"\n");

}

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

}





2-2.

#include <GL/glut.h>

#include <stdio.h>


float angle[20], oldX=0;

int angleID=0;///關節ID

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

    }f後面要加空格

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

}

長按r就會顯示錄到的動作




2-3.


    (1)用notepad++ 開 cbp檔 ---> 把working_dir 裡的路徑改成"."

    (2)把freeglut裡的freeglut.dll複製到專案中


這樣txt就會放到程式專案目錄了


(3)

3-1.開新專案week14_timer

3-2.

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

}



3-3.

#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(1000,timer,1);

    glutTimerFunc(2000,timer,2);

    glutTimerFunc(3000,timer,3);

    glutTimerFunc(4000,timer,4);

    glutTimerFunc(5000,timer,5);


    glutTimerFunc(5000,timer,0);

    glutDisplayFunc(display);

    glutMainLoop();

}



3-4.

#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(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...