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

2019年5月31日 星期五

week13

C語言複習

開啟Console application專案


#include <stdio.h>
FILE * fout=NULL;///宣告檔案變數 指標
int main()
{
    fout = fopen("motion.txt", "w+");
    for(int i=0; i<20; i++){
           fprintf(fout,"Hello World\n");
    }
}



製作左右手的關節



控制四個關節各自轉動

#include <stdio.h> ///(0)  ///NOW4
#include <GL/glut.h>
FILE * fout=NULL;///(1) ///NOW4
FILE * fin =NULL;///(1) ///NOW5
float angle[20];///NOW
int angleID=1;///第幾個要轉動的關節 ///NOW3
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();///body
        glColor3f(1,1,1);///white
        glutSolidTeapot( 0.3 );///body

        glPushMatrix();///right Arm
            glTranslatef(0.18, 0, 0);///(3)掛到肩上
            glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
            glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心

            glColor3f(1,0,0);///red
            glutSolidTeapot( 0.2 );///right upper arm

            glPushMatrix();///NOW2 長出下手臂
                glTranslatef(0.18, 0, 0);///(3)掛到肘上
                glRotatef(angle[2], 0,0,1);///(2)轉動 ///NOW
                glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心

                glColor3f(1,0,0);///red
                glutSolidTeapot( 0.2 );///right lower arm

            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///left Arm
            glTranslatef(-0.18, 0, 0);///(3)掛到肩上
            glRotatef(angle[3], 0,0,1);///(2)轉動 ///NOW
            glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心

            glColor3f(1,0,0);///red
            glutSolidTeapot( 0.2 );///left upper arm

            glPushMatrix();///NOW2 長出下手臂
                glTranslatef(-0.18, 0, 0);///(3)掛到肘上
                glRotatef(angle[4], 0,0,1);///(2)轉動 ///NOW
                glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心

                glColor3f(1,0,0);///red
                glutSolidTeapot( 0.2 );///left lower arm

            glPopMatrix();
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
    oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
    angle[angleID] += (x-oldX);///NOW ///NOW3
    oldX = x;///NOW
    if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
    for(int i=0; i<20; i++){///NOW4
        printf( "%.1f ", angle[i]);///NOW4
        fprintf( fout, "%.1f ", angle[i]);///NOW4
    }
    printf("\n");///NOW4
    fprintf(fout, "\n");///NOW4
    display();///NOW
}
void keyboard(unsigned char key, int x, int y)///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' || key=='W'){///NOW4 小寫的 w
        if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
        for(int i=0; i<20; i++){///NOW4
            printf( "%.1f ", angle[i]);///NOW4
            fprintf( fout, "%.1f ", angle[i]);///NOW4
        }
        printf("\n");///NOW4
        fprintf(fout, "\n");///NOW4
    }
    if(key=='r'){///NOW5
        if(fin==NULL) fin = fopen("motion.txt", "r");///NOW5
        for(int i=0; i<20;i++){///NOW5
            fscanf(fin, "%f", &angle[i]);///NOW5
        }///NOW5
    }
    glutPostRedisplay();///和 display()很像,但更好 ///NOW5
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///我們可以開很大的視窗
    glutCreateWindow("week 13 motion TRT angle file");
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW
    glutMotionFunc(motion);///NOW
    glutDisplayFunc(display);

    glutMainLoop();

}

2019年5月17日 星期五

alanhc/張牧之 電腦圖學-week13

寫檔

1. 新增指標 fout FILE * fout=NULL;
2. 指定指標 fopen("檔名", 方式)  方式是 "w+"
3. 印到檔案 fprintf(指標, "要寫的東西")

1. File * fout=NULL
2. fout = fopen("filename.txt", "w+");
3. fprintf(fout, "hello world");

讀檔

1. 新增指標 fin FILE * fin =NULL;
2. 指定指標 fopen("檔名", 方式)  方式是 "r"
3. 讀檔案 fscanf(指標, "%s", line);
1. File * fin=NULL
2. fin = fopen("filename.txt", "r");
3. fscanf(fin, "hello world");

