顯示具有 09160234_趙敦弘 標籤的文章。 顯示所有文章
顯示具有 09160234_趙敦弘 標籤的文章。 顯示所有文章

2022年6月7日 星期二

END之書 week16

 1.alpha公式: alpha: 0.0~1.0


angle=angle*新+(1-angle)*舊


alpha: 0 => 舊

alpha: 0.5 => 新/舊

alpha: 1 => 新


1.1 動畫補間?

 #include <GL/glut.h>

#include <stdio.h>

float angle[20],oldX=0;


float newangle[20],oldangle[20];


int angleid=0;///0右手 1右手關節 2左手 3左手關節

FILE * fout = NULL , *fin =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]);///印出檔案

    }///印出20個數字

    printf("\n");///每呼叫一次,黑視窗跳行

    fprintf(fout,"\n");///每呼叫一次,檔案跳行

}


void read()

{

    if(fout != NULL) { fclose(fout); fout=NULL; }

    if(fin==NULL) fin=fopen("file.txt","r");

    for(int i=0;i<20;i++)

    {

        oldangle[i]=newangle[i];///原來新的變舊的

        fscanf(fin,"%f", &newangle[i]);讀新角度

    }

    glutPostRedisplay();///重畫畫面

}


void interpolate(float alpha)

{

    for(int i=0;i<20;i++)

    {

        angle[i]=alpha*newangle[i]+(1-alpha)*oldangle[i];

    }

}


int t=0;


void key(unsigned char key,int x,int y)

{

    if(key=='p')  ///p要一直按著

    {

        if(t%30==0) read();

        interpolate( (t%30)/30.0 );///介於0.0到1.0

        glutPostRedisplay();

        t++;

    }


    if(key=='s') write();///調好動作後存檔

    if(key=='r') read();

    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("week15 trt 2");


    glutMouseFunc(mouse);

    glutKeyboardFunc(key);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}


#先存數個座標

1-2 按一次P自動補間

 #include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;

float newangle[20],oldangle[20];

int angleid=0;///0右手 1右手關節 2左手 3左手關節
FILE * fout = NULL , *fin =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]);///印出檔案
    }///印出20個數字
    printf("\n");///每呼叫一次,黑視窗跳行
    fprintf(fout,"\n");///每呼叫一次,檔案跳行
}

void read()
{
    if(fout != NULL) { fclose(fout); fout=NULL; }
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++)
    {
        oldangle[i]=newangle[i];
        fscanf(fin,"%f", &newangle[i]);
    }
    glutPostRedisplay();///重畫畫面
}

void interpolate(float alpha)
{
    for(int i=0;i<20;i++)
    {
        angle[i]=alpha*newangle[i]+(1-alpha)*oldangle[i];
    }
}

///int t=0;

void timer(int t)
{
    if(t%50==0) read();
    interpolate( (t%50)/50.0 );///介於0.0到1.0
    glutPostRedisplay();
    glutTimerFunc(5,timer,t+1);

}

void key(unsigned char key,int x,int y)
{
    if(key=='p')
    {
        read();
        glutTimerFunc(0,timer,0);
        ///if(t%30==0) read();
        ///interpolate( (t%30)/30.0 );///介於0.0到1.0
        ///glutPostRedisplay();
        ///t++;
    }

    if(key=='s') write();///調好動作後存檔
    if(key=='r') read();
    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("week15 trt 2");

    glutMouseFunc(mouse);
    glutKeyboardFunc(key);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

2-1 運鏡

window => Projection.exe

<= eye => 鏡頭位置
<= center => 鏡頭看的位置
<= up => 畫面旋轉角度

2-2 resize

#include <GL/glut.h>

void reshape(int w,int h)   ///不能整數除法
{
    float ar=(float)w/(float)h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);///3D=>2D
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt(1,0,3,///eye
                       0,1,0,///center看哪裡
                       0,0,1);///up向量
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week16 camera");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);///範例用resize
    glutMainLoop();
}


