2021年6月7日 星期一

是我是我詐騙 Week16

 

🎥 動畫 🎥


1.拿上週的程式


程式碼如下:

#include <stdio.h>
#include <GL/glut.h>
float diff=2,angle[20]={};   ///把值換成陣列
int angleID=0;

void timer(int t)
{
    glutTimerFunc(30,timer,t+1);
    angle+=diff;
    if(angle>90)diff=-2;
    if(angle<0)diff=+2;
    glutPostRedisplay();
}
void keyboard(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 display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle,0,0,1);
                glTranslatef(-0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle,0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle,0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("08160554");
    glutDisplayFunc(display);
    glutTimerFunc(0,timer,0);

    glutMainLoop();
}


2.讓他一次動一個關節



程式碼如下:

#include <stdio.h>
#include <GL/glut.h>
float diff=2,angle[20]={};
int angleID=0;

void timer(int t)
{
    glutTimerFunc(30,timer,t+1);
    angle[angleID]+=diff; 
    if(angle[angleID]>90)diff=-2;
    if(angle[angleID]<0)diff=+2;
    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;
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0],0,0,1); ///把angle都改成陣列
            glTranslatef(-0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle[1],0,0,1);
                glTranslatef(-0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2],0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3],0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("08160554");
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);

    glutTimerFunc(0,timer,0);

    glutMainLoop();
}



3.用mouse motion操作關節



程式碼如下:

#include <stdio.h>
#include <GL/glut.h>
float diff=2,angle[20]={};
int angleID=0;

void timer(int t)
{
    /*glutTimerFunc(30,timer,t+1);
    angle[angleID]+=diff;
    if(angle[angleID]>90)diff=-2;
    if(angle[angleID]<0)diff=+2;
    glutPostRedisplay();*/
}
///用mouse motion操作關節
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
///mouse 按下去,記下現在的位置
{
    oldX=x;
    oldY=y;
}
void motion(int x,int y)
///mouse motion時,會叫這個motion
}
    angle[angleID]+=x-oldX;
    glutPostRedisplay();
}
void keyboard(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 display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0],0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle[1],0,0,1);
                glTranslatef(-0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2],0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3],0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("08160554");
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);

    glutTimerFunc(0,timer,0);

    glutMainLoop();
}



4.按S存檔


程式碼如下:

#include <stdio.h>
#include <GL/glut.h>
float diff=2,angle[20]={};
int angleID=0;

void timer(int t)
{
}
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
{
    oldX=x;
    oldY=y;
}
void motion(int x,int y)
{
    angle[angleID]+=x-oldX;
    oldX=x;
    glutPostRedisplay();
}
#include <stdio.h>
FILE * fout=NULL;
void keyboard(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;
    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);
    glutSolidTeapot(0.3);
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0],0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle[1],0,0,1);
                glTranslatef(-0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2],0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3],0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("08160554");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);

    glutTimerFunc(0,timer,0);

    glutMainLoop();
}


5.按r的功能



程式碼如下:

#include <stdio.h>
#include <GL/glut.h>
float diff=2,angle[20]={};
int angleID=0;

void timer(int t)
{
}
///用mouse motion操作關節
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
{///mouse 按下去,記下現在的位置
    oldX=x;
    oldY=y;
}
void motion(int x,int y)
{///mouse motion時,會叫這個motion
    angle[angleID]+=x-oldX;
    oldX=x;
    glutPostRedisplay();
}
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
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");
    }
    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);
    glutSolidTeapot(0.3);
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0],0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle[1],0,0,1);
                glTranslatef(-0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2],0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3],0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("08160554");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);

    glutTimerFunc(0,timer,0);

    glutMainLoop();
}


6.改work_dir及加入freeglut.dll





7.讓他自動存檔



程式碼如下:

#include <stdio.h>
#include <GL/glut.h>
float diff=2,angle[20]={};
int angleID=0;

void timer(int t)
{
}

int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
{
    oldX=x;
    oldY=y;
}
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
void motion(int x,int y)
{
    angle[angleID]+=x-oldX;
    oldX=x;
    glutPostRedisplay();
    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 keyboard(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;
    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");
    }
    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);
    glutSolidTeapot(0.3);
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0],0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotatef(angle[1],0,0,1);
                glTranslatef(-0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2],0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3],0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot(0.3);
            glPopMatrix();
        glPopMatrix();

    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("08160554");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);

    glutTimerFunc(0,timer,0);

    glutMainLoop();
}





沒有留言:

張貼留言

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

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