控制獨立關節轉動

1. main() 宣告
glutKeyboardFunc(keyboard);

2. 程式撰寫
> 使用一個 變數 angleID
使 motion() 裡,陣列 angle[angleID] 
可以控制 display() 裡的 angle[]
void motion(int x, int y)
{
angle[angleID] += (x-oldX);
oldX = x;
display();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='4') angleID=4;
}
圖示



紀錄/播放動作

紀錄

講解

> 使用寫檔 (參見前一章)
> 按下w開始 記錄(write)
> fprintf( 指標, "要印的東西" ) 

keyboard() 裡,加入以下程式
if (key=='w') {
if (fout==NULL) fout=fopen("motion.txt", "w+");
for (int i=0; i<5; i++) {
printf( "%.1f ", angle[i] );
fprintf( fout, "%.1f ", angle[i]);
}
printf("\n");
fprintf(fout, "\n");
}

if (fout==NULL) 檢查是否有開檔

for () 一次寫5個關節

在motion()裡面也加
if(fin==NULL) fin = fopen("motion.txt", "r");
for(int i=0; i<20;i++){
fscanf(fin, "%f", &angle[i]);
}
printf("\n");
fprintf(fout, "\n");


在freeglut\bin資料夾底下找 motion.txt 就可以找到剛剛紀錄的座標

播放

講解

> 使用讀檔 (參見前一章)
> 按下r開始 記錄(write)
> fscanf( 指標, "要讀的東西" ) 

if(key=='r'){
if(fin==NULL) fin = fopen("motion.txt", "r");
for(int i=0; i<20;i++){
fscanf(fin, "%f", &angle[i]);
}
}
glutPostRedisplay();

*glutPostRedisplay(); 加上這 就會重新畫,否則只有按下鍵盤才畫
終於大功告成拉拉拉拉拉拉拉拉拉哈哈哈哈哈哈哈哈哈哈哈哈哈!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


小葉閒聊time 

規劃的重要性

unitize 轉成單位

分開存,才能分開畫




粉色の兔子 week13










耀鑫的筆記week13

1.開啟(Console application)專案

2.程式
#include <stdio.h> FILE * fout=NULL;///宣告 檔案指標 變數 int main() { fout=fopen("filename.txt","w+");///開啟檔案 W+:寫入、更新 for(int i=1;i<20;i++) { fprintf(fout,"Hello World\n");///印在檔案中 } }
3.茶壺練習
#include <GL/glut.h> void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glutSolidTeapot( 0.3 ); glutSwapBuffers(); } int main(int argc, char**argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///glutInitWindowSize(600,600);///我們可以開很大的視窗 glutCreateWindow("week 13 motion TRT angle file"); glutDisplayFunc(display); glutMainLoop(); }
4.茶壺進階
#include <GL/glut.h> float angle[20];///NOW void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix();///body glColor3f(1,1,1);///white glutSolidTeapot( 0.3 );///body glPushMatrix();///right Arm glTranslatef(0.18, 0, 0);///(3)掛到肩上 glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///right upper arm glPopMatrix(); glPopMatrix(); glutSwapBuffers(); } int oldX;///NOW void mouse(int button, int state, int x, int y)///NOW { oldX = x;///NOW } void motion(int x, int y)///NOW { angle[1] += (x-oldX);///NOW oldX = x;///NOW display();///NOW } int main(int argc, char**argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///glutInitWindowSize(600,600);///我們可以開很大的視窗 glutCreateWindow("week 13 motion TRT angle file"); glutMouseFunc(mouse);///NOW glutMotionFunc(motion);///NOW glutDisplayFunc(display); glutMainLoop(); }
5.茶壺左右手
glPushMatrix();///body glColor3f(1,1,1);///white glutSolidTeapot( 0.3 );///body glPushMatrix();///right Arm glTranslatef(0.18, 0, 0);///(3)掛到肩上 glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///right upper arm
glPushMatrix();///right Arm
glTranslatef(0.18, 0, 0);///(3)掛到手肘上 glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///right upper arm
glPopMatrix(); glPopMatrix();
glPushMatrix();///left Arm glTranslatef(-0.18, 0, 0);///(3)掛到肩上 glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///left upper arm glPushMatrix();///left Arm glTranslatef(-0.18, 0, 0);///(3)掛到手肘上 glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///left upper arm glPopMatrix(); glPopMatrix();
glPopMatrix();
6.茶壺手腳

