2022年6月26日 星期日

7ru week17

 


week16-許皓翔

 


OldAngle[i]=NewAngle[i];
        fscanf(fin,"%f",&NewAngle[i]);


void myInterpolate(float alpha)
{
    for(int i=0;i<20;i++)
    {
    angle[i]=alpha*NewAngle[i]+(1-alpha)*OldAngle[i];
    printf("%.2f",angle[i]);
    }
    printf("\n");
    glutPostRedisplay();
}





void timer(int t)
{
    float alpha =(t%50)/50.0;
    if(t%50==0)myRead();
    myInterpolate(alpha);
    glutTimerFunc(33,timer,t+1);
}


eye是看出去的
center是看的中心
up是攝影機的旋轉



aspect ratio=寬度/高度




#include<GL/glut.h>

void motion(int x,int y)
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt((x-150)/15.0,(y-150)/15.0,3,
              0,0,0,
              0,1,0);
    glutPostRedisplay();
}
 void reshape(int w, int h)
{///aspect ratio
    const float ar = (float) w / (float) h;

    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);///投影,把3D投影到2D
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);
    //glFrustum(, ar, -1.0, 1.0, 2.0, 100.0);
    ///3D經過轉換到你最後的攝影機

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
    gluLookAt(0,0,3,0,0,0,0,1,0);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //glColor3f(1,1,0);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week16 camera lookat");



glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMotionFunc(motion);



glutMainLoop();
}









week15-許皓翔

 

新的碼


案Setting ->compiler->linker seeting->add->輸入winmm-

SND_ASYNC練習

mp3撥放
改成每案一下"S"存檔一次
白色鋼彈













week14-讀寫

 

練習開檔關檔





加上以下程式碼
FILE *fout=NULL;
void myWrite()
{
    if(fout==NULL)
        fout=fopen("file.txt","w+");
    for(int i=0;i<20;i++)
    {
        fprintf(fout,"%.2f",angle[i]);
    }
}

 myWrite();




按著R會自己移動(有吃檔案)


用notpad++改workin dir=""




時間軸
會發更新率不一樣



可以有聲音

2022年6月24日 星期五

week13-手臂

 先做出一個正方形


做出小手臂



放到中心




轉角度


移動放置位置







多加一個小手臂

複製一個完整手臂在另一邊

可依讓手臂被控制







2022年6月23日 星期四

學習筆記week17

今天老師的電腦怪怪的,一直發生電腦當機的情況...
他打算提早下課重灌電腦,所以今天沒上很多,可能也沒有什麼特別的重點。

是說我們的期末作品也該開始趕了...


學習筆記week16

上周做出了一個有包含關節的人,可記錄動作亦可讀取內容。但目前只能一步一步來,動作不流暢,感覺所有的關節都瞬間移動了。所以這次要來分割動作,將一步變成幾十步。

time: 這個動作到下個動作之前經過了多少時間(例:0.5表示經過了一半) ,數值可以從0到無限大。
alpha: 幾乎跟time一樣,只不過這是為了做運算用的,而且因為牽扯到函式,數值只能在0到1之間。
angle: 一個關節的角度,可以是任何數值。

因為會用到數學函式,我請來了Excel來幫忙計算。
現在來看看這份表格:

如果開始角度是A,結束時角度是B,則各角度為: B*alpha+(1-alpha)*A
alpha數值雖然只能在0到1之間,但是可以被切成無限多份。
0到1之間的數字越多速度越慢,動作會更流暢。
相反地,0到1之間的數字越多速度越快,但動作更不流暢。

如果要做成動畫,這唯一的函數會運用好幾百次!




學習筆記 week15

先來複習怎麼發出聲音吧


1.創建資料夾(week15_1)
2.開codeblocks創建新檔案
3.存成week15_1.cpp
4.放上程式碼:

#include <windows.h>
int main()
{
    PlaySound("TestSound.wav",NULL,SND_SYNC);
}

