2021年6月7日 星期一

week16

#include <stdio.h>

#include <GL/glut.h>

float angle=0, diff=2;///上週程式,會增加、會減少!!!

///float angle[20]={};//都是0的陣列 angle

void timer(int t)///上週程式,會增加、會減少!!!

{

    glutTimerFunc( 30, timer, t+1 );///上週程式 設定新的timer

    angle += diff;///上週程式,會增加、會減少!!!

    if(angle>90) diff = -2;///上週程式,會增加、會減少!!!

    if(angle<0) diff = +2;///上週程式,會增加、會減少!!!

    glutPostRedisplay();///上週教display();

}

void display()

{

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glPushMatrix();///Step05-2

        glutSolidTeapot( 0.3 );///身體

        glPushMatrix();///Step05-2

            glTranslatef(-0.3,0,0);

            glRotatef(angle, 0,0,1);

            glTranslatef(-0.3,0,0);

            glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置

            glPushMatrix();

                glTranslatef(-0.3, 0,0);

                glRotatef(angle, 0,0,1);

                glTranslatef(-0.3, 0,0);

                glutSolidTeapot( 0.3 );///左手肘

            glPopMatrix();

        glPopMatrix();///Step05-2

        glPushMatrix();///Step05-2

            glTranslatef(+0.3,0,0);

            glRotatef(-angle, 0,0,1);

            glTranslatef(+0.3,0,0);

            glutSolidTeapot( 0.3 );///右手臂

            glPushMatrix();///step05-3

                glTranslatef(+0.3,0,0);

                glRotatef(-angle, 0,0,1);

                glTranslatef(+0.3,0,0);

                glutSolidTeapot( 0.3 );///右手肘

            glPopMatrix();///Step05-3

        glPopMatrix();///Step05-2

    glPopMatrix();///Step05-2

    glutSwapBuffers();

}

int main( int argc, char** argv )

{

    glutInit( &argc, argv );

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow( "week15 many angles" );


    glutDisplayFunc( display );

    glutTimerFunc( 0, timer, 0);

    glutMainLoop();

}///先不放貼圖, 先不放打光