#include <stdio.h> ///(0) ///NOW4 #include <GL/glut.h> FILE * fout=NULL;///(1) ///NOW4 FILE * fin =NULL;///(1) ///NOW5 float angle[20];///NOW int angleID=1;///第幾個要轉動的關節 ///NOW3 void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix();///body glRotatef(0,0,1,0); glColor3f(1,1,1);///white glutSolidTeapot( 0.3 );///body glPushMatrix();///right Arm glTranslatef(0.18, 0, 0);///(3)掛到肩上 glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///right upper arm glPushMatrix();///right Arm glTranslatef(0.18, 0, 0);///(3)掛到手肘上 glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///right upper arm glPopMatrix(); glPopMatrix(); glPushMatrix();///left Arm glRotatef(180,0,1,0); glTranslatef(0.18, 0, 0);///(3)掛到肩上 glRotatef(angle[2], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///left upper arm glPushMatrix();///left Arm glTranslatef(0.18, 0, 0);///(3)掛到手肘上 glRotatef(angle[2], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///left upper arm glPopMatrix(); glPopMatrix(); glPushMatrix();///left Arm glRotatef(180,0,1,0); glRotatef(-90,0,0,1); glTranslatef(0.3,0.3, 0);///(3)掛到肩上 glRotatef(angle[3], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///left upper arm glPushMatrix();///left Arm glTranslatef(0.18, 0, 0);///(3)掛到手肘上 glRotatef(angle[3], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///left upper arm glPopMatrix(); glPopMatrix(); glPushMatrix();///right Arm glRotatef(-90,0,0,1); glTranslatef(0.3, 0.3, 0);///(3)掛到肩上 glRotatef(angle[4], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///right upper arm glPushMatrix();///right Arm glTranslatef(0.18, 0, 0);///(3)掛到手肘上 glRotatef(angle[4], 0,0,1);///(2)轉動 ///NOW glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心 glColor3f(1,0,0);///red glutSolidTeapot( 0.2 );///right upper arm glPopMatrix(); glPopMatrix(); glPopMatrix(); glutSwapBuffers(); } int oldX;///NOW void mouse(int button, int state, int x, int y)///NOW { oldX = x;///NOW } void motion(int x, int y)///NOW { angle[angleID] += (x-oldX);///NOW ///NOW3 oldX = x;///NOW if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4 for(int i=0; i<20; i++){///NOW4 printf( "%.1f ", angle[i]);///NOW4 fprintf( fout, "%.1f ", angle[i]);///NOW4 } printf("\n");///NOW4 fprintf(fout, "\n");///NOW4 display();///NOW } void keyboard(unsigned char key, int x, int y)///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' || key=='W'){///NOW4 小寫的 w if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4 for(int i=0; i<20; i++){///NOW4 printf( "%.1f ", angle[i]);///NOW4 fprintf( fout, "%.1f ", angle[i]);///NOW4 } printf("\n");///NOW4 fprintf(fout, "\n");///NOW4 } if(key=='r'){///NOW5 if(fin==NULL) fin = fopen("motion.txt", "r");///NOW5 for(int i=0; i<20;i++){///NOW5 fscanf(fin, "%f", &angle[i]);///NOW5 }///NOW5 } glutPostRedisplay();///和 display()很像,但更好 ///NOW5 } int main(int argc, char**argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///glutInitWindowSize(600,600);///我們可以開很大的視窗 glutCreateWindow("week 13 motion TRT angle file"); glutKeyboardFunc(keyboard);///NOW3 glutMouseFunc(mouse);///NOW glutMotionFunc(motion);///NOW glutDisplayFunc(display); glutMainLoop(); }