5.將TestSound.wav放在week15_1裡面
6.在執行之前別忘了確認winmm資料庫在不在(Settings>>Complier>>Linker Settings),如果沒有,只需要按add即可
7.執行
一小段聲音就這樣被播放了


PlaySound("TestSound.wav",NULL,SND_SYNC); 可以換成:
PlaySound("TestSound.wav",NULL,SND_ASYNC);

為了比較一下,我們需要<stdio.h>

#include <windows.h>
#include <stdio.h>
int main()
{
    printf("Start\n");
    PlaySound("TestSound.wav",NULL,SND_SYNC);
    printf("End\n");
}

使用SND_SYNC時,Start會先跑出來,播放聲音後,在顯示End。 
換成SND_ASYNC後,Start跟End同時出現,不等待聲音播完。如果程式碼太短導致聲音來不及播放,可以用個scanf拖延一下時間。

目前這個資料庫只能播放wav檔,想播放mp3檔案時...

1.保留現在這個檔案
2.下載CMP3_MCI.h(應該有個地方可以下載,當時老師沒有特別解釋)
3.跟音檔一樣,放在同一個資料夾裡面
4.將TestSound.wav轉成TestSound.mp3,放在同一個資料夾裡面
不要直接改檔按形式(在.wav 後面按BackSpace),會出錯!
5.
#include <windows.h>
#include <stdio.h>
#include "CMP3_MCI.h"
CMP3_MCI mp3;
int main()
{
    printf("Start\n");
    mp3.Load("TestSound.mp3");
    mp3.Play();
    int n;
    scanf("%d",&n);
    printf("End\n");
}

這東西沒有同步的問題,所以必須用scanf,我們也暫時不會需要n



接著,回到上周的存檔讀檔... 最後的檔案應該會像這樣子
#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<8;i++){
        printf("%.2f ",angle[i]);
        fprintf(fout,"%.2f",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<8; i++){
        fscanf(fin,"%f",&angle[i]);
    }
    glutPostRedisplay();
}