2-3 用滑鼠改變鏡頭位置

#include <GL/glut.h>

void reshape(int w,int h)
{
    float ar=(float)w/(float)h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);///3D=>2D
    glLoadIdentity();
    gluPerspective(60,ar,1,10);

    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt(1,0,3,///eye
                       0,1,0,///center看哪裡
                       0,0,2);///up向量
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}

void motion(int x,int y)
{
    glMatrixMode(GL_MODELVIEW);///3D Model+View
    glLoadIdentity();
    gluLookAt( (x-150)/150.0 , (y-150)/150.0 , 3 ,///eye
              0,1,0,///center看哪裡
              0,0,2);///up向量
    glutPostRedisplay();
}

int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week16 camera");

    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);///範例用resize
    glutMainLoop();
}


END之書 week15

 1.播放音檔(有問題找上週)

#include <stdio.h>

#include <windows.h>

int main()

{

    printf("playsound()之前\n");                      (等待)

    PlaySound("07071104.wav",NULL,SND_SYNC);

    printf("playsound()之後\n");

}


# ASYNC 不等待,不會播放
  除非下一行非"Sound"


winmm 可以播放聲音?

SYNC+ASYNC =不用 winmm


2. SYNC(等待)、ASYNC(不等待)

#include <stdio.h>
#include <windows.h>
int main()
{
    PlaySound("07071104.wav",NULL,SND_ASYNC);
    while(1)
    {

        printf("請輸入數字: ");
        int n;
        scanf("%d",&n);
        if(n==1) PlaySound("do.wav",NULL,SND_ASYNC);
        if(n==2) PlaySound("re.wav",NULL,SND_ASYNC);
        if(n==3) PlaySound("mi.wav",NULL,SND_ASYNC);
    }
}


ASYNC => 邊執行while邊撥放、播放中可讀數字
SYNC => 先撥放再執行while、撥放完才能讀,但會記得你按過什麼數字

1-3 mp3

# wav能直接讀,mp3有壓縮過,要再多加程式


#include "CMP3_MCI.h"  moodle下載
#include <stdio.h>
CMP3_MCI mp3;///宣告變數
int main()
{
    mp3.Load("Hurry_Up.mp3");
    mp3.Play();

    printf("請輸入數字: ");
    int n;                                         讀數使音樂停止播放
    scanf("%d",&n);
}

1-4 4個動作

S鍵儲存座標,存4個複製到file.txt,最後Build按r重現儲存位置
(It work this time)

上週copy

#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleid=0;///0右手 1右手關節 2左手 3左手關節
FILE * fout = NULL , *fin =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]);///印出檔案
    }///印出20個數字
    printf("\n");///每呼叫一次,黑視窗跳行
    fprintf(fout,"\n");///每呼叫一次,檔案跳行
}

void read()
{
    if(fout != NULL) { fclose(fout); fout=NULL; }
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++)
    {
        fscanf(fin,"%f", &angle[i]);
    }
    glutPostRedisplay();///重畫畫面
}

void key(unsigned char key,int x,int y)
{
    if(key=='s') write();///調好動作後存檔
    if(key=='r') read();
    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("week15 trt 2");

    glutMouseFunc(mouse);
    glutKeyboardFunc(key);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

#(部分無記錄)

2022年5月24日 星期二

END之書 week14

 1.file output (記錄)


1.fopen() 開啟檔案

2.printf() => fprintf() file output

3.fclose() 關閉檔案


1-1

#include <stdio.h>

int main()

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

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

    printf("Hello\n");

    fprintf(fout,"Hello\n");

    fclose(fout); 關閉檔案

}


build之後會新增exe、o檔






1-2 讀檔

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

}



1-3 20座標

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

}



