顯示具有 week14 標籤的文章。 顯示所有文章
顯示具有 week14 標籤的文章。 顯示所有文章

2022年6月25日 星期六

電腦圖學week14課堂筆記

 課堂內容 : fprintf()、fscanf() 寫檔和讀檔;fprintf()、fscanf() + project

1.寫檔
#include <stdio.h>
int main()
{
    FILE * fout = fopen("file.txt", "w+");
    printf("Hello World\n");
    fprintf(fout,"Hello World\n");
    fclose(fout);
}
2.讀檔
#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);
}
3.加入上週的程式
#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\n", 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();
}
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);
        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);
            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);
        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);
            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("week14");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
}
最後可以做成動畫 : 
#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();
}
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);
        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);
            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);
        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);
            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("week14");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
按 r 可以重複剛剛的動作








2022年5月25日 星期三

諺哥的電腦圖學筆記week14

 Step01

    1.介紹寫檔功能
        -Codeblocks 開啟新檔:week14-1.cpp (先在桌面建立一個資料夾,存進去)

    2.介紹讀檔功能

        -Codeblocks 開啟新檔:week14-2.cpp (先在桌面建立一個資料夾,存進去)

    3. -開啟 GLUT 新專案:week14_angle_fprintf

        -把上一周 week13_rect_many_TRT 的程式碼拿來用

Step02

    1.動畫

        -開啟 GLUT 新專案:week14_angles_fprintf_fscanf

        -copy 上一個程式碼來用

        -按 r 會轉動手臂


    2.了解為甚麼 file.txt 檔會放在 freeglut\bin 目錄裡

        -用 notepad++ 開啟 week14_angles_fprintf_fscanf.cbp

        -把下面兩個地方的路徑改成 "."

        -再把 freeglut\bin 目錄裡的 freeglut.dll 複製到專案裡頭就完成了

Step03

    1.計時器(1)

        -新增 GLUT 專案,檔名:week14_timer

        -設五個鬧鐘,每一秒叫一次


    2.計時器(2)

        -新增 GLUT 專案,檔名:week14_timer_one_by_one

        -增加鬧鐘音效,執行五秒鐘後,每一秒響一次


2022年5月24日 星期二

A

    

 week14

1.今日課程:寫檔fprintf()、讀檔fscanf()、寫檔fprintf()+project、讀檔fscanf()+project

1.1  程式碼:#include<stdio.h>

int main()

{

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


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

    printf("Hello world\n");

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

    fclose(fout);


}

1.2  程式碼:#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);


}

1.3 加上my read 按下鍵盤r可以撥放音樂

#include <opencv/highgui.h>
 #include <opencv/cv.h>  #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();///重畫畫面
}
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) ///main()主函式進階版
{
glutInit(&argc,argv); ///把參數給glutInit初始化
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH); ///雙緩衝區,3D深度功能
glutCreateWindow("第13的程式"); ///開啟GLUT視窗
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyBoard);
glutDisplayFunc(display); ///用來顯示函式
glutMainLoop();
}







VERY BEAUTIFUL, VERY POWERFUL

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