2022年5月24日 星期二

電腦圖學筆記

 File-New-Empty File 存檔改名week14-1.cpp

程式碼 :

#include <stdio.h>

int main(){

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

    printf("HELLO WORLD\n");

    fprintf(fout,"HELLO WORLD\n");

    fclose(fout);

}

=======================================================================
再開一個File-New-Empty File 存檔改名week14-2.cpp

#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. 先建立一個資料夾 week14-3_ fprintf
  2. open-empty file 存檔為 week14-3.cpp
  3. 貼程式碼
 #include <GL/glut.h>

#include <stdio.h>///printf用

float angle[4] , oldx=0;///angle變陣列以致可以放更多選轉角度

int angleID=0;///0號關節 1號關節

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)///根據按數字鍵多少決定第幾個關節要轉動

{ ///按數字鍵決定要改變哪個angleID

    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);///angle變成陣列

    oldx=x;

    mywrite();///執行用

    glutPostRedisplay();///請glut重新re display

}

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軸轉 ///angle[0] 第一個值

        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);;///(3)拉回到右上角

            glRotatef(angle[1],0,0,1);///(2)旋轉角度  ///angle[1] 第二個值

            glTranslatef(-0.7,-0.4,0);///(1)將軸心放到世界中心

            glColor3f(0,1,10);

            glRectf(0.7 , 0.5 , 1.0 , 0.3);///右上手臂

        glPopMatrix();

    glPopMatrix();

///左手跟右手的X軸相反,所以要加負號

    glPushMatrix();

        glTranslatef(-0.3,0.4,0);///

        glRotatef(angle[2],0,0,1);///旋轉 對Z軸轉  ///angle[2] 第三個值

        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);///(3)拉回到左上角

            glRotatef(angle[3],0,0,1);///(2)旋轉角度  ///angle[3] 第四個值

            glTranslatef(0.7,-0.4,0);///(1)將軸心放到世界中心

            glColor3f(0,1,10);

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

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}






沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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