1-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]);///印出檔案
    }///印出20個數字
    printf("\n");///每呼叫一次,黑視窗跳行
    fprintf(fout,"\n");///每呼叫一次,檔案跳行
}沒close
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();
}



1-5 手臂R鍵自動轉動?

#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleid=0;///0右手 1右手關節 2左手 3左手關節
FILE * fout = NULL , *fin =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]);///印出檔案
    }///印出20個數字
    printf("\n");///每呼叫一次,黑視窗跳行
    fprintf(fout,"\n");///每呼叫一次,檔案跳行
}

void read()
{
    if(fout != NULL) { fclose(fout); fout=NULL; }
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++)
    {
        fscanf(fin,"%f", &angle[i]);
    }
    glutPostRedisplay();///重畫畫面
}

void key(unsigned char key,int x,int y)
{
    if(key=='r') read();
    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();
}

2-1 計時器(5秒)

#include <GL/glut.h>
#include <stdio.h>
void time(int t)
{
    printf("起來,現在時間: %d\n",t);
}
void display()
{

}
int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14 time");

    glutTimerFunc(1000,time,1);
    glutTimerFunc(2000,time,2);
    glutTimerFunc(3000,time,3);
    glutTimerFunc(4000,time,4);
    glutTimerFunc(5000,time,5);
    glutDisplayFunc(display);
    glutMainLoop();
}

2-2 持續計時

#include <GL/glut.h>
#include <stdio.h>
void time(int t)
{
    printf("起來,現在時間: %d\n",t);
    glutTimerFunc(1000,time,t+1); 不斷+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();
}

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

#cbp檔(nodepad++) 
=> ("working dir" - ".")*2
=> project reload 
=> freeglut 
=> copy freeglut.dll
=> paste to project 
=> feel free to put stuffs in the  project

2022年5月17日 星期二

END之書 week13

 1-1 四邊形


#include <GL/glut.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glRectf(0.5,0.5,-0.5,-0.5);

    glutSwapBuffers();

}

int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week13 rectangle");

    glutDisplayFunc(display);

    glutMainLoop();

}


1-2 接手臂45度?

#include <GL/glut.h>
float angle=45;
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,1);///對z軸旋轉
        glTranslatef(-0.3,-0.4,0);///手臂旋轉軸放中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week13 rectangle");
    glutDisplayFunc(display);
    glutMainLoop();
}

1-3 滑鼠控制手臂旋轉

#include <GL/glut.h>
float angle=45,oldX=0;
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle+=(x-oldX);
    oldX=x;
    glutPostRedisplay();///請glut重畫畫面
}
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,1);///對z軸旋轉
        glTranslatef(-0.3,-0.4,0);///手臂旋轉軸放中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week13 rectangle");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

1-4 增加手肘

#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle+=(x-oldX);
    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,1);///對z軸旋轉
        glTranslatef(-0.3,-0.4,0);///手臂旋轉軸放中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            ///glTranslatef(0.3,0.4,0);///手臂掛回身體
            ///glRotatef(angle,0,0,1);///對z軸旋轉
            ///glTranslatef(-0.3,-0.4,0);///手臂旋轉軸放中心
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1.5,0.3);///下手肘
        glPopMatrix();

    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week13 rectangle");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

1-5 手肘中心轉
#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle+=(x-oldX);
    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,1);///對z軸旋轉
        glTranslatef(-0.3,-0.4,0);///手臂旋轉軸放中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            ///glTranslatef(0.3,0.4,0);///手臂掛回身體
            glRotatef(angle,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);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

1-6 手肘正常旋轉
#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle+=(x-oldX);
    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,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,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);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

1-7 雙手臂

#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle+=(x-oldX);
    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,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,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,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,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);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

1-8 分節旋轉
#include <GL/glut.h>
float angle[20],oldX=0;
int angleid=0;///0右手 1右手關節 2左手 3左手關節
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);
    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();
}






VERY BEAUTIFUL, VERY POWERFUL

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