開啟上週檔案
#include <stdio.h>
#include <GL/glut.h>
float diff=2;///上週程式,會增加、會減少!!!
float angle[20]={};//都是0的陣列 angle
int angleID=0;//有20個angle 利用angle[0] angle[1]....angle[ID]
void timer(int t)///上週程式,會增加、會減少!!!
{
    glutTimerFunc( 30, timer, t+1 );///上週程式 設定新的timer
    angle[angleID] += diff;///上週程式,會增加、會減少!!!
    if(angle[angleID]>90) diff = -2;///上週程式,會增加、會減少!!!
    if(angle[angleID]<0) diff = +2;///上週程式,會增加、會減少!!!
    glutPostRedisplay();///上週教display();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') angleID=0;//timer()會改變關節 angle[0]
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();///Step05-2
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///Step05-2
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
            glPushMatrix();
                glTranslatef(-0.3, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(-0.3, 0,0);
                glutSolidTeapot( 0.3 );///左手肘
            glPopMatrix();
        glPopMatrix();///Step05-2
        glPushMatrix();///Step05-2
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2], 0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot( 0.3 );///右手臂
            glPushMatrix();///step05-3
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3], 0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot( 0.3 );///右手肘
            glPopMatrix();///Step05-3
        glPopMatrix();///Step05-2
    glPopMatrix();///Step05-2
    glutSwapBuffers();
}
int main( int argc, char** argv )
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow( "week15 many angles" );

    glutKeyboardFunc(keyboard);
    glutDisplayFunc( display );
    glutTimerFunc( 0, timer, 0);
    glutMainLoop();
}///先不放貼圖, 先不放打光
設一個angle陣列配合keyboard即可配合四個關節分開做轉動
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={},diff=2;
int angleID=0;///現在有20個angle (angle[0],angle[1]...angle[angleID])
void timer(int t)
{///timer甚麼都不做
///    glutTimerFunc(30,timer,t+1);
}
int oldX=0,oldY=0;
void mouse(int button ,int state, int x, int y)
{
    oldX=x;
    oldY=y;
}
FILE * fout =NULL;
void motion(int x,int y)
{
    angle[angleID] += x - oldX;
    oldX=x;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x ,int y)
{
    if(key=='0')angleID=0; ///之後timer()會改變關節 angle[0]
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
    if(key=='s')
    {
        if(fout==NULL)fout=fopen("angle.txt","w+");
        for(int i=0;i<20;i++)printf("%.2f ", angle[i] );
        printf("\n");
        for(int i=0;i<20;i++)fprintf(fout, "%.2f ", angle[i] );
        fprintf( fout,"\n");
    }
}
void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();///Step05-2
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///Step05-2
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
            glPushMatrix();
                glTranslatef(-0.3, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(-0.3, 0,0);
                glutSolidTeapot( 0.3 );///左手肘
            glPopMatrix();
        glPopMatrix();///Step05-2
        glPushMatrix();///Step05-2
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2], 0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot( 0.3 );///右手臂
            glPushMatrix();///step05-3
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3], 0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot( 0.3 );///右手肘
            glPopMatrix();///Step05-3
        glPopMatrix();///Step05-2
    glPopMatrix();///Step05-2
    glutSwapBuffers();
}
int main(int argc, char** argv)
{
    glutInit( &argc,argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week16    ");

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutTimerFunc(0,timer,0);
    glutMainLoop();
}
按下0 1 2 3可以選擇關節然後使用滑鼠做關節的移動 
按下s可以存檔和顯示角度


整理程式碼並且修改
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={},diff=2;
int angleID=0;///現在有20個angle (angle[0],angle[1]...angle[angleID])
void timer(int t)
{///timer甚麼都不做
///    glutTimerFunc(30,timer,t+1);
}
int oldX=0,oldY=0;
void mouse(int button ,int state, int x, int y)
{
    oldX=x;
    oldY=y;
}
FILE * fout =NULL;
FILE * fin =NULL;
void motion(int x,int y)
{
    angle[angleID] += x - oldX;
    oldX=x;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x ,int y)///keyboard()需要glutkeyboardfunc(keyboard)
{
    if(key=='0')angleID=0; ///之後timer()會改變關節 angle[0]
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
    if(key=='s')
    {
        if(fout==NULL)fout=fopen("angle.txt","w+");//若NULL fopen()
        for(int i=0;i<20;i++)printf("%.2f ", angle[i] );
        printf("\n");
        for(int i=0;i<20;i++)fprintf(fout, "%.2f ", angle[i] );
        fprintf( fout,"\n");
    }
    if(key=='r'){
        if(fin==NULL)fin=fopen("angle.txt","r");
        for(int i =0;i<20;i++) fscanf(fin,"%f",&angle[i]);
        glutPostRedisplay();
    }
}
void display()
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();///Step05-2
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///Step05-2
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
            glPushMatrix();
                glTranslatef(-0.3, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(-0.3, 0,0);
                glutSolidTeapot( 0.3 );///左手肘
            glPopMatrix();
        glPopMatrix();///Step05-2
        glPushMatrix();///Step05-2
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2], 0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot( 0.3 );///右手臂
            glPushMatrix();///step05-3
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3], 0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot( 0.3 );///右手肘
            glPopMatrix();///Step05-3
        glPopMatrix();///Step05-2
    glPopMatrix();///Step05-2
    glutSwapBuffers();
}
int main(int argc, char** argv)
{
    glutInit( &argc,argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week16    ");

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutTimerFunc(0,timer,0);
    glutMainLoop();
}
按下f9執行之後馬上按r即可讀進剛剛的angle.txt並且套用在裡面


更改working_dir並且把freeglut.dll丟進去專案檔
if(fout==NULL)fout=fopen("angle.txt","w+");//若NULL fopen()
for(int i=0;i<20;i++)printf("%.2f ", angle[i] );
printf("\n");
for(int i=0;i<20;i++)fprintf(fout, "%.2f ", angle[i] );
fprintf( fout,"\n");
放入void motion()的最後面
FILE * fout =NULL;
FILE * fin =NULL;
#include <stdio.h>
這三行要記得放在void motion()的上面
接下來即可完成一邊motion一邊存檔



沒有留言:

張貼留言

Week18期末作業(橘貓的跳舞熊熊)

 期末作業(橘貓的跳舞熊熊) 影片: https://youtu.be/R89tptMaQZw 程式碼: #include <opencv/highgui.h> #include <opencv/cv.h> #include <GL/glut.h...