void keyboard(unsigned char key,int x,int y)
{
    if(key=='s')myWrite(); ///加這一行
    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.2);
    glPushMatrix();
        glTranslatef(0.3,0.5,0);
        glRotatef(angle[0],0,0,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.8,0.3);
        glPushMatrix();
            glTranslatef(0.8,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.8,-0.4,0);
            glColor3f(0,1,0);
            glRectf(0.8,0.5,1.1,0.3);
            glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.5,0);
        glRotatef(angle[2],0,0,1);
        glTranslatef(0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.8,0.3);
        glPushMatrix();
            glTranslatef(-0.8,0.4,0);
            glRotatef(angle[3],0,0,1);
            glTranslatef(0.8,-0.4,0);
            glColor3f(0,1,0);
            glRectf(-0.8,0.5,-1.1,0.3);
            glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main (int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week14_2");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

現在,程式不會再隨便產生資料了。只有你確定姿勢,按下S後這個動作才會被存起來,當然,按R也可以重播動作。但是原本上千筆資料在變成只有幾十筆資料後會變成一筆一筆播放,很不流暢。下周會再檢討如何將動畫變得流暢一點。






學習筆記 week14

可以自由做出想要的動作之後,接著就可以開始製作動畫了
動畫是由很多個動作所組成的,因此我們需要一張包含所有動作的清單。

在此之前,先來了解如何創建檔案
先建立一個空資料夾
創建一個檔案(week14_1.cpp)
放入程式碼:
#include <stdio.h>
int main()
{
    ///如何寫檔:
    FILE*fout=fopen("file.txt","w+");
    fprintf(fout,"angle1 %d\n", 999);
    fclose(fout);
    ///如何讀檔:
    char line[200];
    int a;
    FILE*fin=fopen("file.txt","r");
    fscanf(fin, "%s %d",line,&a);
    printf("Char:%s Int:%d\n",line,a);
    fclose(fin);
}

不需要在意file.txt,等等他會自己生成
現在,資料應該可以被正常讀取了



#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<8;i++){
        printf("%.2f ",angle[i]);
        fprintf(fout,"%.2f",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<8; 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.2);
    glPushMatrix();
        glTranslatef(0.3,0.5,0);
        glRotatef(angle[0],0,0,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.8,0.3);
        glPushMatrix();
            glTranslatef(0.8,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.8,-0.4,0);
            glColor3f(0,1,0);
            glRectf(0.8,0.5,1.1,0.3);
            glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.5,0);
        glRotatef(angle[2],0,0,1);
        glTranslatef(0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.8,0.3);
        glPushMatrix();
            glTranslatef(-0.8,0.4,0);
            glRotatef(angle[3],0,0,1);
            glTranslatef(0.8,-0.4,0);
            glColor3f(0,1,0);
            glRectf(-0.8,0.5,-1.1,0.3);
            glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main (int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week14_2");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}

利用這項功能,我們可以輕鬆紀錄每個動作。但是因為寫檔程式會將全部的內容刪掉後重頭開始寫...所以一次不要做太多,並請務必備份!








week16

 1.內插

用excel學習內插


 2.鏡頭

```c
#include <GL/glut.h>
void motion(int x,int y)
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt((x-150)/15.0,(y-150)/15.0,3,
              0,0,0,
              0,1,0);
    glutPostRedisplay();
}
 void reshape(int w, int h)
{///aspect ratio
    const float ar = (float) w / (float) h;

    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);///投影,把3D投影到2D
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);
    //glFrustum(, ar, -1.0, 1.0, 2.0, 100.0);
    ///3D經過轉換到你最後的攝影機

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
    gluLookAt(0,0,3,0,0,0,0,1,0);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //glColor3f(1,1,0);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week16 camera lookat");



glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMotionFunc(motion);



glutMainLoop();
}
```c

week15

複習怎麼發出聲音

way1:

將TestSound.wav放在程式執行檔裡面
在執行之前Settings>>Complier>>Linker Settings加入winmm

```c

#include <windows.h>
int main()
{
    PlaySound("TestSound.wav",NULL,SND_SYNC);
}

```c

way2:
下載CMP3_MCI.h
放在程式的資料夾裡
```c

#include <windows.h>
#include <stdio.h>
#include "CMP3_MCI.h"
CMP3_MCI mp3;
int main()
{
    printf("Start\n");
    mp3.Load("TestSound.mp3");
    mp3.Play();
    int n;
    scanf("%d",&n);
    printf("End\n");
}
```c


把聲音加入上週的程式碼
```c
#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<8;i++){
        printf("%.2f ",angle[i]);
        fprintf(fout,"%.2f",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<8; i++){
        fscanf(fin,"%f",&angle[i]);
    }
    glutPostRedisplay();
}


void keyboard(unsigned char key,int x,int y)
{
    if(key=='s')myWrite(); ///加這一行
    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.2);
    glPushMatrix();
        glTranslatef(0.3,0.5,0);
        glRotatef(angle[0],0,0,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.8,0.3);
        glPushMatrix();
            glTranslatef(0.8,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.8,-0.4,0);
            glColor3f(0,1,0);
            glRectf(0.8,0.5,1.1,0.3);
            glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.5,0);
        glRotatef(angle[2],0,0,1);
        glTranslatef(0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.8,0.3);
        glPushMatrix();
            glTranslatef(-0.8,0.4,0);
            glRotatef(angle[3],0,0,1);
            glTranslatef(0.8,-0.4,0);
            glColor3f(0,1,0);
            glRectf(-0.8,0.5,-1.1,0.3);
            glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main (int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week14_2");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
    return 0;
}
```c

2022年6月22日 星期三

week12-筆記




 


2-1.去https://120.125.80.50/GL/





glTranslatef(0.4,-0.07,0);



加上變數angle
glRotatef(angle,0,0,1);
angle+=0.1;
glutIdleFunc(display);





glTranslatef(0.3,0,0);
glTranslatef(0.2,0,0);


複製小茶壺到PopMastrix();