2022年5月24日 星期二

week14電腦圖學

 week14

1.讀入(fopen開啟檔案、fprintf寫檔、fclose關檔)

#include <stdio.h>

int main()

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

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

    printf("Hello\n");

    fprintf(fout,"Hello\n");

    fclose(fout); 關閉檔案

}

2.讀入+印出

#include <stdio.h>
int main()
{
    FILE * fout = fopen("file.txt", "w+");
    fprintf(fout,"3.1515926");
    fclose(fout);

    float angle=0;
    FILE * fin = fopen("file.txt", "r");
    fscanf(fin,"%f", &angle);
    printf("讀到了角度%f",angle);
    fclose(fin);
}
3.讀檔

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

}

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]);///印出檔案

    }///沒有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();

}


glutTimerFunc() 計時器

1.void timer (int t) 寫timer,glutTimerFunc( 等多久  ,  timer  ,  t參數  );

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

    glutTimerFunc(2000, timer, 1);
    glutTimerFunc(3000, timer, 2);
    glutTimerFunc(4000, timer, 3);
    glutTimerFunc(5000, timer, 4);
    glutTimerFunc(6000, timer, 5);
    glutDisplayFunc(display);
    glutMainLoop();
}
2.建立新GLUT專案week14_timer_one_by_one,設定過五秒後第0個t

#include <stdio.h>
#include <GL/glut.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( 等多久  ,  timer  ,  t參數  );

    glutTimerFunc(5000, timer, 0);

    glutDisplayFunc(display);
    glutMainLoop();
}

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

4.聲音
修改week14_timer_one_by_one的程式碼,PlaySound("do.wav", NULL , SND_ASYNC);

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

沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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