2019年5月16日 星期四

W13同是腰間盤就你最突出

Week13 課堂重點


主題:Blending and Special Effects


I 實作


1、開啟codeblocks new poject 如圖




2、把原程式刪除鍵入需要的程式碼


#include <stdio.h>

FILE * fout=NULL;

int main()

{

    fout=fopen("filename.txt","w+");

    for(int i=0;i<20;i++){
        fprintf(fout,"Hello world.\n");
    }
}

3、執行後會在檔案資料夾出現我們設定的檔案


點開就是執行的hello world


※W+是write/update


4、開新檔案

加上角度陣列


照上一部點開就是角度


II 實作


1、下載Freeglut windows檔案解壓縮


打開projectlibglut32.a

2、鍵入基礎程式碼


3、加上手臂



4、加上移動關節的程式部分如圖



讓動作流暢↓



5、左邊複製右邊的改glTranslatef裡的負號如圖



6、成果



7、改成左右旋轉幅度對稱

且跟著滑鼠移動



把左邊的旋轉角度加個負號如圖

跟著滑鼠移動加上glutMotionFunc(motion);如圖



8、成果



9、分開各個關節轉動





設定4個關節

加上鍵盤跟滑鼠控制的設定



加上glutKeyboardFunc(kb);程式才會知道(如圖)



成果



10、加上印出角度




成果




III 實作


1、加上最一開始練習的寫檔案的程式





2、執行後檔案就會出現在freeglut的bin資料夾


點開


3、加上讀資料的程式

把剛剛write的檔案讀入執行




#include <GL/glut.h> #include <stdio.h>///NOW_FILE (0) float angle[20];///NOW2 int angleID=1;///NOW3 FILE * fout = NULL;///NOW_FILE (1) FILE * fin = NULL;///NOW_FILE_READ (1) void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glColor3f(1, 1, 1);///white glutSolidTeapot( 0.3 );///body glPushMatrix();///右邊 glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去 glRotatef(angle[1], 0,0,1);///NOW2 ///(2)旋轉 glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心 glColor3f(1, 0, 0);///red glutSolidTeapot( 0.2 );///right arm glPushMatrix(); glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去 glRotatef(angle[2], 0,0,1);///NOW2 ///(2)旋轉 glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心 glColor3f(1, 0, 0);///red glutSolidTeapot( 0.2 );///right lower arm glPopMatrix(); glPopMatrix(); glPushMatrix();///左邊 glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去 glRotatef(angle[3], 0,0,1);///NOW2 ///(2)旋轉 glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心 glColor3f(1, 0, 0);///red glutSolidTeapot( 0.2 );///right arm glPushMatrix(); glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去 glRotatef(angle[4], 0,0,1);///NOW2 ///(2)旋轉 glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心 glColor3f(1, 0, 0);///red glutSolidTeapot( 0.2 );///right lower arm glPopMatrix(); glPopMatrix(); glPopMatrix(); glutSwapBuffers(); } int oldX=0;///NOW2 void mouse(int button, int state, int x, int y)///NOW2 {///NOW2 oldX = x;///NOW2 } void motion(int x, int y)///NOW2 {///NOW2 angle[angleID] += x-oldX;///NOW3 oldX = x;///NOW2 if(fout == NULL) fout = fopen("motion.txt", "w+");///NOW_FILE (2) for(int i=0;i<20;i++){///NOW3 printf(" %.2f ", angle[i]);///NOW3 fprintf(fout, " %.2f ", angle[i]);///NOW_FILE } printf("\n");///NOW3 fprintf(fout, "\n");///NOW_FILE display();///NOW2 } void keyboard(unsigned char key, int x, int y)///NOW3 {///NOW3 if(key=='1') angleID=1;///NOW3 if(key=='2') angleID=2;///NOW3 if(key=='3') angleID=3;///NOW3 if(key=='4') angleID=4;///NOW3 if(key=='w'){///NOW_FILE fout = fopen("motion.txt", "w+");///NOW_FILE (2) } if(key=='r'){ if(fin==NULL) fin = fopen("motion.txt", "r");///NOW_FILE_READ (2) ///scanf("%d", &n); for(int i=0;i<20; i++){///NOW_FILE_READ fscanf(fin, "%f", &angle[i]);///NOW_FILE_READ printf(" %.2f ", angle[i]);///順便印出來看數字對不對 } } glutPostRedisplay();///NOW_FILE_READ 和 display()很像 } int main(int argc, char**argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///glutInitWindowSize(600,600);///可以開大一點的window glutCreateWindow("Week 13 angle motion file"); glutKeyboardFunc(keyboard);///NOW3 glutMouseFunc(mouse);///NOW2 glutMotionFunc(motion);///NOW2 glutDisplayFunc(display); glutMainLoop(); }


成果

(類似錄影的功能,把之前的motioin再執行一次)



沒有留言:

張貼留言