2022年5月24日 星期二

END之書 week14

 1.file output (記錄)


1.fopen() 開啟檔案

2.printf() => fprintf() file output

3.fclose() 關閉檔案


1-1

#include <stdio.h>

int main()

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

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

    printf("Hello\n");

    fprintf(fout,"Hello\n");

    fclose(fout); 關閉檔案

}


build之後會新增exe、o檔






1-2 讀檔

#include <stdio.h>

int main()

{

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

    fprintf(fout,"3.1415926\n");

    fclose(fout);


    float angle=0;

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

    fscanf(fin, "%f", &angle); %f => 小數後6位,第7由第6取概

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

    fclose(fin);

}



1-3 20座標

#include <GL/glut.h>

#include <stdio.h>

float angle[20],oldX=0;

int angleid=0;///0右手 1右手關節 2左手 3左手關節

FILE * fout = NULL;

void write()

{

    if(fout = NULL) fout = fopen("file.txt","w+");


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

    {

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

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

    }///沒有fclose

}

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

    write();

    oldX=x;

    glutPostRedisplay();

}

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

    glutCreateWindow("week13 rectangle");


    glutMouseFunc(mouse);

    glutKeyboardFunc(key);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}



1-4

#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleid=0;///0右手 1右手關節 2左手 3左手關節
FILE * fout = NULL;
void write()
{
    if(fout = NULL) fout = fopen("file.txt","w+");

    for(int i=0;i<20;i++)
    {
        printf("%.1f",angle[i]);///印出黑視窗
        fprintf(fout,"%.1f",angle[i]);///印出檔案
    }///印出20個數字
    printf("\n");///每呼叫一次,黑視窗跳行
    fprintf(fout,"\n");///每呼叫一次,檔案跳行
}沒close
void key(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);
    write();
    oldX=x;
    glutPostRedisplay();
}
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);
    glutCreateWindow("week13 rectangle");

    glutMouseFunc(mouse);
    glutKeyboardFunc(key);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}



1-5 手臂R鍵自動轉動?

#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleid=0;///0右手 1右手關節 2左手 3左手關節
FILE * fout = NULL , *fin =NULL;
void write()
{
    if(fout = NULL) fout = fopen("file.txt","w+");

    for(int i=0;i<20;i++)
    {
        printf("%.1f",angle[i]);///印出黑視窗
        fprintf(fout,"%.1f",angle[i]);///印出檔案
    }///印出20個數字
    printf("\n");///每呼叫一次,黑視窗跳行
    fprintf(fout,"\n");///每呼叫一次,檔案跳行
}

void read()
{
    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 key(unsigned char key,int x,int y)
{
    if(key=='r') read();
    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);
    write();
    oldX=x;
    glutPostRedisplay();
}
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);
    glutCreateWindow("week13 rectangle");

    glutMouseFunc(mouse);
    glutKeyboardFunc(key);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

2-1 計時器(5秒)

#include <GL/glut.h>
#include <stdio.h>
void time(int t)
{
    printf("起來,現在時間: %d\n",t);
}
void display()
{

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

    glutTimerFunc(1000,time,1);
    glutTimerFunc(2000,time,2);
    glutTimerFunc(3000,time,3);
    glutTimerFunc(4000,time,4);
    glutTimerFunc(5000,time,5);
    glutDisplayFunc(display);
    glutMainLoop();
}

2-2 持續計時

#include <GL/glut.h>
#include <stdio.h>
void time(int t)
{
    printf("起來,現在時間: %d\n",t);
    glutTimerFunc(1000,time,t+1); 不斷+1
}
void display()
{

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

    glutTimerFunc(5000,time,0);///5秒後出現第0個
    glutDisplayFunc(display);
    glutMainLoop();
}

2-3 聲音

#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
void time(int t)
{
    printf("起來,現在時間: %d\n",t);
    PlaySound("do.wav",NULL,SND_ASYNC);
    glutTimerFunc(2000,time,t+1);
}
void display()
{

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

    glutTimerFunc(5000,time,0);///5秒後出現第0個
    glutDisplayFunc(display);
    glutMainLoop();
}

#cbp檔(nodepad++) 
=> ("working dir" - ".")*2
=> project reload 
=> freeglut 
=> copy freeglut.dll
=> paste to project 
=> feel free to put stuffs in the  project

沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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