2021年6月7日 星期一

week16

 下載老師給我們week15的專案、程式碼

主題:動畫、內插

放入上週程式碼:

#include <stdio.h>

#include <GLUT/glut.h>

float angle=0, diff=2;


void timer(int t)

{

    glutTimerFunc( 30, timer, t+1 );

    angle += diff;

    if(angle>90) diff = -2;

    if(angle<0) diff = +2;

    glutPostRedisplay();

}

void display()

{

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glPushMatrix();

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

            glutSolidTeapot( 0.3 );

            glutSolidTeapot( 0.3 );

        glPopMatrix();

    glPopMatrix();

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

}








1.1修改一下 flot angle 的陣列(鍵盤)

加上angle的程式碼:

#include <stdio.h>

#include <GLUT/glut.h>

float angle[20]={},diff=2;

int angleID=0;///目前20個angle (angle[0],angle[1]...angle[angleID])

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;

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

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

    glPopMatrix();

    glutSwapBuffers();

}

int main(int argc, char** argv)

{

    glutInit( &argc,argv);

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week16    ");

    glutKeyboardFunc(keyboard);

    glutDisplayFunc(display);

    glutTimerFunc(0,timer,0);

    glutMainLoop();

}









1.2 加上滑鼠控制(希望用滑鼠取代Timer)

#include <stdio.h>

#include <GLUT/glut.h>

float angle[20]={},diff=2;

int angleID=0;///目前20個angle (angle[0],angle[1]...angle[angleID])

void timer(int t)

{

///    glutTimerFunc(30,timer,t+1);

}

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

}

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

    glPushMatrix();

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

    glPopMatrix();

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

}








注意:

❗main 裡面也要程式❗ 

🔎 glutMouseFunc(mouse); 
🔎 glutMotionFunc(motion); 

1.3 用for迴圈來印出陣列

程式碼:

#include <stdio.h>

#include <GLUT/glut.h>

float angle[20]={},diff=2;

int angleID=0;///有20個angle (angle[0],angle[1]...angle[angleID])

void timer(int t)

{

///    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;

    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迴圈

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

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

    glPopMatrix();

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

}

   









1.4 按鍵 r 開啟檔案

程式碼:

#include <stdio.h>

#include <GLUT/glut.h>

float angle[20]={},diff=2;

int angleID=0;///有20個angle (angle[0],angle[1]...angle[angleID])

void timer(int t)

{

///    glutTimerFunc(30,timer,t+1);

}

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

}

FILE * fout =NULL;

FILE * fin=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");

    }

    if(key=='r')    ///按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();

        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();///step05-3

                glTranslatef(+0.3,0,0);

                glRotatef(-angle[3], 0,0,1);

                glTranslatef(+0.3,0,0);

                glutSolidTeapot( 0.3 );///右手肘

            glPopMatrix();

        glPopMatrix();    glPopMatrix();

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

}







1.5 自動儲存角度與讀取檔案

程式碼:

#include <stdio.h>

#include <GLUT/glut.h>

float angle[20]={}, diff=2;

int angleID=0;

void timer(int t)

{

}

int oldX=0, oldY=0;

void mouse(int botton,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 );

    glPushMatrix();

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

    glPopMatrix();

    glutSwapBuffers();

}

int main( int argc, char** argv )

{

    glutInit( &argc, argv );

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow( "week16" );


    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...